diff --git a/swh/loader/package/build_revision.py b/swh/loader/package/build_revision.py new file mode 100644 --- /dev/null +++ b/swh/loader/package/build_revision.py @@ -0,0 +1,92 @@ +# Copyright (C) 2019 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +from datetime import datetime +from swh.model.identifiers import normalize_timestamp +from dateutil import parser as date_parser + + +class BuildRevision: + """Build revision from the metadata of the package version + + Build the revision for a package version using the metadata provided. + There are several hookpoints that can be overridden as per the need of + package manager. + + """ + + SWH_PERSON = { + 'name': b'Software Heritage', + 'fullname': b'Software Heritage', + 'email': b'robot@softwareheritage.org' + } + REVISION_MESSAGE = b'swh-loader-package: synthetic revision message' + + def modify_revision(self, revision): + """Make modification on revision created + If the revision of a package manager needs to be in a specific format, + this method can be overridden to perform that operation insted of + overriding the `compute_revision()`. + + Args: + revision (dict): Created revision + + Returns: + dict: Modified revision + + """ + return revision + + def build_revision(self, directory, package_source_data): + """Build a revision. + + Args: + directory (str): absolute path to the tarball + package_source_data (dict): Information about the package + release version + + Returns: + dict: Revision + + """ + revision = { + 'metadata': self.find_metadata(package_source_data), + 'date': self.find_date(package_source_data), + 'committer_date': self.find_date(package_source_data), + 'author': self.find_author(package_source_data), + 'committer': self.find_author(package_source_data), + 'type': self.find_type(package_source_data), + 'message': self.find_message(package_source_data), + 'directory': directory.hash, + 'synthetic': True, + 'parents': [], + } + + return self.modify_revision(revision) + + def find_type(self, package_source_data): + return package_source_data['nature'] + + def find_message(self, package_source_data): + return self.REVISION_MESSAGE + + def find_author(self, package_source_data): + if 'author' in package_source_data: + return package_source_data['author'] + return self.SWH_PERSON + + def find_metadata(self, package_source_data): + return { + 'package': package_source_data + } + + def find_date(self, package_source_data): + try: + # if `date` key in package_source_data: + date = date_parser.parse(package_source_data['date']) + return normalize_timestamp(int(date.timestamp())) + except Exception: + now = datetime.now() + return normalize_timestamp(int(datetime.timestamp(now))) diff --git a/swh/loader/package/loader.py b/swh/loader/package/loader.py --- a/swh/loader/package/loader.py +++ b/swh/loader/package/loader.py @@ -15,11 +15,12 @@ from swh.model.hashutil import MultiHash, HASH_BLOCK_SIZE from swh.storage.algos.snapshot import snapshot_get_all_branches +from .build_revision import BuildRevision DEBUG_MODE = '** DEBUG MODE **' -class PackageLoader(BufferedLoader): +class PackageLoader(BufferedLoader, BuildRevision): """Package loader class for package manager loader A loader is a component of the Software Heritage architecture responsible