-
-
Notifications
You must be signed in to change notification settings - Fork 791
Expand file tree
/
Copy path__init__.py
More file actions
271 lines (238 loc) · 8.42 KB
/
Copy path__init__.py
File metadata and controls
271 lines (238 loc) · 8.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
#
# 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 attr
from packagedcode import about
from packagedcode import bower
from packagedcode import build
from packagedcode import cargo
from packagedcode import chef
from packagedcode import debian
from packagedcode import conda
from packagedcode import cocoapods
from packagedcode import freebsd
from packagedcode import golang
from packagedcode import haxe
from packagedcode import jar_manifest
from packagedcode import maven
from packagedcode import models
from packagedcode import msi
from packagedcode import npm
from packagedcode import nuget
from packagedcode import opam
from packagedcode import phpcomposer
from packagedcode import pubspec
from packagedcode import pypi
from packagedcode import readme
from packagedcode import rpm
from packagedcode import rubygems
from packagedcode import win_pe
from packagedcode import windows
# Note: the order matters: from the most to the least specific
# Package classes MUST be added to this list to be active
PACKAGE_MANIFEST_TYPES = [
rpm.RpmManifest,
debian.DebianPackage,
models.JavaJar,
jar_manifest.JavaManifest,
models.JavaEar,
models.JavaWar,
maven.MavenPomPackage,
jar_manifest.IvyJar,
models.JBossSar,
models.Axis2Mar,
about.Aboutfile,
npm.PackageJson,
npm.PackageLockJson,
npm.YarnLockJson,
phpcomposer.ComposerJson,
phpcomposer.ComposerLock,
haxe.HaxelibJson,
cargo.CargoToml,
cargo.CargoLock,
cocoapods.Podspec,
cocoapods.PodfileLock,
cocoapods.PodspecJson,
opam.OpamFile,
models.MeteorPackage,
bower.BowerJson,
freebsd.CompactManifest,
models.CpanModule,
rubygems.GemArchive,
rubygems.GemArchiveExtracted,
rubygems.GemSpec,
rubygems.GemfileLock,
models.AndroidApp,
models.AndroidLibrary,
models.MozillaExtension,
models.ChromeExtension,
models.IOSApp,
pypi.MetadataFile,
pypi.BinaryDistArchive,
pypi.SourceDistArchive,
pypi.SetupPy,
pypi.DependencyFile,
pypi.PipfileLock,
pypi.RequirementsFile,
golang.GoMod,
golang.GoSum,
models.CabPackage,
models.InstallShieldPackage,
models.NSISInstallerPackage,
nuget.Nuspec,
models.SharPackage,
models.AppleDmgPackage,
models.IsoImagePackage,
models.SquashfsPackage,
chef.MetadataJson,
chef.Metadatarb,
build.BazelPackage,
build.BuckPackage,
build.AutotoolsPackage,
conda.Condayml,
win_pe.WindowsExecutableManifest,
readme.ReadmeManifest,
build.MetadataBzl,
msi.MsiInstallerPackage,
windows.MicrosoftUpdateManifest,
pubspec.PubspecYaml,
pubspec.PubspecLock
]
PACKAGE_MANIFESTS_BY_TYPE = {
(
cls.package_manifest_type
if isinstance(cls, models.PackageManifest)
else cls.default_type
): cls
for cls in PACKAGE_MANIFEST_TYPES
}
# We cannot have two package classes with the same type
if len(PACKAGE_MANIFESTS_BY_TYPE) != len(PACKAGE_MANIFEST_TYPES):
seen_types = {}
for pmt in PACKAGE_MANIFEST_TYPES:
manifest = pmt()
assert manifest.package_manifest_type
seen = seen_types.get(manifest.package_manifest_type)
if seen:
msg = ('Invalid duplicated packagedcode.Package types: '
'"{}:{}" and "{}:{}" have the same type.'
.format(manifest.package_manifest_type, manifest.__name__, seen.package_manifest_type, seen.__name__,))
raise Exception(msg)
else:
seen_types[manifest.package_manifest_type] = manifest
def get_package_class(scan_data, default=models.Package):
"""
Return the Package subclass that corresponds to the package type in a
mapping of package `scan_data`.
For example:
>>> data = {'type': 'cpan'}
>>> assert models.CpanModule == get_package_class(data)
>>> data = {'type': 'some stuff'}
>>> assert models.Package == get_package_class(data)
>>> data = {'type': None}
>>> assert models.Package == get_package_class(data)
>>> data = {}
>>> assert models.Package == get_package_class(data)
>>> data = []
>>> assert models.Package == get_package_class(data)
>>> data = None
>>> assert models.Package == get_package_class(data)
"""
ptype = scan_data and scan_data.get('type') or None
if not ptype:
# basic type for default package types
return default
ptype_class = PACKAGE_MANIFESTS_BY_TYPE.get(ptype)
return ptype_class or default
def get_package_instance(scan_data):
"""
Return a Package instance re-built from a mapping of ``scan_data`` native
Python data that has the structure of a scan. Known attributes that store a
list of objects are also "rehydrated" (such as models.Party).
The Package instance will use the Package subclass that supports the
provided package "type" when possible or the base Package class otherwise.
Unknown attributes provided in ``scan_data`` that do not exist as fields in
the Package class are kept as items in the Package.extra_data mapping.
An Exception is raised if an "unknown attribute" name already exists as
a Package.extra_data key.
"""
# TODO: consider using a proper library for this such as cattrs,
# marshmallow, etc. or use the field type that we declare.
# Each of these are lists of class instances tracked here, which are stored
# as a list of mappings in scanc_data
list_field_types_by_name = {
'parties': models.Party,
'dependencies': models.DependentPackage,
'installed_files': models.PackageFile,
}
# these are computed attributes serialized on a package
# that should not be recreated when serializing
computed_attributes = set([
'purl',
'repository_homepage_url',
'repository_download_url',
'api_data_url'
])
# re-hydrate lists of typed objects
klas = get_package_class(scan_data)
existing_fields = attr.fields_dict(klas)
extra_data = scan_data.get('extra_data', {}) or {}
package_data = {}
for key, value in scan_data.items():
if not value or key in computed_attributes:
continue
field = existing_fields.get(key)
if not field:
if key not in extra_data:
# keep unknown field as extra data
extra_data[key] = value
continue
else:
raise Exception(
f'Invalid scan_data with duplicated key: {key}={value!r} '
f'present both as attribute AND as extra_data: '
f'{key}={extra_data[key]!r}'
)
list_field_type = list_field_types_by_name.get(key)
if not list_field_type:
# this is a plain known field
package_data[key] = value
continue
# Since we have a list_field_type, value must be a list of mappings:
# we transform it in a list of objects.
if not isinstance(value, list):
raise Exception(
f'Invalid scan_data with unknown data structure. '
f'Expected the value to be a list of dicts and not a '
f'{type(value)!r} for {key}={value!r}'
)
objects = list(_build_objects_list(values=value, klass=list_field_type))
package_data[key] = objects
return klas(**package_data)
def _build_objects_list(values, klass):
"""
Yield ``klass`` objects built from a ``values`` list of mappings.
"""
# Since we have a list_field_type, value must be a list of mappings:
# we transform it in a list of objects.
if not isinstance(values, list):
raise Exception(
f'Invalid scan_data with unknown data structure. '
f'Expected the value to be a list of dicts and not a '
f'{type(values)!r} for {values!r}'
)
for val in values:
if not val:
continue
if not isinstance(val, dict):
raise Exception(
f'Invalid scan_data with unknown data structure. '
f'Expected the value to be a mapping for and not a '
f'{type(val)!r} for {values!r}'
)
yield klass.create(**val)