6
6
from os .path import isfile , dirname , basename , abspath
7
7
from hashlib import sha256
8
8
from subprocess import check_call as run
9
+ from contextlib import contextmanager
10
+ from datetime import datetime
9
11
10
12
from boto .s3 .connection import S3Connection
11
13
from boto .s3 .key import Key
20
22
raise SystemExit ("TWBS_S3_BUCKET environment variable not set!" )
21
23
22
24
25
+ @contextmanager
26
+ def timer ():
27
+ start = datetime .utcnow ()
28
+ yield
29
+ end = datetime .utcnow ()
30
+ elapsed = end - start
31
+ print ("\t Done. Took" , int (elapsed .total_seconds ()), "seconds." )
32
+
33
+
23
34
def _sha256_of_file (filename ):
24
35
hasher = sha256 ()
25
36
with open (filename , 'rb' ) as input_file :
@@ -47,19 +58,22 @@ def _tarball_filename_for(directory):
47
58
48
59
def _create_tarball (directory ):
49
60
print ("Creating tarball of {}..." .format (directory ))
50
- run (['tar' , '-czf' , _tarball_filename_for (directory ), '-C' , dirname (directory ), basename (directory )])
61
+ with timer ():
62
+ run (['tar' , '-czf' , _tarball_filename_for (directory ), '-C' , dirname (directory ), basename (directory )])
51
63
52
64
53
65
def _extract_tarball (directory ):
54
66
print ("Extracting tarball of {}..." .format (directory ))
55
- run (['tar' , '-xzf' , _tarball_filename_for (directory ), '-C' , dirname (directory )])
67
+ with timer ():
68
+ run (['tar' , '-xzf' , _tarball_filename_for (directory ), '-C' , dirname (directory )])
56
69
57
70
58
71
def download (directory ):
59
72
_delete_file_quietly (NEED_TO_UPLOAD_MARKER )
60
73
try :
61
74
print ("Downloading {} tarball from S3..." .format (friendly_name ))
62
- key .get_contents_to_filename (_tarball_filename_for (directory ))
75
+ with timer ():
76
+ key .get_contents_to_filename (_tarball_filename_for (directory ))
63
77
except S3ResponseError as err :
64
78
open (NEED_TO_UPLOAD_MARKER , 'a' ).close ()
65
79
print (err )
@@ -72,7 +86,8 @@ def download(directory):
72
86
def upload (directory ):
73
87
_create_tarball (directory )
74
88
print ("Uploading {} tarball to S3... ({})" .format (friendly_name , _tarball_size (directory )))
75
- key .set_contents_from_filename (_tarball_filename_for (directory ))
89
+ with timer ():
90
+ key .set_contents_from_filename (_tarball_filename_for (directory ))
76
91
print ("{} cache successfully updated." .format (friendly_name ))
77
92
_delete_file_quietly (NEED_TO_UPLOAD_MARKER )
78
93
0 commit comments