Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Utility functions common to the Google storage scripts.""" | |
| 6 | |
| 7 import hashlib | |
| 8 import os | |
| 9 import re | |
| 10 import subprocess2 | |
|
M-A Ruel
2013/02/25 15:15:06
It's not a stdlib, so put it in a subgroup after,
Ryan Tseng
2013/02/27 02:06:56
Done.
| |
| 11 import sys | |
| 12 | |
| 13 | |
| 14 class Gsutil(object): | |
| 15 """Call gsutil with some predefined settings.""" | |
| 16 def __init__(self, path, boto_path=None, timeout=None): | |
| 17 if not os.path.exists(path): | |
| 18 raise OSError('GSUtil not found in %s' % path) | |
| 19 self.path = path | |
| 20 self.timeout = timeout | |
| 21 self.boto_path = boto_path | |
| 22 | |
| 23 def call(self, *args): | |
| 24 env = os.environ.copy() | |
| 25 if self.boto_path is not None: | |
| 26 env['AWS_CREDENTIAL_FILE'] = self.boto_path | |
| 27 return subprocess2.call((sys.executable, self.path) + args, | |
| 28 env=env, | |
| 29 timeout=self.timeout) | |
| 30 | |
| 31 def check_call(self, *args): | |
| 32 env = os.environ.copy() | |
| 33 if self.boto_path is not None: | |
| 34 env['AWS_CREDENTIAL_FILE'] = self.boto_path | |
| 35 ((out, err), code) = subprocess2.communicate( | |
| 36 (sys.executable, self.path) + args, | |
| 37 stdout=subprocess2.PIPE, | |
| 38 stderr=subprocess2.PIPE, | |
| 39 env=env, | |
| 40 timeout=self.timeout) | |
| 41 | |
| 42 # Parse output. | |
| 43 status_code_match = re.search('status=([0-9]+)', err) | |
| 44 if status_code_match: | |
| 45 return int(status_code_match.groups(1)) | |
| 46 elif ('You are attempting to access protected data with ' | |
| 47 'no configured credentials.' in err): | |
| 48 return (403, out, err) | |
| 49 elif 'No such object' in err: | |
| 50 return (404, out, err) | |
| 51 else: | |
| 52 return (code, out, err) | |
| 53 | |
| 54 def clone(self): | |
| 55 return Gsutil(self.path, self.boto_path, self.timeout) | |
| 56 | |
| 57 | |
| 58 def GetSHA1(filename): | |
| 59 sha1 = hashlib.sha1() | |
| 60 with open(filename, 'rb') as f: | |
| 61 while True: | |
| 62 # Read in 1mb chunks, so it doesn't all have to be loaded into memory. | |
| 63 chunk = f.read(1024*1024) | |
| 64 if not chunk: | |
| 65 break | |
| 66 sha1.update(chunk) | |
| 67 return sha1.hexdigest() | |
| 68 | |
| 69 | |
| 70 def GetMD5(filename, lock, use_md5): | |
| 71 # See if we can find an existing MD5 sum stored in a file. | |
| 72 if use_md5 and os.path.exists('%s.md5' % filename): | |
| 73 with open('%s.md5' % filename) as f: | |
| 74 md5_match = re.search('([a-z0-9]{32})', f.read()) | |
| 75 if md5_match: | |
| 76 return md5_match.groups()[0] | |
| 77 | |
| 78 # Calculate the MD5 checksum of the file. | |
| 79 md5_calculator = hashlib.md5() | |
| 80 with lock: | |
| 81 with open(filename, 'rb') as f: | |
| 82 while True: | |
| 83 chunk = f.read(1024*1024) | |
| 84 if not chunk: | |
| 85 break | |
| 86 md5_calculator.update(chunk) | |
| 87 local_md5 = md5_calculator.hexdigest() | |
| 88 if use_md5: | |
| 89 with open('%s.md5' % filename, 'w') as f: | |
| 90 f.write(local_md5) | |
| 91 return local_md5 | |
| OLD | NEW |