|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script to identify changed or added YAML dataset files for replay. |
| 4 | +This script simplifies the bash logic from the GitHub Actions workflow. |
| 5 | +""" |
| 6 | + |
| 7 | +import sys |
| 8 | +import argparse |
| 9 | +import subprocess |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | + |
| 13 | +def run_git_command(cmd): |
| 14 | + """Run a git command and return the output.""" |
| 15 | + try: |
| 16 | + result = subprocess.run( |
| 17 | + cmd, shell=True, capture_output=True, text=True, check=True |
| 18 | + ) |
| 19 | + return result.stdout.strip() |
| 20 | + except subprocess.CalledProcessError as e: |
| 21 | + print(f"Git command failed: {cmd}") |
| 22 | + print(f"Error: {e.stderr}") |
| 23 | + return "" |
| 24 | + |
| 25 | + |
| 26 | +def find_changed_files(base_sha, head_sha): |
| 27 | + """Find files that changed between two commits.""" |
| 28 | + if not base_sha or not head_sha: |
| 29 | + print("Error: Both base and head SHA are required") |
| 30 | + return [] |
| 31 | + |
| 32 | + cmd = f"git diff --name-only {base_sha}...{head_sha}" |
| 33 | + output = run_git_command(cmd) |
| 34 | + |
| 35 | + if not output: |
| 36 | + return [] |
| 37 | + |
| 38 | + # Filter for files in datasets directory |
| 39 | + changed_files = [] |
| 40 | + for line in output.split('\n'): |
| 41 | + if line.strip() and line.startswith('datasets/'): |
| 42 | + changed_files.append(line.strip()) |
| 43 | + |
| 44 | + return changed_files |
| 45 | + |
| 46 | + |
| 47 | +def find_yaml_files_in_directories(changed_files): |
| 48 | + """Find directories containing YAML files from changed files.""" |
| 49 | + yaml_dirs = set() |
| 50 | + |
| 51 | + for file_path in changed_files: |
| 52 | + # Get the directory containing the changed file |
| 53 | + current_dir = Path(file_path).parent |
| 54 | + |
| 55 | + # Walk up the directory tree to find YAML files |
| 56 | + while current_dir != Path("datasets") and current_dir != Path("."): |
| 57 | + # Check if this directory contains YAML files |
| 58 | + yaml_files = (list(current_dir.glob("*.yml")) + |
| 59 | + list(current_dir.glob("*.yaml"))) |
| 60 | + |
| 61 | + if yaml_files: |
| 62 | + yaml_dirs.add(str(current_dir)) |
| 63 | + break |
| 64 | + |
| 65 | + current_dir = current_dir.parent |
| 66 | + |
| 67 | + return sorted(yaml_dirs) |
| 68 | + |
| 69 | + |
| 70 | +def find_all_yaml_files(directories): |
| 71 | + """Find all YAML files in the given directories.""" |
| 72 | + yaml_files = [] |
| 73 | + |
| 74 | + for dir_path in directories: |
| 75 | + dir_path = Path(dir_path) |
| 76 | + if dir_path.exists() and dir_path.is_dir(): |
| 77 | + # Find YAML files in this directory (not recursive) |
| 78 | + yaml_files.extend(dir_path.glob("*.yml")) |
| 79 | + yaml_files.extend(dir_path.glob("*.yaml")) |
| 80 | + |
| 81 | + return [str(f) for f in sorted(yaml_files)] |
| 82 | + |
| 83 | + |
| 84 | +def main(): |
| 85 | + parser = argparse.ArgumentParser( |
| 86 | + description="Find changed dataset YAML files for replay", |
| 87 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 88 | + epilog=""" |
| 89 | +Examples: |
| 90 | + # Find changes between two commits |
| 91 | + python find_changed_datasets.py --base-sha abc123 --head-sha def456 |
| 92 | +
|
| 93 | + # Find changes in current branch vs main |
| 94 | + python find_changed_datasets.py --compare-branch main |
| 95 | +
|
| 96 | + # List all YAML files in a specific directory |
| 97 | + python find_changed_datasets.py --directory datasets/attack_techniques/T1003.003 |
| 98 | +
|
| 99 | +Output formats: |
| 100 | + --output directories : Print directories containing YAML files (default) |
| 101 | + --output files : Print individual YAML file paths |
| 102 | + """ |
| 103 | + ) |
| 104 | + |
| 105 | + group = parser.add_mutually_exclusive_group(required=True) |
| 106 | + group.add_argument( |
| 107 | + '--base-sha', |
| 108 | + help='Base commit SHA to compare from' |
| 109 | + ) |
| 110 | + group.add_argument( |
| 111 | + '--compare-branch', |
| 112 | + help='Compare current HEAD against this branch (e.g., main, origin/main)' |
| 113 | + ) |
| 114 | + group.add_argument( |
| 115 | + '--directory', |
| 116 | + help='Specific directory to find YAML files in' |
| 117 | + ) |
| 118 | + |
| 119 | + parser.add_argument( |
| 120 | + '--head-sha', |
| 121 | + help='Head commit SHA to compare to (defaults to HEAD if using --base-sha)' |
| 122 | + ) |
| 123 | + parser.add_argument( |
| 124 | + '--output', |
| 125 | + choices=['directories', 'files'], |
| 126 | + default='directories', |
| 127 | + help='Output format: directories or individual files' |
| 128 | + ) |
| 129 | + |
| 130 | + args = parser.parse_args() |
| 131 | + |
| 132 | + try: |
| 133 | + if args.directory: |
| 134 | + # Direct directory mode |
| 135 | + if not Path(args.directory).exists(): |
| 136 | + print(f"Error: Directory {args.directory} does not exist") |
| 137 | + sys.exit(1) |
| 138 | + |
| 139 | + if args.output == 'files': |
| 140 | + yaml_files = find_all_yaml_files([args.directory]) |
| 141 | + for f in yaml_files: |
| 142 | + print(f) |
| 143 | + else: |
| 144 | + if find_all_yaml_files([args.directory]): |
| 145 | + print(args.directory) |
| 146 | + |
| 147 | + elif args.compare_branch: |
| 148 | + # Compare against a branch |
| 149 | + head_sha = run_git_command("git rev-parse HEAD") |
| 150 | + base_sha = run_git_command(f"git merge-base HEAD {args.compare_branch}") |
| 151 | + |
| 152 | + if not head_sha or not base_sha: |
| 153 | + print("Error: Could not determine commit SHAs") |
| 154 | + sys.exit(1) |
| 155 | + |
| 156 | + changed_files = find_changed_files(base_sha, head_sha) |
| 157 | + if not changed_files: |
| 158 | + print("No dataset files changed") |
| 159 | + sys.exit(0) |
| 160 | + |
| 161 | + print(f"Changed files: {len(changed_files)}", file=sys.stderr) |
| 162 | + for f in changed_files: |
| 163 | + print(f" {f}", file=sys.stderr) |
| 164 | + |
| 165 | + yaml_dirs = find_yaml_files_in_directories(changed_files) |
| 166 | + |
| 167 | + if args.output == 'files': |
| 168 | + yaml_files = find_all_yaml_files(yaml_dirs) |
| 169 | + for f in yaml_files: |
| 170 | + print(f) |
| 171 | + else: |
| 172 | + for d in yaml_dirs: |
| 173 | + print(d) |
| 174 | + |
| 175 | + else: |
| 176 | + # Base/head SHA mode |
| 177 | + head_sha = args.head_sha or run_git_command("git rev-parse HEAD") |
| 178 | + |
| 179 | + changed_files = find_changed_files(args.base_sha, head_sha) |
| 180 | + if not changed_files: |
| 181 | + print("No dataset files changed") |
| 182 | + sys.exit(0) |
| 183 | + |
| 184 | + print(f"Changed files: {len(changed_files)}", file=sys.stderr) |
| 185 | + for f in changed_files: |
| 186 | + print(f" {f}", file=sys.stderr) |
| 187 | + |
| 188 | + yaml_dirs = find_yaml_files_in_directories(changed_files) |
| 189 | + |
| 190 | + if args.output == 'files': |
| 191 | + yaml_files = find_all_yaml_files(yaml_dirs) |
| 192 | + for f in yaml_files: |
| 193 | + print(f) |
| 194 | + else: |
| 195 | + for d in yaml_dirs: |
| 196 | + print(d) |
| 197 | + |
| 198 | + except Exception as e: |
| 199 | + print(f"Error: {e}", file=sys.stderr) |
| 200 | + sys.exit(1) |
| 201 | + |
| 202 | + |
| 203 | +if __name__ == "__main__": |
| 204 | + main() |
0 commit comments