Compute SHA256 hash of file content + compiler args + source tree state. Args: file_path: Path to source file. compiler_args_key: String representation of compiler args. source_tree_hash: Combined hash of all header mtimes. Returns: Hex digest string.
(
file_path: Path, compiler_args_key: str, source_tree_hash: str
)
| 58 | |
| 59 | |
| 60 | def _compute_file_hash( |
| 61 | file_path: Path, compiler_args_key: str, source_tree_hash: str |
| 62 | ) -> str: |
| 63 | """Compute SHA256 hash of file content + compiler args + source tree state. |
| 64 | |
| 65 | Args: |
| 66 | file_path: Path to source file. |
| 67 | compiler_args_key: String representation of compiler args. |
| 68 | source_tree_hash: Combined hash of all header mtimes. |
| 69 | |
| 70 | Returns: |
| 71 | Hex digest string. |
| 72 | """ |
| 73 | hasher = hashlib.sha256() |
| 74 | hasher.update(compiler_args_key.encode("utf-8")) |
| 75 | hasher.update(source_tree_hash.encode("utf-8")) |
| 76 | try: |
| 77 | with open(file_path, "rb") as f: |
| 78 | for chunk in iter(lambda: f.read(8192), b""): |
| 79 | hasher.update(chunk) |
| 80 | except OSError: |
| 81 | hasher.update(str(time.time()).encode("utf-8")) |
| 82 | return hasher.hexdigest() |
| 83 | |
| 84 | |
| 85 | class IWYUCache: |