pmd/simulate_local_edits.py

67 lines
2.0 KiB
Python

import os
import random
# Constants
NUM_FILES = 100 # Number of files to edit
LINES_TO_ADD = 5 # Number of lines to add per file
LINES_TO_DELETE = 3 # Number of lines to delete per file
# Generate random text
def random_text(num_lines):
return [f"Random line {random.randint(1, 10000)}\n" for _ in range(num_lines)]
# Modify a file
def modify_file(file_path):
try:
with open(file_path, 'r') as f:
lines = f.readlines()
# Add random lines
for _ in range(LINES_TO_ADD):
insert_pos = random.randint(0, len(lines)) # Insert at a random position
lines.insert(insert_pos, f"Added line: {random.randint(1, 10000)}\n")
# Delete random lines (if possible)
for _ in range(min(LINES_TO_DELETE, len(lines))):
delete_pos = random.randint(0, len(lines) - 1)
lines.pop(delete_pos)
# Write changes back
with open(file_path, 'w') as f:
f.writelines(lines)
print(f"Modified: {file_path}")
except Exception as e:
print(f"Error modifying {file_path}: {e}")
# Get a list of files in the repository
def get_repo_files():
files = []
for root, _, filenames in os.walk("."):
for filename in filenames:
if filename.endswith(".txt") or filename.endswith(".md") or filename.endswith(".py"): # Edit suitable file types
files.append(os.path.join(root, filename))
return files
# Main script logic
def main():
# Get files in the repository
repo_files = get_repo_files()
if len(repo_files) == 0:
print("No suitable files found in the repository.")
return
# Shuffle and select a subset of files
random.shuffle(repo_files)
files_to_edit = repo_files[:NUM_FILES]
# Modify selected files
for file_path in files_to_edit:
modify_file(file_path)
print("All changes have been made locally. Review the files and commit the changes if needed.")
if __name__ == "__main__":
main()