def parse_rdf_file(self, file_name):
     with open(file_name, mode='rb') as infile:
         rdfparser = RDFParser(RDFBuilder(), StandardLogger())
         doc, error = rdfparser.parse(infile)
     assert not error
     assert doc.validate([]) == []
     return doc
def RDF_to_TAG(infile_name, outfile_name):
    # if __name__ == "__main__":
    # import sys
    import codecs
    from spdx.parsers.rdf import Parser
    from spdx.parsers.loggers import StandardLogger
    from spdx.parsers.rdfbuilders import Builder
    from spdx.writers.tagvalue import write_document, InvalidDocumentError

    # infile_name = sys.argv[1]
    # outfile_name = sys.argv[2]
    rdfparser = Parser(Builder(), StandardLogger())
    with open(infile_name) as infile:
        document, error = rdfparser.parse(infile)
        if not error:
            # print(map(lambda c: c.name, document.creation_info.creators))
            print("Parsing Successful")
            with codecs.open(outfile_name, mode="w",
                             encoding="utf-8") as outfile:
                try:
                    write_document(document, outfile)
                except InvalidDocumentError:
                    # Note document is valid if error is False
                    print("Document is Invalid")
        else:
            print("Errors encountered while parsing RDF file.")
            messages = []
            document.validate(messages)
            print("\n".join(messages))
Exemple #3
0
 def test_file_lic_conc(self):
     parser = Parser(Builder(), StandardLogger())
     test_file = utils_test.get_test_loc('../../data/SPDXRdfExample.rdf', test_data_dir=utils_test.test_data_dir)
     with open(test_file, 'r') as f:
         document, _ = parser.parse(f)
     files = sorted(document.package.files)
     assert files[0].conc_lics.identifier.toPython() == 'LicenseRef-1'
     assert files[1].conc_lics.identifier == 'Apache-2.0'
Exemple #4
0
 def test_pkg_lic_conc(self):
     parser = Parser(Builder(), StandardLogger())
     test_file = utils_test.get_test_loc('../../data/SPDXRdfExample.rdf', test_data_dir=utils_test.test_data_dir)
     with open(test_file, 'r') as f:
         document, _ = parser.parse(f)
     # It is needed to ckeck it as sorted lists because the order changes when parsing
     lic_expected = ['Apache-1.0', 'Apache-2.0', 'LicenseRef-1', 'LicenseRef-2', 'LicenseRef-3', 'LicenseRef-4', 'MPL-1.1']
     lic_result = sorted(document.package.conc_lics.identifier.split(' AND '))
     assert lic_result == lic_expected
Exemple #5
0
 def test_extracted_licenses(self):
     parser = Parser(Builder(), StandardLogger())
     test_file = utils_test.get_test_loc('../../data/SPDXRdfExample.rdf', test_data_dir=utils_test.test_data_dir)
     with open(test_file, 'r') as f:
         document, _ = parser.parse(f)
     assert len(document.extracted_licenses) == 4
     # It is needed to sort the list because the order changes when parsing
     licenses = sorted(document.extracted_licenses)
     assert licenses[0].identifier.toPython() == 'LicenseRef-1'
     assert licenses[1].identifier.toPython() == 'LicenseRef-2'
     assert licenses[2].identifier.toPython() == 'LicenseRef-3'
     assert licenses[3].identifier.toPython() == 'LicenseRef-4'
def RDF_to_YAML(infile_name, outfile_name):
    # if __name__ == "__main__":
    # import sys
    from spdx.parsers.rdf import Parser
    from spdx.parsers.loggers import StandardLogger
    from spdx.parsers.rdfbuilders import Builder
    from spdx.writers.yaml import write_document

    # file = sys.argv[1]
    p = Parser(Builder(), StandardLogger())
    with open(infile_name) as f:
        document, error = p.parse(f)

        if not error:
            with open(outfile_name, "w") as out:
                write_document(document, out)
        else:
            print("Errors encountered while parsing")
Exemple #7
0
 def parse_rdf_file(self, file_name):
     """Returns tuple error, document."""
     with open(file_name, mode='rb') as infile:
         rdfparser = RDFParser(RDFBuilder(), StandardLogger())
         return rdfparser.parse(infile)
#!/usr/bin/env python
if __name__ == "__main__":
    import sys
    import spdx.file as spdxfile
    from spdx.parsers.rdf import Parser
    from spdx.parsers.loggers import StandardLogger
    from spdx.parsers.rdfbuilders import Builder

    file = sys.argv[1]
    p = Parser(Builder(), StandardLogger())
    with open(file) as f:
        doc, error = p.parse(f)
        if not error:
            print "doc comment: {0}".format(doc.comment)
            print "Creators:"
            for c in doc.creation_info.creators:
                print "\t{0}".format(c)
            print "Document review information:"
            for review in doc.reviews:
                print "\tReviewer: {0}".format(review.reviewer)
                print "\tDate: {0}".format(review.review_date)
                print "\tComment: {0}".format(review.comment)
            print "Creation comment: {0}".format(doc.creation_info.comment)
            print "Package Name: {0}".format(doc.package.name)
            print "Package Version: {0}".format(doc.package.version)
            print "Package Download Location: {0}".format(doc.package.download_location)
            print "Package Homepage: {0}".format(doc.package.homepage)
            print "Package Checksum: {0}".format(doc.package.check_sum.value)
            print "Package verification code: {0}".format(doc.package.verif_code)
            print "Package excluded from verif: {0}".format(",".join(doc.package.verif_exc_files))
            print "Package license concluded: {0}".format(doc.package.conc_lics)
Exemple #9
0
#!/usr/bin/env python
# Converts an RDF file to tag/value format.
# usage rdf_to_tv <rdffile> <tagvaluefile>
if __name__ == '__main__':
    import sys
    import codecs
    from spdx.parsers.rdf import Parser
    from spdx.parsers.loggers import StandardLogger
    from spdx.parsers.rdfbuilders import Builder
    from spdx.writers.tagvalue import write_document, InvalidDocumentError
    infile_name = sys.argv[1]
    outfile_name = sys.argv[2]
    rdfparser = Parser(Builder(), StandardLogger())
    with open(infile_name) as infile:
        document, error = rdfparser.parse(infile)
        if not error:
            # print map(lambda c: c.name, document.creation_info.creators)
            print 'Parsing Successful'
            with codecs.open(outfile_name, mode='w', encoding='utf-8') as outfile:
                try:
                    write_document(document, outfile)
                except InvalidDocumentError:
                    # Note document is valid if error is False
                    print 'Document is Invalid'
        else:
            print 'Errors encountered while parsing RDF file.'
            messages = []
            document.validate(messages)
            print '\n'.join(messages)
Exemple #10
0
#!/usr/bin/env python

# Parses an RDF file and prints out some basic information.
# Usage: parse_rdf.py <rdffile>

if __name__ == '__main__':
    import sys
    import spdx.file as spdxfile
    from spdx.parsers.rdf import Parser
    from spdx.parsers.loggers import StandardLogger
    from spdx.parsers.rdfbuilders import Builder
    file = sys.argv[1]
    p = Parser(Builder(), StandardLogger())
    with open(file) as f:
        doc, error = p.parse(f)
        if not error:
            print 'doc comment: {0}'.format(doc.comment)
            print 'Creators:'
            for c in doc.creation_info.creators:
                print '\t{0}'.format(c)
            print 'Document review information:'
            for review in doc.reviews:
                print '\tReviewer: {0}'.format(review.reviewer)
                print '\tDate: {0}'.format(review.review_date)
                print '\tComment: {0}'.format(review.comment)
            print 'Creation comment: {0}'.format(doc.creation_info.comment)
            print 'Package Name: {0}'.format(doc.package.name)
            print 'Package Version: {0}'.format(doc.package.version)
            print 'Package Download Location: {0}'.format(
                doc.package.download_location)
            print 'Package Homepage: {0}'.format(doc.package.homepage)
Exemple #11
0
def parse_RDF(file):
    import spdx.file as spdxfile
    from spdx.parsers.rdf import Parser
    from spdx.parsers.loggers import StandardLogger
    from spdx.parsers.rdfbuilders import Builder

    p = Parser(Builder(), StandardLogger())
    with open(file) as f:
        doc, error = p.parse(f)
        if not error:
            print("doc comment: {0}".format(doc.comment))
            print("Creators:")
            for c in doc.creation_info.creators:
                print("\t{0}".format(c))
            print("Document review information:")
            for review in doc.reviews:
                print("\tReviewer: {0}".format(review.reviewer))
                print("\tDate: {0}".format(review.review_date))
                print("\tComment: {0}".format(review.comment))
            print("Creation comment: {0}".format(doc.creation_info.comment))
            print("Package Name: {0}".format(doc.package.name))
            print("Package Version: {0}".format(doc.package.version))
            print("Package Download Location: {0}".format(
                doc.package.download_location))
            print("Package Homepage: {0}".format(doc.package.homepage))
            print("Package Checksum: {0}".format(doc.package.check_sum.value))
            print("Package Attribution Text: {0}".format(
                doc.package.attribution_text))
            print("Package verification code: {0}".format(
                doc.package.verif_code))
            print("Package excluded from verif: {0}".format(",".join(
                doc.package.verif_exc_files)))
            print("Package license concluded: {0}".format(
                doc.package.conc_lics))
            print("Package license declared: {0}".format(
                doc.package.license_declared))
            print("Package licenses from files:")
            for lics in doc.package.licenses_from_files:
                print("\t{0}".format(lics))
            print("Package Copyright text: {0}".format(doc.package.cr_text))
            print("Package summary: {0}".format(doc.package.summary))
            print("Package description: {0}".format(doc.package.description))
            print("Package Files:")
            VALUES = {
                spdxfile.FileType.SOURCE: "SOURCE",
                spdxfile.FileType.OTHER: "OTHER",
                spdxfile.FileType.BINARY: "BINARY",
                spdxfile.FileType.ARCHIVE: "ARCHIVE",
            }
            for f in doc.files:
                print("\tFile name: {0}".format(f.name))
                print("\tFile type: {0}".format(VALUES[f.type]))
                print("\tFile Checksum: {0}".format(f.chk_sum.value))
                print("\tFile license concluded: {0}".format(f.conc_lics))
                print("\tFile license info in file: {0}".format(",".join(
                    map(lambda l: l.identifier, f.licenses_in_file))))
                print("\tFile artifact of project name: {0}".format(",".join(
                    f.artifact_of_project_name)))

            print("Document Extracted licenses:")
            for lics in doc.extracted_licenses:
                print("\tIdentifier: {0}".format(lics.identifier))
                print("\tName: {0}".format(lics.full_name))

            print("Annotations:")
            for an in doc.annotations:
                print("\tAnnotator: {0}".format(an.annotator))
                print("\tAnnotation Date: {0}".format(an.annotation_date))
                print("\tAnnotation Comment: {0}".format(an.comment))
                print("\tAnnotation Type: {0}".format(an.annotation_type))
                print("\tAnnotation SPDX Identifier: {0}".format(an.spdx_id))

            # print(doc.__dict__)

            print("Relationships: ")
            for relation in doc.relationships:
                print("\tRelationship: {0}".format(relation.relationship))
                try:
                    print("\tRelationship: {0}".format(relation.comment))
                except:
                    continue

        else:
            print("Errors while parsing")
# Converts an RDF file to tag/value format.
# Usage: rdf_to_tv <rdffile> <tagvaluefile>
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals

if __name__ == '__main__':
    import sys
    import codecs
    from spdx.parsers.rdf import Parser
    from spdx.parsers.loggers import StandardLogger
    from spdx.parsers.rdfbuilders import Builder
    from spdx.writers.tagvalue import write_document, InvalidDocumentError
    infile_name = sys.argv[1]
    outfile_name = sys.argv[2]
    rdfparser = Parser(Builder(), StandardLogger())
    with open(infile_name) as infile:
        document, error = rdfparser.parse(infile)
        if not error:
            # print(map(lambda c: c.name, document.creation_info.creators))
            print('Parsing Successful')
            with codecs.open(outfile_name, mode='w',
                             encoding='utf-8') as outfile:
                try:
                    write_document(document, outfile)
                except InvalidDocumentError:
                    # Note document is valid if error is False
                    print('Document is Invalid')
        else:
            print('Errors encountered while parsing RDF file.')
            messages = []
 def parse_rdf_file(self, file_name):
     """Returns tuple error, document."""
     with open(file_name, mode="r") as infile:
         rdfparser = RDFParser(RDFBuilder(), StandardLogger())
         return rdfparser.parse(infile)