Skip to content

Instantly share code, notes, and snippets.

@jorendumoulin
Last active January 7, 2025 10:16
Show Gist options
  • Save jorendumoulin/855686adb8197ee207fb3224a480cfd6 to your computer and use it in GitHub Desktop.
Save jorendumoulin/855686adb8197ee207fb3224a480cfd6 to your computer and use it in GitHub Desktop.
import os
import re
# Define the regex for matching
regex = r'(\/\/.*\{( *|[^[}\n]*, *))(\"[a-zA-Z_][a-zA-Z0-9_$.]*\")'
# Folder to search
folder_path = "tests/filecheck"
def process_file(file_path):
change_count = 0
iterations = 0
with open(file_path, "r") as file:
content = file.read()
while True:
# Apply regex substitution
new_content, replacements = re.subn(
regex,
lambda m: f"{m.group(1)}{m.group(3).strip('\"')}", # Keep group 1 (prefix) intact, modify group 3 (quoted attribute)
content
)
# If no replacements were made, break the loop
if replacements == 0:
break
content = new_content
change_count += replacements
iterations += 1
# Write changes back to the file if any replacements were made
if change_count > 0:
with open(file_path, "w") as file:
file.write(content)
return change_count, iterations
def process_folder(folder_path):
report = []
# Walk through the directory structure
for root, _, files in os.walk(folder_path):
for file in files:
if file.endswith(".mlir") and "invalid" not in file:
file_path = os.path.join(root, file)
print(f"Processing file: {file_path}")
changes, iterations = process_file(file_path)
if changes > 0:
report.append({
"file": file_path,
"changes": changes,
"iterations": iterations
})
return report
# Run the script
report = process_folder(folder_path)
# Report results
for entry in report:
print(f"File: {entry['file']}, Changes Made: {entry['changes']}, Iterations: {entry['iterations']}")
if not report:
print("No changes were made in any files.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment