diff --git a/customize-WebUI.py b/customize-WebUI.py index 85d404a..dc0f7c3 100644 --- a/customize-WebUI.py +++ b/customize-WebUI.py @@ -6,7 +6,7 @@ import sys # Global variable to track errors -errorList = {"copies": 0, "modifications": 0} +errorList = {"copies": 0, "modifications": 0, "copyWarnings": 0, "modWarnings": 0} # Initialize results results = {"copies": 0, "modifications": 0} @@ -98,13 +98,22 @@ def copySources(config, destinationDirectory): # Copy files or directories if os.path.isdir(sourcePath): - print(f"Copying directory: {sourcePath} -> {targetPath}") - shutil.copytree(sourcePath, targetPath) - results['copies'] += 1 + if not os.path.exists(targetPath): + print(f"Copying directory: {sourcePath} -> {targetPath}") + shutil.copytree(sourcePath, targetPath) + results['copies'] += 1 + else: + print(f"Skipping directory: {sourcePath} -> {targetPath} (already exists)") + errorList['copyWarnings'] += 1 else: - print(f"Copying file: {sourcePath} -> {targetPath}") - shutil.copy2(sourcePath, targetPath) - results['copies'] += 1 + if not os.path.exists(targetPath): + print(f"Copying file: {sourcePath} -> {targetPath}") + shutil.copy2(sourcePath, targetPath) + results['copies'] += 1 + else: + print(f"Skipping file: {sourcePath} -> {targetPath} (already exists)") + errorList['copyWarnings'] += 1 + except FileNotFoundError as e: print(f"Error: {e}") errorList["copies"] += 1 @@ -125,7 +134,7 @@ def copySources(config, destinationDirectory): errorList["copies"] += 1 continue - print(f"\nSource file & folder copy/replace process completed with {errorList['copies']} errors.\n") + print(f"\nSource file & folder copy/replace process completed with {errorList['copies']} errors and {errorList['copyWarnings']} warnings.\n") def modifyFiles(config, destinationDirectory): @@ -150,31 +159,55 @@ def modifyFiles(config, destinationDirectory): # Perform text replacements for insertRule in rule.get('insert_rules', []): if 'after_text' in insertRule: - print(f" Inserting text after: {insertRule['after_text']}") - content = content.replace( - insertRule['after_text'], - insertRule['after_text'] + insertRule['insert_text'] - ) + after_text = insertRule['after_text'] + insert_text = insertRule['insert_text'] + if after_text not in content: + raise ValueError(f"Text '{after_text}' not found in file: {filePath}") + elif insert_text not in content: + print(f" Inserting text after: {after_text}") + content = content.replace(after_text, after_text + insert_text) + else: + print(f" Text already present after: {after_text}") + errorList['modWarnings'] += 1 + if 'before_text' in insertRule: - print(f" Inserting text before: {insertRule['before_text']}") - content = content.replace( - insertRule['before_text'], - insertRule['insert_text'] + insertRule['before_text'] - ) + before_text = insertRule['before_text'] + insert_text = insertRule['insert_text'] + if before_text not in content: + raise ValueError(f"Text '{before_text}' not found in file: {filePath}") + elif insert_text not in content: + print(f" Inserting text before: {before_text}") + content = content.replace(before_text, before_text + insert_text) + else: + print(f" Text already present after: {before_text}") + errorList['modWarnings'] += 1 + if 'old_text' in insertRule: - print(f" Replacing text: {insertRule['old_text']} -> {insertRule['new_text']}") - content = content.replace( - insertRule['old_text'], - insertRule['new_text'] - ) + old_text = insertRule['old_text'] + new_text = insertRule['new_text'] + if old_text not in content: + raise ValueError(f"Text '{old_text}' not found in file: {filePath}") + elif new_text not in content: + print(f" Replacing text: {old_text} -> {new_text}") + content = content.replace(old_text, new_text) + else: + print(f" Text already replaced: {old_text} -> {new_text}") + errorList['modWarnings'] += 1 + # Write modified contents with open(filePath, 'w', encoding='utf-8') as f: f.write(content) results['modifications'] += 1 + + except ValueError as e: + print(f"Error: {e}") + errorList["modifications"] += 1 + continue + except PermissionError: print(f"Error: Permission denied when modifying {filePath}") errorList["modifications"] += 1 @@ -218,6 +251,8 @@ def main(): print("Errors occurred during the process. Check the output for details.") print(f"{errorList['copies']} copy errors.") print(f"{errorList['modifications']} modification errors.") + elif errorList["warnings"] > 0: + print(f"All operations in {destinationDirectory} from {configPath} completed successfully! But there were {errorList['warnings']} warnings. Mybe you should check them.") else: print(f"All operations in {destinationDirectory} from {configPath} completed successfully!")