diff --git a/swh/loader/tar/build.py b/swh/loader/tar/build.py index 1ee03b7..83f9372 100755 --- a/swh/loader/tar/build.py +++ b/swh/loader/tar/build.py @@ -1,134 +1,136 @@ # Copyright (C) 2015 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 os from swh.loader.tar import utils # Static setup EPOCH = 0 -UTC_OFFSET = '+0000' -SWH_PERSON = 'Software Heritage' -SWH_MAIL = 'robot@softwareheritage.org' +UTC_OFFSET = 0 +SWH_PERSON = { + 'name': 'Software Heritage', + 'fullname': 'Software Heritage', + 'email': 'robot@softwareheritage.org' +} REVISION_MESSAGE = 'synthetic revision message' RELEASE_MESSAGE = 'synthetic release message' REVISION_TYPE = 'tar' def compute_origin(url_scheme, url_type, root_dirpath, tarpath): """Compute the origin. Args: - url_scheme: scheme to build the origin's url - url_type: origin's type - root_dirpath: the top level root directory path - tarpath: file's absolute path Returns: Dictionary origin with keys: - url: origin's url - type: origin's type """ relative_path = utils.commonname(root_dirpath, tarpath) return { 'url': ''.join([url_scheme, os.path.dirname(relative_path)]), 'type': url_type, } def occurrence_with_date(date, tarpath): """Compute the occurrence using the tarpath's ctime. Args: authority: the authority's uuid tarpath: file's path Returns: Occurrence dictionary (cf. _build_occurrence) """ return { 'branch': os.path.basename(tarpath), 'date': date } def _time_from_path(tarpath): """Compute the modification time from the tarpath. """ return os.lstat(tarpath).st_mtime def compute_revision(tarpath): """Compute a revision. Args: tarpath: absolute path to the tarball Returns: Revision as dict: - - author_date: the modification timestamp as returned by a fstat call - - author_offset: +0000 + - date: the modification timestamp as returned by a fstat call - committer_date: the modification timestamp as returned by a fstat call - - committer_offset: +0000 - - author_name: cf. SWH_PERSON - - author_email: cf. SWH_MAIL - - committer_name: cf. SWH_MAIL - - committer_email: cf. SWH_MAIL + - author: cf. SWH_PERSON + - committer: cf. SWH_PERSON - type: cf. REVISION_TYPE - message: cf. REVISION_MESSAGE """ ts = _time_from_path(tarpath) return { - 'author_date': ts, - 'author_offset': UTC_OFFSET, - 'committer_date': ts, - 'committer_offset': UTC_OFFSET, - 'author_name': SWH_PERSON, - 'author_email': SWH_MAIL, - 'committer_name': SWH_PERSON, - 'committer_email': SWH_MAIL, + 'date': { + 'timestamp': ts, + 'offset': UTC_OFFSET, + }, + 'committer_date': { + 'timestamp': ts, + 'offset': UTC_OFFSET, + }, + 'author': SWH_PERSON, + 'committer': SWH_PERSON, 'type': REVISION_TYPE, 'message': REVISION_MESSAGE, } def compute_release(filename, tarpath): """Compute a release from a given tarpath, filename. If the tarpath does not contain a recognizable release number, the release can be skipped. Args: filename: file's name without path tarpath: file's absolute path Returns: None if the release number cannot be extracted from the filename. Otherwise a synthetic release is computed with the following keys: - name: the release computed from the filename - date: the modification timestamp as returned by a fstat call - - offset: +0000 + - offset: 0 - author_name: '' - author_email: '' - comment: '' """ release_number = utils.release_number(filename) if release_number: return { 'name': release_number, - 'date': _time_from_path(tarpath), - 'offset': UTC_OFFSET, - 'author_name': SWH_PERSON, - 'author_email': SWH_MAIL, - 'comment': RELEASE_MESSAGE, + 'date': { + 'timestamp': _time_from_path(tarpath), + 'offset': UTC_OFFSET, + }, + 'author': SWH_PERSON, + 'message': RELEASE_MESSAGE, } return None diff --git a/swh/loader/tar/tests/test_build.py b/swh/loader/tar/tests/test_build.py index d51afbc..e4a7763 100644 --- a/swh/loader/tar/tests/test_build.py +++ b/swh/loader/tar/tests/test_build.py @@ -1,103 +1,106 @@ # Copyright (C) 2015 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 unittest from nose.tools import istest from unittest.mock import patch from swh.loader.tar import build class TestBuildUtils(unittest.TestCase): @istest def compute_origin(self): # given expected_origin = { 'url': 'rsync://some/url/package-foo', 'type': 'rsync', } # when actual_origin = build.compute_origin( 'rsync://some/url/', 'rsync', '/some/root/path/', '/some/root/path/package-foo/package-foo-1.2.3.tgz') # then self.assertEquals(actual_origin, expected_origin) @istest def occurrence_with_date(self): # given expected_occurrence = { 'branch': b'package-bar.tgz', 'date': '2015-10-22 08:44:47.422384+00' } # when actual_occurrence = build.occurrence_with_date( '2015-10-22 08:44:47.422384+00', b'/path/to/package-bar.tgz',) # then self.assertEquals(actual_occurrence, expected_occurrence) @istest def compute_release__no_release(self): # given # when actual_release = build.compute_release( 'pack-without-version.tgz', '/some/path/to/pack-without-version.tgz') # then self.assertIsNone(actual_release) @istest def compute_release(self): # given expected_release = { 'name': '1.2.3rc1', - 'date': 'some-time', - 'offset': build.UTC_OFFSET, - 'author_name': build.SWH_PERSON, - 'author_email': build.SWH_MAIL, - 'comment': build.RELEASE_MESSAGE, + 'date': { + 'timestamp': 'some-time', + 'offset': build.UTC_OFFSET, + }, + 'author': build.SWH_PERSON, + 'message': build.RELEASE_MESSAGE, } # when with patch('swh.loader.tar.build._time_from_path', return_value='some-time'): actual_release = build.compute_release( 'foobar-1.2.3rc1.tgz', '/some/path/to/path-without-version.tgz') # then self.assertEquals(expected_release, actual_release) @istest def compute_revision(self): # when with patch('swh.loader.tar.build._time_from_path', return_value='some-other-time'): actual_revision = build.compute_revision('/some/path') expected_revision = { - 'author_date': 'some-other-time', - 'author_offset': build.UTC_OFFSET, - 'committer_date': 'some-other-time', - 'committer_offset': build.UTC_OFFSET, - 'author_name': build.SWH_PERSON, - 'author_email': build.SWH_MAIL, - 'committer_name': build.SWH_PERSON, - 'committer_email': build.SWH_MAIL, + 'date': { + 'timestamp': 'some-other-time', + 'offset': build.UTC_OFFSET, + }, + 'committer_date': { + 'timestamp': 'some-other-time', + 'offset': build.UTC_OFFSET, + }, + 'author': build.SWH_PERSON, + 'committer': build.SWH_PERSON, 'type': build.REVISION_TYPE, 'message': build.REVISION_MESSAGE, } # then self.assertEquals(actual_revision, expected_revision)