Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/base/build.py
- This file was added.
# 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 | |||||
import arrow | |||||
import time | |||||
from swh.model.identifiers import normalize_timestamp | |||||
from dateutil import parser as date_parser | |||||
class construct_revision: | |||||
"""Construct revision from the metadata of the package version | |||||
Construct 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. | |||||
""" | |||||
UTC_OFFSET = 0 | |||||
SWH_PERSON = { | |||||
'name': 'Software Heritage', | |||||
'fullname': 'Software Heritage', | |||||
'email': 'robot@softwareheritage.org' | |||||
} | |||||
REVISION_MESSAGE = 'swh-loader-base: 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 _time_from_last_modified(self, last_modified): | |||||
"""Compute the modification time from the tarpath. | |||||
Args: | |||||
last_modified (str): Last modification time | |||||
Returns: | |||||
dict representing a timestamp with keys {seconds, microseconds} | |||||
""" | |||||
last_modified = arrow.get(last_modified) | |||||
mtime = last_modified.float_timestamp | |||||
normalized_time = list(map(int, str(mtime).split('.'))) | |||||
return { | |||||
'seconds': normalized_time[0], | |||||
'microseconds': normalized_time[1] | |||||
} | |||||
def compute_revision(self, directory, package_source_data): | |||||
"""Compute 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': self.directory(directory), | |||||
'synthetic': self.find_synthetic(), | |||||
} | |||||
return self.modify_revision(revision) | |||||
def find_synthetic(self): | |||||
return True | |||||
def find_type(self, package_source_data): | |||||
return package_source_data['nature'] | |||||
def find_message(self, package_source_data): | |||||
return self.REVISION_MESSAGE | |||||
def directory(self, directory): | |||||
return directory.hash | |||||
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): | |||||
date = date_parser.parse(package_source_data['date']) | |||||
date = normalize_timestamp(int(date.timestamp())) | |||||
if 'date' in package_source_data: | |||||
date = date_parser.parse(package_source_data['date']) | |||||
date = normalize_timestamp(int(date.timestamp())) | |||||
return date | |||||
timestamp = time.time() | |||||
timestamp = time.strftime('%a, %d %b %Y %H:%M:%S GMT', | |||||
time.gmtime(timestamp)) | |||||
return self._time_from_last_modified(timestamp) |