Merge lp:~cjwatson/swoffsite/tz-aware-last-modified-comparison into lp:swoffsite

Proposed by Colin Watson
Status: Merged
Approved by: Gareth Woolridge
Approved revision: 47
Merged at revision: 47
Proposed branch: lp:~cjwatson/swoffsite/tz-aware-last-modified-comparison
Merge into: lp:swoffsite
Diff against target: 48 lines (+20/-4)
1 file modified
swoffsite/mirror.py (+20/-4)
To merge this branch: bzr merge lp:~cjwatson/swoffsite/tz-aware-last-modified-comparison
Reviewer Review Type Date Requested Status
Gareth Woolridge Approve
Canonical IS Reviewers Pending
Review via email: mp+406282@code.launchpad.net

Commit message

Do timezone-aware comparisons of last_modified from Swift.

Description of the change

Swift returns last_modified without a timezone indication, but RadosGW returns it with a trailing 'Z', so iso8601.parse_date parses it into a timezone-aware datetime which cannot be compared directly to the naive datetime returned by datetime.utcnow. Handle this difference: we now parse last_modified into an aware datetime in both cases and compare it only with other aware datetimes.

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Gareth Woolridge (moon127) wrote :

LGTM

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 47

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'swoffsite/mirror.py'
2--- swoffsite/mirror.py 2021-07-09 14:45:12 +0000
3+++ swoffsite/mirror.py 2021-07-28 09:25:41 +0000
4@@ -19,7 +19,7 @@
5 __all__ = ['s3_walk', 'swift_walk', 'mirror_swift_to_s3']
6
7 from cStringIO import StringIO
8-from datetime import datetime
9+from datetime import datetime, timedelta, tzinfo
10 import fnmatch
11 import multiprocessing
12 import os.path
13@@ -45,6 +45,23 @@
14 CHUNK_SIZE = 512*1024
15
16
17+# In Python 3 we could just use datetime.timezone.utc, but since this is
18+# still Python 2 we have to do it ourselves.
19+class UTC(tzinfo):
20+
21+ def utcoffset(self, dt):
22+ return timedelta(0)
23+
24+ def tzname(self, dt):
25+ return 'UTC'
26+
27+ def dst(self, dt):
28+ return timedelta(0)
29+
30+
31+utc = UTC()
32+
33+
34 def get_s3_name(container, name, md5):
35 assert '/' not in container, 'Container name must not contain / char'
36 return '{}/{}.{}.gpg'.format(container, name, md5)
37@@ -467,9 +484,8 @@
38 # Object found in Swift that isn't in S3.
39 log.debug('{!r} in Swift but not S3'.format(swift_cmp))
40 if direction == 's3':
41- lm = iso8601.parse_date(next_swift_obj[1]['last_modified'],
42- None)
43- if not recent or datetime.utcnow() > lm + recent:
44+ lm = iso8601.parse_date(next_swift_obj[1]['last_modified'])
45+ if not recent or datetime.now(utc) > lm + recent:
46 num_new_files += 1
47 pool.do(to_s3_task, dry_run, lock, bucket, gpg_key,
48 next_swift_obj,

Subscribers

People subscribed via source and target branches

to all changes: