Spaces:
Build error
Build error
import os | |
# Define the directories that contain the test files | |
directories_to_fix = ['core/_test', 'utils/_test'] | |
def backup_files(directory): | |
""" | |
Backup all test files in the specified directory by renaming them with a ".bak" extension. | |
""" | |
for filename in os.listdir(directory): | |
if filename.startswith('test_') and filename.endswith('.py'): | |
file_path = os.path.join(directory, filename) | |
backup_path = file_path + ".bak" | |
if not os.path.exists(backup_path): # Only backup if backup doesn't exist yet | |
os.rename(file_path, backup_path) | |
def fix_imports_for_current_directory(file_path): | |
""" | |
Adjust the imports in the given test file to be relative imports. | |
""" | |
with open(file_path, 'r') as f: | |
content = f.readlines() | |
updated_content = [ | |
line.replace('from core.', 'from .') if 'from core.' in line else line | |
for line in content | |
] | |
with open(file_path, 'w') as f: | |
f.writelines(updated_content) | |
# Backup all the test files | |
for dir in directories_to_fix: | |
backup_files(dir) | |
# Adjust the imports in all test files | |
for directory in directories_to_fix: | |
for filename in os.listdir(directory): | |
if filename.startswith('test_') and filename.endswith('.py.bak'): | |
original_file_path = os.path.join(directory, filename) | |
fixed_file_path = os.path.join(directory, filename.replace('.bak', '')) | |
# Copy content from backup to new file | |
with open(original_file_path, 'r') as original, open(fixed_file_path, 'w') as fixed: | |
fixed.write(original.read()) | |
# Fix the imports in the new file | |
fix_imports_for_current_directory(fixed_file_path) | |
print("Done backing up and fixing imports!") | |