diff --git a/README.md b/README.md index 7744497..3b859c0 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,8 @@ Below is an overview of its main functionality and usage: 3. **File and Directory Copying**: - Supports multiple copy modes: - **Replace**: Overwrites existing files or directories in the destination. - - **Update**: Copies only if the source file is newer than the destination file. + - **Update**: Copies only if the source file is newer than the destination file (files only). + - **Copy**: Creates directories, if already exsisting, only copies non existing files in that folder - Handles both files and directories. - Generates warnings and error messages if issues occur (e.g., source file not found). diff --git a/customize-WebUI.py b/customize-WebUI.py index e485047..7cdd27c 100644 --- a/customize-WebUI.py +++ b/customize-WebUI.py @@ -111,8 +111,33 @@ def copySources(config, destinationDirectory): shutil.copytree(sourcePath, targetPath) results['copies'] += 1 else: - print(f"{Colors.YELLOW}Skipping directory copy: {sourcePath} -> {targetPath} (already exists){Colors.RESET}") - errorList['copyWarnings'] += 1 + mode = rule.get('mode') + if mode == 'replace': + # handled earlier + print(f"{Colors.YELLOW}Unexpected existing directory in replace mode (already handled): {targetPath}{Colors.RESET}") + errorList['copyWarnings'] += 1 + elif mode in ('copy','merge'): + added = 0 + for rootDir, subdirs, files in os.walk(sourcePath): + relRoot = os.path.relpath(rootDir, sourcePath) + relRoot = '' if relRoot == '.' else relRoot + destRoot = os.path.join(targetPath, relRoot) if relRoot else targetPath + ensureDirectory(destRoot) + for fname in files: + srcFile = os.path.join(rootDir, fname) + destFile = os.path.join(destRoot, fname) + if not os.path.exists(destFile): + shutil.copy2(srcFile, destFile) + added += 1 + if added: + print(f"{Colors.GREEN}Added {added} new file(s) into existing directory (copy merge behavior): {targetPath}{Colors.RESET}") + results['copies'] += added + else: + print(f"{Colors.YELLOW}Directory already up to date (no new files): {targetPath}{Colors.RESET}") + errorList['copyWarnings'] += 1 + else: + print(f"{Colors.YELLOW}Skipping directory copy (mode {mode}): already exists {targetPath}{Colors.RESET}") + errorList['copyWarnings'] += 1 else: if not os.path.exists(targetPath): print(f"{Colors.GREEN}Copying file: {sourcePath} -> {targetPath}{Colors.RESET}") @@ -195,6 +220,7 @@ def modifyFiles(config, destinationDirectory): else: print(f" {Colors.GREEN}Regex inserting after anchor: /{pattern}/ -> {insert_text[:60]}...{Colors.RESET}") content = content[:match.end()] + insert_text + content[match.end():] + results['modifications'] += 1 else: # Plain (substring) variant – use first occurrence only (consistent with replace count=1) anchor = raw_after @@ -209,6 +235,7 @@ def modifyFiles(config, destinationDirectory): else: print(f" {Colors.GREEN}Inserting text after (plain): {anchor[:40]} -> {insert_text[:60]}...{Colors.RESET}") content = content[:after_pos] + insert_text + content[after_pos:] + results['modifications'] += 1 # BEFORE TEXT INSERTION if 'before_text' in insertRule: @@ -227,6 +254,7 @@ def modifyFiles(config, destinationDirectory): else: print(f" {Colors.GREEN}Regex inserting before anchor: /{pattern}/ <- {insert_text[:60]}...{Colors.RESET}") content = content[:match.start()] + insert_text + content[match.start():] + results['modifications'] += 1 else: # Plain (substring) variant – first occurrence logic anchor = raw_before @@ -241,6 +269,7 @@ def modifyFiles(config, destinationDirectory): else: print(f" {Colors.GREEN}Inserting text before (plain): {insert_text[:60]}... <- {anchor[:40]}{Colors.RESET}") content = content[:before_pos] + insert_text + content[before_pos:] + results['modifications'] += 1 # REPLACE RULES for replaceRules in rule.get('replace_rules', []): @@ -259,6 +288,7 @@ def modifyFiles(config, destinationDirectory): if count: print(f" {Colors.GREEN}Regex replacing pattern /{pattern}/ -> {new_text[:60]}...{Colors.RESET}") content = content_new + results['modifications'] += 1 else: print(f" {Colors.YELLOW}Regex replacement produced no change for /{pattern}/.{Colors.RESET}") errorList['modWarnings'] += 1 @@ -269,6 +299,7 @@ def modifyFiles(config, destinationDirectory): elif new_text not in content: print(f" {Colors.GREEN}Replacing text: {old_text[:60]}... -> {new_text[:60]}...{Colors.RESET}") content = content.replace(old_text, new_text, 1) + results['modifications'] += 1 else: print(f" {Colors.YELLOW}Text already replaced: {old_text[:40]} -> {new_text[:40]}{Colors.RESET}") errorList['modWarnings'] += 1 @@ -278,7 +309,6 @@ def modifyFiles(config, destinationDirectory): with open(filePath, 'w', encoding='utf-8') as f: f.write(content) - results['modifications'] += 1 #else: # print(f"Skipping file: {filename} (not found)")