-
-
Notifications
You must be signed in to change notification settings - Fork 723
Expand file tree
/
Copy pathspec.py
More file actions
324 lines (268 loc) · 9.42 KB
/
spec.py
File metadata and controls
324 lines (268 loc) · 9.42 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#
# 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.
#
import re
from gemfileparser import GemfileParser
from packagedcode import models
from packageurl import PackageURL
from functools import partial
"""
Handle Cocoapods (.podspec/Pofile) and Ruby(.gemspec/Gemfile) files.
"""
# FIXME: code needs cleanup
parse_name = re.compile(
r'.*\.name\s*=\s*'
r'(?P<name>.*)'
)
parse_version = re.compile(
r'.*\.version\s*=\s*'
f'(?P<version>.*)'
)
parse_summary = re.compile(
r'.*\.summary\s*=\s*'
r'(?P<summary>.*)',
)
# simple single line quoted description
parse_description = re.compile(
r'.*\.description\s*=\s*'
r'(?P<description>.*)',
)
parse_homepage = re.compile(
r'.*\.homepage\s*=\s*'
r'(?P<homepage>.*)'
)
parse_license = re.compile(
r'.*\.license\s*=\s*'
r'(?P<license>.*)'
)
parse_podspec_source = re.compile(
r'.*\.source\s*=\s*'
r'(?P<source>.*)'
)
# See in gemspec
# for file refs:
# files
# test_files
# extensions
# executables
# extra_rdoc_files
# date = "2017-11-07"
# metadata
# rubygems_version
# licenses = ['MIT']
# authors = ["Javan Makhmali".freeze, "Sam Stephenson".freeze, "David Heinemeier Hansson".freeze]
# email = ["javan@javan.us".freeze, "sstephenson@gmail.com".freeze, "david@loudthinking.com".freeze]
# email = 'rubocop@googlegroups.com'
def get_value(name, matcher, line, clean=True):
"""
Return the value for the ``name`` attribute collected using the ``matcher``
regex in the ``line`` text.
"""
match = matcher.match(line)
if match:
value = match.group(name)
return get_cleaned_string(value) if clean else value
# FIXME: this does not smell right as we ignore the matched value
def get_podspec_source(line):
if '.source' in line:
match = get_value(name='source', matcher=parse_podspec_source, line=line)
if match:
source = re.sub(r'/*.*source.*?>', '', line)
stripped_source = re.sub(r',.*', '', source)
return get_cleaned_string(stripped_source)
def get_emails(line):
"""
Return a list of emails extracted from a text ``line``, or None
"""
if '.email' in line:
_key, _sep, value = line.rpartition('=')
stripped_emails = get_cleaned_string(value)
stripped_emails = stripped_emails.strip()
stripped_emails = stripped_emails.split(',')
return stripped_emails
def get_authors(line):
"""
Return a list of authors extracted from a text ``line``, or None
"""
if '.author' in line:
authors = re.sub(r'/*.*author.*?=', '', line)
stripped_authors = get_cleaned_string(authors)
stripped_authors = re.sub(r'(\s*=>\s*)', '=>', stripped_authors)
stripped_authors = stripped_authors.strip()
stripped_authors = stripped_authors.split(',')
return stripped_authors
# mapping of parser callable by its field name
PARSER_BY_NAME = {
'name': partial(get_value, name='name', matcher=parse_name),
'version': partial(get_value, name='version', matcher=parse_version),
'license': partial(get_value, name='license', matcher=parse_license),
'summary': partial(get_value, name='summary', matcher=parse_summary),
'description': partial(get_value, name='description', matcher=parse_description, clean=False),
'homepage': partial(get_value, name='homepage', matcher=parse_homepage),
'source': get_podspec_source,
'email': get_emails,
'author': get_authors,
}
def is_ruby_version_constant(value):
"""
Return True if value looks like a Ruby constant expression
that cannot be resolved statically, such as:
Elasticsearch::API::VERSION or MyGem::VERSION
These are dynamic values that reference Ruby constants
and cannot be determined without executing the Ruby code.
For example:
>>> is_ruby_version_constant('Elasticsearch::API::VERSION')
True
>>> is_ruby_version_constant('MyGem::VERSION')
True
>>> is_ruby_version_constant('1.0.0')
False
>>> is_ruby_version_constant("'2.3.4'")
False
>>> is_ruby_version_constant(None)
False
"""
if not value:
return False
# Ruby constants use :: as namespace separator
if '::' in value:
return True
# A bare constant starts with uppercase and has no dots/quotes
# e.g. VERSION (unlikely but possible)
stripped = value.strip('\'"')
if stripped and stripped[0].isupper() and '.' not in stripped:
return True
return False
def parse_spec(location, package_type):
"""
Return a mapping of data parsed from a podspec/gemspec/Pofile/Gemfile file
at ``location``. Use ``package_type`` a Package URL type for dependencies.
dependencies is a list of DependentPackage.
"""
spec_data = {}
with open(location) as spec:
content = spec.read()
lines = content.splitlines()
for line in lines:
line = pre_process(line)
for attribute_name, parser in PARSER_BY_NAME.items():
parsed = parser(line=line)
if parsed:
spec_data[attribute_name] = parsed
version = spec_data.get('version')
if is_ruby_version_constant(version):
spec_data['version'] = None
# description can be in single or multi-lines
# There are many different ways to write description.
# we reparse for multline
description = spec_data.get("description")
if description:
if '<<-' in description:
# a Ruby multiline text
spec_data['description'] = get_multiline_description(
description_start=description,
lines=lines,
)
else:
# a single quoted description
spec_data['description'] = get_cleaned_string(description)
# We avoid reloading twice the file but we are still parsing twice: need to
# merge all in gemfileparser or write a better parser.
spec_data['dependencies'] = list(get_dependent_packages(
lines=lines,
location=location,
package_type=package_type,
))
return spec_data
def get_dependent_packages(lines, location, package_type):
"""
Yield DependentPackage from the file at ``location`` for the given
``package_type``.
"""
flags_by_scope = {
'runtime': dict(is_runtime=True, is_optional=False),
'dependency': dict(is_runtime=True, is_optional=False),
'production': dict(is_runtime=True, is_optional=False),
'development': dict(is_runtime=False, is_optional=True),
'test': dict(is_runtime=False, is_optional=True),
'metrics': dict(is_runtime=False, is_optional=True),
}
dependencies = LinesBasedGemfileParser(lines=lines, filepath=location).parse()
for key in dependencies:
depends = dependencies.get(key, []) or []
for dep in depends:
flags = flags_by_scope.get(key, 'runtime')
yield models.DependentPackage(
purl=PackageURL(type=package_type, name=dep.name).to_string(),
extracted_requirement=', '.join(dep.requirement),
scope=key,
is_pinned=False,
**flags,
)
class LinesBasedGemfileParser(GemfileParser):
"""
A GemfileParser that works from a text lines rather than re-reading a file.
Done to avoid reading a file twice.
"""
def __init__(self, lines, filepath, appname=''):
self.filepath = filepath
self.current_group = 'runtime'
self.appname = appname
self.dependencies = {
'development': [],
'runtime': [],
'dependency': [],
'test': [],
'production': [],
'metrics': [],
}
self.contents = lines
self.gemspec = filepath.endswith(('.gemspec', '.podspec'))
def pre_process(line):
"""
Return a ``line`` cleaned from comments markers and space.
"""
if '#' in line:
line = line[:line.index('#')]
return line.strip()
def get_cleaned_string(string):
"""
Return ``string`` removing unnecessary special character
"""
if string:
for replaceable in ("'", '"', '{', '}', '[', ']', '%q', '<', '>', '.freeze'):
string = string.replace(replaceable, '')
return string.strip()
def get_multiline_description(description_start, lines):
"""
Return a multiline description given the ``description_start`` start of the
decsription and a ``lines`` list. These are common in .podspec.
https://guides.cocoapods.org/syntax/podspec.html#description
description is in the form:
spec.description = <<-DESC
Computes the meaning of life.
Features:
1. Is self aware
...
42. Likes candies.
DESC
"""
# from "<<-DESC" to "DESC"
description_end = description_start.strip('<-')
description_lines = []
started = False
for line in lines:
if started:
ended = line.strip().startswith(description_end)
if not ended:
description_lines.append(line)
else:
break
elif '.description' in line and description_start in line:
started = True
return '\n'.join(description_lines)