|
OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
2 # Copyright 201t The Chromium Authors. All rights reserved. | |
kjellander_chromium
2014/12/08 13:58:08
201t -> 2014.
phoglund_chromium
2014/12/08 14:26:16
Done.
| |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Utilities for all our deps-management stuff.""" | |
7 | |
8 import hashlib | |
9 import os | |
10 import shutil | |
11 import sys | |
12 import tarfile | |
13 import zipfile | |
14 | |
15 | |
16 def ComputeSHA1(path): | |
17 if not os.path.exists(path): | |
kjellander_chromium
2014/12/08 13:58:08
Returning 0 for non-existing files would be unexpe
phoglund_chromium
2014/12/08 14:26:16
That would introduce a lot of cruft in the calling
kjellander_chromium
2014/12/08 14:52:25
Fair enough.
| |
18 return 0 | |
19 | |
20 sha1 = hashlib.sha1() | |
21 file_to_hash = open(path, 'rb') | |
22 try: | |
23 sha1.update(file_to_hash.read()) | |
24 finally: | |
25 file_to_hash.close() | |
26 | |
27 return sha1.hexdigest() | |
28 | |
29 | |
30 # This is necessary since Windows won't allow us to unzip onto an existing dir. | |
kjellander_chromium
2014/12/08 13:58:08
"This is" is referencing what? I think this should
phoglund_chromium
2014/12/08 14:26:16
Deleted the comment.
phoglund_chromium
2014/12/08 14:26:16
Done.
| |
31 def DeleteDirNextToGclient(directory): | |
32 # Sanity check to avoid nuking the wrong dirs. | |
33 if not os.path.exists('.gclient'): | |
34 raise Exception('Invoked from wrong dir; invoke from dir with .gclient') | |
35 print 'Deleting %s in %s...' % (directory, os.getcwd()) | |
36 shutil.rmtree(directory, ignore_errors=True) | |
37 | |
38 | |
39 def UnpackToWorkingDir(archive_path): | |
40 extension = os.path.splitext(archive_path)[1] | |
41 if extension == '.zip': | |
42 _Unzip(archive_path) | |
43 else: | |
44 _Untar(archive_path) | |
45 | |
46 | |
47 def _Unzip(path): | |
48 print 'Unzipping %s in %s...' % (path, os.getcwd()) | |
49 zip_file = zipfile.ZipFile(path) | |
50 try: | |
51 zip_file.extractall() | |
52 finally: | |
53 zip_file.close() | |
54 | |
55 | |
56 def _Untar(path): | |
57 print 'Untarring %s in %s...' % (path, os.getcwd()) | |
58 tar_file = tarfile.open(path, 'r:gz') | |
59 try: | |
60 tar_file.extractall() | |
61 finally: | |
62 tar_file.close() | |
63 | |
64 | |
65 def GetPlatform(): | |
66 if sys.platform.startswith('win'): | |
67 return 'win' | |
68 if sys.platform.startswith('linux'): | |
69 return 'linux' | |
70 if sys.platform.startswith('darwin'): | |
71 return 'mac' | |
72 raise Exception("Can't run on platform %s." % sys.platform) | |
kjellander_chromium
2014/12/08 13:58:08
Single-quote string and escape it like Can\'t inst
phoglund_chromium
2014/12/08 14:26:16
I watched a python readability review and this was
kjellander_chromium
2014/12/08 14:52:25
Acknowledged.
| |
73 | |
OLD | NEW |