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 difflib | 5 import difflib |
6 import hashlib | 6 import hashlib |
7 import itertools | 7 import itertools |
8 import json | 8 import json |
9 import os | 9 import os |
10 import sys | 10 import sys |
11 import zipfile | 11 import zipfile |
12 | 12 |
13 | 13 |
14 # When set and a difference is detected, a diff of what changed is printed. | 14 # When set and a difference is detected, a diff of what changed is printed. |
15 _PRINT_MD5_DIFFS = int(os.environ.get('PRINT_MD5_DIFFS', 0)) | 15 PRINT_EXPLANATIONS = int(os.environ.get('PRINT_INCREMENTAL_EXPLANATIONS', 0)) |
16 | 16 |
17 | 17 |
18 def CallAndRecordIfStale( | 18 def CallAndRecordIfStale( |
19 function, record_path=None, input_paths=None, input_strings=None, | 19 function, record_path=None, input_paths=None, input_strings=None, |
20 output_paths=None, force=False, pass_changes=False): | 20 output_paths=None, force=False, pass_changes=False): |
21 """Calls function if outputs are stale. | 21 """Calls function if outputs are stale. |
22 | 22 |
23 Outputs are considered stale if: | 23 Outputs are considered stale if: |
24 - any output_paths are missing, or | 24 - any output_paths are missing, or |
25 - the contents of any file within input_paths has changed, or | 25 - the contents of any file within input_paths has changed, or |
26 - the contents of input_strings has changed. | 26 - the contents of input_strings has changed. |
27 | 27 |
28 To debug which files are out-of-date, set the environment variable: | 28 To debug which files are out-of-date, set the environment variable: |
29 PRINT_MD5_DIFFS=1 | 29 PRINT_INCREMENTAL_EXPLANATIONS=1 |
30 | 30 |
31 Args: | 31 Args: |
32 function: The function to call. | 32 function: The function to call. |
33 record_path: Path to record metadata. | 33 record_path: Path to record metadata. |
34 Defaults to output_paths[0] + '.md5.stamp' | 34 Defaults to output_paths[0] + '.md5.stamp' |
35 input_paths: List of paths to calcualte an md5 sum on. | 35 input_paths: List of paths to calcualte an md5 sum on. |
36 input_strings: List of strings to record verbatim. | 36 input_strings: List of strings to record verbatim. |
37 output_paths: List of output paths. | 37 output_paths: List of output paths. |
38 force: Whether to treat outputs as missing regardless of whether they | 38 force: Whether to treat outputs as missing regardless of whether they |
39 actually are. | 39 actually are. |
(...skipping 26 matching lines...) Expand all Loading... |
66 with open(record_path, 'r') as jsonfile: | 66 with open(record_path, 'r') as jsonfile: |
67 try: | 67 try: |
68 old_metadata = _Metadata.FromFile(jsonfile) | 68 old_metadata = _Metadata.FromFile(jsonfile) |
69 except: # pylint: disable=bare-except | 69 except: # pylint: disable=bare-except |
70 pass # Not yet using new file format. | 70 pass # Not yet using new file format. |
71 | 71 |
72 changes = Changes(old_metadata, new_metadata, force, missing_outputs) | 72 changes = Changes(old_metadata, new_metadata, force, missing_outputs) |
73 if not changes.HasChanges(): | 73 if not changes.HasChanges(): |
74 return | 74 return |
75 | 75 |
76 if _PRINT_MD5_DIFFS: | 76 if PRINT_EXPLANATIONS: |
77 print '=' * 80 | 77 print '=' * 80 |
78 print 'Target is stale: %s' % record_path | 78 print 'Target is stale: %s' % record_path |
79 print changes.DescribeDifference() | 79 print changes.DescribeDifference() |
80 print '=' * 80 | 80 print '=' * 80 |
81 | 81 |
82 # Delete the old metdata beforehand since failures leave it in an | 82 # Delete the old metdata beforehand since failures leave it in an |
83 # inderterminate state. | 83 # inderterminate state. |
84 if old_metadata: | 84 if old_metadata: |
85 os.unlink(record_path) | 85 os.unlink(record_path) |
86 | 86 |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 | 390 |
391 def _ExtractZipEntries(path): | 391 def _ExtractZipEntries(path): |
392 """Returns a list of (path, CRC32) of all files within |path|.""" | 392 """Returns a list of (path, CRC32) of all files within |path|.""" |
393 entries = [] | 393 entries = [] |
394 with zipfile.ZipFile(path) as zip_file: | 394 with zipfile.ZipFile(path) as zip_file: |
395 for zip_info in zip_file.infolist(): | 395 for zip_info in zip_file.infolist(): |
396 # Skip directories and empty files. | 396 # Skip directories and empty files. |
397 if zip_info.CRC: | 397 if zip_info.CRC: |
398 entries.append((zip_info.filename, zip_info.CRC)) | 398 entries.append((zip_info.filename, zip_info.CRC)) |
399 return entries | 399 return entries |
OLD | NEW |