Top-level dependency instance from parsed package data collected from data files such as a package manifest or lockfile.
| 397 | |
| 398 | @attr.attributes(slots=True) |
| 399 | class Dependency(DependentPackage): |
| 400 | """ |
| 401 | Top-level dependency instance from parsed package data collected from data |
| 402 | files such as a package manifest or lockfile. |
| 403 | """ |
| 404 | dependency_uid = String( |
| 405 | label='Dependency unique id', |
| 406 | help='A unique identifier for this dependency instance.' |
| 407 | 'Consists of the dependency purl with a UUID qualifier.' |
| 408 | ) |
| 409 | |
| 410 | # TODO: should we also repeat the purl here: this may be redundant but this |
| 411 | # would help avoid lookups |
| 412 | for_package_uid = String( |
| 413 | label='A Package unique id', |
| 414 | help='The unique id of the package instance to which this dependency ' |
| 415 | 'file belongs. This is the purl with a uuid qualifier.' |
| 416 | ) |
| 417 | |
| 418 | datafile_path = String( |
| 419 | label='Path to datafile.', |
| 420 | help='A POSIX path string to the package datafile that describes this ' |
| 421 | 'dependency.' |
| 422 | ) |
| 423 | |
| 424 | datasource_id = String( |
| 425 | label='datasource id', |
| 426 | help='Datasource identifier for the source of these package data.' |
| 427 | ) |
| 428 | |
| 429 | def __attrs_post_init__(self, *args, **kwargs): |
| 430 | if not self.dependency_uid: |
| 431 | self.dependency_uid = build_package_uid(self.purl) |
| 432 | |
| 433 | @classmethod |
| 434 | def from_dependent_package( |
| 435 | cls, |
| 436 | dependent_package, |
| 437 | datafile_path, |
| 438 | datasource_id, |
| 439 | package_uid=None, |
| 440 | ): |
| 441 | """ |
| 442 | Return a Dependency from a ``dependent_package`` DependentPackage object |
| 443 | or mapping. |
| 444 | """ |
| 445 | if isinstance(dependent_package, DependentPackage): |
| 446 | dependent_package = dependent_package.to_dict() |
| 447 | else: |
| 448 | # make a copy |
| 449 | dependent_package = dict(dependent_package) |
| 450 | |
| 451 | dependent_package['datafile_path'] = datafile_path |
| 452 | dependent_package['datasource_id'] = datasource_id |
| 453 | dependent_package['for_package_uid'] = package_uid |
| 454 | |
| 455 | return cls.from_dict(dependent_package) |
| 456 |