From dc1796075ba452c8e92df5fa252aaaaf91bd0f9b Mon Sep 17 00:00:00 2001 From: "Ahmed H. Ismail" Date: Mon, 4 Aug 2014 01:09:08 +0300 Subject: [PATCH] Updates README files and example files. Adds new example file rdf_to_tv.py. Signed-off-by: Ahmed H. Ismail --- README | 11 ++++++++++- README.md | 10 +++++++++- parse_rdf.py | 23 +++++++++++++++++++++++ parse_tv_ex.py | 1 + pp_tv.py | 4 +++- rdf_to_tv.py | 29 +++++++++++++++++++++++++++++ write_tv_ex.py | 4 +++- 7 files changed, 78 insertions(+), 4 deletions(-) mode change 100644 => 100755 parse_rdf.py mode change 100644 => 100755 parse_tv_ex.py mode change 100644 => 100755 pp_tv.py create mode 100755 rdf_to_tv.py mode change 100644 => 100755 write_tv_ex.py diff --git a/README b/README index 83f2217..367d989 100644 --- a/README +++ b/README @@ -18,9 +18,12 @@ Expected Features: Current Status: =============== -* RDF Parser unimplemented. +* RDF Parser implemented. +* Tag/value parser implemented +* Tag/value writer implemented. * RDF/Writer incomplete. + How to use: =========== @@ -46,6 +49,12 @@ Run 'python write_tv_ex.py sample.tag' to test it. The file 'pp_tv.py' demonstrates how to parse a tag/value file and print it out. To test it run 'python pp_tv.py Examples/SPDXTagExample.tag pretty.tag'. +The file 'parse_rdf.py' demonstrates how to parse a RDF/xml file and print out +document information. +To test it run 'python parse_rdf.py Examples/SPDXRdfExample.rdf' + +The file 'rdf_to_tv.py' demonstrates how to convert a RDF file to a tag/value one. +To test it run 'python rdf_to_tv.py Examples/SPDXRdfExample.rdf converted.tag' Installation: ============= diff --git a/README.md b/README.md index ba4a3fd..90fc0b0 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,9 @@ Expected Features: Current Status: =============== -* RDF Parser unimplemented. +* RDF Parser implemented. +* Tag/value parser implemented +* Tag/value writer implemented. * RDF/Writer incomplete. @@ -46,6 +48,12 @@ Run `python write_tv_ex.py sample.tag` to test it. The file `pp_tv.py` demonstrates how to parse a tag/value file and print it out. To test it run `python pp_tv.py Examples/SPDXTagExample.tag pretty.tag`. +The file `parse_rdf.py` demonstrates how to parse a RDF/xml file and print out +document information. +To test it run `python parse_rdf.py Examples/SPDXRdfExample.rdf` + +The file `rdf_to_tv.py` demonstrates how to convert a RDF file to a tag/value one. +To test it run `python rdf_to_tv.py Examples/SPDXRdfExample.rdf converted.tag` Installation: ============= diff --git a/parse_rdf.py b/parse_rdf.py old mode 100644 new mode 100755 index 973c31c..61e4d32 --- a/parse_rdf.py +++ b/parse_rdf.py @@ -1,5 +1,7 @@ +#!/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 @@ -12,6 +14,11 @@ 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) @@ -28,6 +35,22 @@ 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) diff --git a/parse_tv_ex.py b/parse_tv_ex.py old mode 100644 new mode 100755 index 5cc0a8c..b75c07b --- a/parse_tv_ex.py +++ b/parse_tv_ex.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python if __name__ == '__main__': import sys from spdx.parsers.tagvalue import Parser diff --git a/pp_tv.py b/pp_tv.py old mode 100644 new mode 100755 index cff978f..bfc6c1d --- a/pp_tv.py +++ b/pp_tv.py @@ -1,9 +1,11 @@ +#!/usr/bin/env python # Parses a tag/value file # and prints it out formatted. # usage pp_tv 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 @@ -17,7 +19,7 @@ document, error = p.parse(data) if not error: print 'Parsing Successful' - with open(target, 'w') as out: + with codecs.open(target, mode='w', encoding='utf-8') as out: try: write_document(document, out) except InvalidDocumentError: diff --git a/rdf_to_tv.py b/rdf_to_tv.py new file mode 100755 index 0000000..8460983 --- /dev/null +++ b/rdf_to_tv.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# Converts an RDF file to tag/value format. +# usage rdf_to_tv +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) \ No newline at end of file diff --git a/write_tv_ex.py b/write_tv_ex.py old mode 100644 new mode 100755 index 5835945..a6765e8 --- a/write_tv_ex.py +++ b/write_tv_ex.py @@ -1,5 +1,7 @@ +#!/usr/bin/env python 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 @@ -70,7 +72,7 @@ doc.add_extr_lic(lic) file = sys.argv[1] - with open(file, 'w') as out: + with codecs.open(file, mode='w', encoding='utf-8') as out: try: write_document(doc, out) except InvalidDocumentError: