diff options
author | Richard Maw <richard.maw@codethink.co.uk> | 2013-01-29 17:55:47 +0000 |
---|---|---|
committer | Richard Maw <richard.maw@codethink.co.uk> | 2013-02-11 14:12:14 +0000 |
commit | b40ed1c2bbff27cb16e95c4a1d22381e67509e44 (patch) | |
tree | a7bbe2e6465289140e9469d52dfffcd615db5072 /morphlib/repoaliasresolver.py | |
parent | 8c6c178bbe73c35bee15359b72e0cb816cbd48f9 (diff) | |
download | morph-b40ed1c2bbff27cb16e95c4a1d22381e67509e44.tar.gz |
RepoAliasResolver: Add aliases_from_url method
This returns a sorted list of possible aliases for a url.
Diffstat (limited to 'morphlib/repoaliasresolver.py')
-rw-r--r-- | morphlib/repoaliasresolver.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/morphlib/repoaliasresolver.py b/morphlib/repoaliasresolver.py index b5da524a..653eba90 100644 --- a/morphlib/repoaliasresolver.py +++ b/morphlib/repoaliasresolver.py @@ -26,6 +26,19 @@ class RepoAlias(object): self.pullpat = pullpat self.pushpat = pushpat + def _pattern_to_regex(self, pattern): + if '%s' in pattern: + return r'(?P<path>.+)'.join(map(re.escape, pattern.split('%s'))) + else: + return re.escape(pattern) + r'(?P<path>.+)' + + def match_url(self, url): + '''Given a URL, return what its alias would be if it matches''' + for pat in (self.pullpat, self.pushpat): + m = re.match(self._pattern_to_regex(pat), url) + if m: + return '%s:%s' % (self.prefix, m.group('path')) + return None class RepoAliasResolver(object): @@ -55,6 +68,17 @@ class RepoAliasResolver(object): '''Expand a possibly shortened repo name to a push url.''' return self._expand_reponame(reponame, 'pushpat') + def aliases_from_url(self, url): + '''Find aliases the url could have expanded from. + + Returns an ascii-betically sorted list. + ''' + potential_matches = (repo_alias.match_url(url) + for repo_alias in self.aliases.itervalues()) + known_aliases = (url_alias for url_alias in potential_matches + if url_alias is not None) + return sorted(known_aliases) + def _expand_reponame(self, reponame, patname): logging.debug('expanding: reponame=%s' % reponame) logging.debug('expanding: patname=%s' % patname) |