113 lines
4.8 KiB
Python
113 lines
4.8 KiB
Python
import os
|
|
import shutil
|
|
import yaml
|
|
import re
|
|
|
|
def loadConfig(config_path):
|
|
"""Load YAML configuration."""
|
|
print(f"Loading configuration from {config_path}")
|
|
with open(config_path, 'r', encoding='utf-8') as file:
|
|
config = yaml.safe_load(file)
|
|
print("Configuration loaded successfully.")
|
|
return config
|
|
|
|
def ensureDirectory(path):
|
|
"""Ensure that a directory exists."""
|
|
print(f"Checking/creating directory: {path}")
|
|
os.makedirs(path, exist_ok=True)
|
|
|
|
def copySources(config, destination_directory):
|
|
"""Copy files and folders according to copy rules."""
|
|
print("Starting source file/folder copy process...")
|
|
for rule in config.get('copy_rules', []):
|
|
for source in rule.get('sources', []):
|
|
# Distinguish between sources with explicit target and without
|
|
if isinstance(source, dict):
|
|
src_path = source['source']
|
|
target_path = os.path.join(destination_directory, source['target'])
|
|
else:
|
|
src_path = source
|
|
target_path = os.path.join(destination_directory, os.path.basename(src_path))
|
|
|
|
# Create target directory
|
|
ensureDirectory(os.path.dirname(target_path))
|
|
|
|
# Copy or replace mode
|
|
if rule.get('mode') == 'replace' and os.path.exists(target_path):
|
|
print(f"Replacing existing path: {target_path}")
|
|
shutil.rmtree(target_path) if os.path.isdir(target_path) else os.remove(target_path)
|
|
|
|
# Copy files or directories
|
|
if os.path.isdir(src_path):
|
|
print(f"Copying directory: {src_path} -> {target_path}")
|
|
shutil.copytree(src_path, target_path)
|
|
else:
|
|
print(f"Copying file: {src_path} -> {target_path}")
|
|
shutil.copy2(src_path, target_path)
|
|
print("Source file/folder copy process completed.")
|
|
|
|
def modifyFiles(config, destination_directory):
|
|
"""Modify files according to modification rules."""
|
|
print("Starting file modification process...")
|
|
for rule in config.get('modification_rules', []):
|
|
file_pattern = rule.get('file_pattern', '*')
|
|
print(f"Processing files matching pattern: {file_pattern}")
|
|
|
|
# Recursively search the destination directory
|
|
for root, _, files in os.walk(destination_directory):
|
|
for filename in files:
|
|
if re.match(file_pattern, filename):
|
|
file_path = os.path.join(root, filename)
|
|
print(f"Modifying file: {file_path}")
|
|
|
|
# Read file content
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
content = f.read()
|
|
|
|
# Perform text replacements
|
|
for insert_rule in rule.get('insert_rules', []):
|
|
if 'after_text' in insert_rule:
|
|
print(f" Inserting text after: {insert_rule['after_text']}")
|
|
content = content.replace(
|
|
insert_rule['after_text'],
|
|
insert_rule['after_text'] + insert_rule['insert_text']
|
|
)
|
|
|
|
if 'before_text' in insert_rule:
|
|
print(f" Inserting text before: {insert_rule['before_text']}")
|
|
content = content.replace(
|
|
insert_rule['before_text'],
|
|
insert_rule['insert_text'] + insert_rule['before_text']
|
|
)
|
|
|
|
if 'old_text' in insert_rule:
|
|
print(f" Replacing text: {insert_rule['old_text']} -> {insert_rule['new_text']}")
|
|
content = content.replace(
|
|
insert_rule['old_text'],
|
|
insert_rule['new_text']
|
|
)
|
|
|
|
# Write modified contents
|
|
with open(file_path, 'w', encoding='utf-8') as f:
|
|
f.write(content)
|
|
print("File modification process completed.")
|
|
|
|
def main(config_path):
|
|
"""Main function to execute all operations."""
|
|
# Load configuration
|
|
config = loadConfig(config_path)
|
|
|
|
# Ensure destination directory
|
|
destination_directory = config.get('destination_directory', './web')
|
|
ensureDirectory(destination_directory)
|
|
|
|
# Copy files and folders
|
|
copySources(config, destination_directory)
|
|
|
|
# Modify files
|
|
modifyFiles(config, destination_directory)
|
|
|
|
print(f"All operations in {destination_directory} completed successfully.")
|
|
|
|
if __name__ == '__main__':
|
|
main('config.yaml') |