forked from spdx/tools-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
write_tv.py
executable file
·86 lines (76 loc) · 3.18 KB
/
write_tv.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
#!/usr/bin/env python
# Writes a new tag/value file from scratch.
# Usage: write_tv <tagvaluefile>
if __name__ == '__main__':
import sys
import codecs
from spdx.writers.tagvalue import write_document, InvalidDocumentError
from spdx.document import Document, License, LicenseConjuction, ExtractedLicense
from spdx.version import Version
from spdx.creationinfo import Person
from spdx.review import Review
from spdx.package import Package
from spdx.file import File, FileType
from spdx.checksum import Algorithm
from spdx.utils import SPDXNone, NoAssert, UnKnown
doc = Document()
doc.version = Version(1, 2)
doc.comment = 'Example Document'
doc.data_license = License.from_identifier('CC0-1.0')
doc.creation_info.add_creator(Person('Alice', '[email protected]'))
doc.creation_info.set_created_now()
review = Review(Person('Joe', None))
review.set_review_date_now()
review.comment = 'Joe reviewed this document'
doc.add_review(review)
# File
testfile1 = File('TestFile1')
testfile1.type = FileType.BINARY
testfile1.comment = 'This is a test file.'
testfile1.chk_sum = Algorithm('SHA1', 'c537c5d99eca5333f23491d47ededd083fefb7ad')
testfile1.conc_lics = License.from_identifier('BSD-2-Clause')
testfile1.add_lics(License.from_identifier('BSD-2-Clause'))
testfile1.copyright = SPDXNone()
testfile1.add_artifact('name', 'TagWriteTest')
testfile1.add_artifact('home', UnKnown())
testfile1.add_artifact('uri', 'http://tagwritetest.test')
testfile2 = File('TestFile2')
testfile2.type = FileType.SOURCE
testfile2.comment = 'This is a test file.'
testfile2.chk_sum = Algorithm('SHA1', 'bb154f28d1cf0646ae21bb0bec6c669a2b90e113')
testfile2.conc_lics = License.from_identifier('Apache-2.0')
testfile2.add_lics(License.from_identifier('Apache-2.0'))
testfile2.copyright = NoAssert()
# Package
package = Package()
package.name = 'TagWriteTest'
package.version = '1.0'
package.file_name = 'twt.jar'
package.download_location = 'http://www.tagwritetest.test/download'
package.homepage = SPDXNone()
package.verif_code = '4e3211c67a2d28fced849ee1bb76e7391b93feba'
license_set = LicenseConjuction(License.from_identifier('Apache-2.0'),
License.from_identifier('BSD-2-Clause'))
package.conc_lics = license_set
package.license_declared = license_set
package.add_lics_from_file(License.from_identifier('Apache-2.0'))
package.add_lics_from_file(License.from_identifier('BSD-2-Clause'))
package.cr_text = NoAssert()
package.summary = 'Simple package.'
package.description = 'Really simple package.'
package.add_file(testfile1)
package.add_file(testfile2)
doc.package = package
# An extracted license
lic = ExtractedLicense('LicenseRef-1')
lic.text = 'Some non legal legal text..'
doc.add_extr_lic(lic)
file = sys.argv[1]
with codecs.open(file, mode='w', encoding='utf-8') as out:
try:
write_document(doc, out)
except InvalidDocumentError:
print 'Document is Invalid'
messages = []
doc.validate(messages)
print '\n'.join(messages)