From b40ed1c2bbff27cb16e95c4a1d22381e67509e44 Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Tue, 29 Jan 2013 17:55:47 +0000 Subject: RepoAliasResolver: Add aliases_from_url method This returns a sorted list of possible aliases for a url. --- morphlib/repoaliasresolver.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'morphlib/repoaliasresolver.py') 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.+)'.join(map(re.escape, pattern.split('%s'))) + else: + return re.escape(pattern) + r'(?P.+)' + + 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) -- cgit v1.2.1