|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Cross-platform replacement for GitHub Actions hashFiles() function. |
| 4 | +
|
| 5 | +This script computes a hash of files matching the given glob patterns, |
| 6 | +compatible with Linux, macOS, and Windows runners. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python hash-files.py 'pattern1' 'pattern2' ... |
| 10 | +
|
| 11 | +Example: |
| 12 | + python hash-files.py 'requirements/**/*.txt' 'noxfile.py' |
| 13 | +""" |
| 14 | +import hashlib |
| 15 | +import sys |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | + |
| 19 | +def find_files(patterns): |
| 20 | + """ |
| 21 | + Find all files matching the given glob patterns. |
| 22 | +
|
| 23 | + Args: |
| 24 | + patterns: List of glob patterns (e.g., 'requirements/**/*.txt') |
| 25 | +
|
| 26 | + Returns: |
| 27 | + Sorted list of Path objects for matching files |
| 28 | + """ |
| 29 | + files = set() |
| 30 | + repo_root = Path.cwd() |
| 31 | + |
| 32 | + for pattern in patterns: |
| 33 | + # Handle both absolute and relative patterns |
| 34 | + pattern = pattern.strip() |
| 35 | + if not pattern: |
| 36 | + continue |
| 37 | + |
| 38 | + # Check if pattern is absolute |
| 39 | + pattern_path = Path(pattern) |
| 40 | + if pattern_path.is_absolute(): |
| 41 | + # For absolute paths, extract the pattern relative to repo root |
| 42 | + # e.g., /home/runner/work/salt/salt/.relenv/**/*.xz -> .relenv/**/*.xz |
| 43 | + try: |
| 44 | + # Try to make it relative to repo root |
| 45 | + relative_pattern = pattern_path.relative_to(repo_root) |
| 46 | + pattern = str(relative_pattern) |
| 47 | + except ValueError: |
| 48 | + # Pattern is outside repo root, use as-is |
| 49 | + # Try to glob from root |
| 50 | + if "**" in pattern or "*" in pattern or "?" in pattern: |
| 51 | + # It's a glob pattern with absolute base |
| 52 | + # Extract the base directory and the glob part |
| 53 | + parts = pattern.split("/") |
| 54 | + # Find the first part with a glob character |
| 55 | + for i, part in enumerate(parts): |
| 56 | + if "*" in part or "?" in part: |
| 57 | + base = Path("/".join(parts[:i])) |
| 58 | + glob_pattern = "/".join(parts[i:]) |
| 59 | + matching_paths = base.glob(glob_pattern) |
| 60 | + for path in matching_paths: |
| 61 | + if path.is_file(): |
| 62 | + files.add(path) |
| 63 | + break |
| 64 | + continue |
| 65 | + else: |
| 66 | + # It's an absolute path to a single file |
| 67 | + if pattern_path.is_file(): |
| 68 | + files.add(pattern_path) |
| 69 | + continue |
| 70 | + |
| 71 | + # Use glob for patterns |
| 72 | + matching_paths = repo_root.glob(pattern) |
| 73 | + |
| 74 | + # Add only files (not directories) |
| 75 | + for path in matching_paths: |
| 76 | + if path.is_file(): |
| 77 | + files.add(path) |
| 78 | + |
| 79 | + # Sort for consistent ordering across platforms |
| 80 | + return sorted(files) |
| 81 | + |
| 82 | + |
| 83 | +def hash_files(file_paths): |
| 84 | + """ |
| 85 | + Compute SHA256 hash of the contents of all files. |
| 86 | +
|
| 87 | + Args: |
| 88 | + file_paths: List of Path objects to hash |
| 89 | +
|
| 90 | + Returns: |
| 91 | + Hexadecimal hash string |
| 92 | + """ |
| 93 | + hasher = hashlib.sha256() |
| 94 | + |
| 95 | + for file_path in file_paths: |
| 96 | + try: |
| 97 | + # Add the relative path to the hash for consistency |
| 98 | + # Try to make it relative to cwd, otherwise use the full path |
| 99 | + try: |
| 100 | + rel_path = file_path.relative_to(Path.cwd()) |
| 101 | + except ValueError: |
| 102 | + # File is outside cwd, use absolute path |
| 103 | + rel_path = file_path |
| 104 | + hasher.update(str(rel_path).encode("utf-8")) |
| 105 | + |
| 106 | + # Read and hash file contents in binary mode |
| 107 | + with open(file_path, "rb") as f: |
| 108 | + # Read in chunks to handle large files efficiently |
| 109 | + while chunk := f.read(8192): |
| 110 | + hasher.update(chunk) |
| 111 | + except (OSError, IOError) as e: |
| 112 | + # Print warning but continue with other files |
| 113 | + print(f"Warning: Could not read {file_path}: {e}", file=sys.stderr) |
| 114 | + continue |
| 115 | + |
| 116 | + return hasher.hexdigest() |
| 117 | + |
| 118 | + |
| 119 | +def main(): |
| 120 | + """Main entry point.""" |
| 121 | + if len(sys.argv) < 2: |
| 122 | + print("Usage: python hash-files.py 'pattern1' 'pattern2' ...", file=sys.stderr) |
| 123 | + print("", file=sys.stderr) |
| 124 | + print( |
| 125 | + "Example: python hash-files.py 'requirements/**/*.txt' 'noxfile.py'", |
| 126 | + file=sys.stderr, |
| 127 | + ) |
| 128 | + sys.exit(1) |
| 129 | + |
| 130 | + patterns = sys.argv[1:] |
| 131 | + |
| 132 | + # Find all matching files |
| 133 | + files = find_files(patterns) |
| 134 | + |
| 135 | + if not files: |
| 136 | + # Return empty hash if no files found (mimics hashFiles behavior) |
| 137 | + print("") |
| 138 | + return |
| 139 | + |
| 140 | + # Compute and print hash |
| 141 | + file_hash = hash_files(files) |
| 142 | + print(file_hash) |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + main() |
0 commit comments