-
-
Notifications
You must be signed in to change notification settings - Fork 561
/
models.py
1276 lines (1014 loc) · 41.4 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import fnmatch
import logging
import os
import sys
import attr
from packageurl import normalize_qualifiers
from packageurl import PackageURL
from commoncode.datautils import choices
from commoncode.datautils import Boolean
from commoncode.datautils import Date
from commoncode.datautils import Integer
from commoncode.datautils import List
from commoncode.datautils import Mapping
from commoncode.datautils import String
from commoncode.datautils import TriBoolean
from commoncode import filetype
from commoncode.fileutils import file_name
from commoncode.fileutils import splitext_name
from typecode import contenttype
"""
Data models for package information and dependencies, and also data models
for package data for abstracting the differences existing between
package formats and tools.
A package has a somewhat fuzzy definition and is code that can be consumed and
provisioned by a package manager or can be installed.
It can be a single file such as script; more commonly a package is stored in an
archive or directory.
A package contains:
- information typically in a "manifest" file,
- a payload of code, doc, data.
Structured package information are found in multiple places:
- in manifest file proper (such as a Maven POM, NPM package.json and many others)
- in binaries (such as an Elf or LKM, Windows PE or RPM header).
- in code (JavaDoc tags or Python __copyright__ magic)
There are collectively named "manifests" in ScanCode.
We handle package information at two levels:
1. package data information collected in a "manifest" or similar at a file level
2. packages at the codebase level, where a package contains
one or more package data, and files for that package.
The second requires the first to be computed.
These are case classes to extend:
- PackageData:
Base class with shared package attributes and methods.
- PackageDataFile:
Mixin class that represents a specific package manifest file.
- Package:
Mixin class that represents a package that's constructed from one or more
package data. It also tracks package files. Basically a package instance.
Here is an example of the classes that would need to exist to support a new fictitious
package type or ecosystem `dummy`.
- DummyPackageData(PackageData):
This class provides type wide defaults and basic implementation for type specific methods.
- DummyManifest(DummyPackageData, PackageDataFile) or DummyLockFile(DummyPackageData, PackageDataFile):
This class provides methods to recognize and parse a package manifest file format.
- DummyPackage(DummyPackageData, Package):
This class provides methods to create package instances for one or more manifests and to
collect package file paths.
"""
SCANCODE_DEBUG_PACKAGE_API = os.environ.get('SCANCODE_DEBUG_PACKAGE_API', False)
TRACE = False or SCANCODE_DEBUG_PACKAGE_API
TRACE_MERGING = False or SCANCODE_DEBUG_PACKAGE_API
def logger_debug(*args):
pass
logger = logging.getLogger(__name__)
if TRACE or TRACE_MERGING:
import logging
logging.basicConfig(stream=sys.stdout)
logger.setLevel(logging.DEBUG)
def logger_debug(*args):
return logger.debug(' '.join(isinstance(a, str) and a or repr(a) for a in args))
logger_debug = print
class BaseModel(object):
"""
Base class for all package models.
"""
def to_dict(self, **kwargs):
"""
Return an dict of primitive Python types.
"""
return attr.asdict(self)
@classmethod
def create(cls, **kwargs):
"""
Return an object built from ``kwargs``. Always ignore unknown attributes
provided in ``kwargs`` that do not exist as declared attr fields in
``cls``.
"""
known_attr = cls.fields()
kwargs = {k: v for k, v in kwargs.items() if k in known_attr}
return cls(**kwargs)
@classmethod
def fields(cls):
"""
Return a list of field names defined on this model.
"""
return list(attr.fields_dict(cls))
party_person = 'person'
# often loosely defined
party_project = 'project'
# more formally defined
party_org = 'organization'
PARTY_TYPES = (
None,
party_person,
party_project,
party_org,
)
@attr.s()
class Party(BaseModel):
"""
A party is a person, project or organization related to a package.
"""
type = String(
repr=True,
validator=choices(PARTY_TYPES),
label='party type',
help='the type of this party: One of: '
+', '.join(p for p in PARTY_TYPES if p))
role = String(
repr=True,
label='party role',
help='A role for this party. Something such as author, '
'maintainer, contributor, owner, packager, distributor, '
'vendor, developer, owner, etc.')
name = String(
repr=True,
label='name',
help='Name of this party.')
email = String(
repr=True,
label='email',
help='Email for this party.')
url = String(
repr=True,
label='url',
help='URL to a primary web page for this party.')
@attr.s()
class BasePackageData(BaseModel):
"""
A base identifiable package object using discrete identifying attributes as
specified here https://github.com/package-url/purl-spec.
"""
# class-level attributes used to recognize a package
filetypes = tuple()
mimetypes = tuple()
extensions = tuple()
# list of known metafiles for a package type
file_patterns = tuple()
# Optional. Public default web base URL for package homepages of this
# package type on the default repository.
default_web_baseurl = None
# Optional. Public default download base URL for direct downloads of this
# package type the default repository.
default_download_baseurl = None
# Optional. Public default API repository base URL for package API calls of
# this package type on the default repository.
default_api_baseurl = None
# Optional. Public default type for a package class.
default_type = None
# TODO: add description of the Package type for info
# type_description = None
type = String(
repr=True,
label='package type',
help='Optional. A short code to identify what is the type of this '
'package. For instance gem for a Rubygem, docker for container, '
'pypi for Python Wheel or Egg, maven for a Maven Jar, '
'deb for a Debian package, etc.')
namespace = String(
repr=True,
label='package namespace',
help='Optional namespace for this package.')
name = String(
repr=True,
label='package name',
help='Name of the package.')
version = String(
repr=True,
label='package version',
help='Optional version of the package as a string.')
qualifiers = Mapping(
default=None,
value_type=str,
converter=lambda v: normalize_qualifiers(v, encode=False),
label='package qualifiers',
help='Optional mapping of key=value pairs qualifiers for this package')
subpath = String(
label='extra package subpath',
help='Optional extra subpath inside a package and relative to the root '
'of this package')
def __attrs_post_init__(self, *args, **kwargs):
if not self.type and hasattr(self, 'default_type'):
self.type = self.default_type
@property
def purl(self):
"""
Return a compact purl package URL string.
"""
if not self.name:
return
return PackageURL(
self.type, self.namespace, self.name, self.version,
self.qualifiers, self.subpath).to_string()
def repository_homepage_url(self, baseurl=default_web_baseurl):
"""
Return the package repository homepage URL for this package, e.g. the
URL to the page for this package in its package repository. This is
typically different from the package homepage URL proper.
Subclasses should override to provide a proper value.
"""
return
def repository_download_url(self, baseurl=default_download_baseurl):
"""
Return the package repository download URL to download the actual
archive of code of this package. This may be different than the actual
download URL and is computed from the default public respoitory baseurl.
Subclasses should override to provide a proper value.
"""
return
def api_data_url(self, baseurl=default_api_baseurl):
"""
Return the package repository API URL to obtain structured data for this
package such as the URL to a JSON or XML api.
Subclasses should override to provide a proper value.
"""
return
def set_purl(self, package_url):
"""
Update this Package object with the `package_url` purl string or
PackageURL attributes.
"""
if not package_url:
return
if not isinstance(package_url, PackageURL):
package_url = PackageURL.from_string(package_url)
attribs = ['type', 'namespace', 'name', 'version', 'qualifiers', 'subpath']
for att in attribs:
self_val = getattr(self, att)
purl_val = getattr(package_url, att)
if not self_val and purl_val:
setattr(self, att, purl_val)
def to_dict(self, **kwargs):
"""
Return an dict of primitive Python types.
"""
mapping = attr.asdict(self)
if self.name:
mapping['purl'] = self.purl
mapping['repository_homepage_url'] = self.repository_homepage_url()
mapping['repository_download_url'] = self.repository_download_url()
mapping['api_data_url'] = self.api_data_url()
else:
mapping['purl'] = None
mapping['repository_homepage_url'] = None
mapping['repository_download_url'] = None
mapping['api_data_url'] = None
if self.qualifiers:
mapping['qualifiers'] = normalize_qualifiers(
qualifiers=self.qualifiers,
encode=False,
)
return mapping
@classmethod
def create(cls, **kwargs):
"""
Return a Package built from ``kwargs``. Always ignore unknown attributes
provided in ``kwargs`` that do not exist as declared attr fields in
``cls``.
"""
from packagedcode import get_package_class
type_cls = get_package_class(kwargs, default=cls)
return super(BasePackageData, type_cls).create(**kwargs)
@attr.s()
class DependentPackage(BaseModel):
"""
An identifiable dependent package package object.
"""
purl = String(
repr=True,
label='Dependent package URL',
help='A compact purl package URL. Typically when there is an '
'unresolved requirement, there is no version. '
'If the dependency is resolved, the version should be added to '
'the purl')
extracted_requirement = String(
repr=True,
label='dependent package version requirement',
help='A string defining version(s)requirements. Package-type specific '
'and as found in the lockfile/package-data.')
# ToDo: add `vers` support. See https://github.com/nexB/univers/blob/main/src/univers/version_range.py
scope = String(
repr=True,
label='dependency scope',
help='The scope of this dependency, such as runtime, install, etc. '
'This is package-type specific and is the original scope string.')
is_runtime = Boolean(
default=True,
label='is runtime flag',
help='True if this dependency is a runtime dependency.')
is_optional = Boolean(
default=False,
label='is optional flag',
help='True if this dependency is an optional dependency')
is_resolved = Boolean(
default=False,
label='is resolved flag',
help='True if this dependency version requirement has '
'been resolved and this dependency url points to an '
'exact version.')
resolved_package = Mapping(
label='resolved package data',
help='A mapping containing the package data for this DependentPackage, '
'resolved from the lockfile itself or by fetching from other sources.')
@attr.s
class Dependency(DependentPackage):
dependency_uuid = String(
label='Dependency instance UUID',
help='A unique ID for dependency instances in a codebase scan.'
'Consists of a pURL and an UUID field as a pURL qualifier.'
)
for_package = String(
label='A Package UUID',
help='The UUID of the package instance to which this dependency file belongs'
)
lockfile = String(
label='path to a lockfile',
help='A path string from where this dependency instance was created'
'Consists of a pURL and an UUID field as a pURL qualifier.'
)
@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 PackageData(BasePackageData):
"""
A package object as represented by either data from one of its different types of
package data or that of a package instance created from one or more of these
package data, and files for that package.
"""
# Optional. Public default type for a package class.
default_primary_language = None
primary_language = String(
label='Primary programming language',
help='Primary programming language',)
description = String(
label='Description',
help='Description for this package. '
'By convention the first should be a summary when available.')
release_date = Date(
label='release date',
help='Release date of the package')
parties = List(
item_type=Party,
label='parties',
help='A list of parties such as a person, project or organization.')
keywords = List(
item_type=str,
label='keywords',
help='A list of keywords.')
homepage_url = String(
label='homepage URL',
help='URL to the homepage for this package.')
download_url = String(
label='Download URL',
help='A direct download URL.')
size = Integer(
default=None,
label='download size',
help='size of the package download in bytes')
sha1 = String(
label='SHA1 checksum',
help='SHA1 checksum for this download in hexadecimal')
md5 = String(
label='MD5 checksum',
help='MD5 checksum for this download in hexadecimal')
sha256 = String(
label='SHA256 checksum',
help='SHA256 checksum for this download in hexadecimal')
sha512 = String(
label='SHA512 checksum',
help='SHA512 checksum for this download in hexadecimal')
bug_tracking_url = String(
label='bug tracking URL',
help='URL to the issue or bug tracker for this package')
code_view_url = String(
label='code view URL',
help='a URL where the code can be browsed online')
vcs_url = String(
help='a URL to the VCS repository in the SPDX form of: '
'https://github.com/nexb/scancode-toolkit.git@405aaa4b3 '
'See SPDX specification "Package Download Location" '
'at https://spdx.org/spdx-specification-21-web-version#h.49x2ik5 ')
copyright = String(
label='Copyright',
help='Copyright statements for this package. Typically one per line.')
license_expression = String(
label='license expression',
help='The license expression for this package typically derived '
'from its declared license or from some other type-specific '
'routine or convention.')
declared_license = String(
label='declared license',
help='The declared license mention, tag or text as found in a '
'package manifest. This can be a string, a list or dict of '
'strings possibly nested, as found originally in the manifest.')
notice_text = String(
label='notice text',
help='A notice text for this package.')
contains_source_code = TriBoolean(
label='contains source code',
help='Flag set to True if this package contains its own source code, None '
'if this is unknown, False if not.')
source_packages = List(
item_type=String,
label='List of related source code packages',
help='A list of related source code Package URLs (aka. "purl") for '
'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.')
extra_data = Mapping(
label='extra data',
help='A mapping of arbitrary extra Package data.')
def __attrs_post_init__(self, *args, **kwargs):
if not self.type and hasattr(self, 'default_type'):
self.type = self.default_type
if not self.primary_language and hasattr(self, 'default_primary_language'):
self.primary_language = self.default_primary_language
@classmethod
def get_package_resources(cls, package_root, codebase):
"""
Yield the Resources of a Package starting from `package_root`
"""
if not cls.is_ignored_package_resource(package_root, codebase):
yield package_root
for resource in package_root.walk(codebase, topdown=True, ignored=cls.is_ignored_package_resource):
yield resource
@classmethod
def ignore_resource(cls, resource, codebase):
"""
Return True if `resource` should be ignored.
"""
return False
@staticmethod
def is_ignored_package_resource(resource, codebase):
from packagedcode import PACKAGE_DATA_CLASSES
return any(pt.ignore_resource(resource, codebase) for pt in PACKAGE_DATA_CLASSES)
def compute_normalized_license(self):
"""
Return a normalized license_expression string using the declared_license
field. Return 'unknown' if there is a declared license but it cannot be
detected and return None if there is no declared license
Subclasses can override to handle specifics such as supporting specific
license ids and conventions.
"""
return compute_normalized_license(self.declared_license)
@classmethod
def extra_key_files(cls):
"""
Return a list of extra key file paths (or path glob patterns) beyond
standard, well known key files for this Package. List items are strings
that are either paths or glob patterns and are relative to the package
root.
Knowing if a file is a "key-file" file is important for classification
and summarization. For instance, a JAR can have key files that are not
top level under the META-INF directory. Or a .gem archive contains a
metadata.gz file.
Sub-classes can implement as needed.
"""
return []
@classmethod
def extra_root_dirs(cls):
"""
Return a list of extra package root-like directory paths (or path glob
patterns) that should be considered to determine if a files is a top
level file or not. List items are strings that are either paths or glob
patterns and are relative to the package root.
Knowing if a file is a "top-level" file is important for classification
and summarization.
Sub-classes can implement as needed.
"""
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, expression_symbols=None):
"""
Return a normalized license_expression string from the ``declared_license``.
Return 'unknown' if there is a declared license but it cannot be detected
(including on errors) and return None if there is no declared license.
Use the ``expression_symbols`` mapping of {lowered key: LicenseSymbol}
if provided. Otherwise use the standard SPDX license symbols.
"""
if not declared_license:
return
from packagedcode import licensing
try:
return licensing.get_normalized_expression(
query_string=declared_license,
expression_symbols=expression_symbols
)
except Exception:
# FIXME: add logging
# we never fail just for this
return 'unknown'
@attr.s
class PackageDataFile:
"""
A mixin for package manifests, lockfiles and other package data files
that can be recognized.
When creating a new package data, a class should be created that extends
both PackageDataFile and PackageData.
"""
dependencies = List(
item_type=DependentPackage,
label='dependencies',
help='A list of DependentPackage for this package. ')
# class-level attributes used to recognize a package
filetypes = tuple()
mimetypes = tuple()
extensions = tuple()
# list of known file_patterns for a package manifest type
file_patterns = tuple()
@property
def package_data_type(self):
"""
A tuple unique across package data, created from the default package type
and the manifest type.
"""
return self.default_type, self.manifest_type()
@classmethod
def manifest_type(cls):
return f"{cls.__module__}.{cls.__qualname__}"
@classmethod
def is_package_data_file(cls, location):
"""
Return True if the file at ``location`` is likely a manifest/lockfile/other
package data file of this type.
Sub-classes should override to implement their own package data file recognition.
"""
if not filetype.is_file(location):
return
filename = file_name(location)
file_patterns = cls.file_patterns
if any(fnmatch.fnmatchcase(filename, metaf) for metaf in file_patterns):
return True
T = contenttype.get_type(location)
ftype = T.filetype_file.lower()
mtype = T.mimetype_file
_base_name, extension = splitext_name(location, is_file=True)
extension = extension.lower()
if TRACE:
logger_debug(
'is_manifest: ftype:', ftype, 'mtype:', mtype,
'pygtype:', T.filetype_pygment,
'fname:', filename, 'ext:', extension,
)
type_matched = False
if cls.filetypes:
type_matched = any(t in ftype for t in cls.filetypes)
mime_matched = False
if cls.mimetypes:
mime_matched = any(m in mtype for m in cls.mimetypes)
extension_matched = False
extensions = cls.extensions
if extensions:
extensions = (e.lower() for e in extensions)
extension_matched = any(
fnmatch.fnmatchcase(extension, ext_pat)
for ext_pat in extensions
)
if type_matched and mime_matched and extension_matched:
return True
@classmethod
def recognize(cls, location):
"""
Yield one or more PackageData objects given a file at `location`
pointing to a package archive, manifest, lockfile or other package data.
Sub-classes should override to implement their own package recognition and creation.
This should be called on the file at `location` only if `is_manifest` function
of the same class returns True.
"""
raise NotImplementedError
@attr.s()
class Package:
"""
A package mixin as represented by its package data, files and data
from its package data. Here package obviously represents a package
instance.
Subclasses must extend a Package subclass for a given ecosystem.
"""
package_uuid = String(
label='Package instance UUID',
help='A unique ID for package instances in a codebase scan.'
'Consists of a pURL and an UUID field as a pURL qualifier.'
)
package_data_files = List(
item_type=String,
label='Package data paths',
help='List of package data file paths for this package'
)
files = List(
item_type=PackageFile,
label='Provided files',
help='List of files provided by this package.'
)
@property
def ignore_paths(self):
"""
Paths to ignore when looking for other package_data files.
Override the default empty list by defining for each package ecosystems specifically.
"""
return []
def get_package_data(self):
"""
Returns a mapping of package data attributes and corresponding values.
"""
mapping = self.to_dict()
# Removes Package specific attributes
for attribute in ('package_uuid', 'package_data_files', 'files'):
mapping.pop(attribute, None)
# Remove attributes which are BasePackageData functions
for attribute in ('repository_homepage_url', 'repository_download_url', 'api_data_url'):
mapping.pop(attribute, None)
return mapping
def populate_package_data(self, package_data_by_path, uuid):
"""
Create a package instance object from one or multiple package data.
"""
paths_with_mismatch = []
for path, package_data in package_data_by_path.items():
success = True
if TRACE:
logger.debug('Merging package manifest data for: {}'.format(path))
logger.debug('package manifest data: {}'.format(repr(package_data)))
success = self.update(package_data.copy())
if not success:
paths_with_mismatch.append(path)
if TRACE:
logger.debug('Failed to merge package manifest data for: {}'.format(path))
continue
self.package_data_files.append(path)
for path in paths_with_mismatch:
package_data_by_path.pop(path)
self.package_data_files = tuple(self.package_data_files)
# Set `package_uuid` as pURL for the package + it's uuid as a qualifier
# in the pURL string
try:
purl_with_uuid = PackageURL.from_string(self.purl)
except ValueError:
if TRACE:
logger.debug("Couldn't create purl for: {}".format(path))
return
purl_with_uuid.qualifiers["uuid"] = str(uuid)
self.package_uuid = purl_with_uuid.to_string()
def get_package_files(self, resource, codebase):
"""
Return a list of all the file paths for a package instance.
Sub-classes should override to implement their own package files finding methods.
"""
files = []
if codebase.has_single_resource:
files.append(resource.path)
return files
parent = resource.parent(codebase)
for resource in parent.walk(codebase):
if resource.is_dir:
continue
files.append(resource.path)
return files
def get_other_package_data(self, resource, codebase):
"""
Return a dictionary of other package data by their paths for a given package instance.
Sub-classes can override to implement their own package manifest finding methods.
"""
package_data_by_path = {}
if codebase.has_single_resource:
return package_data_by_path
parent = resource.parent(codebase)
paths_to_ignore = self.ignore_paths
for resource in parent.walk(codebase):
if resource.is_dir:
continue
if paths_to_ignore:
if any(
path in resource.path
for path in paths_to_ignore
):
continue
filename = file_name(resource.location)
file_patterns = self.get_file_patterns(manifests=self.manifests)
if any(fnmatch.fnmatchcase(filename, pattern) for pattern in file_patterns):
if not resource.package_data:
continue # Raise Exception(?)
#ToDo: Implement for multiple package data per path
package_data_by_path[resource.path] = resource.package_data[0]
return package_data_by_path
def get_file_patterns(self, manifests):
"""
Return a list of all `file_patterns` for all the PackageData classes
in `manifests`.
"""
manifest_file_patterns = []
for manifest in manifests:
manifest_file_patterns.extend(manifest.file_patterns)
return manifest_file_patterns
def update(self, package_data, replace=False):
"""
Returns False if there's a type/name/version mismatch between the package
and the package_data, otherwise returns True if update is successful.
Update the Package object with data from the `package_data`
object.
When an `package_instance` field has no value one side and and the
`package_data` field has a value, the `package_instance` field is always
set to this value.
If `replace` is True and a field has a value on both sides, then
package_instance field value will be replaced by the package_data
field value. Otherwise if `replace` is False, the package_instance
field value is left unchanged in this case.
"""
existing_mapping = self.get_package_data()
# Remove PackageData specific attributes
for attribute in ['root_path']:
package_data.pop(attribute, None)
existing_mapping.pop(attribute, None)
for existing_field, existing_value in existing_mapping.items():
new_value = package_data[existing_field]
if TRACE_MERGING:
logger.debug(
'\n'.join([
'existing_field:', repr(existing_field),
' existing_value:', repr(existing_value),
' new_value:', repr(new_value)])
)
# FIXME: handle Booleans???
# These fields has to be same across the package_data
if existing_field in ('name', 'version', 'type', 'primary_language'):
if existing_value and new_value and existing_value != new_value:
if TRACE_MERGING:
logger.debug(
'\n'.join([
'Mismatched {}:'.format(existing_field),
' existing_value: {}'.format(existing_value),
' new_value: {}'.format(new_value)
])
)
return False
if not new_value:
if TRACE_MERGING:
logger.debug(' No new value for {}: skipping'.format(existing_field))
continue
if not existing_value or replace:
if TRACE_MERGING and not existing_value:
logger.debug(