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 ctypes.wintypes |
5 import hashlib | 6 import hashlib |
| 7 import json |
6 import os | 8 import os |
7 import subprocess | 9 import subprocess |
8 import sys | 10 import sys |
9 | 11 |
10 | 12 |
11 BASEDIR = os.path.dirname(os.path.abspath(__file__)) | 13 BASEDIR = os.path.dirname(os.path.abspath(__file__)) |
12 | 14 |
13 | 15 |
| 16 GetFileAttributes = ctypes.windll.kernel32.GetFileAttributesW |
| 17 GetFileAttributes.argtypes = (ctypes.wintypes.LPWSTR,) |
| 18 GetFileAttributes.restype = ctypes.wintypes.DWORD |
| 19 FILE_ATTRIBUTE_HIDDEN = 0x2 |
| 20 FILE_ATTRIBUTE_SYSTEM = 0x4 |
| 21 |
| 22 |
| 23 def IsHidden(file_path): |
| 24 """Returns whether the given |file_path| has the 'system' or 'hidden' |
| 25 attribute set.""" |
| 26 p = GetFileAttributes(file_path) |
| 27 assert p != 0xffffffff |
| 28 return bool(p & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) |
| 29 |
| 30 |
| 31 def GetFileList(root): |
| 32 """Gets a normalized list of files under |root|.""" |
| 33 assert not os.path.isabs(root) |
| 34 assert os.path.normpath(root) == root |
| 35 file_list = [] |
| 36 for base, _, files in os.walk(root): |
| 37 paths = [os.path.join(base, f) for f in files] |
| 38 file_list.extend(x.lower() for x in paths if not IsHidden(x)) |
| 39 return sorted(file_list) |
| 40 |
| 41 |
| 42 def MakeTimestampsFileName(root): |
| 43 return os.path.join(root, '..', '.timestamps') |
| 44 |
| 45 |
14 def CalculateHash(root): | 46 def CalculateHash(root): |
15 """Calculates the sha1 of the paths to all files in the given |root| and the | 47 """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.""" | 48 contents of those files, and returns as a hex string.""" |
17 assert not os.path.isabs(root) | 49 file_list = GetFileList(root) |
18 assert os.path.normpath(root) == root | 50 |
| 51 # Check whether we previously saved timestamps in $root/../.timestamps. If |
| 52 # we didn't, or they don't match, then do the full calculation, otherwise |
| 53 # return the saved value. |
| 54 timestamps_file = MakeTimestampsFileName(root) |
| 55 timestamps_data = {'files': [], 'sha1': ''} |
| 56 if os.path.exists(timestamps_file): |
| 57 with open(timestamps_file, 'rb') as f: |
| 58 try: |
| 59 timestamps_data = json.load(f) |
| 60 except ValueError: |
| 61 # json couldn't be loaded, empty data will force a re-hash. |
| 62 pass |
| 63 |
| 64 matches = len(file_list) == len(timestamps_data['files']) |
| 65 if matches: |
| 66 for disk, cached in zip(file_list, timestamps_data['files']): |
| 67 if disk != cached[0] or os.stat(disk).st_mtime != cached[1]: |
| 68 matches = False |
| 69 break |
| 70 if matches: |
| 71 return timestamps_data['sha1'] |
| 72 |
19 digest = hashlib.sha1() | 73 digest = hashlib.sha1() |
20 count = 0 | 74 for path in file_list: |
21 for root, dirs, files in os.walk(root): | 75 digest.update(path) |
22 dirs.sort() | 76 with open(path, 'rb') as f: |
23 for name in sorted(f.lower() for f in files): | 77 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() | 78 return digest.hexdigest() |
29 | 79 |
30 | 80 |
| 81 def SaveTimestampsAndHash(root, sha1): |
| 82 """Save timestamps and the final hash to be able to early-out more quickly |
| 83 next time.""" |
| 84 file_list = GetFileList(root) |
| 85 timestamps_data = { |
| 86 'files': [[f, os.stat(f).st_mtime] for f in file_list], |
| 87 'sha1': sha1, |
| 88 } |
| 89 with open(MakeTimestampsFileName(root), 'wb') as f: |
| 90 json.dump(timestamps_data, f) |
| 91 |
| 92 |
31 def main(): | 93 def main(): |
32 if sys.platform not in ('win32', 'cygwin'): | 94 if sys.platform not in ('win32', 'cygwin'): |
33 return 0 | 95 return 0 |
34 | 96 |
35 if len(sys.argv) != 1: | 97 if len(sys.argv) != 1: |
36 print >> sys.stderr, 'Unexpected arguments.' | 98 print >> sys.stderr, 'Unexpected arguments.' |
37 return 1 | 99 return 1 |
38 | 100 |
39 # Move to same location as .gclient. This is a no-op when run via gclient. | 101 # 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, '..\\..\\..\\..'))) | 102 os.chdir(os.path.normpath(os.path.join(BASEDIR, '..\\..\\..\\..'))) |
41 toolchain_dir = 'src\\third_party\\win_toolchain' | 103 toolchain_dir = 'src\\third_party\\win_toolchain' |
42 target_dir = os.path.join(toolchain_dir, 'files') | 104 target_dir = os.path.join(toolchain_dir, 'files') |
43 | 105 |
44 sha1path = os.path.join(toolchain_dir, 'toolchain.sha1') | 106 sha1path = os.path.join(toolchain_dir, 'toolchain.sha1') |
45 desired_hash = '' | 107 desired_hash = '' |
46 if os.path.isfile(sha1path): | 108 if os.path.isfile(sha1path): |
47 with open(sha1path, 'rb') as f: | 109 with open(sha1path, 'rb') as f: |
48 desired_hash = f.read().strip() | 110 desired_hash = f.read().strip() |
49 | 111 |
50 # If the current hash doesn't match what we want in the file, nuke and pave. | 112 # 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) | 113 # 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 | 114 # directly calling "gclient runhooks" will also run it, so we cache |
53 # the toolchain is updated. | 115 # based on timestamps to make that case fast. |
54 current_hash = CalculateHash(target_dir) | 116 current_hash = CalculateHash(target_dir) |
55 if current_hash != desired_hash: | 117 if current_hash != desired_hash: |
56 print 'Windows toolchain out of date or doesn\'t exist, updating...' | 118 print 'Windows toolchain out of date or doesn\'t exist, updating...' |
57 if os.path.isdir(target_dir): | 119 if os.path.isdir(target_dir): |
58 subprocess.check_call('rmdir /s/q "%s"' % target_dir, shell=True) | 120 subprocess.check_call('rmdir /s/q "%s"' % target_dir, shell=True) |
59 subprocess.check_call([ | 121 subprocess.check_call([ |
60 sys.executable, | 122 sys.executable, |
61 'src\\tools\\win\\toolchain\\toolchain2013.py', | 123 'src\\tools\\win\\toolchain\\toolchain2013.py', |
62 '--targetdir', target_dir]) | 124 '--targetdir', target_dir]) |
| 125 current_hash = CalculateHash(target_dir) |
| 126 if current_hash != desired_hash: |
| 127 print >> sys.stderr, ( |
| 128 'Got wrong hash after pulling a new toolchain. ' |
| 129 'Wanted \'%s\', got \'%s\'.' % ( |
| 130 desired_hash, current_hash)) |
| 131 return 1 |
| 132 SaveTimestampsAndHash(target_dir, current_hash) |
63 | 133 |
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 | 134 return 0 |
72 | 135 |
73 | 136 |
74 if __name__ == '__main__': | 137 if __name__ == '__main__': |
75 sys.exit(main()) | 138 sys.exit(main()) |
OLD | NEW |