Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

apply-release-changes.py 3.3 KB

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

Press p or to see the previous file or, n or to see the next file

Comments

Loading...