-
-
Notifications
You must be signed in to change notification settings - Fork 586
/
Copy pathutils.py
306 lines (249 loc) · 9.36 KB
/
utils.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
from packageurl import PackageURL
try:
from license_expression import Licensing
from license_expression import combine_expressions as le_combine_expressions
except:
Licensing = None
le_combine_expressions = None
PLAIN_URLS = (
'https://',
'http://',
)
VCS_URLS = (
'git://',
'git+git://',
'git+https://',
'git+http://',
'hg://',
'hg+http://',
'hg+https://',
'svn://',
'svn+https://',
'svn+http://',
)
# TODO this does not really normalize the URL
# TODO handle vcs_tool
def normalize_vcs_url(repo_url, vcs_tool=None):
"""
Return a normalized vcs_url version control URL given some `repo_url` and an
optional `vcs_tool` hint (such as 'git', 'hg', etc.
Handles shortcuts for GitHub, GitHub gist, Bitbucket, or GitLab repositories
and more using the same approach as npm install:
See https://docs.npmjs.com/files/package.json#repository
or https://getcomposer.org/doc/05-repositories.md
This is done here in npm:
https://github.com/npm/npm/blob/d3c858ce4cfb3aee515bb299eb034fe1b5e44344/node_modules/hosted-git-info/git-host-info.js
These should be resolved:
npm/npm
gist:11081aaa281
bitbucket:example/repo
gitlab:another/repo
expressjs/serve-static
git://github.com/angular/di.js.git
git://github.com/hapijs/boom
git@github.com:balderdashy/waterline-criteria.git
http://github.com/ariya/esprima.git
http://github.com/isaacs/nopt
https://github.com/chaijs/chai
https://github.com/christkv/kerberos.git
https://gitlab.com/foo/private.git
git@gitlab.com:foo/private.git
"""
if not repo_url or not isinstance(repo_url, str):
return
repo_url = repo_url.strip()
if not repo_url:
return
# TODO: If we match http and https, we may should add more check in
# case if the url is not a repo one. For example, check the domain
# name in the url...
if repo_url.startswith(VCS_URLS + PLAIN_URLS):
return repo_url
if repo_url.startswith('git@'):
tool, _, right = repo_url.partition('@')
if ':' in repo_url:
host, _, repo = right.partition(':')
else:
# git@github.com/Filirom1/npm2aur.git
host, _, repo = right.partition('/')
if any(r in host for r in ('bitbucket', 'gitlab', 'github')):
scheme = 'https'
else:
scheme = 'git'
return '%(scheme)s://%(host)s/%(repo)s' % locals()
# FIXME: where these URL schemes come from??
if repo_url.startswith(('bitbucket:', 'gitlab:', 'github:', 'gist:')):
hoster_urls = {
'bitbucket': 'https://bitbucket.org/%(repo)s',
'github': 'https://github.com/%(repo)s',
'gitlab': 'https://gitlab.com/%(repo)s',
'gist': 'https://gist.github.com/%(repo)s', }
hoster, _, repo = repo_url.partition(':')
return hoster_urls[hoster] % locals()
if len(repo_url.split('/')) == 2:
# implicit github, but that's only on NPM?
return f'https://github.com/{repo_url}'
return repo_url
def build_description(summary, description):
"""
Return a description string from a summary and description
"""
summary = (summary or '').strip()
description = (description or '').strip()
if not description:
description = summary
else:
if summary and summary not in description:
description = '\n'.join([summary , description])
return description
_LICENSING = Licensing and Licensing() or None
def combine_expressions(
expressions,
relation='AND',
unique=True,
licensing=_LICENSING,
):
"""
Return a combined license expression string with relation, given a sequence of
license ``expressions`` strings or LicenseExpression objects.
"""
if not licensing:
raise Exception('combine_expressions: cannot combine combine_expressions without license_expression package.')
return expressions and str(le_combine_expressions(expressions, relation, unique, licensing)) or None
def get_ancestor(levels_up, resource, codebase):
"""
Return the nth-``levels_up`` ancestor Resource of ``resource`` in
``codebase`` or None.
For example, with levels_up=2 and starting with a resource path of
`gem-extract/metadata.gz-extract/metadata.gz-extract`,
then `gem-extract/` should be returned.
"""
rounds = 0
while rounds < levels_up:
resource = resource.parent(codebase)
if not resource:
return
rounds += 1
return resource
def find_root_from_paths(paths, resource, codebase):
"""
Return the resource for the root directory of this filesystem or None, given
a ``resource`` in ``codebase`` with a list of possible resource root-
relative ``paths`` (e.g. extending from the root directory we are looking
for).
"""
for path in paths:
if not resource.path.endswith(path):
continue
return find_root_resource(path=path, resource=resource, codebase=codebase)
def find_root_resource(path, resource, codebase):
"""
Return the resource for the root directory of this filesystem or None, given
a ``resource`` in ``codebase`` with a possible resource root-relative
``path`` (e.g. extending from the root directory we are looking for).
"""
if not resource.path.endswith(path):
return
for _seg in path.split('/'):
resource = resource.parent(codebase)
if not resource:
return
return resource
def yield_dependencies_from_package_data(package_data, datafile_path, package_uid):
"""
Yield a Dependency for each dependency from ``package_data.dependencies``
"""
from packagedcode import models
dependent_packages = package_data.dependencies
if dependent_packages:
yield from models.Dependency.from_dependent_packages(
dependent_packages=dependent_packages,
datafile_path=datafile_path,
datasource_id=package_data.datasource_id,
package_uid=package_uid,
)
def parse_maintainer_name_email(maintainer):
"""
Get name and email values from a author/maintainer string.
Example string:
Debian systemd Maintainers <pkg-systemd-maintainers@lists.alioth.debian.org>
"""
email_wrappers = ["<", ">"]
has_email = "@" in maintainer and all([
True
for char in email_wrappers
if char in maintainer
])
if not has_email:
return maintainer, None
name, _, email = maintainer.rpartition("<")
return name.rstrip(" "), email.rstrip(">")
def yield_dependencies_from_package_resource(resource, package_uid=None):
"""
Yield a Dependency for each dependency from each package from``resource.package_data``
"""
from packagedcode import models
for pkg_data in resource.package_data:
pkg_data = models.PackageData.from_dict(pkg_data)
yield from yield_dependencies_from_package_data(pkg_data, resource.path, package_uid)
def update_dependencies_as_resolved(dependencies):
"""
For a list of dependency mappings with their respective
resolved packages, update in place the dependencies for those
resolved packages as resolved (update `is_pinned` as True),
if the requirement is also present as a resolved package.
"""
#TODO: Use vers to mark update `is_pinned` even in the case
# of incomplete resolution/partially pinned dependencies
# These are only type, namespace and name (without version and qualifiers)
base_resolved_purls = []
resolved_packages = [
dep.get("resolved_package")
for dep in dependencies
if dep.get("resolved_package")
]
# No resolved packages are present for dependencies
if not resolved_packages:
return
for pkg in resolved_packages:
purl=pkg.get("purl")
if purl:
base_resolved_purls.append(
get_base_purl(purl=purl)
)
for dependency in dependencies:
resolved_package = dependency.get("resolved_package")
dependencies_from_resolved = []
if resolved_package:
dependencies_from_resolved = resolved_package.get("dependencies")
if not dependencies_from_resolved:
continue
for dep in dependencies_from_resolved:
dep_purl = dep.get("purl")
if dep_purl in base_resolved_purls:
dep["is_pinned"] = True
def get_base_purl(purl):
"""
Get a base purl with only the type, name and namespace from
a given purl.
"""
base_purl_fields = ["type", "namespace", "name"]
purl_mapping = PackageURL.from_string(purl=purl).to_dict()
base_purl_mapping = {
purl_field: purl_value
for purl_field, purl_value in purl_mapping.items()
if purl_field in base_purl_fields
}
return PackageURL(**base_purl_mapping).to_string()
def is_simple_path(path):
return '*' not in path
def is_simple_path_pattern(path):
return path.endswith('*') and path.count('*') == 1