Skip to content

Commit 0b6f190

Browse files
committed
better way of tracking what caches need to be uploaded; fixes twbs#13270
1 parent 4c049e3 commit 0b6f190

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

test-infra/s3_cache.py

+41-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#!/usr/bin/env python2.7
2+
# pylint: disable=C0301
23
from __future__ import absolute_import, unicode_literals, print_function, division
34

45
from sys import argv
56
from os import environ, stat, chdir, remove as _delete_file
6-
from os.path import isfile, dirname, basename, abspath, realpath, expandvars
7+
from os.path import dirname, basename, abspath, realpath, expandvars
78
from hashlib import sha256
89
from subprocess import check_call as run
9-
from json import load
10+
from json import load, dump as save
1011
from contextlib import contextmanager
1112
from datetime import datetime
1213

@@ -16,7 +17,7 @@
1617

1718

1819
CONFIG_FILE = './S3Cachefile.json'
19-
NEED_TO_UPLOAD_MARKER = '.need-to-upload'
20+
UPLOAD_TODO_FILE = './S3CacheTodo.json'
2021
BYTES_PER_MB = 1024 * 1024
2122

2223

@@ -29,6 +30,24 @@ def timer():
2930
print("\tDone. Took", int(elapsed.total_seconds()), "second(s).")
3031

3132

33+
@contextmanager
34+
def todo_file(writeback=True):
35+
try:
36+
with open(UPLOAD_TODO_FILE, 'rt') as json_file:
37+
todo = load(json_file)
38+
except (IOError, OSError, ValueError):
39+
todo = {}
40+
41+
yield todo
42+
43+
if writeback:
44+
try:
45+
with open(UPLOAD_TODO_FILE, 'wt') as json_file:
46+
save(todo, json_file)
47+
except (OSError, IOError) as save_err:
48+
print("Error saving {}:".format(UPLOAD_TODO_FILE), save_err)
49+
50+
3251
def _sha256_of_file(filename):
3352
hasher = sha256()
3453
with open(filename, 'rb') as input_file:
@@ -45,6 +64,21 @@ def _delete_file_quietly(filename):
4564
pass
4665

4766

67+
def mark_needs_uploading(cache_name):
68+
with todo_file() as todo:
69+
todo[cache_name] = True
70+
71+
72+
def mark_uploaded(cache_name):
73+
with todo_file() as todo:
74+
todo.pop(cache_name, None)
75+
76+
77+
def need_to_upload(cache_name):
78+
with todo_file(writeback=False) as todo:
79+
return todo.get(cache_name, False)
80+
81+
4882
def _tarball_size(directory):
4983
kib = stat(_tarball_filename_for(directory)).st_size // BYTES_PER_MB
5084
return "{} MiB".format(kib)
@@ -67,14 +101,13 @@ def _extract_tarball(directory):
67101

68102

69103
def download(directory):
70-
_delete_file_quietly(NEED_TO_UPLOAD_MARKER)
104+
mark_uploaded(cache_name) # reset
71105
try:
72106
print("Downloading {} tarball from S3...".format(cache_name))
73107
with timer():
74108
key.get_contents_to_filename(_tarball_filename_for(directory))
75109
except S3ResponseError as err:
76-
open(NEED_TO_UPLOAD_MARKER, 'a').close()
77-
print(err)
110+
mark_needs_uploading(cache_name)
78111
raise SystemExit("Cached {} download failed!".format(cache_name))
79112
print("Downloaded {}.".format(_tarball_size(directory)))
80113
_extract_tarball(directory)
@@ -87,7 +120,7 @@ def upload(directory):
87120
with timer():
88121
key.set_contents_from_filename(_tarball_filename_for(directory))
89122
print("{} cache successfully updated.".format(cache_name))
90-
_delete_file_quietly(NEED_TO_UPLOAD_MARKER)
123+
mark_uploaded(cache_name)
91124

92125

93126
if __name__ == '__main__':
@@ -135,7 +168,7 @@ def upload(directory):
135168
if mode == 'download':
136169
download(directory)
137170
elif mode == 'upload':
138-
if isfile(NEED_TO_UPLOAD_MARKER): # FIXME
171+
if need_to_upload(cache_name):
139172
upload(directory)
140173
else:
141174
print("No need to upload anything.")

0 commit comments

Comments
 (0)