Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(291)

Side by Side Diff: common.py

Issue 12042069: Scripts to download files from google storage based on sha1 sums (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Removed gsutil/tests and gsutil/docs Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | download_from_google_storage.py » ('j') | download_from_google_storage.py » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 # common.py: Utility functions common to the Google storage scripts.
M-A Ruel 2013/02/22 01:15:56 docstring
Ryan Tseng 2013/02/22 02:38:00 Done.
5
6 import subprocess2
7 import hashlib
M-A Ruel 2013/02/22 01:15:56 sort
Ryan Tseng 2013/02/22 02:38:00 Done.
8 import os
9 import sys
10 import re
11
M-A Ruel 2013/02/22 01:15:56 2 lines
Ryan Tseng 2013/02/22 02:38:00 Done.
12 class Gsutil(object):
13 """A convenience class to call gsutil with some predefined settings."""
M-A Ruel 2013/02/22 01:15:56 """Calls gsutil ...
Ryan Tseng 2013/02/22 02:38:00 Done.
14 def __init__(self, path, boto_path=None, timeout=None):
15 if not os.path.exists(path):
16 raise OSError('GSUtil not found in %s' % path)
17 self.path = path
18
M-A Ruel 2013/02/22 01:15:56 Remove empty line
Ryan Tseng 2013/02/22 02:38:00 Done.
19 self.timeout = timeout
20 self.boto_path = boto_path
21
22 def call(self, *args):
23 env = os.environ.copy()
24 if self.boto_path is not None:
25 env['AWS_CREDENTIAL_FILE'] = self.boto_path
26 return subprocess2.call((sys.executable, self.path) + args,
27 env=env,
28 timeout=self.timeout)
29
30 def check_call(self, *args):
31 env = os.environ.copy()
32 if self.boto_path is not None:
33 env['AWS_CREDENTIAL_FILE'] = self.boto_path
34 ((out, err), code) = subprocess2.communicate(
35 (sys.executable, self.path) + args,
36 stdout=subprocess2.PIPE,
37 stderr=subprocess2.PIPE,
38 env=env,
39 timeout=self.timeout)
40
41 # Parse output.
42 status_code_match = re.search('status=([0-9]+)', err)
43 if status_code_match:
44 return int(status_code_match.groups(1))
45 elif ('You are attempting to access protected data with '
46 'no configured credentials.' in err):
47 return (403, out, err)
48 elif 'No such object' in err:
49 return (404, out, err)
50 else:
51 return (code, out, err)
52
53 def clone(self):
54 return Gsutil(self.path, self.boto_path, self.timeout)
55
56
57 def GetSHA1(filename):
58 sha1 = hashlib.sha1()
59 with open(filename, 'rb') as f:
60 while True:
61 # Read in 1mb chunks, so it doesn't all have to be loaded into memory.
62 chunk = f.read(1024*1024)
63 if not chunk:
64 break
65 sha1.update(chunk)
66 return sha1.hexdigest()
67
68
69 def CheckSHA1(sha1_sum, filename):
M-A Ruel 2013/02/22 01:15:56 I don't think this function is super useful.
Ryan Tseng 2013/02/22 02:38:00 Removed.
70 return sha1_sum == GetSHA1(filename)
71
72
73 def GetMD5(filename, lock, use_md5):
M-A Ruel 2013/02/22 01:15:56 Why is this function so different/asymetric than G
Ryan Tseng 2013/02/22 02:38:00 There is an option to cache the md5 sum into a fil
M-A Ruel 2013/02/25 15:15:06 Then wrap the caching logic into a separate functi
Ryan Tseng 2013/02/27 02:06:55 Done.
74 # See if we can find an existing MD5 sum stored in a file.
75 if use_md5 and os.path.exists('%s.md5' % filename):
76 with open('%s.md5' % filename) as f:
77 md5_match = re.search('([a-z0-9]{32})', f.read())
78 if md5_match:
79 return md5_match.groups()[0]
80
81 # Calculate the MD5 checksum of the file.
82 md5_calculator = hashlib.md5()
83 with lock:
84 with open(filename, 'rb') as f:
85 while True:
86 chunk = f.read(1024*1024)
87 if not chunk:
88 break
89 md5_calculator.update(chunk)
90 local_md5 = md5_calculator.hexdigest()
91 if use_md5:
92 with open('%s.md5' % filename, 'w') as f:
93 f.write(local_md5)
94 return local_md5
OLDNEW
« no previous file with comments | « no previous file | download_from_google_storage.py » ('j') | download_from_google_storage.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698