77# See https://aboutcode.org for more information about nexB OSS projects.
88#
99
10+ import os
11+ import logging
1012from pathlib import Path
1113
1214from commoncode import fileutils
2224Handle Debian package archives, control files and installed databases.
2325"""
2426
27+ SCANCODE_DEBUG_PACKAGE_API = os .environ .get ('SCANCODE_DEBUG_PACKAGE_API' , False )
28+
29+ TRACE = SCANCODE_DEBUG_PACKAGE_API
30+
31+ def logger_debug (* args ):
32+ pass
33+
34+ logger = logging .getLogger (__name__ )
35+
36+ if TRACE :
37+ import sys
38+ logging .basicConfig (stream = sys .stdout )
39+ logger .setLevel (logging .DEBUG )
40+
41+ def logger_debug (* args ):
42+ return logger .debug (
43+ ' ' .join (isinstance (a , str ) and a or repr (a ) for a in args )
44+ )
45+
46+
2547# TODO: add dependencies
2648
2749
@@ -213,6 +235,9 @@ def assemble(cls, package_data, resource, codebase):
213235 package_data = package_data ,
214236 datafile_path = resource .path ,
215237 )
238+
239+ package_file_references = []
240+ package_file_references .extend (package_data .file_references )
216241 package_uid = package .package_uid
217242
218243 dependent_packages = package_data .dependencies
@@ -230,7 +255,13 @@ def assemble(cls, package_data, resource, codebase):
230255 # We only need to adjust the md5sum/list path in the case of `same`
231256 qualifiers = package_data .qualifiers or {}
232257 architecture = qualifiers .get ('architecture' )
233- multi_arch = package_data .extra_data .get ('multi-arch' )
258+
259+ multi_arch = package_data .extra_data .get ('multi_arch' )
260+
261+ if TRACE :
262+ logger_debug (f' debian: assemble: multi_arch: { multi_arch } ' )
263+ logger_debug (f' debian: assemble: architecture: { architecture } ' )
264+
234265 if multi_arch == 'same' :
235266 arch_path = f':{ architecture } '
236267 else :
@@ -254,21 +285,23 @@ def assemble(cls, package_data, resource, codebase):
254285 continue
255286
256287 for pkgdt in res .package_data :
288+ package_data = models .PackageData .from_dict (pkgdt )
257289 package .update (
258- package_data = pkgdt ,
290+ package_data = package_data ,
259291 datafile_path = res .path ,
260- )
292+ )
293+ package_file_references .extend (package_data .file_references )
261294
262295 res .for_packages .append (package_uid )
263296 res .save (codebase )
264297
265298 # yield possible dependencies
266- dependent_packages = pkgdt .dependencies
299+ dependent_packages = package_data .dependencies
267300 if dependent_packages :
268301 yield from models .Dependency .from_dependent_packages (
269302 dependent_packages = dependent_packages ,
270303 datafile_path = res .path ,
271- datasource_id = pkgdt .datasource_id ,
304+ datasource_id = package_data .datasource_id ,
272305 package_uid = package_uid ,
273306 )
274307
@@ -280,7 +313,7 @@ def assemble(cls, package_data, resource, codebase):
280313 # FIXME: should we consider ONLY the md5sums?
281314 # merge references for the same path (e.g. .list amd .md5sum)
282315 file_references_by_path = {}
283- for ref in package . file_references :
316+ for ref in package_file_references :
284317 # a file ref extends from the root of the filesystem
285318 ref_path = str (root_path / ref .path )
286319 existing = file_references_by_path .get (ref_path )
@@ -289,8 +322,6 @@ def assemble(cls, package_data, resource, codebase):
289322 else :
290323 file_references_by_path [ref_path ] = ref
291324
292- package .file_references = sorted (file_references_by_path .values (), key = lambda r : r .path )
293-
294325 for res in root_resource .walk (codebase ):
295326 ref = file_references_by_path .get (res .path )
296327 if not ref :
@@ -514,6 +545,9 @@ def parse_debian_files_list(location, datasource_id, package_type):
514545
515546 ref = models .FileReference (path = path , md5 = md5sum )
516547 file_references .append (ref )
548+
549+ if not file_references :
550+ return
517551
518552 yield models .PackageData (
519553 datasource_id = datasource_id ,
0 commit comments