Skip to content

Commit

Permalink
Move installed_files to Package model #2058
Browse files Browse the repository at this point in the history
And add size to PackageFile model

Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
  • Loading branch information
pombredanne committed Mar 3, 2021
1 parent 6dd99c2 commit eb52eb5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 53 deletions.
6 changes: 0 additions & 6 deletions src/packagedcode/alpine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 4 additions & 16 deletions src/packagedcode/debian.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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'):
Expand Down
79 changes: 48 additions & 31 deletions src/packagedcode/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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
Expand Down

0 comments on commit eb52eb5

Please sign in to comment.