##// END OF EJS Templates
schemes: move re construction to module-level and python3-ify...
Augie Fackler -
r31181:150cd512 default
parent child Browse files
Show More
@@ -1,133 +1,134
1 1 # Copyright 2009, Alexander Solovyov <piranha@piranha.org.ua>
2 2 #
3 3 # This software may be used and distributed according to the terms of the
4 4 # GNU General Public License version 2 or any later version.
5 5
6 6 """extend schemes with shortcuts to repository swarms
7 7
8 8 This extension allows you to specify shortcuts for parent URLs with a
9 9 lot of repositories to act like a scheme, for example::
10 10
11 11 [schemes]
12 12 py = http://code.python.org/hg/
13 13
14 14 After that you can use it like::
15 15
16 16 hg clone py://trunk/
17 17
18 18 Additionally there is support for some more complex schemas, for
19 19 example used by Google Code::
20 20
21 21 [schemes]
22 22 gcode = http://{1}.googlecode.com/hg/
23 23
24 24 The syntax is taken from Mercurial templates, and you have unlimited
25 25 number of variables, starting with ``{1}`` and continuing with
26 26 ``{2}``, ``{3}`` and so on. This variables will receive parts of URL
27 27 supplied, split by ``/``. Anything not specified as ``{part}`` will be
28 28 just appended to an URL.
29 29
30 30 For convenience, the extension adds these schemes by default::
31 31
32 32 [schemes]
33 33 py = http://hg.python.org/
34 34 bb = https://bitbucket.org/
35 35 bb+ssh = ssh://hg@bitbucket.org/
36 36 gcode = https://{1}.googlecode.com/hg/
37 37 kiln = https://{1}.kilnhg.com/Repo/
38 38
39 39 You can override a predefined scheme by defining a new scheme with the
40 40 same name.
41 41 """
42 42 from __future__ import absolute_import
43 43
44 44 import os
45 45 import re
46 46
47 47 from mercurial.i18n import _
48 48 from mercurial import (
49 49 cmdutil,
50 50 error,
51 51 extensions,
52 52 hg,
53 53 pycompat,
54 54 templater,
55 55 util,
56 56 )
57 57
58 58 cmdtable = {}
59 59 command = cmdutil.command(cmdtable)
60 60 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
61 61 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
62 62 # be specifying the version(s) of Mercurial they are tested with, or
63 63 # leave the attribute unspecified.
64 64 testedwith = 'ships-with-hg-core'
65 65
66 _partre = re.compile(r'\{(\d+)\}'.encode(u'latin1'))
66 67
67 68 class ShortRepository(object):
68 69 def __init__(self, url, scheme, templater):
69 70 self.scheme = scheme
70 71 self.templater = templater
71 72 self.url = url
72 73 try:
73 self.parts = max(map(int, re.findall(r'\{(\d+)\}', self.url)))
74 self.parts = max(map(int, _partre.findall(self.url)))
74 75 except ValueError:
75 76 self.parts = 0
76 77
77 78 def __repr__(self):
78 79 return '<ShortRepository: %s>' % self.scheme
79 80
80 81 def instance(self, ui, url, create):
81 82 url = self.resolve(url)
82 83 return hg._peerlookup(url).instance(ui, url, create)
83 84
84 85 def resolve(self, url):
85 86 # Should this use the util.url class, or is manual parsing better?
86 87 try:
87 88 url = url.split('://', 1)[1]
88 89 except IndexError:
89 90 raise error.Abort(_("no '://' in scheme url '%s'") % url)
90 91 parts = url.split('/', self.parts)
91 92 if len(parts) > self.parts:
92 93 tail = parts[-1]
93 94 parts = parts[:-1]
94 95 else:
95 96 tail = ''
96 97 context = dict((str(i + 1), v) for i, v in enumerate(parts))
97 98 return ''.join(self.templater.process(self.url, context)) + tail
98 99
99 100 def hasdriveletter(orig, path):
100 101 if path:
101 102 for scheme in schemes:
102 103 if path.startswith(scheme + ':'):
103 104 return False
104 105 return orig(path)
105 106
106 107 schemes = {
107 108 'py': 'http://hg.python.org/',
108 109 'bb': 'https://bitbucket.org/',
109 110 'bb+ssh': 'ssh://hg@bitbucket.org/',
110 111 'gcode': 'https://{1}.googlecode.com/hg/',
111 112 'kiln': 'https://{1}.kilnhg.com/Repo/'
112 113 }
113 114
114 115 def extsetup(ui):
115 116 schemes.update(dict(ui.configitems('schemes')))
116 117 t = templater.engine(lambda x: x)
117 118 for scheme, url in schemes.items():
118 119 if (pycompat.osname == 'nt' and len(scheme) == 1 and scheme.isalpha()
119 120 and os.path.exists('%s:\\' % scheme)):
120 121 raise error.Abort(_('custom scheme %s:// conflicts with drive '
121 122 'letter %s:\\\n') % (scheme, scheme.upper()))
122 123 hg.schemes[scheme] = ShortRepository(url, scheme, t)
123 124
124 125 extensions.wrapfunction(util, 'hasdriveletter', hasdriveletter)
125 126
126 127 @command('debugexpandscheme', norepo=True)
127 128 def expandscheme(ui, url, **opts):
128 129 """given a repo path, provide the scheme-expanded path
129 130 """
130 131 repo = hg._peerlookup(url)
131 132 if isinstance(repo, ShortRepository):
132 133 url = repo.resolve(url)
133 134 ui.write(url + '\n')
General Comments 0
You need to be logged in to leave comments. Login now