summaryrefslogtreecommitdiff
path: root/morphlib/repoaliasresolver.py
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@codethink.co.uk>2013-01-29 15:46:02 +0000
committerRichard Maw <richard.maw@codethink.co.uk>2013-02-11 14:12:14 +0000
commit8c6c178bbe73c35bee15359b72e0cb816cbd48f9 (patch)
treedaa7f112af815855b355407d6c592e0bff8d61cd /morphlib/repoaliasresolver.py
parent6047fce7f234db6a26dcd7b704636906290a48a5 (diff)
downloadmorph-8c6c178bbe73c35bee15359b72e0cb816cbd48f9.tar.gz
repoaliasresolver: compile patterns on creation
This also involved codifying it ignoring malformed aliases. Such behaviour was implemented before, but wasn't obvious. The restructuring made it have a line of code of its own, which meant it needed unit testing.
Diffstat (limited to 'morphlib/repoaliasresolver.py')
-rw-r--r--morphlib/repoaliasresolver.py53
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.