aboutcode-org / commoncode

A library of common functions shared in many other AboutCode projects
3 stars 11 forks source link

Implement `for_packages_append` on `Resource` #43

Closed JonoYang closed 2 years ago

JonoYang commented 2 years ago

We are updating the application package scanning process on scancode.io in https://github.com/nexB/scancode.io/issues/447. We are implementing the package assembly step from scancode-toolkit in scancode.io. The assembly methods from packagedcode associates Resources to packages by appending the package_uid to the for_packages attribute on Resources. This method of associating Resources to Packages does not work on scancode.io because for_packages is an property on CodebaseResource that generates a list of purls from DiscoveredPackages associated with a CodebaseResource.

A solution would be to create a method on the Resource class named for_packages_append that appends a package_uid to Resource.for_packages. This extra level of indirection allows us to create a different implementation on CodebaseResource for associating Packages to Resources using the same interface.

pombredanne commented 2 years ago

re:

A solution would be to create a method on the Resource class named for_packages_append that appends a package_uid to Resource.for_packages

This can work.

An alternative could be use a new Codebase argument that would be the Resource class to use... today this class is crafted based on the provided attributes https://github.com/nexB/commoncode/blob/d3eed9acb55e2437205e54746a8a7e9f0307f249/src/commoncode/resource.py#L402

Another possibility could be to add and pass a add_to_package(resource, ...) method as an argument to the assemble(..., package_adder=add_to_package, ...) functions? With a default that would be using a list-backed implementation... and then in SCIO you would have a Django db-backed implmentation of the same function and you would call it when you call assemble?

JonoYang commented 2 years ago

Another possibility could be to add and pass a add_to_package(resource, ...) method as an argument to the assemble(..., package_adder=add_to_package, ...) functions? With a default that would be using a list-backed implementation... and then in SCIO you would have a Django db-backed implmentation of the same function and you would call it when you call assemble?

I like this approach better than the one I posted before. It makes more sense to pass in the package adding function as an argument into assemble than to add a method to Resource that deals with an attribute that may not be there.

pombredanne commented 2 years ago

This has been implemented and merged in https://github.com/nexB/scancode-toolkit/pull/3035 Thanks!