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
=== modified file 'swoffsite/mirror.py'
--- swoffsite/mirror.py 2021-07-09 14:45:12 +0000
+++ swoffsite/mirror.py 2021-07-28 09:25:41 +0000
@@ -19,7 +19,7 @@
19__all__ = ['s3_walk', 'swift_walk', 'mirror_swift_to_s3']19__all__ = ['s3_walk', 'swift_walk', 'mirror_swift_to_s3']
2020
21from cStringIO import StringIO21from cStringIO import StringIO
22from datetime import datetime22from datetime import datetime, timedelta, tzinfo
23import fnmatch23import fnmatch
24import multiprocessing24import multiprocessing
25import os.path25import os.path
@@ -45,6 +45,23 @@
45CHUNK_SIZE = 512*102445CHUNK_SIZE = 512*1024
4646
4747
48# In Python 3 we could just use datetime.timezone.utc, but since this is
49# still Python 2 we have to do it ourselves.
50class UTC(tzinfo):
51
52 def utcoffset(self, dt):
53 return timedelta(0)
54
55 def tzname(self, dt):
56 return 'UTC'
57
58 def dst(self, dt):
59 return timedelta(0)
60
61
62utc = UTC()
63
64
48def get_s3_name(container, name, md5):65def get_s3_name(container, name, md5):
49 assert '/' not in container, 'Container name must not contain / char'66 assert '/' not in container, 'Container name must not contain / char'
50 return '{}/{}.{}.gpg'.format(container, name, md5)67 return '{}/{}.{}.gpg'.format(container, name, md5)
@@ -467,9 +484,8 @@
467 # Object found in Swift that isn't in S3.484 # Object found in Swift that isn't in S3.
468 log.debug('{!r} in Swift but not S3'.format(swift_cmp))485 log.debug('{!r} in Swift but not S3'.format(swift_cmp))
469 if direction == 's3':486 if direction == 's3':
470 lm = iso8601.parse_date(next_swift_obj[1]['last_modified'],487 lm = iso8601.parse_date(next_swift_obj[1]['last_modified'])
471 None)488 if not recent or datetime.now(utc) > lm + recent:
472 if not recent or datetime.utcnow() > lm + recent:
473 num_new_files += 1489 num_new_files += 1
474 pool.do(to_s3_task, dry_run, lock, bucket, gpg_key,490 pool.do(to_s3_task, dry_run, lock, bucket, gpg_key,
475 next_swift_obj,491 next_swift_obj,

Subscribers

People subscribed via source and target branches

to all changes: