-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdirectory.py
271 lines (210 loc) · 7.9 KB
/
directory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# directory.py -- Directory service that uses Debian Vcs-* fields
# Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
#
# This file is part of bzr-builddeb.
#
# bzr-builddeb is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# bzr-builddeb is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with bzr-builddeb; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
from ... import urlutils
from ...directory_service import directories
from ...errors import DependencyNotPresent
from ...trace import note, warning
from debian.deb822 import Deb822
from debian.changelog import Version
from debmutate.vcs import source_package_vcs, split_vcs_url
def fixup_broken_git_url(url):
"""Attempt to fix up broken Git URLs.
A common misspelling is to add an extra ":" after the hostname
"""
(scheme, netloc, path, params,
query, fragment) = urlutils.urlparse.urlparse(url, allow_fragments=False)
if '@' in netloc:
credentials, host = netloc.rsplit('@', 1)
else:
credentials = None
host = netloc
if ':' in host and not (host[0] == '[' and host[-1] == ']'):
# there *is* port
host, port = host.rsplit(':', 1)
if not port or port.isdigit():
return url
else:
port = None
if host in ('salsa.debian.org', 'github.com'):
if '/' not in path[1:] and port:
path = '{}/{}'.format(port, path.lstrip('/'))
netloc = host
if ":" in netloc:
netloc = "[%s]" % netloc
if (credentials is not None and
not (credentials == 'git' and
scheme not in ('git', 'http', 'https'))):
netloc = '{}@{}'.format(credentials, netloc)
if host == 'salsa.debian.org':
scheme = 'https'
if host == 'salsa.debian.org' and path.startswith('/cgit/'):
path = path[5:]
new_url = urlutils.urlparse.urlunparse(
(scheme, netloc, path, params, query, fragment))
if url != new_url:
warning('Fixing up URL: %s -> %s', url, new_url)
return new_url
return url
def vcs_git_url_to_bzr_url(url):
"""Convert a Vcs-Git string to a Breezy URL."""
(url, branch, subpath) = split_vcs_url(url)
from breezy.git.urls import git_url_to_bzr_url
url = fixup_broken_git_url(url)
url = git_url_to_bzr_url(url)
if branch:
branch = urlutils.quote(branch, '')
url = urlutils.join_segment_parameters(
url, {'branch': branch})
if subpath:
url = urlutils.join(url, subpath)
return url
def vcs_bzr_url_to_bzr_url(url):
return directories.dereference(url)
def vcs_darcs_url_to_bzr_url(url):
return url
def vcs_mtn_url_to_bzr_url(url):
return url
def vcs_arch_url_to_bzr_url(url):
return url
def vcs_cvs_url_to_bzr_url(location):
from breezy.location import cvs_to_url
try:
(loc, module) = location.split(' ', 1)
except ValueError:
loc = location
module = None
url = cvs_to_url(loc)
if module is not None:
url = url + '?module=' + urlutils.quote(module)
return url
def vcs_hg_url_to_bzr_url(url):
(url, branch, subpath) = split_vcs_url(url)
if branch:
branch = urlutils.quote(branch, '')
url = urlutils.join_segment_parameters(
url, {'branch': branch})
if subpath:
url = urlutils.join(url, subpath)
return url
def vcs_svn_url_to_bzr_url(url):
return url
vcs_field_to_bzr_url_converters = [
("Bzr", vcs_bzr_url_to_bzr_url),
("Darcs", vcs_darcs_url_to_bzr_url),
("Svn", vcs_svn_url_to_bzr_url),
("Git", vcs_git_url_to_bzr_url),
("Hg", vcs_hg_url_to_bzr_url),
("Cvs", vcs_cvs_url_to_bzr_url),
("Mtn", vcs_mtn_url_to_bzr_url),
("Arch", vcs_arch_url_to_bzr_url),
]
def source_package_vcs_url(control):
"""Extract a Breezy-compatible URL from a source package.
"""
(vcs_type, vcs_url) = source_package_vcs(control)
return vcs_type, dict(vcs_field_to_bzr_url_converters)[vcs_type](vcs_url)
class AptDirectory:
"""Simple Bazaar directory service which uses dpkg Vcs-* fields."""
def look_up(self, name, url, purpose=None):
if "/" in name:
(name, version) = name.split("/", 1)
else:
version = None
try:
import apt_pkg
except ImportError as e:
raise DependencyNotPresent('apt_pkg', e) from e
apt_pkg.init()
sources = apt_pkg.SourceRecords()
by_version = {}
while sources.lookup(name):
by_version[sources.version] = sources.record
if len(by_version) == 0:
raise urlutils.InvalidURL(path=url, extra='package not found')
if version is None:
# Try the latest version
version = sorted(by_version, key=Version)[-1]
if version not in by_version:
raise urlutils.InvalidURL(
path=url, extra='version %s not found' % version)
control = Deb822(by_version[version])
try:
vcs, url = source_package_vcs_url(control)
except KeyError as e:
note("Retrieving Vcs locating from %s Debian version %s", name,
version)
raise urlutils.InvalidURL(
path=url, extra='no VCS URL found') from e
note("Resolved package URL from Debian package %s/%s: %s",
name, version, url)
return url
class DgitDirectory:
"""Directory that looks up the URL according to a Dgit control field."""
def look_up(self, name, url, purpose=None):
if "/" in name:
(name, version) = name.split("/", 1)
else:
version = None
try:
import apt_pkg
except ImportError as e:
raise DependencyNotPresent('apt_pkg', e) from e
apt_pkg.init()
sources = apt_pkg.SourceRecords()
urls = {}
while sources.lookup(name):
control = Deb822(sources.record)
pkg_version = control["Version"]
try:
urls[pkg_version] = control["Dgit"].split(' ')
except KeyError:
pass
if len(urls) == 0:
raise urlutils.InvalidURL(path=url, extra='no URLs found')
if version is None:
# Try the latest version
version = sorted(urls, key=Version)[-1]
if version not in urls:
raise urlutils.InvalidURL(
path=url, extra='version %s not found' % version)
if len(urls[version]) < 3:
raise urlutils.InvalidURL(
path=url,
extra='dgit header does not have location information')
url = urlutils.join_segment_parameters(
urls[version][3],
{"tag": urlutils.quote(urls[version][2], '')})
note("Resolved package URL from Debian package %s/%s: %s",
name, version, url)
return url
class VcsDirectory:
"""Use local Vcs Directory."""
def look_up(self, name, url, purpose=None):
from debian.deb822 import Deb822
with open('debian/control') as f:
source = Deb822(f)
vcs, url = source_package_vcs_url(source)
return url
def upstream_branch_alias(b):
from .util import debuild_config
with b.lock_read():
tree = b.basis_tree()
config = debuild_config(tree, subpath='.')
return directories.dereference(config.upstream_branch)