Skip to content

Commit cfebc0c

Browse files
committed
Update assemble method for npm packages #2929
* Properly assign Package resources to correct package * Update test results Signed-off-by: Jono Yang <[email protected]>
1 parent 16ae20a commit cfebc0c

File tree

66 files changed

+607
-767
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+607
-767
lines changed

src/packagedcode/npm.py

Lines changed: 88 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,106 @@
3636
# TODO: add support for "lockfileVersion": 2 for package-lock.json and lockfileVersion: 3
3737

3838

39+
def yield_npm_dependencies_from_package_data(package_data, datafile_path, package_uid):
40+
"""
41+
Yield a Dependency for each dependency from ``package_data.dependencies``
42+
"""
43+
dependent_packages = package_data.dependencies
44+
if dependent_packages:
45+
yield from models.Dependency.from_dependent_packages(
46+
dependent_packages=dependent_packages,
47+
datafile_path=datafile_path,
48+
datasource_id=package_data.datasource_id,
49+
package_uid=package_uid,
50+
)
51+
52+
53+
def yield_npm_dependencies_from_package_resource(resource, package_uid=None):
54+
"""
55+
Yield a Dependency for each dependency from each package from``resource.package_data``
56+
"""
57+
for pkg_data in resource.package_data:
58+
pkg_data = models.PackageData.from_dict(pkg_data)
59+
yield_npm_dependencies_from_package_data(pkg_data, resource.location, package_uid)
60+
61+
3962
class BaseNpmHandler(models.DatafileHandler):
4063

4164
@classmethod
4265
def assemble(cls, package_data, resource, codebase):
66+
"""
67+
If ``resource``, or one of its siblings, is a package.json file, use it
68+
to create and yield the package, the package dependencies, and the
69+
package resources.
70+
71+
When reporting the resources of a package, we alk the codebase, skipping
72+
the node_modules directory, assign resources to the package and yield
73+
resources.
74+
75+
For each lock file, assign dependencies to package instances and yield dependencies.
76+
77+
If there is no package.json, we do not have a package instance. In this
78+
case, we yield each of the dependencies in each lock file.
79+
"""
4380
datafile_name_patterns = (
44-
'package.json',
4581
'package-lock.json',
4682
'.package-lock.json',
4783
'npm-shrinkwrap.json',
4884
'yarn.lock',
4985
)
5086

51-
if resource.has_parent():
52-
dir_resource=resource.parent(codebase)
53-
else:
54-
dir_resource=resource
55-
56-
for assembled in cls.assemble_from_many_datafiles(
57-
datafile_name_patterns=datafile_name_patterns,
58-
directory=dir_resource,
59-
codebase=codebase,
60-
):
61-
if isinstance(assembled, models.Package):
62-
cls.assign_package_to_resources(
63-
package=assembled,
64-
resource=resource,
65-
codebase=codebase,
87+
package_resource = None
88+
if resource.name == 'package.json':
89+
package_resource = resource
90+
elif resource.name in datafile_name_patterns:
91+
if resource.has_parent():
92+
siblings = resource.siblings(codebase)
93+
package_resource = [r for r in siblings if r.name == 'package.json']
94+
if package_resource:
95+
package_resource = package_resource[0]
96+
97+
if package_resource:
98+
# do we have enough to create a package?
99+
if package_data.purl:
100+
package = models.Package.from_package_data(
101+
package_data=package_data,
102+
datafile_path=package_resource.path,
66103
)
67-
yield assembled
104+
package_uid = package.package_uid
105+
106+
if not package.license_expression:
107+
package.license_expression = compute_normalized_license(package.declared_license)
108+
109+
root = resource.parent(codebase)
110+
if root:
111+
for npm_res in cls.walk_npm(resource=root, codebase=codebase):
112+
if package_uid not in npm_res.for_packages:
113+
npm_res.for_packages.append(package_uid)
114+
npm_res.save(codebase)
115+
yield npm_res
116+
117+
yield package
118+
else:
119+
# we have no package, so deps are not for a specific package uid
120+
package_uid = None
121+
122+
# in all cases yield possible dependencies
123+
yield_npm_dependencies_from_package_data(package_data, package_resource.path, package_uid)
124+
125+
# we yield this as we do not want this further processed
126+
yield package_resource
127+
128+
for sibling in package_resource.siblings(codebase):
129+
if sibling.name in datafile_name_patterns:
130+
yield_npm_dependencies_from_package_resource(sibling, package_uid)
131+
132+
if package_uid not in sibling.for_packages:
133+
sibling.for_packages.append(package_uid)
134+
sibling.save(codebase)
135+
yield sibling
136+
else:
137+
# we do not have a package.json
138+
yield_npm_dependencies_from_package_resource(resource)
68139

69140
@classmethod
70141
def walk_npm(cls, resource, codebase, depth=0):
@@ -87,16 +158,6 @@ def walk_npm(cls, resource, codebase, depth=0):
87158
for subchild in cls.walk_skip(child, codebase, depth=depth):
88159
yield subchild
89160

90-
# TODO: this MUST BE USED
91-
@classmethod
92-
def assign_package_to_resources(cls, package, resource, codebase):
93-
"""
94-
Yield the Resources of an npm Package, ignoring nested mode_modules.
95-
"""
96-
root = resource.parent(codebase)
97-
if root:
98-
yield from cls.walk_npm(resource=root, codebase=codebase)
99-
100161

101162
def get_urls(namespace, name, version):
102163
return dict(

tests/licensedcode/data/plugin_licenses_reference/scan.expected.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@
1313
"output_format_version": "2.0.0",
1414
"message": null,
1515
"errors": [],
16+
"warnings": [],
1617
"extra_data": {
18+
"system_environment": {
19+
"operating_system": "linux",
20+
"cpu_architecture": "64",
21+
"platform": "Linux-5.4.0-107-generic-x86_64-with-Ubuntu-18.04-bionic",
22+
"platform_version": "#121~18.04.1-Ubuntu SMP Thu Mar 24 17:21:33 UTC 2022",
23+
"python_version": "3.6.9 (default, Mar 15 2022, 13:55:28) \n[GCC 8.4.0]"
24+
},
1725
"spdx_license_list_version": "3.16",
1826
"files_count": 2
1927
}
@@ -314,7 +322,9 @@
314322
],
315323
"percentage_of_license_text": 100.0,
316324
"package_data": [],
317-
"for_packages": [],
325+
"for_packages": [
326+
"pkg:npm/[email protected]?uuid=fixed-uid-done-for-testing-5642512d1758"
327+
],
318328
"scan_errors": []
319329
},
320330
{

tests/packagedcode/data/about/aboutfiles.expected.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
"errors": [],
1414
"warnings": [],
1515
"extra_data": {
16+
"system_environment": {
17+
"operating_system": "linux",
18+
"cpu_architecture": "64",
19+
"platform": "Linux-5.4.0-107-generic-x86_64-with-Ubuntu-18.04-bionic",
20+
"platform_version": "#121~18.04.1-Ubuntu SMP Thu Mar 24 17:21:33 UTC 2022",
21+
"python_version": "3.6.9 (default, Mar 15 2022, 13:55:28) \n[GCC 8.4.0]"
22+
},
1623
"spdx_license_list_version": "3.16",
1724
"files_count": 3
1825
}

tests/packagedcode/data/bower/scan-expected.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
"errors": [],
1414
"warnings": [],
1515
"extra_data": {
16+
"system_environment": {
17+
"operating_system": "linux",
18+
"cpu_architecture": "64",
19+
"platform": "Linux-5.4.0-107-generic-x86_64-with-Ubuntu-18.04-bionic",
20+
"platform_version": "#121~18.04.1-Ubuntu SMP Thu Mar 24 17:21:33 UTC 2022",
21+
"python_version": "3.6.9 (default, Mar 15 2022, 13:55:28) \n[GCC 8.4.0]"
22+
},
1623
"spdx_license_list_version": "3.16",
1724
"files_count": 1
1825
}

tests/packagedcode/data/build/bazel/end2end-expected.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
"errors": [],
1414
"warnings": [],
1515
"extra_data": {
16+
"system_environment": {
17+
"operating_system": "linux",
18+
"cpu_architecture": "64",
19+
"platform": "Linux-5.4.0-107-generic-x86_64-with-Ubuntu-18.04-bionic",
20+
"platform_version": "#121~18.04.1-Ubuntu SMP Thu Mar 24 17:21:33 UTC 2022",
21+
"python_version": "3.6.9 (default, Mar 15 2022, 13:55:28) \n[GCC 8.4.0]"
22+
},
1623
"spdx_license_list_version": "3.16",
1724
"files_count": 6
1825
}

tests/packagedcode/data/build/buck/end2end-expected.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
"errors": [],
1414
"warnings": [],
1515
"extra_data": {
16+
"system_environment": {
17+
"operating_system": "linux",
18+
"cpu_architecture": "64",
19+
"platform": "Linux-5.4.0-107-generic-x86_64-with-Ubuntu-18.04-bionic",
20+
"platform_version": "#121~18.04.1-Ubuntu SMP Thu Mar 24 17:21:33 UTC 2022",
21+
"python_version": "3.6.9 (default, Mar 15 2022, 13:55:28) \n[GCC 8.4.0]"
22+
},
1623
"spdx_license_list_version": "3.16",
1724
"files_count": 7
1825
}

tests/packagedcode/data/build_gradle/end2end-expected.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
"errors": [],
1414
"warnings": [],
1515
"extra_data": {
16+
"system_environment": {
17+
"operating_system": "linux",
18+
"cpu_architecture": "64",
19+
"platform": "Linux-5.4.0-107-generic-x86_64-with-Ubuntu-18.04-bionic",
20+
"platform_version": "#121~18.04.1-Ubuntu SMP Thu Mar 24 17:21:33 UTC 2022",
21+
"python_version": "3.6.9 (default, Mar 15 2022, 13:55:28) \n[GCC 8.4.0]"
22+
},
1623
"spdx_license_list_version": "3.16",
1724
"files_count": 1
1825
}

tests/packagedcode/data/cargo/scan.expected.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
"errors": [],
1414
"warnings": [],
1515
"extra_data": {
16+
"system_environment": {
17+
"operating_system": "linux",
18+
"cpu_architecture": "64",
19+
"platform": "Linux-5.4.0-107-generic-x86_64-with-Ubuntu-18.04-bionic",
20+
"platform_version": "#121~18.04.1-Ubuntu SMP Thu Mar 24 17:21:33 UTC 2022",
21+
"python_version": "3.6.9 (default, Mar 15 2022, 13:55:28) \n[GCC 8.4.0]"
22+
},
1623
"spdx_license_list_version": "3.16",
1724
"files_count": 3
1825
}

tests/packagedcode/data/chef/package.scan.expected.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
"errors": [],
1414
"warnings": [],
1515
"extra_data": {
16+
"system_environment": {
17+
"operating_system": "linux",
18+
"cpu_architecture": "64",
19+
"platform": "Linux-5.4.0-107-generic-x86_64-with-Ubuntu-18.04-bionic",
20+
"platform_version": "#121~18.04.1-Ubuntu SMP Thu Mar 24 17:21:33 UTC 2022",
21+
"python_version": "3.6.9 (default, Mar 15 2022, 13:55:28) \n[GCC 8.4.0]"
22+
},
1623
"spdx_license_list_version": "3.16",
1724
"files_count": 2
1825
}

tests/packagedcode/data/cocoapods/assemble/many-podspecs-expected.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
"errors": [],
1414
"warnings": [],
1515
"extra_data": {
16+
"system_environment": {
17+
"operating_system": "linux",
18+
"cpu_architecture": "64",
19+
"platform": "Linux-5.4.0-107-generic-x86_64-with-Ubuntu-18.04-bionic",
20+
"platform_version": "#121~18.04.1-Ubuntu SMP Thu Mar 24 17:21:33 UTC 2022",
21+
"python_version": "3.6.9 (default, Mar 15 2022, 13:55:28) \n[GCC 8.4.0]"
22+
},
1623
"spdx_license_list_version": "3.16",
1724
"files_count": 9
1825
}

0 commit comments

Comments
 (0)