add more colored prints

This commit is contained in:
MLH
2024-12-10 02:26:41 +01:00
parent 2be1eb8fdc
commit 09e051b23e

View File

@ -64,7 +64,7 @@ def copySources(config, destinationDirectory):
# Check for absolute paths in target and correct them if necessary
if os.path.isabs(checkTargetPath):
targetPath = os.path.normpath(os.path.join(destinationDirectory, checkTargetPath.lstrip('./')))
raise ValueError(f"Absolute path incorrect: Corrected {checkTargetPath} to {targetPath}.")
raise ValueError(f"{Colors.RED}Absolute path incorrect: {Colors.YELLOW}Corrected {checkTargetPath} to {targetPath}.{Colors.RESET}")
else:
#targetPath = os.path.join(destinationDirectory, source['target']) # old code
targetPath = os.path.normpath(os.path.join(destinationDirectory, checkTargetPath))
@ -98,10 +98,10 @@ def copySources(config, destinationDirectory):
srcMtime = os.path.getmtime(sourcePath)
destMtime = os.path.getmtime(targetPath)
if srcMtime <= destMtime:
print(f"Skipping {sourcePath}: Destination is up to date")
print(f"{Colors.YELLOW}Skipping {sourcePath}: Destination is up to date{Colors.RESET}")
break # Skip this source file/folder
elif rule.get('mode') != 'update' and not os.path.exists(sourcePath):
print(f"Should update {sourcePath} to {targetPath}, but {sourcePath} is not (yet) existing.")
print(f"{Colors.YELLOW}Should update {sourcePath} to {targetPath}, but {sourcePath} is not (yet) existing.{Colors.RESET}")
# Copy files or directories
if os.path.isdir(sourcePath):
@ -110,7 +110,7 @@ def copySources(config, destinationDirectory):
shutil.copytree(sourcePath, targetPath)
results['copies'] += 1
else:
print(f"Skipping directory copy: {sourcePath} -> {targetPath} (already exists)")
print(f"{Colors.YELLOW}Skipping directory copy: {sourcePath} -> {targetPath} (already exists){Colors.RESET}")
errorList['copyWarnings'] += 1
else:
if not os.path.exists(targetPath):
@ -118,30 +118,30 @@ def copySources(config, destinationDirectory):
shutil.copy2(sourcePath, targetPath)
results['copies'] += 1
else:
print(f"Skipping file copy: {sourcePath} -> {targetPath} (already exists)")
print(f"{Colors.YELLOW}Skipping file copy: {sourcePath} -> {targetPath} (already exists){Colors.RESET}")
errorList['copyWarnings'] += 1
except FileNotFoundError as e:
print(f"Error: {e}")
print(f"{Colors.RED}Error: {e}{Colors.RESET}")
errorList["copies"] += 1
continue
except ValueError as e:
print(f"Configuration error: {e}")
print(f"{Colors.RED}Configuration error: {e}{Colors.RESET}")
errorList["copies"] += 1
continue
except PermissionError:
print(f"Error: Permission denied when copying {sourcePath}")
print(f"{Colors.RED}Error: Permission denied when copying {sourcePath}{Colors.RESET}")
errorList["copies"] += 1
continue
except OSError as e:
print(f"Error copying {sourcePath}: {e}")
print(f"{Colors.RED}Error copying {sourcePath}: {e}{Colors.RESET}")
errorList["copies"] += 1
continue
print(f"\nSource file & folder copy/replace process completed with {Colors.RED}{errorList['copies']} errors{Colors.RESET} and {Colors.YELLOW}{errorList['copyWarnings']} warnings{Colors.RESET}.\n")
print(f"\n{Colors.GREEN}Source file & folder copy/replace process completed{Colors.RESET} with {Colors.RED}{errorList['copies']} errors{Colors.RESET} and {Colors.YELLOW}{errorList['copyWarnings']} warnings{Colors.RESET}.\n")
def modifyFiles(config, destinationDirectory):
@ -174,7 +174,7 @@ def modifyFiles(config, destinationDirectory):
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}")
print(f" {Colors.YELLOW}Text already present after: {after_text}{Colors.RESET}")
errorList['modWarnings'] += 1
@ -187,7 +187,7 @@ def modifyFiles(config, destinationDirectory):
print(f" Inserting text before: {before_text}")
content = content.replace(before_text, insert_text + before_text)
else:
print(f" Text already present before: {before_text}")
print(f" {Colors.YELLOW}Text already present before: {before_text}{Colors.RESET}")
errorList['modWarnings'] += 1
@ -200,7 +200,7 @@ def modifyFiles(config, destinationDirectory):
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}")
print(f" {Colors.YELLOW}Text already replaced: {old_text} -> {new_text}{Colors.RESET}")
errorList['modWarnings'] += 1
@ -211,28 +211,28 @@ def modifyFiles(config, destinationDirectory):
results['modifications'] += 1
except ValueError as e:
print(f"Error: {e}")
print(f"{Colors.RED}Error: {e}{Colors.RESET}")
errorList["modifications"] += 1
continue
except PermissionError:
print(f"Error: Permission denied when modifying {filePath}")
print(f"{Colors.RED}Error: Permission denied when modifying {filePath}{Colors.RESET}")
errorList["modifications"] += 1
continue
except IOError as e:
print(f"Error reading/writing file {filePath}: {e}")
print(f"{Colors.RED}Error reading/writing file {filePath}: {e}{Colors.RESET}")
errorList["modifications"] += 1
continue
print(f"\nFile modification process completed with {Colors.RED}{errorList['modifications']} errors{Colors.RESET} and {Colors.YELLOW}{errorList['modWarnings']} warnings{Colors.RESET}.\n")
print(f"\n{Colors.GREEN}File modification process completed{Colors.RESET} with {Colors.RED}{errorList['modifications']} errors{Colors.RESET} and {Colors.YELLOW}{errorList['modWarnings']} warnings{Colors.RESET}.\n")
def main():
"""Main function to execute all operations."""
# Check command-line argument
if len(sys.argv) < 2:
print("Please provide the path to the configuration file.")
print(f"{Colors.RED}Please provide the path to the configuration file.{Colors.RESET}")
sys.exit(1)
configPath = sys.argv[1]
@ -259,7 +259,7 @@ def main():
print(f"Total copy errors: {errorList['copies']}")
print(f"Total modification errors: {errorList['modifications']}{Colors.RESET}")
elif errorList["copyWarnings"] > 0 or errorList["modWarnings"] > 0:
print(f"{Colors.YELLOW}All operations in {destinationDirectory} from {configPath} completed successfully! But there were {errorList['modWarnings'] + errorList['copyWarnings']} warnings. Maybe you should check them.{Colors.RESET}")
print(f"{Colors.GREEN}All operations in {destinationDirectory} from {configPath} completed successfully!{Colors.YELLOW} But there were {errorList['modWarnings'] + errorList['copyWarnings']} warnings. Maybe you should check them.{Colors.RESET}")
else:
print(f"{Colors.GREEN}All operations in {destinationDirectory} from {configPath} completed successfully!{Colors.RESET}")