diff options
author | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2012-05-03 14:28:08 +0100 |
---|---|---|
committer | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2012-05-04 14:24:07 +0100 |
commit | dac0e52f5aebcd99fdaf6e9f7df05a4597c17d43 (patch) | |
tree | 21c67eafc634babe51f3fa35c48b99d2c2e3de2f /morphlib/localrepocache.py | |
parent | 223475d1059e15ee75366354ab1a88dfdf2afac0 (diff) | |
download | morph-dac0e52f5aebcd99fdaf6e9f7df05a4597c17d43.tar.gz |
Implement repo-alias expansion
Changed the delimieter between push and pull patterns to be #,
since = is reasonably frequent in URLs, and # should never be
necessary in git urls.
Oh my how many tests now fail.
Diffstat (limited to 'morphlib/localrepocache.py')
-rw-r--r-- | morphlib/localrepocache.py | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/morphlib/localrepocache.py b/morphlib/localrepocache.py index a5487cd9..4fb82a3b 100644 --- a/morphlib/localrepocache.py +++ b/morphlib/localrepocache.py @@ -16,6 +16,7 @@ import logging import os +import re import urllib2 import urlparse import shutil @@ -88,9 +89,9 @@ class LocalRepoCache(object): ''' - def __init__(self, cachedir, baseurls, bundle_base_url=None): + def __init__(self, cachedir, aliases, bundle_base_url=None): self._cachedir = cachedir - self._baseurls = baseurls + self._aliases = aliases if bundle_base_url and not bundle_base_url.endswith('/'): bundle_base_url += '/' # pragma: no cover self._bundle_base_url = bundle_base_url @@ -185,11 +186,49 @@ class LocalRepoCache(object): path = self._cache_name(repourl) yield repourl, path + def _split_reponame(self, reponame): + '''Split reponame into prefix and suffix. + + The prefix is returned as None if there was no prefix. + + ''' + + pat = r'^(?P<prefix>[a-z0-9]+):(?P<rest>.*)$' + m = re.match(pat, reponame) + if m: + return m.group('prefix'), m.group('rest') + else: + return None, reponame + + def _apply_url_pattern(self, pattern, shortname): + assert '%s' in pattern + return shortname.join(pattern.split('%s')) + + def _expand_reponame(self, reponame, patname): + prefix, suffix = self._split_reponame(reponame) + + # There was no prefix. + if prefix is None: + return reponame + + pat = r'^(?P<prefix>[a-z0-9]+)=(?P<pullpat>[^#]+)#(?P<pushpat>[^#]+)' + for alias in self._aliases: + m = re.match(pat, alias) + if m and m.group('prefix') == prefix: + pullpat = m.group(patname) + return self._apply_url_pattern(pullpat, suffix) + + # Unknown prefix. Which means it may be a real URL instead. + # Let the caller deal with it. + return reponame + def pull_url(self, reponame): '''Expand a possibly shortened repo name to a pull url.''' - + return self._expand_reponame(reponame, 'pullpat') + def push_url(self, reponame): '''Expand a possibly shortened repo name to a push url.''' + return self._expand_reponame(reponame, 'pushpat') def has_repo(self, reponame): '''Have we already got a cache of a given repo?''' |