From 21dba45bfc859c3127c6a5b6b5a6c8495da00c13 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Mon, 13 Mar 2023 12:37:31 +0100 Subject: [PATCH] Fix mtime logic for py2.x Related: https://pagure.io/koji/issue/3711 --- cli/koji_cli/lib.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cli/koji_cli/lib.py b/cli/koji_cli/lib.py index bfbf7710..2ec1a1fd 100644 --- a/cli/koji_cli/lib.py +++ b/cli/koji_cli/lib.py @@ -591,6 +591,7 @@ def download_file(url, relpath, quiet=False, noprogress=False, size=None, # rewrite f = open(relpath, 'wb') + mtime = None try: # closing needs to be used for requests < 2.18.0 with closing(koji.request_with_retry().get(url, headers=headers, stream=True)) as response: @@ -615,12 +616,14 @@ def download_file(url, relpath, quiet=False, noprogress=False, size=None, if mtime.tzinfo is None: mtime = mtime.replace(tzinfo=dateutil.tz.gettz()) mtime = mtime.astimezone(dateutil.tz.gettz()) - os.utime(relpath, (time.time(), time.mktime(mtime.timetuple()))) finally: f.close() if pos == 0: # nothing was downloaded, e.g file not found os.unlink(relpath) + elif mtime: + # mtime could be changed after close, otherwise py 2.x will update it. + os.utime(relpath, (time.time(), time.mktime(mtime.timetuple()))) if not (quiet or noprogress): print('')