-
-
Notifications
You must be signed in to change notification settings - Fork 561
/
models.py
1270 lines (1005 loc) · 40.7 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) 2017 nexB Inc. and others. All rights reserved.
# http://nexb.com and https://github.com/nexB/scancode-toolkit/
# The ScanCode software is licensed under the Apache License version 2.0.
# Data generated with ScanCode require an acknowledgment.
# ScanCode is a trademark of nexB Inc.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
# When you publish or redistribute any data created with ScanCode or any ScanCode
# derivative work, you must accompany this data with the following acknowledgment:
#
# Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
# ScanCode should be considered or used as legal advice. Consult an Attorney
# for any legal advice.
# ScanCode is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from collections import namedtuple
import string
import re
from schematics.exceptions import StopValidation
from schematics.exceptions import ValidationError
from schematics.models import Model
from schematics.types import fill_template
from schematics.types import random_string
from schematics.types import BaseType
from schematics.types import BooleanType
from schematics.types import DateTimeType
from schematics.types import EmailType
from schematics.types import HashType
from schematics.types import LongType
from schematics.types import MD5Type
from schematics.types import SHA1Type
from schematics.types import StringType
from schematics.types import URLType
from schematics.types.compound import DictType
from schematics.types.compound import ListType
from schematics.types.compound import ModelType
from schematics.transforms import blacklist
"""
Common data model for package information and dependencies, abstracting the
many small differences existing between package management formats and tools.
At a high level a package is some piece of code that can be consumed as a unit
and provisioned by some package manager or can be installed as such.
In the simplest case, it can be a single file such as script; more commonly a
package is a complex set of archives, directories structures, file systems
images or self-executable installers.
A package typically contains:
- some metadata,
- some payload of code, doc, data.
Package metadata are found in multiple places:
- inside code text (JavaDoc tags or Python __copyright__ magic)
- inside binaries (such as a Linux Elf or LKM or a Windows PE or an RPM header).
- in dedicated metafiles (such as a Maven POM, NPM package.json and many others)
These metadata provide details for:
- information on the version of the file format of the current metadata file or header.
- the package id or name and version.
- a package namespace such as a central registry ensuing uniqueness of names.
- some pre-requisite such as a specific OS (Linux), runtime (Java) or
processor or architecture, some API level (such as an ABI or else),
- info on the programming language used or needed or technical domain, either
implicitly or explicitly.
- documentation such as descriptions, notes, categories, tags,
classifications or groups of sorts. This can include urls to external
documentation, either explicitly or implicitly.
- origin information: author, provider, distributor, vendor, owner, home
urls, etc.
- some contact or support information such as emails, mailing lists, forums,
- some checksum or crypto signature of sorts to verify the integrity of the
package, often stored outside the package itself,
- version control info such as a Git or SVN repo where the source came from.
- license and copyright info, either structured or not, eventually per-file
or directory.
- dependent packages, possibly grouped by purpose (dev, build, test, prod)
The dependencies are either a complete tree or only the first level direct
dependencies as a flat list. They can point to a name or some remote of local
files. Dependencies are expressed usually as a package name and a version
constraint or in some cases as lower level programming language-specific
dependencies (such as OSGi Java package imports).
- build and packaging instructions. Several package metadata formats mix
build/packaging directives or instructions with the package metadata (such
as RPM spec files).
- installation directives and installation scripts to actually install the
payload. These can be implicit such as with RPM directory structures.
- where to fetch corresponding sources when compiled, either explicitly or
implicitly, such as a VCS or some other package.
- modification or patches applied and the patches themselves, with possibly
changelog docs.
- pointers to package registries where to fetch this or dependent packages,
either explicitly or implicitly, including local files, VCS pointers or
some central registry/repository URL.
- description of the specific things provided by the payload, such as a
binary, some API/ABI level, some library, some language namespace or some
capability of sorts.
The payload of files and directories possibly contains:
-- documentation,
-- data files,
-- code in source or compiled form or both.
"""
class ListType(ListType):
"""
ListType with a default of an empty list.
"""
def __init__(self, field, **kwargs):
super(ListType, self).__init__(field=field, default=[], **kwargs)
PackageId = namedtuple('PackageId', 'type name version')
class PackageIndentifierType(BaseType):
"""
Global identifier for a package
"""
def __init__(self, **kwargs):
super(PackageIndentifierType, self).__init__(**kwargs)
def to_primitive(self, value, context=None):
"""
Return a package id string, joining segments with a pipe separator.
"""
if not value:
return value
if isinstance(value, PackageId):
return u'|'.join(v or u'' for v in value)
else:
raise TypeError('Invalid package identifier')
def to_native(self, value):
return value
def validate_id(self, value):
if not isinstance(value, PackageId):
raise StopValidation(self.messages['Invalid Package ID: must be PackageId named tuple'])
class SHA256Type(HashType):
LENGTH = 64
class SHA512Type(HashType):
LENGTH = 128
class URIType(StringType):
"""
A field that validates input as a URI with several supported schemes beyond
HTTP.
"""
# Regex is derived from the code of schematics.URLType. BSD-licensed, see corresponding ABOUT file
URI_REGEX = re.compile(
r'^'
r'(?:'
r'(?:https?'
r'|ftp|sftp|ftps'
r'|rsync'
r'|ssh'
r'|git|git\+https?|git\+ssh|git\+git|git\+file'
r'|hg|hg\+https?|hg\+ssh|hg\+static-https?'
r'|bzr|bzr\+https?|bzr\+ssh|bzr\+sftp|bzr\+ftp|bzr+lp'
r'|svn|svn\+http?|svn\+ssh|svn\+svn'
r')'
r'://'
r'|'
r'git\@'
r')'
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,2000}[A-Z0-9])?\.)+[A-Z]{2,63}\.?|'
r'localhost|'
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
r'(?::\d+)?'
r'(?:/?|[/?]\S+)$', re.IGNORECASE
)
def __init__(self, **kwargs):
super(URIType, self).__init__(**kwargs)
def _mock(self, context=None):
return fill_template('https://a%s.ZZ', self.min_length, self.max_length)
def validate_url(self, value):
if not self.__class__.URI_REGEX.match(value):
raise StopValidation(self.messages['Invalid URI'])
def canonical(self):
"""
Return a canonical representation of this URI.
"""
raise NotImplementedError()
class VersionType(BaseType):
"""
A Package version is a string or a sequence of strings (list or tuple).
'separator' is the separator string used to join a version sequence made of
parts such as major, minor and patch.
Packages with alternative versioning can subclass to define their own
versioning scheme with add extra methods, a refined compare method for
instance when storing a tuple, namedtuple or list for each each version
parts or parsing the version in parts or exposiing parts.
"""
metadata = dict(
label='version',
description='The version of the package as a string. '
'Package types may implement specialized handling for versions, but this seralizes as a string')
def __init__(self, separator=None, **kwargs):
if not separator:
separator = ''
self.separator = separator
super(VersionType, self).__init__(**kwargs)
def validate_version(self, value):
"""
Accept strings and empty strings or None as values in a value list
"""
if value is None or isinstance(value, basestring):
return
if isinstance(value, (list, tuple,)) and all(isinstance(v, basestring) or v in (None, '') for v in value):
return
msg = 'Version must be a string or sequence of strings, not %r'
raise ValidationError(msg % value)
def to_primitive(self, value, context=None):
"""
Return a version string. If the version is a sequence, join segments with separators.
Subclasses can override.
"""
if not value:
return value
if isinstance(value, (list, tuple,)):
return unicode(self.separator).join(v for v in value if v)
return unicode(value)
def to_native(self, value):
return value
def sortable(self, value):
"""
Return an opaque tuple to sort or compare versions. When not defined, a
version sorts first.
Each segment of a version is split on spaces, underscore, period and
dash and returned separately in a sequence. Number are converted to int. This allows
for a more natural sort.
"""
if not value:
return (-1,)
srt = []
if isinstance(value, (list, tuple,)):
return tuple(value)
# isinstance(value, basestring):
for v in re.split('[\.\s\-_]', value):
srt.append(v.isdigit() and int(v) or v)
return tuple(srt)
def _mock(self, context=None):
a = random_string(1, string.digits)
b = random_string(1, string.digits)
return self.separator.join([a, b])
class BaseModel(Model):
"""
Base class for all schematics models.
"""
def __init__(self, **kwargs):
super(BaseModel, self).__init__(raw_data=kwargs)
def to_dict(self, **kwargs):
"""
Return a dict of primitive Python types for this model instance.
This is an OrderedDict because each model has a 'field_order' option.
"""
return self.to_primitive(**kwargs)
# Package repo types
###############################
repo_bower = 'Bower'
repo_cpan = 'CPAN'
repo_debian = 'Debian'
repo_gems = 'Rubygems'
repo_godoc = 'Godoc'
repo_ivy = 'IVY'
repo_maven = 'Maven'
repo_npm = 'NPM'
repo_phpcomposer = 'Packagist'
repo_nuget = 'Nuget'
repo_python = 'Pypi'
repo_yum = 'YUM'
REPO_TYPES = (
repo_bower,
repo_cpan,
repo_debian,
repo_gems,
repo_godoc,
repo_ivy,
repo_maven,
repo_npm,
repo_phpcomposer,
repo_nuget,
repo_python,
repo_yum,
)
class Repository(BaseModel):
metadata = dict(
label='package repository',
description='Represents a package repository.')
type = StringType(choices=REPO_TYPES)
type.metadata = dict(
label='package repository type',
description='The type of package repository for this repository. '
'One of: ' + ', '.join(REPO_TYPES))
url = URIType()
url.metadata = dict(
label='url',
description='URL to this repository.')
public = BooleanType(default=False)
public.metadata = dict(
label='public repository',
description='A flag set to true if this is a public repository.')
mirror_urls = ListType(URIType)
mirror_urls.metadata = dict(
label='repository mirror urls',
description='A list of URLs for mirrors of this repository.')
nickname = StringType()
nickname.metadata = dict(
label='repository nickname',
description='nickname used for well known "named" public repos such as: '
'Maven Central, Pypi, RubyGems, npmjs.org or their mirrors')
class Options:
fields_order = 'type', 'url', 'public', 'mirror_urls', 'name'
def download_url(self, package):
"""
Return a download URL for this package in this repository.
"""
return NotImplementedError()
def packages(self):
"""
Return an iterable of Package objects available in this repository.
"""
return NotImplementedError()
class AssertedLicense(BaseModel):
metadata = dict(
label='asserted license',
description='Represents the licensing as asserted in a package metadata.')
license = StringType()
license.metadata = dict(
label='license',
description='license as asserted. This can be a text, a name or anything.')
url = URIType()
url.metadata = dict(
label='url',
description='URL to a web page for this license.')
text = StringType()
text.metadata = dict(
label='license text',
description='license text as asserted.')
notice = StringType()
notice.metadata = dict(
label='notice',
description='a license notice for this package')
class Options:
fields_order = 'license', 'url', 'text', 'notice'
# Party types
#################################
party_person = 'person'
party_project = 'project' # often loosely defined
party_org = 'organization' # more formally defined
PARTY_TYPES = (party_person, party_project, party_org,)
class Party(BaseModel):
metadata = dict(
label='party',
description='A party is a person, project or organization related to a package.')
type = StringType(choices=PARTY_TYPES)
type.metadata = dict(
label='party type',
description='the type of this party: One of: ' + ', '.join(PARTY_TYPES))
name = StringType()
name.metadata = dict(
label='name',
description='Name of this party.')
url = URLType()
name.metadata = dict(
label='url',
description='URL to a primary web page for this party.')
email = EmailType()
email.metadata = dict(
label='email',
description='Email for this party.')
class Options:
fields_order = 'type', 'name', 'email', 'url'
# Groupings of package dependencies
###################################
dep_runtime = 'runtime'
dep_dev = 'development'
dep_test = 'test'
dep_build = 'build'
dep_optional = 'optional'
dep_bundled = 'bundled'
dep_ci = 'continuous integration'
DEPENDENCY_GROUPS = (dep_runtime, dep_dev, dep_optional, dep_test, dep_build, dep_ci, dep_bundled,)
# FIXME: this is broken ... OSGi uses "Java packages" deps as an indirection and not directly package deps.
class Dependency(BaseModel):
metadata = dict(
label='dependency',
description='A dependency points to a Package via a package name and a version constraint '
'(such as ">= 3.4"). The version is the effective version that has been '
'picked and resolved.')
name = StringType(required=True)
name.metadata = dict(
label='name',
description='Name of the package for this dependency.')
version = VersionType()
version.metadata = dict(
label='version',
description='Version of this dependent package: '
'The effective or concrete resolved and used version.')
version_constraint = StringType()
version_constraint.metadata = dict(
label='version',
description='The version constraints (aka. possible versions) '
'for this dependent package: The meaning of this constraings is '
'package type-specific. ')
class Options:
fields_order = 'type', 'name', 'version', 'version_constraint'
def resolve(self):
"""
Compute a concrete version.
"""
# A normalized list of version constraints for this dep. This is package-
# independent and should be a normalized data structure describing all the
# different version range constraints
# normalized_version_constraints = ListType(StringType())
raise NotImplementedError()
# Types of the payload of a Package
###################################
payload_src = 'source'
# binaries include minified JavaScripts and similar obfuscated texts formats
payload_bin = 'binary'
payload_doc = 'doc'
PAYLOADS = (payload_src, payload_bin, payload_doc)
# Packaging types
#################################
as_archive = 'archive'
as_dir = 'directory'
as_file = 'file'
PACKAGINGS = (as_archive, as_dir, as_file)
# TODO: define relations. See SPDX specs
class RelatedPackage(BaseModel):
metadata = dict(
label='related package',
description='A generic related package.')
type = StringType(required=True)
type.metadata = dict(
label='type',
description='Descriptive name of the type of package: '
'RubyGem, Python Wheel, Java Jar, Debian package, etc.')
name = StringType(required=True)
name.metadata = dict(
label='name',
description='Name of the package.')
version = VersionType()
version.metadata = dict(
label='version',
description='Version of the package')
payload_type = StringType(choices=PAYLOADS)
payload_type.metadata = dict(
label='Payload type',
description='The type of payload for this package. One of: ' + ', '.join(PAYLOADS))
class Options:
fields_order = 'type', 'name', 'version', 'payload_type'
class Package(BaseModel):
"""
A package object.
Override for specific package behaviour. The way a
package is created and serialized should be uniform across all Package
types.
"""
metadata = dict(
label='package',
description='A package object.')
###############################
# real class-level attributes
###############################
# content types data to recognize a package
filetypes = tuple()
mimetypes = tuple()
extensions = tuple()
# list of known metafiles for a package type, to recognize a package
metafiles = []
# list of supported repository types a package type, for reference
repo_types = []
###############################
# Field descriptors
###############################
# from here on, these are actual instance attributes, using descriptors
type = StringType(required=True)
type.metadata = dict(
label='package type',
description='Descriptive name of the type of package: '
'RubyGem, Python Wheel, Java Jar, Debian package, etc.')
name = StringType(required=True)
name.metadata = dict(
label='package name',
description='Name of the package.')
version = VersionType()
version.metadata = dict(
label='package version',
description='Version of the package. '
'Package types may implement specific handling for versions but this is always serialized as a string.')
primary_language = StringType()
primary_language.metadata = dict(
label='Primary programming language',
description='Primary programming language of the package, such as Java, C, C++. '
'Derived from the package type: i.e. RubyGems are primarily ruby, etc.')
packaging = StringType(choices=PACKAGINGS)
packaging.metadata = dict(
label='Packaging',
description='How a package is packaged. One of: ' + ', '.join(PACKAGINGS))
# TODO: add os and arches!!
summary = StringType()
summary.metadata = dict(
label='Summary',
description='Summary for this package i.e. a short description')
description = StringType()
description.metadata = dict(
label='Description',
description='Description for this package '
'i.e. a long description, often several pages of text')
payload_type = StringType(choices=PAYLOADS)
payload_type.metadata = dict(
label='Payload type',
description='The type of payload for this package. One of: ' + ', '.join(PAYLOADS))
# we useLongType instead of IntType is because
# IntType 2147483647 is the max size which means we cannot store
# more than 2GB files
size = LongType()
size.metadata = dict(
label='size',
description='size of the package download in bytes')
release_date = DateTimeType()
release_date.metadata = dict(
label='release date',
description='Release date of the package')
# FIXME: this would be simpler as a list where each Party has also a type
authors = ListType(ModelType(Party))
authors.metadata = dict(
label='authors',
description='A list of party objects. Note: this model schema will change soon.')
maintainers = ListType(ModelType(Party))
maintainers.metadata = dict(
label='maintainers',
description='A list of party objects. Note: this model schema will change soon.')
contributors = ListType(ModelType(Party))
contributors.metadata = dict(
label='contributors',
description='A list of party objects. Note: this model schema will change soon.')
owners = ListType(ModelType(Party))
owners.metadata = dict(
label='owners',
description='A list of party objects. Note: this model schema will change soon.')
packagers = ListType(ModelType(Party))
packagers.metadata = dict(
label='owners',
description='A list of party objects. Note: this model schema will change soon.')
distributors = ListType(ModelType(Party))
distributors.metadata = dict(
label='distributors',
description='A list of party objects. Note: this model schema will change soon.')
vendors = ListType(ModelType(Party))
vendors.metadata = dict(
label='vendors',
description='A list of party objects. Note: this model schema will change soon.')
keywords = ListType(StringType())
keywords.metadata = dict(
label='keywords',
description='A list of keywords or tags.')
# FIXME: this is a Package-class attribute
keywords_doc_url = URLType()
keywords_doc_url.metadata = dict(
label='keywords documentation URL',
description='URL to a reference documentation for keywords or '
'tags (such as a Pypi or SF.net Trove map)')
metafile_locations = ListType(StringType())
metafile_locations.metadata = dict(
label='metafile locations',
description='A list of metafile locations for this package '
'(such as a package.json, a setup.py). '
'Relative to the package root directory or archive root')
metafile_urls = ListType(URIType())
metafile_urls.metadata = dict(
label='metafile URLs',
description='A list of metafile remote URLs for this package '
'(such as a package.json, a setup.py)')
homepage_url = URIType()
homepage_url.metadata = dict(
label='homepage URL',
description='URL to the homepage for this package')
notes = StringType()
notes.metadata = dict(
label='Notes',
description='Notes, free text about this package')
download_urls = ListType(URIType())
download_urls.metadata = dict(
label='Download URLs',
description='A list of direct download URLs, possibly in SPDX VCS url form. '
'The first item is considered to be the primary download URL')
download_sha1 = SHA1Type()
download_sha1.metadata = dict(label='Download SHA1', description='Shecksum for the download')
download_sha256 = SHA256Type()
download_sha256.metadata = dict(label='Download SHA256', description='Shecksum for the download')
download_md5 = MD5Type()
download_md5.metadata = dict(label='Download MD5', description='Shecksum for the download')
bug_tracking_url = URLType()
bug_tracking_url.metadata = dict(
label='bug tracking URL',
description='URL to the issue or bug tracker for this package')
support_contacts = ListType(StringType())
support_contacts.metadata = dict(
label='Support contacts',
description='A list of strings (such as email, urls, etc) for support contacts')
code_view_url = URIType()
code_view_url.metadata = dict(
label='code view URL',
description='a URL where the code can be browsed online')
VCS_CHOICES = ['git', 'svn', 'hg', 'bzr', 'cvs']
vcs_tool = StringType(choices=VCS_CHOICES)
vcs_tool.metadata = dict(
label='Version control system tool',
description='The type of VCS tool for this package. One of: ' + ', '.join(VCS_CHOICES))
vcs_repository = URIType()
vcs_repository.metadata = dict(
label='VCS Repository URL',
description='a URL to the VCS repository in the SPDX form of:'
'git+https://github.com/nexb/scancode-toolkit.git')
vcs_revision = StringType()
vcs_revision.metadata = dict(
label='VCS revision',
description='a revision, commit, branch or tag reference, etc. '
'(can also be included in the URL)')
copyright_top_level = StringType()
copyright_top_level.metadata = dict(
label='Top level Copyright',
description='a top level copyright often asserted in package metadata')
copyrights = ListType(StringType())
copyrights.metadata = dict(
label='Copyrights',
description='A list of effective copyrights as detected and eventually summarized')
asserted_licenses = ListType(ModelType(AssertedLicense))
asserted_licenses.metadata = dict(
label='asserted licenses',
description='A list of asserted license objects representing '
'the asserted licensing information for this package')
legal_file_locations = ListType(StringType())
legal_file_locations.metadata = dict(
label='legal file locations',
description='A list of paths to legal files '
'(such as COPYING, NOTICE, LICENSE, README, etc.). '
'Paths are relative to the root of the package')
license_expression = StringType()
license_expression.metadata = dict(
label='license expression',
description='license expression: either resolved or detected license expression')
license_texts = ListType(StringType())
license_texts.metadata = dict(
label='license texts',
description='A list of license texts for this package.')
notice_texts = ListType(StringType())
license_texts.metadata = dict(
label='notice texts',
description='A list of notice texts for this package.')
# Map a DEPENDENCY_GROUPS group name to a list of Dependency
# FIXME: we should instead just have a plain list where each dep contain a group.
dependencies = DictType(ListType(ModelType(Dependency)), default={})
dependencies.metadata = dict(
label='dependencies',
description='An object mapping a dependency group to a '
'list of dependency objects for this package. '
'Note: the schema for this will change soon.'
'The possible values for dependency grousp are:' + ', '.join(DEPENDENCY_GROUPS)
)
related_packages = ListType(ModelType(RelatedPackage))
related_packages.metadata = dict(
label='related packages',
description='A list of related_package objects for this package. '
'For instance the SRPM source of a binary RPM.')
class Options:
# this defines the important serialization order
fields_order = [
'type',
'name',
'version',
'primary_language',
'packaging',
'summary',
'description',
'payload_type',
'size',
'release_date',
'authors',
'maintainers',
'contributors',
'owners',
'packagers',
'distributors',
'vendors',
'keywords',
'keywords_doc_url',
'metafile_locations',
'metafile_urls',
'homepage_url',
'notes',
'download_urls',
'download_sha1',
'download_sha256',
'download_md5',
'bug_tracking_url',
'support_contacts',
'code_view_url',
'vcs_tool',
'vcs_repository',
'vcs_revision',
'copyright_top_level',
'copyrights',
'asserted_licenses',
'legal_file_locations',
'license_expression',
'license_texts',
'notice_texts',
'dependencies',
'related_packages'
]
# we use for now a "role" that excludes deps and relationships from the
# serailization
roles = {'no_deps': blacklist('dependencies', 'related_packages')}
def __init__(self, location=None, **kwargs):
"""
Initialize a new Package.
Subclass can override but should override the recognize method to populate a package accordingly.
"""
# path to a file or directory where this Package is found in a scan
self.location = location
super(Package, self).__init__(**kwargs)
@classmethod
def recognize(cls, location):
"""
Return a Package object or None given a location to a file or directory
pointing to a package archive, metafile or similar.
Sub-classes should override to implement their own package recognition.
"""
return
@property
def component_version(self):
"""
Return the component-level version representation for this package.
Subclasses can override.
"""
return self.version
def compare_version(self, other, component_level=False):
"""
Compare the version of this package with another package version.
Use the same semantics as the builtin cmp function. It returns:
- a negaitive integer if this version < other version,
- zero if this version== other.version
- a positive interger if this version > other version.
Use the component-level version if `component_level` is True.
Subclasses can override for package-type specific comparisons.
For example:
# >>> q=Package(version='2')
# >>> p=Package(version='1')
# >>> p.compare_version(q)
# -1
# >>> p.compare_version(p)
# 0
# >>> r=Package(version='0')
# >>> p.compare_version(r)
# 1
# >>> s=Package(version='1')
# >>> p.compare_version(s)
# 0
"""
x = component_level and self.component_version() or self.version
y = component_level and other.component_version() or self.version
return cmp(x, y)
def sort_key(self):
"""
Return some data suiatble to use as a key function when sorting.
"""
return (self.type, self.name, self.version.sortable(self.version),)
@property
def identifier(self):
"""
Return a PackageId object for this package.
"""
return PackageId(self.type, self.name, self.version)
#
# Package sub types
# NOTE: this is somewhat redundant with extractcode archive handlers
# yet the purpose and semantics are rather different here
class DebianPackage(Package):
metafiles = ('*.control',)
extensions = ('.deb',)
filetypes = ('debian binary package',)
mimetypes = ('application/x-archive', 'application/vnd.debian.binary-package',)
repo_types = (repo_debian,)
type = StringType(default='Debian package')
packaging = StringType(default=as_archive)
class JavaJar(Package):
metafiles = ('META-INF/MANIFEST.MF',)
extensions = ('.jar',)
filetypes = ('java archive ', 'zip archive',)
mimetypes = ('application/java-archive', 'application/zip',)
repo_types = (repo_maven, repo_ivy,)
type = StringType(default='Java Jar')
packaging = StringType(default=as_archive)
primary_language = StringType(default='Java')
class JavaWar(Package):
metafiles = ('WEB-INF/web.xml',)
extensions = ('.war',)
filetypes = ('java archive ', 'zip archive',)
mimetypes = ('application/java-archive', 'application/zip')
repo_types = (repo_maven, repo_ivy,)
type = StringType(default='Java Web application')
packaging = StringType(default=as_archive)
primary_language = StringType(default='Java')
class JavaEar(Package):
metafiles = ('META-INF/application.xml', 'META-INF/ejb-jar.xml')
extensions = ('.ear',)
filetypes = ('java archive ', 'zip archive',)
mimetypes = ('application/java-archive', 'application/zip')
repo_types = (repo_maven, repo_ivy,)
type = StringType(default='Enterprise Java application')
packaging = StringType(default=as_archive)
primary_language = StringType(default='Java')