diff --git a/src/packagedcode/alpine.py b/src/packagedcode/alpine.py index e4f5d944c82..df5c6b38a6c 100644 --- a/src/packagedcode/alpine.py +++ b/src/packagedcode/alpine.py @@ -29,12 +29,6 @@ class AlpinePackage(models.Package): extensions = ('.apk',) default_type = 'alpine' - - installed_files = List( - item_type=models.PackageFile, - label='installed files', - help='List of files installed by this package.') - def compute_normalized_license(self): _declared, detected = detect_declared_license(self.declared_license) return detected diff --git a/src/packagedcode/debian.py b/src/packagedcode/debian.py index 1b49999e353..92796f2ac75 100644 --- a/src/packagedcode/debian.py +++ b/src/packagedcode/debian.py @@ -15,12 +15,9 @@ from packageurl import PackageURL from commoncode import filetype -from commoncode import fileutils -from commoncode.datautils import List from commoncode.datautils import String from packagedcode import models - """ Handle Debian packages. """ @@ -47,24 +44,17 @@ class DebianPackage(models.Package): label='Multi-Arch', help='Multi-Arch value from status file') - installed_files = List( - item_type=models.PackageFile, - label='installed files', - help='List of files installed by this package.') - def to_dict(self, _detailed=False, **kwargs): - data = models.Package.to_dict(self, **kwargs) + data = super().to_dict(_detailed=_detailed, **kwargs) if _detailed: ################################################# - # remove temporary fields + # populate temporary fields data['multi_arch'] = self.multi_arch - data['installed_files'] = [istf.to_dict() for istf in (self.installed_files or [])] ################################################# else: ################################################# # remove temporary fields data.pop('multi_arch', None) - data.pop('installed_files', None) ################################################# return data @@ -178,8 +168,7 @@ def get_copyright_file_path(self, root_dir): def get_installed_packages(root_dir, distro='debian', detect_licenses=False, **kwargs): """ - Given a directory to a rootfs, yield a DebianPackage and a list of `installed_files` - (path, md5sum) tuples. + Yield installed Package objects given a ``root_dir`` rootfs directory. """ base_status_file_loc = os.path.join(root_dir, 'var/lib/dpkg/status') @@ -199,8 +188,7 @@ def get_installed_packages(root_dir, distro='debian', detect_licenses=False, **k def is_debian_status_file(location): - return (filetype.is_file(location) - and fileutils.file_name(location).lower() == 'status') + return filetype.is_file(location) and location.endswith('/status') def parse_status_file(location, distro='debian'): diff --git a/src/packagedcode/models.py b/src/packagedcode/models.py index fa32e0d7002..840d3aa5ab1 100644 --- a/src/packagedcode/models.py +++ b/src/packagedcode/models.py @@ -336,6 +336,41 @@ class DependentPackage(BaseModel): 'exact version.') +@attr.s() +class PackageFile(BaseModel): + """ + A file that belongs to a package. + """ + + path = String( + label='Path of this installed file', + help='The path of this installed file either relative to a rootfs ' + '(typical for system packages) or a path in this scan (typical for ' + 'application packages).', + repr=True, + ) + + size = Integer( + label='file size', + help='size of the file in bytes') + + sha1 = String( + label='SHA1 checksum', + help='SHA1 checksum for this file in hexadecimal') + + md5 = String( + label='MD5 checksum', + help='MD5 checksum for this file in hexadecimal') + + sha256 = String( + label='SHA256 checksum', + help='SHA256 checksum for this file in hexadecimal') + + sha512 = String( + label='SHA512 checksum', + help='SHA512 checksum for this file in hexadecimal') + + @attr.s() class Package(BasePackage): """ @@ -451,6 +486,11 @@ class Package(BasePackage): 'this package. For instance an SRPM is the "source package" for a ' 'binary RPM.') + installed_files = List( + item_type=PackageFile, + label='installed files', + help='List of files installed by this package.') + def __attrs_post_init__(self, *args, **kwargs): if not self.type and hasattr(self, 'default_type'): self.type = self.default_type @@ -552,6 +592,14 @@ def extra_root_dirs(cls): """ return [] + def to_dict(self, _detailed=False, **kwargs): + data = super().to_dict(**kwargs) + if _detailed: + data['installed_files'] = [istf.to_dict() for istf in (self.installed_files or [])] + else: + data.pop('installed_files', None) + return data + def compute_normalized_license(declared_license): """ @@ -573,37 +621,6 @@ def compute_normalized_license(declared_license): return 'unknown' -@attr.s() -class PackageFile(BaseModel): - """ - A file that belongs to a package. - """ - - path = String( - label='Path of this installed file', - help='The path of this installed file either relative to a rootfs ' - '(typical for system packages) or a path in this scan (typical for ' - 'application packages).', - repr=True, - ) - - sha1 = String( - label='SHA1 checksum', - help='SHA1 checksum for this file in hexadecimal') - - md5 = String( - label='MD5 checksum', - help='MD5 checksum for this file in hexadecimal') - - sha256 = String( - label='SHA256 checksum', - help='SHA256 checksum for this file in hexadecimal') - - sha512 = String( - label='SHA512 checksum', - help='SHA512 checksum for this file in hexadecimal') - - # Package types # NOTE: this is somewhat redundant with extractcode archive handlers # yet the purpose and semantics are rather different here