fix count
This commit is contained in:
@@ -31,7 +31,8 @@ Below is an overview of its main functionality and usage:
|
|||||||
3. **File and Directory Copying**:
|
3. **File and Directory Copying**:
|
||||||
- Supports multiple copy modes:
|
- Supports multiple copy modes:
|
||||||
- **Replace**: Overwrites existing files or directories in the destination.
|
- **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.
|
- Handles both files and directories.
|
||||||
- Generates warnings and error messages if issues occur (e.g., source file not found).
|
- Generates warnings and error messages if issues occur (e.g., source file not found).
|
||||||
|
|
||||||
|
|||||||
@@ -111,8 +111,33 @@ def copySources(config, destinationDirectory):
|
|||||||
shutil.copytree(sourcePath, targetPath)
|
shutil.copytree(sourcePath, targetPath)
|
||||||
results['copies'] += 1
|
results['copies'] += 1
|
||||||
else:
|
else:
|
||||||
print(f"{Colors.YELLOW}Skipping directory copy: {sourcePath} -> {targetPath} (already exists){Colors.RESET}")
|
mode = rule.get('mode')
|
||||||
errorList['copyWarnings'] += 1
|
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:
|
else:
|
||||||
if not os.path.exists(targetPath):
|
if not os.path.exists(targetPath):
|
||||||
print(f"{Colors.GREEN}Copying file: {sourcePath} -> {targetPath}{Colors.RESET}")
|
print(f"{Colors.GREEN}Copying file: {sourcePath} -> {targetPath}{Colors.RESET}")
|
||||||
@@ -195,6 +220,7 @@ def modifyFiles(config, destinationDirectory):
|
|||||||
else:
|
else:
|
||||||
print(f" {Colors.GREEN}Regex inserting after anchor: /{pattern}/ -> {insert_text[:60]}...{Colors.RESET}")
|
print(f" {Colors.GREEN}Regex inserting after anchor: /{pattern}/ -> {insert_text[:60]}...{Colors.RESET}")
|
||||||
content = content[:match.end()] + insert_text + content[match.end():]
|
content = content[:match.end()] + insert_text + content[match.end():]
|
||||||
|
results['modifications'] += 1
|
||||||
else:
|
else:
|
||||||
# Plain (substring) variant – use first occurrence only (consistent with replace count=1)
|
# Plain (substring) variant – use first occurrence only (consistent with replace count=1)
|
||||||
anchor = raw_after
|
anchor = raw_after
|
||||||
@@ -209,6 +235,7 @@ def modifyFiles(config, destinationDirectory):
|
|||||||
else:
|
else:
|
||||||
print(f" {Colors.GREEN}Inserting text after (plain): {anchor[:40]} -> {insert_text[:60]}...{Colors.RESET}")
|
print(f" {Colors.GREEN}Inserting text after (plain): {anchor[:40]} -> {insert_text[:60]}...{Colors.RESET}")
|
||||||
content = content[:after_pos] + insert_text + content[after_pos:]
|
content = content[:after_pos] + insert_text + content[after_pos:]
|
||||||
|
results['modifications'] += 1
|
||||||
|
|
||||||
# BEFORE TEXT INSERTION
|
# BEFORE TEXT INSERTION
|
||||||
if 'before_text' in insertRule:
|
if 'before_text' in insertRule:
|
||||||
@@ -227,6 +254,7 @@ def modifyFiles(config, destinationDirectory):
|
|||||||
else:
|
else:
|
||||||
print(f" {Colors.GREEN}Regex inserting before anchor: /{pattern}/ <- {insert_text[:60]}...{Colors.RESET}")
|
print(f" {Colors.GREEN}Regex inserting before anchor: /{pattern}/ <- {insert_text[:60]}...{Colors.RESET}")
|
||||||
content = content[:match.start()] + insert_text + content[match.start():]
|
content = content[:match.start()] + insert_text + content[match.start():]
|
||||||
|
results['modifications'] += 1
|
||||||
else:
|
else:
|
||||||
# Plain (substring) variant – first occurrence logic
|
# Plain (substring) variant – first occurrence logic
|
||||||
anchor = raw_before
|
anchor = raw_before
|
||||||
@@ -241,6 +269,7 @@ def modifyFiles(config, destinationDirectory):
|
|||||||
else:
|
else:
|
||||||
print(f" {Colors.GREEN}Inserting text before (plain): {insert_text[:60]}... <- {anchor[:40]}{Colors.RESET}")
|
print(f" {Colors.GREEN}Inserting text before (plain): {insert_text[:60]}... <- {anchor[:40]}{Colors.RESET}")
|
||||||
content = content[:before_pos] + insert_text + content[before_pos:]
|
content = content[:before_pos] + insert_text + content[before_pos:]
|
||||||
|
results['modifications'] += 1
|
||||||
|
|
||||||
# REPLACE RULES
|
# REPLACE RULES
|
||||||
for replaceRules in rule.get('replace_rules', []):
|
for replaceRules in rule.get('replace_rules', []):
|
||||||
@@ -259,6 +288,7 @@ def modifyFiles(config, destinationDirectory):
|
|||||||
if count:
|
if count:
|
||||||
print(f" {Colors.GREEN}Regex replacing pattern /{pattern}/ -> {new_text[:60]}...{Colors.RESET}")
|
print(f" {Colors.GREEN}Regex replacing pattern /{pattern}/ -> {new_text[:60]}...{Colors.RESET}")
|
||||||
content = content_new
|
content = content_new
|
||||||
|
results['modifications'] += 1
|
||||||
else:
|
else:
|
||||||
print(f" {Colors.YELLOW}Regex replacement produced no change for /{pattern}/.{Colors.RESET}")
|
print(f" {Colors.YELLOW}Regex replacement produced no change for /{pattern}/.{Colors.RESET}")
|
||||||
errorList['modWarnings'] += 1
|
errorList['modWarnings'] += 1
|
||||||
@@ -269,6 +299,7 @@ def modifyFiles(config, destinationDirectory):
|
|||||||
elif new_text not in content:
|
elif new_text not in content:
|
||||||
print(f" {Colors.GREEN}Replacing text: {old_text[:60]}... -> {new_text[:60]}...{Colors.RESET}")
|
print(f" {Colors.GREEN}Replacing text: {old_text[:60]}... -> {new_text[:60]}...{Colors.RESET}")
|
||||||
content = content.replace(old_text, new_text, 1)
|
content = content.replace(old_text, new_text, 1)
|
||||||
|
results['modifications'] += 1
|
||||||
else:
|
else:
|
||||||
print(f" {Colors.YELLOW}Text already replaced: {old_text[:40]} -> {new_text[:40]}{Colors.RESET}")
|
print(f" {Colors.YELLOW}Text already replaced: {old_text[:40]} -> {new_text[:40]}{Colors.RESET}")
|
||||||
errorList['modWarnings'] += 1
|
errorList['modWarnings'] += 1
|
||||||
@@ -278,7 +309,6 @@ def modifyFiles(config, destinationDirectory):
|
|||||||
with open(filePath, 'w', encoding='utf-8') as f:
|
with open(filePath, 'w', encoding='utf-8') as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
|
|
||||||
results['modifications'] += 1
|
|
||||||
#else:
|
#else:
|
||||||
# print(f"Skipping file: {filename} (not found)")
|
# print(f"Skipping file: {filename} (not found)")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user