Skip to content

Commit 6467b37

Browse files
committed
Add 'spdx_id' field to the 'Document' class
Signed-off-by: Yash Nisar <[email protected]>
1 parent 354eaf6 commit 6467b37

17 files changed

+98
-11
lines changed

data/SPDXRdfExample.rdf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns:j.0="http://usefulinc.com/ns/doap#"
44
xmlns="http://spdx.org/rdf/terms#"
55
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
6-
<SpdxDocument rdf:about="http://www.spdx.org/tools#SPDXANALYSIS">
6+
<SpdxDocument rdf:about="https://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#SPDXRef-DOCUMENT">
77
<creationInfo>
88
<CreationInfo>
99
<created>2010-02-03T00:00:00Z</created>

data/SPDXSimpleTag.tag

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Document info
22
SPDXVersion: SPDX-1.2
33
DataLicense: CC0-1.0
4+
SPDXID: SPDXRef-DOCUMENT
45
DocumentComment: <text>Sample Comment</text>
56

67
# Creation info

data/SPDXTagExample.tag

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
SPDXVersion: SPDX-1.2
22
DataLicense: CC0-1.0
3+
SPDXID: SPDXRef-DOCUMENT
34
DocumentComment: <text>This is a sample spreadsheet</text>
45

56
## Creation Information

spdx/document.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ class Document(object):
189189
Represent an SPDX document with these fields:
190190
- version: Spec version. Mandatory, one - Type: Version.
191191
- data_license: SPDX-Metadata license. Mandatory, one. Type: License.
192+
- spdx_id: SPDX Identifier for the document to refer to itself in
193+
relationship to other elements. Mandatory, one. Type: str.
192194
- comment: Comments on the SPDX file, optional one. Type: str
193195
- creation_info: SPDX file creation info. Mandatory, one. Type: CreationInfo
194196
- package: Package described by this document. Mandatory, one. Type: Package
@@ -198,11 +200,13 @@ class Document(object):
198200
Type: Review.
199201
"""
200202

201-
def __init__(self, version=None, data_license=None, comment=None, package=None):
203+
def __init__(self, version=None, data_license=None, spdx_id=None,
204+
comment=None, package=None):
202205
# avoid recursive impor
203206
from spdx.creationinfo import CreationInfo
204207
self.version = version
205208
self.data_license = data_license
209+
self.spdx_id = spdx_id
206210
self.comment = comment
207211
self.creation_info = CreationInfo()
208212
self.package = package
@@ -237,6 +241,7 @@ def validate(self, messages=None):
237241

238242
return (self.validate_version(messages)
239243
and self.validate_data_lics(messages)
244+
and self.validate_spdx_id(messages)
240245
and self.validate_creation_info(messages)
241246
and self.validate_package(messages)
242247
and self.validate_extracted_licenses(messages)
@@ -268,6 +273,20 @@ def validate_data_lics(self, messages=None):
268273
messages.append('Document data license must be CC0-1.0.')
269274
return False
270275

276+
def validate_spdx_id(self, messages=None):
277+
# FIXME: messages should be returned
278+
messages = messages if messages is not None else []
279+
280+
if self.spdx_id is None:
281+
messages.append('Document has no SPDX Identifier.')
282+
return False
283+
284+
if self.spdx_id.endswith('SPDXRef-DOCUMENT'):
285+
return True
286+
else:
287+
messages.append('Invalid Document SPDX Identifier value.')
288+
return False
289+
271290
def validate_reviews(self, messages=None):
272291
# FIXME: messages should be returned
273292
messages = messages if messages is not None else []

spdx/parsers/lexers/tagvalue.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Lexer(object):
2121
# Top level fields
2222
'SPDXVersion': 'DOC_VERSION',
2323
'DataLicense': 'DOC_LICENSE',
24+
'SPDXID': 'DOC_SPDX_ID',
2425
'DocumentComment': 'DOC_COMMENT',
2526
# Creation info
2627
'Creator': 'CREATOR',

spdx/parsers/rdf.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
ERROR_MESSAGES = {
3434
'DOC_VERS_VALUE': 'Invalid specVersion \'{0}\' must be SPDX-M.N where M and N are numbers.',
3535
'DOC_D_LICS': 'Invalid dataLicense \'{0}\' must be http://spdx.org/licenses/CC0-1.0.',
36+
'DOC_SPDX_ID_VALUE': 'Invalid SPDXID value, SPDXID must be the document namespace appended '
37+
'by "#SPDXRef-DOCUMENT", line: {0}',
3638
'LL_VALUE': 'Invalid licenseListVersion \'{0}\' must be of the format N.N where N is a number',
3739
'CREATED_VALUE': 'Invalid created value \'{0}\' must be date in ISO 8601 format.',
3840
'CREATOR_VALUE': 'Invalid creator value \'{0}\' must be Organization, Tool or Person.',
@@ -799,7 +801,11 @@ def parse_creation_info(self, ci_term):
799801
self.value_error('LL_VALUE', o)
800802

801803
def parse_doc_fields(self, doc_term):
802-
"""Parses the version, data license and comment."""
804+
"""Parses the version, data license, SPDX Identifier and comment."""
805+
try:
806+
self.builder.set_doc_spdx_id(self.doc, doc_term)
807+
except SPDXValueError:
808+
self.value_error('DOC_SPDX_ID_VALUE', doc_term)
803809
for _s, _p, o in self.graph.triples((doc_term, self.spdx_namespace['specVersion'], None)):
804810
try:
805811
self.builder.set_doc_version(self.doc, six.text_type(o))

spdx/parsers/rdfbuilders.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ def set_doc_data_lic(self, doc, res):
7979
else:
8080
raise CardinalityError('Document::License')
8181

82+
def set_doc_spdx_id(self, doc, doc_spdx_id_line):
83+
"""Sets the document SPDX Identifier. Raises value error if malformed
84+
value.
85+
"""
86+
if not self.doc_spdx_id_set:
87+
self.doc_spdx_id_set = True
88+
if not doc_spdx_id_line.endswith('#SPDXRef-DOCUMENT'):
89+
raise SPDXValueError('Document::SPDX Identifier')
90+
else:
91+
doc.spdx_id = doc_spdx_id_line
92+
return True
93+
else:
94+
raise CardinalityError('Document::SPDX Identifier')
95+
8296
def set_doc_comment(self, doc, comment):
8397
"""Sets document comment, Raises CardinalityError if
8498
comment already set.
@@ -97,6 +111,7 @@ def reset_document(self):
97111
self.doc_version_set = False
98112
self.doc_comment_set = False
99113
self.doc_data_lics_set = False
114+
self.doc_spdx_id_set = False
100115

101116

102117
class EntityBuilder(tagvaluebuilders.EntityBuilder):

spdx/parsers/tagvalue.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
'DOC_LICENSE_VALUE_TYPE': 'DataLicense must be CC0-1.0, line: {0}',
4141
'DOC_VERSION_VALUE': 'Invalid SPDXVersion \'{0}\' must be SPDX-M.N where M and N are numbers. Line: {1}',
4242
'DOC_VERSION_VALUE_TYPE': 'Invalid SPDXVersion value, must be SPDX-M.N where M and N are numbers. Line: {0}',
43+
'DOC_SPDX_ID_VALUE': 'Invalid SPDXID value, SPDXID must be SPDXRef-DOCUMENT, line: {0}',
4344
'DOC_COMMENT_VALUE_TYPE': 'DocumentComment value must be free form text between <text></text> tags, line:{0}',
4445
'REVIEWER_VALUE_TYPE': 'Invalid Reviewer value must be a Person, Organization or Tool. Line: {0}',
4546
'CREATOR_VALUE_TYPE': 'Invalid Reviewer value must be a Person, Organization or Tool. Line: {0}',
@@ -107,6 +108,7 @@ def p_start_2(self, p):
107108
def p_attrib(self, p):
108109
"""attrib : spdx_version
109110
| data_lics
111+
| doc_spdx_id
110112
| doc_comment
111113
| creator
112114
| created
@@ -1099,6 +1101,21 @@ def p_data_license_2(self, p):
10991101
msg = ERROR_MESSAGES['DOC_LICENSE_VALUE_TYPE'].format(p.lineno(1))
11001102
self.logger.log(msg)
11011103

1104+
def p_doc_spdx_id(self, p):
1105+
"""doc_spdx_id : DOC_SPDX_ID LINE"""
1106+
try:
1107+
if six.PY2:
1108+
value = p[2].decode(encoding='utf-8')
1109+
else:
1110+
value = p[2]
1111+
self.builder.set_doc_spdx_id(self.document, value)
1112+
except SPDXValueError:
1113+
self.error = True
1114+
msg = ERROR_MESSAGES['DOC_SPDX_ID_VALUE'].format(p.lineno(2))
1115+
self.logger.log(msg)
1116+
except CardinalityError:
1117+
self.more_than_one_error('SPDXID', p.lineno(1))
1118+
11021119
def p_spdx_version_1(self, p):
11031120
"""spdx_version : DOC_VERSION LINE"""
11041121
try:

spdx/parsers/tagvaluebuilders.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ def set_doc_data_lics(self, doc, lics):
108108
else:
109109
raise CardinalityError('Document::DataLicense')
110110

111+
def set_doc_spdx_id(self, doc, doc_spdx_id_line):
112+
"""Sets the document SPDX Identifier.
113+
Raises value error if malformed value, CardinalityError
114+
if already defined.
115+
"""
116+
if not self.doc_spdx_id_set:
117+
self.doc_spdx_id_set = True
118+
if doc_spdx_id_line != 'SPDXRef-DOCUMENT':
119+
raise SPDXValueError('Document::SPDX Identifier')
120+
else:
121+
doc.spdx_id = doc_spdx_id_line
122+
return True
123+
else:
124+
raise CardinalityError('Document::SPDX Identifier')
125+
111126
def set_doc_comment(self, doc, comment):
112127
"""Sets document comment, Raises CardinalityError if
113128
comment already set.
@@ -129,6 +144,7 @@ def reset_document(self):
129144
self.doc_version_set = False
130145
self.doc_comment_set = False
131146
self.doc_data_lics_set = False
147+
self.doc_spdx_id_set = False
132148

133149

134150
class EntityBuilder(object):

spdx/writers/rdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ def create_doc(self):
512512
"""
513513
Add and return the root document node to graph.
514514
"""
515-
doc_node = URIRef('http://www.spdx.org/tools#SPDXANALYSIS')
515+
doc_node = URIRef('http://www.spdx.org/tools#SPDXRef-DOCUMENT')
516516
# Doc type
517517
self.graph.add((doc_node, RDF.type, self.spdx_namespace.SpdxDocument))
518518
# Version

0 commit comments

Comments
 (0)