diff options
Diffstat (limited to 'morphlib/repoaliasresolver.py')
-rw-r--r-- | morphlib/repoaliasresolver.py | 53 |
1 files changed, 35 insertions, 18 deletions
diff --git a/morphlib/repoaliasresolver.py b/morphlib/repoaliasresolver.py index 731c1a76..b5da524a 100644 --- a/morphlib/repoaliasresolver.py +++ b/morphlib/repoaliasresolver.py @@ -1,4 +1,4 @@ -# Copyright (C) 2012 Codethink Limited +# Copyright (C) 2012-2013 Codethink Limited # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -18,10 +18,34 @@ import logging import re +class RepoAlias(object): + + def __init__(self, alias, prefix, pullpat, pushpat): + self.alias = alias + self.prefix = prefix + self.pullpat = pullpat + self.pushpat = pushpat + + class RepoAliasResolver(object): def __init__(self, aliases): - self.aliases = aliases + self.aliases = {} + + alias_pattern = (r'^(?P<prefix>[a-z0-9]+)' + r'=(?P<pullpat>[^#]+)#(?P<pushpat>[^#]+)$') + for alias in aliases: + logging.debug('expanding: alias="%s"' % alias) + m = re.match(alias_pattern, alias) + logging.debug('expanding: m=%s' % repr(m)) + if not m: + logging.warning('Alias %s is malformed' % alias) + continue + prefix = m.group('prefix') + logging.debug('expanding: prefix group=%s' % prefix) + self.aliases[prefix] = RepoAlias(alias, prefix, m.group('pullpat'), + m.group('pushpat')) + def pull_url(self, reponame): '''Expand a possibly shortened repo name to a pull url.''' @@ -44,22 +68,15 @@ class RepoAliasResolver(object): logging.debug('expanding: no prefix') return reponame - pat = r'^(?P<prefix>[a-z0-9]+)=(?P<pullpat>[^#]+)#(?P<pushpat>[^#]+)$' - for alias in self.aliases: - logging.debug('expanding: alias="%s"' % alias) - m = re.match(pat, alias) - logging.debug('expanding: m=%s' % repr(m)) - if m: - logging.debug('expanding: prefix group=%s' % m.group('prefix')) - if m and m.group('prefix') == prefix: - pullpat = m.group(patname) - logging.debug('expanding: pullpat=%s' % pullpat) - return self._apply_url_pattern(pullpat, suffix) - - # Unknown prefix. Which means it may be a real URL instead. - # Let the caller deal with it. - logging.debug('expanding: unknown prefix') - return reponame + if prefix not in self.aliases: + # Unknown prefix. Which means it may be a real URL instead. + # Let the caller deal with it. + logging.debug('expanding: unknown prefix') + return reponame + + pat = getattr(self.aliases[prefix], patname) + return self._apply_url_pattern(pat, suffix) + def _split_reponame(self, reponame): '''Split reponame into prefix and suffix. |