summaryrefslogtreecommitdiff
path: root/morphlib
diff options
context:
space:
mode:
authorLars Wirzenius <lars.wirzenius@codethink.co.uk>2012-05-03 14:28:08 +0100
committerLars Wirzenius <lars.wirzenius@codethink.co.uk>2012-05-04 14:24:07 +0100
commitdac0e52f5aebcd99fdaf6e9f7df05a4597c17d43 (patch)
tree21c67eafc634babe51f3fa35c48b99d2c2e3de2f /morphlib
parent223475d1059e15ee75366354ab1a88dfdf2afac0 (diff)
downloadmorph-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')
-rw-r--r--morphlib/localrepocache.py45
-rw-r--r--morphlib/localrepocache_tests.py4
2 files changed, 44 insertions, 5 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?'''
diff --git a/morphlib/localrepocache_tests.py b/morphlib/localrepocache_tests.py
index 5ee28a99..917de598 100644
--- a/morphlib/localrepocache_tests.py
+++ b/morphlib/localrepocache_tests.py
@@ -23,7 +23,7 @@ import morphlib
class LocalRepoCacheTests(unittest.TestCase):
def setUp(self):
- aliases = ['upstream=git://example.com/%s=example.com:%s.git']
+ aliases = ['upstream=git://example.com/%s#example.com:%s.git']
bundle_base_url = 'http://lorry.example.com/bundles/'
self.reponame = 'upstream:reponame'
self.repourl = 'git://example.com/reponame'
@@ -82,7 +82,7 @@ class LocalRepoCacheTests(unittest.TestCase):
self.assertEqual(self.lrc.pull_url(self.reponame), self.repourl)
def test_expands_shortened_url_correctly_for_pushing(self):
- self.assertEqual(self.lrc.push_url(self.reponame), self.repourl)
+ self.assertEqual(self.lrc.push_url(self.reponame), self.pushurl)
def test_has_not_got_shortened_repo_initially(self):
self.assertFalse(self.lrc.has_repo(self.reponame))