Skip to content

Commit 5ca30b1

Browse files
committed
Update DatafileHandler default methods
* Yield Packages and Dependencies before associating Packages to Resources Signed-off-by: Jono Yang <jyang@nexb.com>
1 parent ffc47b1 commit 5ca30b1

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/packagedcode/models.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -938,14 +938,14 @@ def assemble(cls, package_data, resource, codebase, package_adder=add_to_package
938938
if not package.license_expression:
939939
package.license_expression = cls.compute_normalized_license(package)
940940

941+
yield package
942+
941943
cls.assign_package_to_resources(
942944
package=package,
943945
resource=resource,
944946
codebase=codebase,
945947
package_adder=package_adder,
946948
)
947-
948-
yield package
949949
else:
950950
# we have no package, so deps are not for a specific package uid
951951
package_uid = None
@@ -1047,6 +1047,12 @@ def assemble_from_many(cls, pkgdata_resources, codebase, package_adder=add_to_pa
10471047

10481048
# process each package in sequence. The first item creates a package and
10491049
# the other only update
1050+
# We are saving the Packages, Dependencies, and Resources in lists until
1051+
# after we go through `pkgdata_resources` for all Package data, then we
1052+
# yield Packages, then Dependencies, then Resources.
1053+
dependencies = []
1054+
resources = []
1055+
resources_from_package = []
10501056
for package_data, resource in pkgdata_resources:
10511057
if not base_resource:
10521058
base_resource = resource
@@ -1059,8 +1065,6 @@ def assemble_from_many(cls, pkgdata_resources, codebase, package_adder=add_to_pa
10591065
datafile_path=resource.path,
10601066
)
10611067
package_uid = package.package_uid
1062-
if package_uid:
1063-
package_adder(package_uid, resource, codebase)
10641068
else:
10651069
# FIXME: What is the package_data is NOT for the same package as package?
10661070
# FIXME: What if the update did not do anything? (it does return True or False)
@@ -1069,31 +1073,40 @@ def assemble_from_many(cls, pkgdata_resources, codebase, package_adder=add_to_pa
10691073
package_data=package_data,
10701074
datafile_path=resource.path,
10711075
)
1072-
if package_uid:
1073-
package_adder(package_uid, resource, codebase)
1076+
1077+
if package_uid:
1078+
resources_from_package.append((package_uid, resource,))
10741079

10751080
# in all cases yield possible dependencies
10761081
dependent_packages = package_data.dependencies
10771082
if dependent_packages:
1078-
yield from Dependency.from_dependent_packages(
1083+
p_deps = Dependency.from_dependent_packages(
10791084
dependent_packages=dependent_packages,
10801085
datafile_path=resource.path,
10811086
datasource_id=package_data.datasource_id,
10821087
package_uid=package_uid,
10831088
)
1089+
dependencies.extend(list(p_deps))
10841090

10851091
# we yield this as we do not want this further processed
1086-
yield resource
1087-
1088-
# the whole parent subtree of the base_resource is for this package
1089-
if package_uid:
1090-
for res in base_resource.walk(codebase):
1091-
package_adder(package_uid, res, codebase)
1092+
resources.append(resource)
10921093

1094+
# Yield Packages, Dependencies, and Resources
10931095
if package:
10941096
if not package.license_expression:
10951097
package.license_expression = cls.compute_normalized_license(package)
10961098
yield package
1099+
yield from dependencies
1100+
yield from resources
1101+
1102+
# Associate Package to Resources once they have been yielded
1103+
for package_uid, resource in resources_from_package:
1104+
package_adder(package_uid, resource, codebase)
1105+
1106+
# the whole parent subtree of the base_resource is for this package
1107+
if package_uid:
1108+
for res in base_resource.walk(codebase):
1109+
package_adder(package_uid, res, codebase)
10971110

10981111
@classmethod
10991112
def assemble_from_many_datafiles(

0 commit comments

Comments
 (0)