Skip to content

Instantly share code, notes, and snippets.

@JasonGross
Last active March 14, 2024 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JasonGross/59fc3c03664f2280849abf50b531be42 to your computer and use it in GitHub Desktop.
Save JasonGross/59fc3c03664f2280849abf50b531be42 to your computer and use it in GitHub Desktop.
Replace `:>` with `::` in `Class` definitions for coq/coq#18590
#!/usr/bin/env python
import subprocess
import re
def get_git_grep_files(pattern):
"""Run git grep to find files matching a pattern."""
result = subprocess.run(['git', 'grep', '--name-only', pattern], capture_output=True, text=True)
if result.returncode != 0:
raise Exception("git grep command failed")
files = result.stdout.strip().split('\n')
return files
def process_files(files, search_pattern, replace_pattern):
"""Process each file, searching for patterns and replacing them."""
fs = {}
for fname in files:
with open(fname, "r") as f:
fs[fname] = f.read()
fs2 = {
f: [m for m in re.findall(search_pattern, c, flags=re.MULTILINE|re.DOTALL)
if not m.startswith("Existing") and ":>" in m and re.search(replace_pattern, m, flags=re.MULTILINE|re.DOTALL)]
for f, c in fs.items()
}
fs3 = {f: m for f, m in fs2.items() if m}
for f, matches in fs3.items():
original_content = fs[f]
modified_content = original_content
for val in matches:
modified_content = modified_content.replace(val, val.replace(":>", "::"))
with open(f, "w") as file:
file.write(modified_content)
if __name__ == "__main__":
class_pattern = 'Class '
search_pattern = r'(?:Existing\s+)?Class\s.*?\.\s'
replace_pattern = r':=\s*'
files = get_git_grep_files(class_pattern)
process_files(files, search_pattern, replace_pattern)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment