✨ use stdin
This commit is contained in:
parent
482ff64189
commit
76768dd143
|
|
@ -4,93 +4,104 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
|
|
||||||
my_parser = argparse.ArgumentParser(description='Rename all suspicious files found by clamav')
|
# Setup argparse
|
||||||
|
argparser = argparse.ArgumentParser(description='Rename files identified as dangerous by clamav')
|
||||||
my_parser.add_argument('File',
|
argparser.add_argument('-f',
|
||||||
metavar='file',
|
'--file',
|
||||||
type=str,
|
help='Path to clamav log file',
|
||||||
help='path to clamav log file')
|
type=argparse.FileType('r', encoding='UTF-8'),
|
||||||
|
default=(None if sys.stdin.isatty() else sys.stdin))
|
||||||
my_parser.add_argument('-s',
|
argparser.add_argument('-s',
|
||||||
'--suffix',
|
'--suffix',
|
||||||
type=str,
|
type=str,
|
||||||
help='suffix to add to the end of the file names',
|
help='Suffix to be appended to the end of the filenames',
|
||||||
default='.VIRUS')
|
default='.VIRUS')
|
||||||
|
argparser.add_argument('-u',
|
||||||
my_parser.add_argument('-u',
|
|
||||||
'--undo',
|
'--undo',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help='undo renaming')
|
help='Undo renaming')
|
||||||
|
argparser.add_argument('-d',
|
||||||
my_parser.add_argument('--dry-run',
|
'--dry-run',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help='perform a test run where no file names are actually changed')
|
help='Perform a test run where no file names are actually changed')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = my_parser.parse_args()
|
args = argparser.parse_args()
|
||||||
file = args.File
|
|
||||||
success_count = 0
|
success_count = 0
|
||||||
error_count = 0
|
error_count = 0
|
||||||
warning_count = 0
|
warning_count = 0
|
||||||
|
|
||||||
if not os.path.isfile(file):
|
# Print info if no file name is specified or passed via stdin
|
||||||
print(f"The file '{file}' does not exist")
|
if not args.file:
|
||||||
|
print('Please specify a path for a clamav log file using the -f argument or by piping the output directly into '
|
||||||
|
'this program.')
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
with open(file) as f:
|
# Read input file
|
||||||
lines = f.readlines()
|
with args.file as file:
|
||||||
files_to_rename = [re.split(r":", line.strip())[0] for line in lines if re.search(r"FOUND$", line)]
|
lines = file.readlines()
|
||||||
|
|
||||||
for file_to_rename in files_to_rename:
|
# Find identified file names and paths
|
||||||
new_name = file_to_rename + args.suffix
|
files_to_rename = [re.split(r":", line.strip())[0] for line in lines if re.search(r"FOUND$", line)]
|
||||||
if not args.undo:
|
|
||||||
if not os.path.isfile(file_to_rename):
|
for file_to_rename in files_to_rename:
|
||||||
if os.path.isfile(new_name):
|
new_name = file_to_rename + args.suffix
|
||||||
print(f"WARNING: The file {file_to_rename} was already renamed to {new_name}")
|
|
||||||
warning_count += 1
|
if not args.undo:
|
||||||
else:
|
# Print warning if file can't be found
|
||||||
print(f"ERROR: Could not rename {file_to_rename}")
|
if not os.path.isfile(file_to_rename):
|
||||||
error_count += 1
|
if os.path.isfile(new_name):
|
||||||
continue
|
print(f"WARNING: The file {file_to_rename} was already renamed to {new_name}")
|
||||||
|
warning_count += 1
|
||||||
else:
|
else:
|
||||||
try:
|
print(f"ERROR: Could not rename {file_to_rename}")
|
||||||
if not args.dry_run:
|
error_count += 1
|
||||||
os.rename(file_to_rename, new_name)
|
continue
|
||||||
print(f"SUCCESS: {file_to_rename} -> {new_name}")
|
# Do the renaming
|
||||||
success_count += 1
|
|
||||||
except Exception as e:
|
|
||||||
print(f"ERROR: Could not rename {file_to_rename}: {e}")
|
|
||||||
error_count += 1
|
|
||||||
else:
|
else:
|
||||||
if not os.path.isfile(new_name):
|
try:
|
||||||
if os.path.isfile(file_to_rename):
|
if not args.dry_run:
|
||||||
print(f"WARNING: The file {file_to_rename} was not yet renamed")
|
os.rename(file_to_rename, new_name)
|
||||||
warning_count += 1
|
print(f"SUCCESS: {file_to_rename} -> {new_name}")
|
||||||
else:
|
success_count += 1
|
||||||
print(f"ERROR: Could not undo renaming of {new_name}")
|
except Exception as e:
|
||||||
error_count += 1
|
print(f"ERROR: Could not rename {file_to_rename}: {e}")
|
||||||
continue
|
error_count += 1
|
||||||
else:
|
|
||||||
try:
|
|
||||||
if not args.dry_run:
|
|
||||||
os.rename(new_name, file_to_rename)
|
|
||||||
print(f"SUCCESS: {new_name} -> {file_to_rename}")
|
|
||||||
success_count += 1
|
|
||||||
except Exception as e:
|
|
||||||
print(f"ERROR: Could not undo renaming of {new_name}: {e}")
|
|
||||||
error_count += 1
|
|
||||||
|
|
||||||
|
# If the changes are to be undone
|
||||||
|
else:
|
||||||
|
# Print warning if file can't be found
|
||||||
|
if not os.path.isfile(new_name):
|
||||||
|
if os.path.isfile(file_to_rename):
|
||||||
|
print(f"WARNING: The file {file_to_rename} was not yet renamed")
|
||||||
|
warning_count += 1
|
||||||
|
else:
|
||||||
|
print(f"ERROR: Could not undo renaming of {new_name}")
|
||||||
|
error_count += 1
|
||||||
|
continue
|
||||||
|
# Undo the renaming
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
if not args.dry_run:
|
||||||
|
os.rename(new_name, file_to_rename)
|
||||||
|
print(f"SUCCESS: {new_name} -> {file_to_rename}")
|
||||||
|
success_count += 1
|
||||||
|
except Exception as e:
|
||||||
|
print(f"ERROR: Could not undo renaming of {new_name}: {e}")
|
||||||
|
error_count += 1
|
||||||
|
|
||||||
|
# Print result
|
||||||
if error_count or warning_count:
|
if error_count or warning_count:
|
||||||
print(f"--------------------------------------------------------------\n"
|
print(f"--------------------------------------------------------------\n"
|
||||||
f"Renaming {'would have' if args.dry_run else ''} finished with "
|
f"Renaming {'would have ' if args.dry_run else ''}finished with "
|
||||||
f"{error_count} error{'s' if error_count > 1 else ''} and "
|
f"{error_count} error{'s' if error_count > 1 else ''} and "
|
||||||
f"{warning_count} warning{'s' if warning_count > 1 else ''}.\n"
|
f"{warning_count} warning{'s' if warning_count > 1 else ''}.\n"
|
||||||
f"{success_count if success_count > 0 else 'No'} files {'would have been ' if args.dry_run else 'were '} "
|
f"{success_count if success_count > 0 else 'No'} files {'would have been' if args.dry_run else 'were'} "
|
||||||
"renamed successfully.")
|
"renamed successfully.")
|
||||||
else:
|
else:
|
||||||
print(f"--------------------------------------------------------------\n"
|
print(f"--------------------------------------------------------------\n"
|
||||||
f"{success_count if success_count > 0 else 'No'} files {'would have been ' if args.dry_run else 'were '}"
|
f"{success_count if success_count > 0 else 'No'} files {'would have been' if args.dry_run else 'were'}"
|
||||||
"renamed successfully.")
|
"renamed successfully.")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue