From 8c6c178bbe73c35bee15359b72e0cb816cbd48f9 Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Tue, 29 Jan 2013 15:46:02 +0000 Subject: 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. --- morphlib/repoaliasresolver.py | 53 ++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 18 deletions(-) (limited to 'morphlib/repoaliasresolver.py') 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[a-z0-9]+)' + r'=(?P[^#]+)#(?P[^#]+)$') + 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[a-z0-9]+)=(?P[^#]+)#(?P[^#]+)$' - 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. -- cgit v1.2.1