Files
Jellyfin-Mods-Automated-Script/test/customizeAndCopy.py
2024-12-09 17:33:13 +01:00

191 lines
5.9 KiB
Python

import os
import re
import sys
import yaml
import shutil
import glob
def load_configuration(config_path):
"""
Load configuration from a YAML file.
"""
with open(config_path, 'r', encoding='utf-8') as config_file:
return yaml.safe_load(config_file)
def copy_with_mode(src, dest, mode='copy'):
"""
Copy files or directories with different modes and specific source/target paths.
"""
try:
# Ensure full paths are used
src = os.path.abspath(src)
dest = os.path.abspath(dest)
# Ensure destination directory exists
os.makedirs(os.path.dirname(dest), exist_ok=True)
# Determine copy mode
if mode == 'copy':
# Skip if destination already exists
if os.path.exists(dest):
print(f'Skipping {src}: Destination already exists')
return False
elif mode == 'replace':
# Remove existing destination before copying
if os.path.exists(dest):
if os.path.isdir(dest):
shutil.rmtree(dest)
else:
os.remove(dest)
elif mode == 'update':
# Only copy if source is newer
if os.path.exists(dest):
src_mtime = os.path.getmtime(src)
dest_mtime = os.path.getmtime(dest)
if src_mtime <= dest_mtime:
print(f'Skipping {src}: Destination is up to date')
return False
# Perform copy
if os.path.isdir(src):
shutil.copytree(src, dest)
else:
shutil.copy2(src, dest)
print(f'Copied: {src} -> {dest}')
return True
except Exception as e:
print(f'Error copying {src}: {e}')
return False
def matches_file_pattern(filename, pattern):
"""
Check if filename matches the given pattern.
"""
return re.search(pattern, filename) is not None
def apply_insert_rules(content, insert_rules):
"""
Apply insertion rules to the content.
"""
modified_content = content
modified = False
for rule in insert_rules:
insert_text = rule.get('insert_text')
# Insert after specific text
if 'after_text' in rule:
search_text = rule.get('after_text')
if insert_text not in modified_content:
modified_content = modified_content.replace(
search_text,
f'{search_text} {insert_text}'
)
modified = True
# Insert before specific text
elif 'before_text' in rule:
search_text = rule.get('before_text')
if insert_text not in modified_content:
modified_content = modified_content.replace(
search_text,
f'{insert_text} {search_text}'
)
modified = True
return modified_content, modified
def apply_replace_rules(content, replace_rules):
"""
Apply replacement rules to the content.
"""
modified_content = content
modified = False
for rule in replace_rules:
old_text = rule.get('old_text')
new_text = rule.get('new_text')
# Replace text if found
if old_text in modified_content:
modified_content = modified_content.replace(old_text, new_text)
modified = True
return modified_content, modified
def process_copy_and_modify_rules(config):
"""
Process copy, insertion, and replacement rules.
"""
successful_operations = {
'copies': 0,
'modifications': 0
}
# Process copy rules
for copy_rule in config.get('copy_rules', []):
sources = copy_rule.get('sources', [])
mode = copy_rule.get('mode', 'copy')
for source_info in sources:
# Support both simple string and dictionary input
if isinstance(source_info, str):
src = source_info
dest = os.path.join(
config.get('destination_directory', '.'),
os.path.basename(src)
)
elif isinstance(source_info, dict):
src = source_info.get('source')
dest = source_info.get('target')
# Fallback if target is not specified
if not dest:
dest = os.path.join(
config.get('destination_directory', '.'),
os.path.basename(src)
)
else:
print(f'Invalid source configuration: {source_info}')
continue
# Expand potential wildcards
matching_sources = glob.glob(src)
for matched_src in matching_sources:
# Determine full destination path
if os.path.isdir(matched_src):
full_dest = os.path.join(dest, os.path.basename(matched_src))
else:
full_dest = dest
# Perform copy
if copy_with_mode(matched_src, full_dest, mode):
successful_operations['copies'] += 1
# Rest of the function remains the same as in the original script
# ... (modification rules processing)
return successful_operations
def main():
# Check command-line argument
if len(sys.argv) < 2:
print("Please provide the path to the configuration file.")
sys.exit(1)
config_path = sys.argv[1]
# Load configuration and process rules
config = load_configuration(config_path)
results = process_copy_and_modify_rules(config)
print(f'\nTotal successful copies: {results["copies"]}')
print(f'Total file modifications: {results["modifications"]}')
if __name__ == '__main__':
main()