From 073980f17b5df378b579b1b1b0111d104a12dbbf Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Mon, 2 Dec 2019 22:30:25 -0600 Subject: [PATCH] + add CLI command support * fix inode rename bug - removed pointless req files --- filenuke/__init__.py | 9 ++++++++- filenuke/nuke.py | 19 +++++++++++-------- requirements.in | 0 requirements.txt | 6 ------ 4 files changed, 19 insertions(+), 15 deletions(-) delete mode 100644 requirements.in delete mode 100644 requirements.txt diff --git a/filenuke/__init__.py b/filenuke/__init__.py index e26817b..e7766fd 100755 --- a/filenuke/__init__.py +++ b/filenuke/__init__.py @@ -3,13 +3,20 @@ import sys import os -from . import nuke +try: + from . import nuke +except ImportError: + import nuke if __name__ == "__main__": try: path_to_del = sys.argv[1] except IndexError: sys.exit(1) + if not os.path.exists(path_to_del): + sys.stderr.write(f'Path {path_to_del} does not exist\n') + sys.stderr.flush() + sys.exit(2) if os.path.isfile(path_to_del): nuke.clean(path_to_del) else: diff --git a/filenuke/nuke.py b/filenuke/nuke.py index 557f56b..66e06c5 100644 --- a/filenuke/nuke.py +++ b/filenuke/nuke.py @@ -6,6 +6,8 @@ from typing import NewType NewPath = NewType('NewPath', str) +DEFAULT_PASSES = 2 + def _overwrite_file(file_path: str, passes: int, zeros=False): """overwrite a single file with secure random data passes times @@ -20,7 +22,6 @@ def _overwrite_file(file_path: str, passes: int, zeros=False): with open(file_path, 'wb') as top_secret_file: top_secret_file.write(new_data) - print('wrote to', file_path) def _rename_inode(path_in: str, zeros=False) -> NewPath: @@ -28,6 +29,9 @@ def _rename_inode(path_in: str, zeros=False) -> NewPath: name_len = len(os.path.basename(path_in)) new_name = '' path = os.path.abspath(path_in) + dir_n = os.path.dirname(path_in) + if dir_n: + dir_n += '/' if zeros: new_name = '0' * name_len @@ -35,15 +39,15 @@ def _rename_inode(path_in: str, zeros=False) -> NewPath: for _ in range(name_len): new_name += secrets.choice(string.ascii_letters) - os.rename(path, new_name) + os.rename(path, dir_n + new_name) - return new_name + return dir_n + new_name -def _nuke(path: str): +def _nuke(path: str, passes: int = DEFAULT_PASSES): """properly delete inode""" if os.path.isfile(path): - _overwrite_file(path, 1) + _overwrite_file(path, passes) new_path = _rename_inode(path) os.remove(new_path) else: @@ -51,15 +55,14 @@ def _nuke(path: str): shutil.rmtree(new_path) -def clean_tree(directory: str): +def clean_tree(directory: str, passes: int = DEFAULT_PASSES): """securely delete dir tree""" - for root, dirs, files in os.walk(directory, topdown=False): for name in files: _nuke(os.path.join(root, name)) for name in dirs: _nuke(os.path.join(root, name)) - _nuke(root) + _nuke(directory) def clean(path: str): diff --git a/requirements.in b/requirements.in deleted file mode 100644 index e69de29..0000000 diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 2399115..0000000 --- a/requirements.txt +++ /dev/null @@ -1,6 +0,0 @@ -# -# This file is autogenerated by pip-compile -# To update, run: -# -# pip-compile --generate-hashes --output-file=requirements.txt requirements.in -#