def parse_tagvalue_file(self, file_name): with open(file_name, mode='r') as infile: tvparser = TVParser(TVBuilder(), StandardLogger()) tvparser.build() doc, error = tvparser.parse(infile.read()) assert not error assert doc.validate([]) == [] return doc
def __parse_rdf(self, data): from spdx.parsers.rdf import Parser from spdx.parsers.rdfbuilders import Builder from spdx.parsers.loggers import StandardLogger p = Parser(Builder(), StandardLogger()) document, error = p.parse(data) if error: return (None, error) else: return (document, None)
def __parse_tagvalue(self, data): from spdx.parsers.tagvalue import Parser from spdx.parsers.tagvaluebuilders import Builder from spdx.parsers.loggers import StandardLogger p = Parser(Builder(), StandardLogger()) p.build() document, error = p.parse(data) if error: return (None, error) else: return (document, None)
def TAG_to_RDF(infile_name, outfile_name): tagvalueparser = Parser(Builder(), StandardLogger()) tagvalueparser.build() with open(infile_name) as infile: data = infile.read() document, error = tagvalueparser.parse(data) if not error: # print(map(lambda c: c.name, document.creation_info.creators)) print("Parsing Successful") with open(outfile_name, mode="w") as out: write_document(document, out, validate=True) else: print("Errors encountered while parsing tag value file.") messages = [] document.validate(messages) print("\n".join(messages))
def convert(infile_name, outfile_name): tagvalueparser = Parser(Builder(), StandardLogger()) tagvalueparser.build() with open(infile_name) as infile: data = infile.read() document, error = tagvalueparser.parse(data) if not error: # print map(lambda c: c.name, document.creation_info.creators) print 'Parsing Successful' with open(outfile_name, mode='w') as out: write_document(document, out, validate=True) else: print 'Errors encountered while parsing tag value file.' messages = [] document.validate(messages) print '\n'.join(messages)
def TAG_to_YAML(infile_name, outfile_name): # if __name__ == "__main__": # import sys from spdx.parsers.tagvalue import Parser from spdx.parsers.loggers import StandardLogger from spdx.parsers.tagvaluebuilders import Builder from spdx.writers.yaml import write_document # file = sys.argv[1] p = Parser(Builder(), StandardLogger()) p.build() with open(infile_name) as f: data = f.read() document, error = p.parse(data) if not error: with open(outfile_name, "w") as out: write_document(document, out) else: print("Errors encountered while parsing")
def tv_to_rdf(infile_name, outfile_name): """ Convert a SPDX file from tag/value format to RDF format. Return True on sucess, False otherwise. """ parser = Parser(Builder(), StandardLogger()) parser.build() with open(infile_name) as infile: data = infile.read() document, error = parser.parse(data) if not error: with open(outfile_name, mode="w") as outfile: write_document(document, outfile) return True else: print("Errors encountered while parsing RDF file.") messages = ErrorMessages() document.validate(messages) print("\n".join(messages.messages)) return False
def parse_TAG(file): from spdx.parsers.tagvalue import Parser from spdx.parsers.loggers import StandardLogger from spdx.parsers.tagvaluebuilders import Builder p = Parser(Builder(), StandardLogger()) p.build() with open(file) as f: data = f.read() document, error = p.parse(data) if not error: print("Parsing Successful") print( "Document Version {0}.{1}".format( document.version.major, document.version.minor ) ) print("Package name : {0}".format(document.package.name)) print("Creators : ") for creator in document.creation_info.creators: print(creator.name) else: print("Errors encountered while parsing")
def parse_tagvalue_file(self, file_name): """Returns tuple error, document.""" with open(file_name, mode='r') as infile: tvparser = TVParser(TVBuilder(), StandardLogger()) tvparser.build() return tvparser.parse(infile.read())
# Parses a tag/value file and writes it out pretty-printed. # Usage: pp_tv <infile> <outfile> if __name__ == '__main__': import sys import codecs from spdx.writers.tagvalue import write_document, InvalidDocumentError from spdx.parsers.tagvalue import Parser from spdx.parsers.loggers import StandardLogger from spdx.parsers.tagvaluebuilders import Builder source = sys.argv[1] target = sys.argv[2] p = Parser(Builder(), StandardLogger()) p.build() with open(source, 'r') as f: data = f.read() document, error = p.parse(data) if not error: print 'Parsing Successful' with codecs.open(target, mode='w', encoding='utf-8') as out: try: write_document(document, out) except InvalidDocumentError: print 'Document is Invalid' messages = [] document.validate(messages) print '\n'.join(messages) else: print 'Errors encountered while parsing'
class TestParser(TestCase): document_str = '\n'.join([ 'SPDXVersion: SPDX-1.2', 'DataLicense: CC0-1.0', 'DocumentComment: <text>Sample Comment</text>' ]) creation_str = '\n'.join([ 'Creator: Person: Bob ([email protected])', 'Creator: Organization: Acme.', 'Created: 2010-02-03T00:00:00Z', 'CreatorComment: <text>Sample Comment</text>' ]) review_str = '\n'.join([ 'Reviewer: Person: Bob the Reviewer', 'ReviewDate: 2010-02-10T00:00:00Z', 'ReviewComment: <text>Bob was Here.</text>', 'Reviewer: Person: Alice the Reviewer', 'ReviewDate: 2011-02-10T00:00:00Z', 'ReviewComment: <text>Alice was also here.</text>' ]) package_str = '\n'.join([ 'PackageName: Test', 'PackageVersion: Version 0.9.2', 'PackageDownloadLocation: http://example.com/test', 'PackageSummary: <text>Test package</text>', 'PackageSourceInfo: <text>Version 1.0 of test</text>', 'PackageFileName: test-1.0.zip', 'PackageSupplier: Organization:ACME', 'PackageOriginator: Organization:ACME', 'PackageChecksum: SHA1: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12', 'PackageVerificationCode: 4e3211c67a2d28fced849ee1bb76e7391b93feba (something.rdf, something.txt)', 'PackageDescription: <text>A package.</text>', 'PackageCopyrightText: <text> Copyright 2014 Acme Inc.</text>', 'PackageLicenseDeclared: Apache-2.0', 'PackageLicenseConcluded: (LicenseRef-2.0 and Apache-2.0)', 'PackageLicenseInfoFromFiles: Apache-1.0', 'PackageLicenseInfoFromFiles: Apache-2.0', 'PackageLicenseComments: <text>License Comments</text>' ]) file_str = '\n'.join([ 'FileName: testfile.java', 'FileType: SOURCE', 'FileChecksum: SHA1: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12', 'LicenseConcluded: Apache-2.0', 'LicenseInfoInFile: Apache-2.0', 'FileCopyrightText: <text>Copyright 2014 Acme Inc.</text>', 'ArtifactOfProjectName: AcmeTest', 'ArtifactOfProjectHomePage: http://www.acme.org/', 'ArtifactOfProjectURI: http://www.acme.org/', 'FileComment: <text>Very long file</text>' ]) complete_str = '{0}\n{1}\n{2}\n{3}\n{4}'.format(document_str, creation_str, review_str, package_str, file_str) def setUp(self): self.p = Parser(Builder(), StandardLogger()) self.p.build() def test_doc(self): document, error = self.p.parse(self.complete_str) assert document is not None assert not error assert document.version == Version(major=1, minor=2) assert document.data_license.identifier == 'CC0-1.0' assert document.comment == 'Sample Comment' def test_creation_info(self): document, error = self.p.parse(self.complete_str) assert document is not None assert not error assert len(document.creation_info.creators) == 2 assert document.creation_info.comment == 'Sample Comment' assert (document.creation_info.created_iso_format == '2010-02-03T00:00:00Z') def test_review(self): document, error = self.p.parse(self.complete_str) assert document is not None assert not error assert len(document.reviews) == 2 def test_package(self): document, error = self.p.parse(self.complete_str) assert document is not None assert not error assert document.package.name == 'Test' assert document.package.version == 'Version 0.9.2' assert len(document.package.licenses_from_files) == 2 assert (document.package.conc_lics.identifier == 'LicenseRef-2.0 AND Apache-2.0') def test_file(self): document, error = self.p.parse(self.complete_str) assert document is not None assert not error assert len(document.package.files) == 1 spdx_file = document.package.files[0] assert spdx_file.name == 'testfile.java' assert spdx_file.type == spdx.file.FileType.SOURCE assert len(spdx_file.artifact_of_project_name) == 1 assert len(spdx_file.artifact_of_project_home) == 1 assert len(spdx_file.artifact_of_project_uri) == 1
class TestParser(TestCase): document_str = '\n'.join([ 'SPDXVersion: SPDX-2.1', 'DataLicense: CC0-1.0', 'DocumentName: Sample_Document-V2.1', 'SPDXID: SPDXRef-DOCUMENT', 'DocumentComment: <text>Sample Comment</text>', 'DocumentNamespace: https://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301' ]) creation_str = '\n'.join([ 'Creator: Person: Bob ([email protected])', 'Creator: Organization: Acme.', 'Created: 2010-02-03T00:00:00Z', 'CreatorComment: <text>Sample Comment</text>' ]) review_str = '\n'.join([ 'Reviewer: Person: Bob the Reviewer', 'ReviewDate: 2010-02-10T00:00:00Z', 'ReviewComment: <text>Bob was Here.</text>', 'Reviewer: Person: Alice the Reviewer', 'ReviewDate: 2011-02-10T00:00:00Z', 'ReviewComment: <text>Alice was also here.</text>' ]) package_str = '\n'.join([ 'PackageName: Test', 'PackageVersion: Version 0.9.2', 'PackageDownloadLocation: http://example.com/test', 'PackageSummary: <text>Test package</text>', 'PackageSourceInfo: <text>Version 1.0 of test</text>', 'PackageFileName: test-1.0.zip', 'PackageSupplier: Organization:ACME', 'PackageOriginator: Organization:ACME', 'PackageChecksum: SHA1: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12', 'PackageVerificationCode: 4e3211c67a2d28fced849ee1bb76e7391b93feba (something.rdf, something.txt)', 'PackageDescription: <text>A package.</text>', 'PackageCopyrightText: <text> Copyright 2014 Acme Inc.</text>', 'PackageLicenseDeclared: Apache-2.0', 'PackageLicenseConcluded: (LicenseRef-2.0 and Apache-2.0)', 'PackageLicenseInfoFromFiles: Apache-1.0', 'PackageLicenseInfoFromFiles: Apache-2.0', 'PackageLicenseComments: <text>License Comments</text>' ]) file_str = '\n'.join([ 'FileName: testfile.java', 'SPDXID: SPDXRef-File', 'FileType: SOURCE', 'FileChecksum: SHA1: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12', 'LicenseConcluded: Apache-2.0', 'LicenseInfoInFile: Apache-2.0', 'FileCopyrightText: <text>Copyright 2014 Acme Inc.</text>', 'ArtifactOfProjectName: AcmeTest', 'ArtifactOfProjectHomePage: http://www.acme.org/', 'ArtifactOfProjectURI: http://www.acme.org/', 'FileComment: <text>Very long file</text>' ]) unknown_tag_str = 'SomeUnknownTag: SomeUnknownValue' complete_str = '{0}\n{1}\n{2}\n{3}\n{4}'.format(document_str, creation_str, review_str, package_str, file_str) def setUp(self): self.p = Parser(Builder(), StandardLogger()) self.p.build() def test_doc(self): document, error = self.p.parse(self.complete_str) assert document is not None assert not error assert document.version == Version(major=2, minor=1) assert document.data_license.identifier == 'CC0-1.0' assert document.name == 'Sample_Document-V2.1' assert document.spdx_id == 'SPDXRef-DOCUMENT' assert document.comment == 'Sample Comment' assert document.namespace == 'https://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301' def test_creation_info(self): document, error = self.p.parse(self.complete_str) assert document is not None assert not error assert len(document.creation_info.creators) == 2 assert document.creation_info.comment == 'Sample Comment' assert (document.creation_info.created_iso_format == '2010-02-03T00:00:00Z') def test_review(self): document, error = self.p.parse(self.complete_str) assert document is not None assert not error assert len(document.reviews) == 2 def test_package(self): document, error = self.p.parse(self.complete_str) assert document is not None assert not error assert document.package.name == 'Test' assert document.package.version == 'Version 0.9.2' assert len(document.package.licenses_from_files) == 2 assert (document.package.conc_lics.identifier == 'LicenseRef-2.0 AND Apache-2.0') def test_file(self): document, error = self.p.parse(self.complete_str) assert document is not None assert not error assert len(document.package.files) == 1 spdx_file = document.package.files[0] assert spdx_file.name == 'testfile.java' assert spdx_file.spdx_id == 'SPDXRef-File' assert spdx_file.type == spdx.file.FileType.SOURCE assert len(spdx_file.artifact_of_project_name) == 1 assert len(spdx_file.artifact_of_project_home) == 1 assert len(spdx_file.artifact_of_project_uri) == 1 def test_unknown_tag(self): try: from StringIO import StringIO except ImportError: from io import StringIO saved_out = sys.stdout sys.stdout = StringIO() document, error = self.p.parse(self.unknown_tag_str) self.assertEqual(sys.stdout.getvalue(), 'Found unknown tag : SomeUnknownTag at line: 1\n') sys.stdout = saved_out assert error assert document is not None
class TestParser(object): document_str = '\n'.join(['SPDXVersion: SPDX-1.2', 'DataLicense: CC0-1.0', 'DocumentComment: <text>Sample Comment</text>' ]) creation_str = '\n'.join(['Creator: Person: Bob ([email protected])', 'Creator: Organization: Acme.', 'Created: 2010-02-03T00:00:00Z', 'CreatorComment: <text>Sample Comment</text>' ]) review_str = '\n'.join([ 'Reviewer: Person: Bob the Reviewer', 'ReviewDate: 2010-02-10T00:00:00Z', 'ReviewComment: <text>Bob was Here.</text>', 'Reviewer: Person: Alice the Reviewer', 'ReviewDate: 2011-02-10T00:00:00Z', 'ReviewComment: <text>Alice was also here.</text>' ]) package_str = '\n'.join([ 'PackageName: Test', 'PackageVersion: Version 0.9.2', 'PackageDownloadLocation: http://example.com/test', 'PackageSummary: <text>Test package</text>', 'PackageSourceInfo: <text>Version 1.0 of test</text>', 'PackageFileName: test-1.0.zip', 'PackageSupplier: Organization:ACME', 'PackageOriginator: Organization:ACME', 'PackageChecksum: SHA1: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12', 'PackageVerificationCode: 4e3211c67a2d28fced849ee1bb76e7391b93feba (something.rdf, something.txt)', 'PackageDescription: <text>A package.</text>', 'PackageCopyrightText: <text> Copyright 2014 Acme Inc.</text>', 'PackageLicenseDeclared: Apache-2.0', 'PackageLicenseConcluded: (LicenseRef-2.0 and Apache-2.0)', 'PackageLicenseInfoFromFiles: Apache-1.0', 'PackageLicenseInfoFromFiles: Apache-2.0', 'PackageLicenseComments: <text>License Comments</text>' ]) file_str = '\n'.join([ 'FileName: testfile.java', 'FileType: SOURCE', 'FileChecksum: SHA1: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12', 'LicenseConcluded: Apache-2.0', 'LicenseInfoInFile: Apache-2.0', 'FileCopyrightText: <text>Copyright 2014 Acme Inc.</text>', 'ArtifactOfProjectName: AcmeTest', 'ArtifactOfProjectHomePage: http://www.acme.org/', 'ArtifactOfProjectURI: http://www.acme.org/', 'FileComment: <text>Very long file</text>' ]) complete_str = '{0}\n{1}\n{2}\n{3}\n{4}'.format(document_str, creation_str, review_str, package_str, file_str) def __init__(self): self.p = Parser(Builder(), StandardLogger()) self.p.build() def test_doc(self): document, error = self.p.parse(self.complete_str) assert document is not None assert not error assert document.version == Version(major=1, minor=2) assert document.data_license.identifier == 'CC0-1.0' assert document.comment == 'Sample Comment' def test_creation_info(self): document, error = self.p.parse(self.complete_str) assert document is not None assert not error assert len(document.creation_info.creators) == 2 assert document.creation_info.comment == 'Sample Comment' assert (document.creation_info.created_iso_format == '2010-02-03T00:00:00Z') def test_review(self): document, error = self.p.parse(self.complete_str) assert document is not None assert not error assert len(document.reviews) == 2 def test_package(self): document, error = self.p.parse(self.complete_str) assert document is not None assert not error assert document.package.name == 'Test' assert document.package.version == 'Version 0.9.2' assert len(document.package.licenses_from_files) == 2 assert (document.package.conc_lics.identifier == 'LicenseRef-2.0 AND Apache-2.0') def test_file(self): document, error = self.p.parse(self.complete_str) assert document is not None assert not error assert len(document.package.files) == 1 file = document.package.files[0] assert file.name == 'testfile.java' assert file.type == spdx.file.FileType.SOURCE assert len(file.artifact_of_project_name) == 1 assert len(file.artifact_of_project_home) == 1 assert len(file.artifact_of_project_uri) == 1
#!/usr/bin/env python if __name__ == '__main__': import sys from spdx.parsers.tagvalue import Parser from spdx.parsers.loggers import StandardLogger from spdx.parsers.tagvaluebuilders import Builder file = sys.argv[1] p = Parser(Builder(), StandardLogger()) p.build() with open(file) as f: data = f.read() document, error = p.parse(data) if not error: print 'Parsing Successful' print 'Document Version {0}.{1}'.format(document.version.major, document.version.minor) print 'Package name : {0}'.format(document.package.name) print 'Creators : ' for creator in document.creation_info.creators: print creator.name else: print 'Errors encountered while parsing'
def parse_tagvalue_file(self, file_name): """Returns tuple error, document.""" with open(file_name, mode="r") as infile: tvparser = TVParser(TVBuilder(), StandardLogger()) tvparser.build() return tvparser.parse(infile.read())