Skip to content
Snippets Groups Projects
Commit 45a1f7db authored by Nicolas Dandrimont's avatar Nicolas Dandrimont
Browse files

identifiers: move dates to the root of the objects

parent 56891b94
No related branches found
No related tags found
No related merge requests found
......@@ -208,14 +208,40 @@ def format_offset(offset):
return t.encode()
def format_author(author):
components = [
author['name'], b' <', author['email'], b'> ',
format_date(author['date']), b' ',
format_offset(author['date_offset']),
]
def format_date_offset(date_offset):
"""Format a date-compatible object with its timezone offset.
A date-compatible object is either:
- a dict with two members
timestamp: floating point number of seconds since the unix epoch
offset: (int) number of minutes representing the offset from UTC
- a datetime.datetime object with a timezone
- a numeric value (in which case the offset is hardcoded to 0)
"""
return b''.join(components)
# FIXME: move normalization to another module
if isinstance(date_offset, dict):
date = date_offset['timestamp']
offset = date_offset['offset']
elif isinstance(date_offset, datetime.datetime):
date = date_offset
utcoffset = date_offset.utcoffset()
if utcoffset is None:
raise ValueError('Received a datetime without a timezone')
seconds_offset = utcoffset.total_seconds()
if seconds_offset - int(seconds_offset) != 0 or seconds_offset % 60:
raise ValueError('Offset is not an integer number of minutes')
offset = int(seconds_offset) // 60
else:
date = date_offset
offset = 0
return b''.join([format_date(date), b' ', format_offset(offset)])
def format_author(author):
return b''.join([author['name'], b' <', author['email'], b'>'])
def revision_identifier(revision):
......@@ -231,8 +257,10 @@ def revision_identifier(revision):
])
components.extend([
b'author ', format_author(revision['author']), b'\n',
b'committer ', format_author(revision['committer']), b'\n',
b'author ', format_author(revision['author']),
b' ', format_date_offset(revision['date']), b'\n',
b'committer ', format_author(revision['committer']),
b' ', format_date_offset(revision['committer_date']), b'\n',
b'\n',
revision['message'],
])
......@@ -251,7 +279,8 @@ def release_identifier(release):
if 'author' in release and release['author']:
components.extend([
b'tagger ', format_author(release['author']), b'\n',
b'tagger ', format_author(release['author']), b' ',
format_date_offset(release['date']), b'\n',
])
components.extend([b'\n', release['comment']])
......
......@@ -216,6 +216,9 @@ class DirectoryIdentifier(unittest.TestCase):
class RevisionIdentifier(unittest.TestCase):
def setUp(self):
linus_tz = datetime.timezone(datetime.timedelta(minutes=-420))
self.revision = {
'id': 'bc0195aad0daa2ad5b0d76cce22b167bc3435590',
'directory': '85a74718d377195e1efd0843ba4f3260bad4fe07',
......@@ -223,19 +226,15 @@ class RevisionIdentifier(unittest.TestCase):
'author': {
'name': b'Linus Torvalds',
'email': b'torvalds@linux-foundation.org',
'date': datetime.datetime(2015, 7, 12, 22, 10, 30,
tzinfo=datetime.timezone.utc),
'date_offset': -420,
},
'date': datetime.datetime(2015, 7, 12, 15, 10, 30,
tzinfo=linus_tz),
'committer': {
'name': b'Linus Torvalds',
'email': b'torvalds@linux-foundation.org',
'date': datetime.datetime(2015, 7, 12, 22, 10, 30,
tzinfo=datetime.timezone.utc),
'date_offset': -420,
},
'committer_date': datetime.datetime(2015, 7, 12, 15, 10, 30,
tzinfo=linus_tz),
'message': b'Linux 4.2-rc2\n',
}
......@@ -245,18 +244,17 @@ class RevisionIdentifier(unittest.TestCase):
'author': {
'name': b'Software Heritage',
'email': b'robot@softwareheritage.org',
'date': datetime.datetime(2015, 7, 16, 11, 51, 35,
tzinfo=datetime.timezone.utc),
'date_offset': 0,
},
'date': {
'timestamp': 1437047495.0,
'offset': 0,
},
'type': 'tar',
'committer': {
'name': b'Software Heritage',
'date': datetime.datetime(2015, 7, 16, 11, 51, 35,
tzinfo=datetime.timezone.utc),
'email': b'robot@softwareheritage.org',
'date_offset': 0,
},
'committer_date': 1437047495,
'synthetic': True,
'parents': [None],
'message': b'synthetic revision message\n',
......@@ -289,6 +287,8 @@ class RevisionIdentifier(unittest.TestCase):
class ReleaseIdentifier(unittest.TestCase):
def setUp(self):
linus_tz = datetime.timezone(datetime.timedelta(minutes=-420))
self.release = {
'id': '2b10839e32c4c476e9d94492756bb1a3e1ec4aa8',
'revision': b't\x1b"R\xa5\xe1Ml`\xa9\x13\xc7z`\x99\xab\xe7:\x85J',
......@@ -296,10 +296,9 @@ class ReleaseIdentifier(unittest.TestCase):
'author': {
'name': b'Linus Torvalds',
'email': b'torvalds@g5.osdl.org',
'date': datetime.datetime(2005, 10, 28, 0, 2, 33,
tzinfo=datetime.timezone.utc),
'date_offset': -420,
},
'date': datetime.datetime(2005, 10, 27, 17, 2, 33,
tzinfo=linus_tz),
'comment': b'''\
Linux 2.6.14 release
-----BEGIN PGP SIGNATURE-----
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment