OLD | NEW |
---|---|
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import hashlib | 5 import hashlib |
6 import json | |
6 import os | 7 import os |
7 import subprocess | 8 import subprocess |
8 import sys | 9 import sys |
9 | 10 |
10 | 11 |
11 BASEDIR = os.path.dirname(os.path.abspath(__file__)) | 12 BASEDIR = os.path.dirname(os.path.abspath(__file__)) |
12 | 13 |
13 | 14 |
15 def GetFileList(root): | |
16 """Gets a normalized list of files under |root|.""" | |
17 assert not os.path.isabs(root) | |
18 assert os.path.normpath(root) == root | |
19 file_list = [] | |
20 for root, dirs, files in os.walk(root): | |
21 dirs.sort() | |
22 for name in sorted(f.lower() for f in files): | |
M-A Ruel
2013/11/29 00:33:30
file_list = []
for root, dirs, files in os.walk(ro
scottmg
2013/11/29 01:54:10
Interestingly, it results in a different order. Pr
M-A Ruel
2013/11/29 02:07:28
Ah! I didn't think about that. As you prefer.
| |
23 file_list.append(os.path.join(root, name).lower()) | |
24 return file_list | |
25 | |
26 | |
27 def MakeTimestampsFileName(root): | |
28 return os.path.join(root, '..', '.timestamps') | |
29 | |
30 | |
14 def CalculateHash(root): | 31 def CalculateHash(root): |
15 """Calculates the sha1 of the paths to all files in the given |root| and the | 32 """Calculates the sha1 of the paths to all files in the given |root| and the |
16 contents of those files, and returns as a hex string.""" | 33 contents of those files, and returns as a hex string.""" |
17 assert not os.path.isabs(root) | 34 file_list = GetFileList(root) |
18 assert os.path.normpath(root) == root | 35 |
36 # Check whether we previously saved timestamps in $root/../.timestamps. If | |
37 # we didn't, or they don't match, then do the full calculation, otherwise | |
38 # return the saved value. | |
39 timestamps_file = MakeTimestampsFileName(root) | |
40 timestamps_data = {'files': [], 'sha1': ''} | |
41 if os.path.exists(timestamps_file): | |
42 with open(timestamps_file, 'rb') as f: | |
43 try: | |
44 timestamps_data = json.load(f) | |
45 except ValueError: | |
46 # json couldn't be loaded, empty data will force a re-hash. | |
47 pass | |
48 | |
49 matches = len(file_list) == len(timestamps_data['files']) | |
M-A Ruel
2013/11/29 00:33:30
I wonder about "legitimate" junk files, like thumb
scottmg
2013/11/29 01:54:10
Yeah... If it comes up we could blacklist some fil
| |
50 if matches: | |
51 for disk, cached in zip(file_list, timestamps_data['files']): | |
52 if disk != cached[0] or os.stat(disk).st_mtime != cached[1]: | |
53 matches = False | |
54 break | |
55 if matches: | |
56 return timestamps_data['sha1'] | |
57 | |
19 digest = hashlib.sha1() | 58 digest = hashlib.sha1() |
20 count = 0 | 59 for path in file_list: |
21 for root, dirs, files in os.walk(root): | 60 digest.update(path) |
22 dirs.sort() | 61 with open(path, 'rb') as f: |
23 for name in sorted(f.lower() for f in files): | 62 digest.update(f.read()) |
24 path = os.path.join(root, name) | |
25 digest.update(path.lower()) | |
26 with open(path, 'rb') as f: | |
27 digest.update(f.read()) | |
28 return digest.hexdigest() | 63 return digest.hexdigest() |
29 | 64 |
30 | 65 |
66 def SaveTimestampsAndHash(root, sha1): | |
67 """Save timestamps and the final hash to be able to early-out more quickly | |
68 next time.""" | |
69 file_list = GetFileList(root) | |
70 timestamps_data = { | |
71 'files': [[f, os.stat(f).st_mtime] for f in file_list], | |
72 'sha1': sha1, | |
73 } | |
74 with open(MakeTimestampsFileName(root), 'wb') as f: | |
75 json.dump(timestamps_data, f) | |
76 | |
77 | |
31 def main(): | 78 def main(): |
32 if sys.platform not in ('win32', 'cygwin'): | 79 if sys.platform not in ('win32', 'cygwin'): |
33 return 0 | 80 return 0 |
34 | 81 |
35 if len(sys.argv) != 1: | 82 if len(sys.argv) != 1: |
36 print >> sys.stderr, 'Unexpected arguments.' | 83 print >> sys.stderr, 'Unexpected arguments.' |
37 return 1 | 84 return 1 |
38 | 85 |
39 # Move to same location as .gclient. This is a no-op when run via gclient. | 86 # Move to same location as .gclient. This is a no-op when run via gclient. |
40 os.chdir(os.path.normpath(os.path.join(BASEDIR, '..\\..\\..\\..'))) | 87 os.chdir(os.path.normpath(os.path.join(BASEDIR, '..\\..\\..\\..'))) |
41 toolchain_dir = 'src\\third_party\\win_toolchain' | 88 toolchain_dir = 'src\\third_party\\win_toolchain' |
42 target_dir = os.path.join(toolchain_dir, 'files') | 89 target_dir = os.path.join(toolchain_dir, 'files') |
43 | 90 |
44 sha1path = os.path.join(toolchain_dir, 'toolchain.sha1') | 91 sha1path = os.path.join(toolchain_dir, 'toolchain.sha1') |
45 desired_hash = '' | 92 desired_hash = '' |
46 if os.path.isfile(sha1path): | 93 if os.path.isfile(sha1path): |
47 with open(sha1path, 'rb') as f: | 94 with open(sha1path, 'rb') as f: |
48 desired_hash = f.read().strip() | 95 desired_hash = f.read().strip() |
49 | 96 |
50 # If the current hash doesn't match what we want in the file, nuke and pave. | 97 # If the current hash doesn't match what we want in the file, nuke and pave. |
51 # Note that this script is only run when a .sha1 file is updated (per DEPS) | 98 # Typically this script is only run when the .sha1 one file is updated, but |
52 # so this relatively expensive step of hashing everything only happens when | 99 # directly calling "gclient runhooks" will also run it, so we cache |
53 # the toolchain is updated. | 100 # based on timestamps to make that case fast. |
54 current_hash = CalculateHash(target_dir) | 101 current_hash = CalculateHash(target_dir) |
55 if current_hash != desired_hash: | 102 if current_hash != desired_hash: |
56 print 'Windows toolchain out of date or doesn\'t exist, updating...' | 103 print 'Windows toolchain out of date or doesn\'t exist, updating...' |
57 if os.path.isdir(target_dir): | 104 if os.path.isdir(target_dir): |
58 subprocess.check_call('rmdir /s/q "%s"' % target_dir, shell=True) | 105 subprocess.check_call('rmdir /s/q "%s"' % target_dir, shell=True) |
59 subprocess.check_call([ | 106 subprocess.check_call([ |
60 sys.executable, | 107 sys.executable, |
61 'src\\tools\\win\\toolchain\\toolchain2013.py', | 108 'src\\tools\\win\\toolchain\\toolchain2013.py', |
62 '--targetdir', target_dir]) | 109 '--targetdir', target_dir]) |
110 current_hash = CalculateHash(target_dir) | |
111 if current_hash != desired_hash: | |
112 print >> sys.stderr, ( | |
113 'Got wrong hash after pulling a new toolchain. ' | |
114 'Wanted \'%s\', got \'%s\'.' % ( | |
115 desired_hash, current_hash)) | |
116 return 1 | |
117 SaveTimestampsAndHash(target_dir, current_hash) | |
63 | 118 |
64 current_hash = CalculateHash(target_dir) | |
65 if current_hash != desired_hash: | |
66 print >> sys.stderr, ( | |
67 'Got wrong hash after pulling a new toolchain. ' | |
68 'Wanted \'%s\', got \'%s\'.' % ( | |
69 desired_hash, current_hash)) | |
70 return 1 | |
71 return 0 | 119 return 0 |
72 | 120 |
73 | 121 |
74 if __name__ == '__main__': | 122 if __name__ == '__main__': |
75 sys.exit(main()) | 123 sys.exit(main()) |
OLD | NEW |