1+ #!/usr/bin/env python3
2+ """
3+ apply-release-changes.py - Cross-platform script to replace main with a specified release version in YML files
4+
5+ This script performs two replacements in YML files in .github/workflows/:
6+ 1. Replaces @main with @release/VERSION
7+ 2. Replaces 'test-infra-ref: main' with 'test-infra-ref: release/VERSION'
8+
9+ Usage:
10+ python apply-release-changes.py VERSION
11+
12+ Example:
13+ python apply-release-changes.py 2.7
14+ """
15+
16+ import os
17+ import sys
18+ import pathlib
19+ from typing import Optional
20+
21+
22+ def replace_in_file (file_path : pathlib .Path , old_text : str , new_text : str ) -> None :
23+ """Replace all occurrences of old_text with new_text in the specified file."""
24+ try :
25+ # Try reading the file without specifying encoding to use the default
26+ encoding = None
27+ try :
28+ content = file_path .read_text ()
29+ except UnicodeDecodeError :
30+ # If that fails, try with UTF-8
31+ encoding = 'utf-8'
32+ content = file_path .read_text (encoding = encoding )
33+
34+ # Perform the replacement
35+ new_content = content .replace (old_text , new_text )
36+
37+ # Only write if changes were made
38+ if new_content != content :
39+ # Write with the same encoding we used to read
40+ if encoding :
41+ file_path .write_text (new_content , encoding = encoding )
42+ else :
43+ file_path .write_text (new_content )
44+ print (f"Updated: { file_path } " )
45+
46+ except Exception as e :
47+ print (f"Error processing { file_path } : { e } " )
48+
49+
50+ def find_repo_root () -> Optional [pathlib .Path ]:
51+ """Find the git repository root by searching for .git directory."""
52+ # Start from the current directory and traverse upwards
53+ current_path = pathlib .Path .cwd ().absolute ()
54+
55+ while current_path != current_path .parent :
56+ # Check if .git directory exists
57+ git_dir = current_path / '.git'
58+ if git_dir .exists () and git_dir .is_dir ():
59+ return current_path
60+
61+ # Move up one directory
62+ current_path = current_path .parent
63+
64+ # If we get here, we didn't find a repository root
65+ return None
66+
67+
68+ def main () -> None :
69+ # Check if version is provided as command line argument
70+ if len (sys .argv ) != 2 :
71+ print ("Error: Exactly one version parameter is required" )
72+ print (f"Usage: python { os .path .basename (__file__ )} VERSION" )
73+ print ("Example: python apply-release-changes.py 2.7" )
74+ sys .exit (1 )
75+
76+ # Get version from command line argument
77+ version = sys .argv [1 ]
78+ print (f"Using release version: { version } " )
79+
80+ # Find the repository root by searching for .git directory
81+ repo_root = find_repo_root ()
82+ if not repo_root :
83+ print ("Error: Not inside a git repository. Please run from within a git repository." )
84+ sys .exit (1 )
85+
86+ print (f"Repository root found at: { repo_root } " )
87+
88+ # Get path to workflow directory
89+ workflow_dir = repo_root / '.github' / 'workflows'
90+
91+ # Process all workflow files and perform both replacements on each file
92+ for yml_file in workflow_dir .glob ('*.yml' ):
93+ replace_in_file (yml_file , '@main' , f'@release/{ version } ' )
94+ replace_in_file (yml_file , 'test-infra-ref: main' , f'test-infra-ref: release/{ version } ' )
95+
96+
97+ if __name__ == "__main__" :
98+ print ("Starting YML updates..." )
99+ main ()
100+ print ("YML updates completed." )
0 commit comments