Beispiel #1
0
    def set_doc_version(self, doc, value):
        """
        Set the document version.
        Raise exceptions:
        - SPDXValueError if malformed value,
        - CardinalityError if already defined,
        - IncompatibleVersionError if not 1.2.
        """
        # FIXME: relax version supported
        if not self.doc_version_set:
            self.doc_version_set = True
            m = self.VERS_STR_REGEX.match(value)
            if m is None:
                raise SPDXValueError('Document::Version')
            else:
                vers = version.Version(major=int(m.group(1)),
                                       minor=int(m.group(2)))

                # FIXME: we can deal with newer versions too
                if vers == version.Version(major=1, minor=2):
                    doc.version = vers
                    return True
                else:
                    raise IncompatibleVersionError(value)
        else:
            raise CardinalityError('Document::Version')
Beispiel #2
0
    def handle_lics(self, lics):
        """
        Return a License from a `lics` license resource.
        """
        # Handle extracted licensing info type.
        if (lics, RDF.type, self.spdx_namespace['ExtractedLicensingInfo']) in self.graph:
            return self.parse_only_extr_license(lics)

        # Assume resource, hence the path separator
        ident_start = lics.rfind('/') + 1
        if ident_start == 0:
            # special values such as spdx:noassertion
            special = self.to_special_value(lics)
            if special == lics:
                if self.LICS_REF_REGEX.match(lics):
                    # Is a license ref i.e LicenseRef-1
                    return document.License.from_identifier(six.text_type(lics))
                else:
                    # Not a known license form
                    raise SPDXValueError('License')
            else:
                # is a special value
                return special
        else:
            # license url
            return document.License.from_identifier(lics[ident_start:])
 def set_spdx_doc_uri(self, doc, spdx_doc_uri):
     """
     Sets the `spdx_document_uri` attribute of the `ExternalDocumentRef`
     object.
     """
     if validations.validate_doc_namespace(spdx_doc_uri):
         doc.ext_document_references[-1].spdx_document_uri = spdx_doc_uri
     else:
         raise SPDXValueError('Document::ExternalDocumentRef')
 def create_entity(self, doc, value):
     if self.tool_re.match(value):
         return self.build_tool(doc, value)
     elif self.person_re.match(value):
         return self.build_person(doc, value)
     elif self.org_re.match(value):
         return self.build_org(doc, value)
     else:
         raise SPDXValueError('Entity')
 def set_chksum(self, doc, chk_sum):
     """
     Sets the external document reference's check sum, if not already set.
     chk_sum - The checksum value in the form of a string.
     """
     if chk_sum:
         doc.ext_document_references[-1].check_sum = checksum.Algorithm(
             'SHA1', chk_sum)
     else:
         raise SPDXValueError('ExternalDocumentRef::Checksum')
Beispiel #6
0
 def set_lic_id(self, doc, lic_id):
     """Adds a new extracted license to the document.
     Raises SPDXValueError if data format is incorrect.
     """
     self.reset_extr_lics()
     if validations.validate_extracted_lic_id(lic_id):
         doc.add_extr_lic(document.ExtractedLicense(lic_id))
         return True
     else:
         raise SPDXValueError('ExtractedLicense::id')
 def add_creator(self, doc, creator):
     """Adds a creator to the document's creation info.
     Returns true if creator is valid.
     Creator must be built by an EntityBuilder.
     Raises SPDXValueError if not a creator type.
     """
     if validations.validate_creator(creator):
         doc.creation_info.add_creator(creator)
         return True
     else:
         raise SPDXValueError('CreationInfo::Creator')
 def build_tool(self, doc, entity):
     """Builds a tool object out of a string representation.
     Returns built tool. Raises SPDXValueError if failed to extract
     tool name or name is malformed
     """
     match = self.tool_re.match(entity)
     if match and validations.validate_tool_name(match.group(self.TOOL_NAME_GROUP)):
         name = match.group(self.TOOL_NAME_GROUP)
         return creationinfo.Tool(name)
     else:
         raise SPDXValueError('Failed to extract tool name')
 def set_snippet_lics_info(self, doc, lics_info):
     """
     Raises OrderError if no snippet previously defined.
     Raises SPDXValueError if the data is a malformed value.
     """
     self.assert_snippet_exists()
     if validations.validate_snip_lics_info(lics_info):
         doc.snippet[-1].add_lics(lics_info)
         return True
     else:
         raise SPDXValueError('Snippet::LicenseInfoInSnippet')
 def set_pkg_license_from_file(self, doc, lic):
     """Adds a license from a file to the package.
     Raises SPDXValueError if data malformed.
     Raises OrderError if no package previously defined.
     """
     self.assert_package_exists()
     if validations.validate_lics_from_file(lic):
         doc.package.licenses_from_files.append(lic)
         return True
     else:
         raise SPDXValueError('Package::LicensesFromFile')
Beispiel #11
0
 def add_reviewer(self, doc, reviewer):
     """Adds a reviewer to the SPDX Document.
     Reviwer is an entity created by an EntityBuilder.
     Raises SPDXValueError if not a valid reviewer type.
     """
     # Each reviewer marks the start of a new review object.
     self.reset_reviews()
     if validations.validate_reviewer(reviewer):
         doc.add_review(review.Review(reviewer=reviewer))
         return True
     else:
         raise SPDXValueError('Review::Reviewer')
 def set_file_license_in_file(self, doc, lic):
     """
     Raises OrderError if no package or file defined.
     Raises SPDXValueError if malformed value.
     """
     if self.has_package(doc) and self.has_file(doc):
         if validations.validate_file_lics_in_file(lic):
             self.file(doc).add_lics(lic)
             return True
         else:
             raise SPDXValueError('File::LicenseInFile')
     else:
         raise OrderError('File::LicenseInFile')
 def add_annotator(self, doc, annotator):
     """Adds an annotator to the SPDX Document.
     Annotator is an entity created by an EntityBuilder.
     Raises SPDXValueError if not a valid annotator type.
     """
     # Each annotator marks the start of a new annotation object.
     # FIXME: this state does not make sense
     self.reset_annotations()
     if validations.validate_annotator(annotator):
         doc.add_annotation(annotation.Annotation(annotator=annotator))
         return True
     else:
         raise SPDXValueError('Annotation::Annotator')
 def set_doc_data_lics(self, doc, lics):
     """Sets the document data license.
     Raises value error if malformed value, CardinalityError
     if already defined.
     """
     if not self.doc_data_lics_set:
         self.doc_data_lics_set = True
         if validations.validate_data_lics(lics):
             doc.data_license = document.License.from_identifier(lics)
             return True
         else:
             raise SPDXValueError('Document::DataLicense')
     else:
         raise CardinalityError('Document::DataLicense')
Beispiel #15
0
 def set_doc_spdx_id(self, doc, doc_spdx_id_line):
     """Sets the document SPDX Identifier.
     Raises value error if malformed value, CardinalityError
     if already defined.
     """
     if not self.doc_spdx_id_set:
         if validations.validate_doc_spdx_id(doc_spdx_id_line):
             doc.spdx_id = doc_spdx_id_line
             self.doc_spdx_id_set = True
             return True
         else:
             raise SPDXValueError('Document::SPDXID')
     else:
         raise CardinalityError('Document::SPDXID')
 def create_snippet(self, doc, spdx_id):
     """Creates a snippet for the SPDX Document.
     spdx_id - To uniquely identify any element in an SPDX document which
     may be referenced by other elements.
     Raises SPDXValueError if the data is a malformed value.
     """
     self.reset_snippet()
     spdx_id = spdx_id.split('#')[-1]
     if validations.validate_snippet_spdx_id(spdx_id):
         doc.add_snippet(snippet.Snippet(spdx_id=spdx_id))
         self.snippet_spdx_id_set = True
         return True
     else:
         raise SPDXValueError('Snippet::SnippetSPDXID')
 def set_creation_comment(self, doc, comment):
     """Sets creation comment, Raises CardinalityError if
     comment already set.
     Raises SPDXValueError if not free form text.
     """
     if not self.creation_comment_set:
         self.creation_comment_set = True
         if validations.validate_creation_comment(comment):
             doc.creation_info.comment = str_from_text(comment)
             return True
         else:
             raise SPDXValueError('CreationInfo::Comment')
     else:
         raise CardinalityError('CreationInfo::Comment')
Beispiel #18
0
 def set_doc_namespace(self, doc, namespace):
     """Sets the document namespace.
     Raise SPDXValueError if malformed value, CardinalityError
     if already defined.
     """
     if not self.doc_namespace_set:
         self.doc_namespace_set = True
         if validations.validate_doc_namespace(namespace):
             doc.namespace = namespace
             return True
         else:
             raise SPDXValueError('Document::Namespace')
     else:
         raise CardinalityError('Document::Comment')
 def set_doc_comment(self, doc, comment):
     """Sets document comment, Raises CardinalityError if
     comment already set.
     Raises SPDXValueError if comment is not free form text.
     """
     if not self.doc_comment_set:
         self.doc_comment_set = True
         if validations.validate_doc_comment(comment):
             doc.comment = str_from_text(comment)
             return True
         else:
             raise SPDXValueError('Document::Comment')
     else:
         raise CardinalityError('Document::Comment')
 def set_lics_list_ver(self, doc, value):
     """Sets the license list version, Raises CardinalityError if
     already set, SPDXValueError if incorrect value.
     """
     if not self.lics_list_ver_set:
         self.lics_list_ver_set = True
         vers = version.Version.from_str(value)
         if vers is not None:
             doc.creation_info.license_list_version = vers
             return True
         else:
             raise SPDXValueError('CreationInfo::LicenseListVersion')
     else:
         raise CardinalityError('CreationInfo::LicenseListVersion')
 def build_person(self, doc, entity):
     """Builds an organization object of of a string representation.
     Returns built organization. Raises SPDXValueError if failed to extract
     name.
     """
     match = self.person_re.match(entity)
     if match and validations.validate_person_name(match.group(self.PERSON_NAME_GROUP)):
         name = match.group(self.PERSON_NAME_GROUP).strip()
         email = match.group(self.PERSON_EMAIL_GROUP)
         if (email is not None) and (len(email) != 0):
             return creationinfo.Person(name=name, email=email.strip())
         else:
             return creationinfo.Person(name=name, email=None)
     else:
         raise SPDXValueError('Failed to extract person name')
Beispiel #22
0
 def set_doc_data_lic(self, doc, res):
     """Sets the document data license.
     Raises value error if malformed value, CardinalityError
     if already defined.
     """
     if not self.doc_data_lics_set:
         self.doc_data_lics_set = True
         res_parts = res.split('/')
         if len(res_parts) != 0:
             identifier = res_parts[-1]
             doc.data_license = document.License.from_identifier(identifier)
         else:
             raise SPDXValueError('Document::License')
     else:
         raise CardinalityError('Document::License')
 def set_pkg_desc(self, doc, text):
     """Set's the package's description.
     Raises SPDXValueError if text is not free form text.
     Raises CardinalityError if description already set.
     Raises OrderError if no package previously defined.
     """
     self.assert_package_exists()
     if not self.package_desc_set:
         self.package_desc_set = True
         if validations.validate_pkg_desc(text):
             doc.package.description = str_from_text(text)
         else:
             raise SPDXValueError('Package::Description')
     else:
         raise CardinalityError('Package::Description')
 def set_pkg_summary(self, doc, text):
     """Set's the package summary.
     Raises SPDXValueError if text is not free form text.
     Raises CardinalityError if summary already set.
     Raises OrderError if no package previously defined.
     """
     self.assert_package_exists()
     if not self.package_summary_set:
         self.package_summary_set = True
         if validations.validate_pkg_summary(text):
             doc.package.summary = str_from_text(text)
         else:
             raise SPDXValueError('Package::Summary')
     else:
         raise CardinalityError('Package::Summary')
 def set_created_date(self, doc, created):
     """Sets created date, Raises CardinalityError if
     created date already set.
     Raises SPDXValueError if created is not a date.
     """
     if not self.created_date_set:
         self.created_date_set = True
         date = utils.datetime_from_iso_format(created)
         if date is not None:
             doc.creation_info.created = date
             return True
         else:
             raise SPDXValueError('CreationInfo::Date')
     else:
         raise CardinalityError('CreationInfo::Created')
Beispiel #26
0
 def set_doc_spdx_id(self, doc, doc_spdx_id_line):
     """
     Set the document SPDX Identifier.
     Raise SPDXValueError if malformed value, CardinalityError
     if already defined.
     """
     if not self.doc_spdx_id_set:
         if (doc_spdx_id_line == "SPDXRef-DOCUMENT"
                 or validations.validate_doc_spdx_id(doc_spdx_id_line)):
             doc.spdx_id = doc_spdx_id_line
             self.doc_spdx_id_set = True
             return True
         else:
             raise SPDXValueError("Document::SPDXID")
     else:
         raise CardinalityError("Document::SPDXID")
 def set_file_notice(self, doc, text):
     """Raises OrderError if no package or file defined.
     Raises SPDXValueError if not free form text.
     Raises CardinalityError if more than one.
     """
     if self.has_package(doc) and self.has_file(doc):
         if not self.file_notice_set:
             self.file_notice_set = True
             if validations.validate_file_notice(text):
                 self.file(doc).notice = str_from_text(text)
             else:
                 raise SPDXValueError('File::Notice')
         else:
             raise CardinalityError('File::Notice')
     else:
         raise OrderError('File::Notice')
 def set_pkg_originator(self, doc, entity):
     """Sets the package originator, if not already set.
     entity - Organization, Person or NoAssert.
     Raises CardinalityError if already has an originator.
     Raises OrderError if no package previously defined.
     """
     self.assert_package_exists()
     if not self.package_originator_set:
         self.package_originator_set = True
         if validations.validate_pkg_originator(entity):
             doc.package.originator = entity
             return True
         else:
             raise SPDXValueError('Package::Originator')
     else:
         raise CardinalityError('Package::Originator')
 def set_pkg_license_comment(self, doc, text):
     """Sets the package's license comment.
     Raises OrderError if no package previously defined.
     Raises CardinalityError if already set.
     Raises SPDXValueError if text is not free form text.
     """
     self.assert_package_exists()
     if not self.package_license_comment_set:
         self.package_license_comment_set = True
         if validations.validate_pkg_lics_comment(text):
             doc.package.license_comment = str_from_text(text)
             return True
         else:
             raise SPDXValueError('Package::LicenseComment')
     else:
         raise CardinalityError('Package::LicenseComment')
 def set_pkg_license_declared(self, doc, lic):
     """Sets the package's declared license.
     Raises SPDXValueError if data malformed.
     Raises OrderError if no package previously defined.
     Raises CardinalityError if already set.
     """
     self.assert_package_exists()
     if not self.package_license_declared_set:
         self.package_license_declared_set = True
         if validations.validate_lics_conc(lic):
             doc.package.license_declared = lic
             return True
         else:
             raise SPDXValueError('Package::LicenseDeclared')
     else:
         raise CardinalityError('Package::LicenseDeclared')