Hash source bytes directly, never Git metadata or a writable Atomic open.
()
| 49 | |
| 50 | |
| 51 | def source_fingerprint(): |
| 52 | """Hash source bytes directly, never Git metadata or a writable Atomic open.""" |
| 53 | names = ['Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml', 'libatomic', 'tools'] |
| 54 | names += sorted(p.name for p in ROOT.iterdir() if p.is_dir() and p.name.startswith('atomic-')) |
| 55 | files = [] |
| 56 | for name in names: |
| 57 | path = ROOT / name |
| 58 | if path.is_file(): |
| 59 | files.append(path) |
| 60 | elif path.is_dir(): |
| 61 | for directory, dirs, entries in os.walk(path, followlinks=False): |
| 62 | dirs[:] = sorted(d for d in dirs if d not in ('target', '.git', '.atomic', '__pycache__')) |
| 63 | links = [d for d in dirs if (Path(directory) / d).is_symlink()] |
| 64 | files.extend(Path(directory) / d for d in links) |
| 65 | dirs[:] = [d for d in dirs if d not in links] |
| 66 | files.extend(Path(directory) / entry for entry in entries) |
| 67 | value = hashlib.sha256() |
| 68 | for path in sorted(set(files), key=lambda p: os.fsencode(str(p.relative_to(ROOT)))): |
| 69 | relative = os.fsencode(str(path.relative_to(ROOT))) |
| 70 | value.update(len(relative).to_bytes(8, 'big')) |
| 71 | value.update(relative) |
| 72 | value.update(b'L' if path.is_symlink() else b'F') |
| 73 | value.update(bytes([bool(path.lstat().st_mode & 0o111)])) |
| 74 | content = os.fsencode(os.readlink(path)) if path.is_symlink() else path.read_bytes() |
| 75 | value.update(len(content).to_bytes(8, 'big')) |
| 76 | value.update(content) |
| 77 | return value.hexdigest() |
| 78 | |
| 79 | |
| 80 | def distribution(values): |