+ add CLI command support

* fix inode rename bug
- removed pointless req files
This commit is contained in:
Kevin Froman 2019-12-02 22:30:25 -06:00
parent b17f6656d4
commit 073980f17b
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
4 changed files with 19 additions and 15 deletions

View File

@ -3,13 +3,20 @@
import sys import sys
import os import os
try:
from . import nuke from . import nuke
except ImportError:
import nuke
if __name__ == "__main__": if __name__ == "__main__":
try: try:
path_to_del = sys.argv[1] path_to_del = sys.argv[1]
except IndexError: except IndexError:
sys.exit(1) 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): if os.path.isfile(path_to_del):
nuke.clean(path_to_del) nuke.clean(path_to_del)
else: else:

View File

@ -6,6 +6,8 @@ from typing import NewType
NewPath = NewType('NewPath', str) NewPath = NewType('NewPath', str)
DEFAULT_PASSES = 2
def _overwrite_file(file_path: str, passes: int, zeros=False): def _overwrite_file(file_path: str, passes: int, zeros=False):
"""overwrite a single file with secure random data passes times """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: with open(file_path, 'wb') as top_secret_file:
top_secret_file.write(new_data) top_secret_file.write(new_data)
print('wrote to', file_path)
def _rename_inode(path_in: str, zeros=False) -> NewPath: 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)) name_len = len(os.path.basename(path_in))
new_name = '' new_name = ''
path = os.path.abspath(path_in) path = os.path.abspath(path_in)
dir_n = os.path.dirname(path_in)
if dir_n:
dir_n += '/'
if zeros: if zeros:
new_name = '0' * name_len new_name = '0' * name_len
@ -35,15 +39,15 @@ def _rename_inode(path_in: str, zeros=False) -> NewPath:
for _ in range(name_len): for _ in range(name_len):
new_name += secrets.choice(string.ascii_letters) 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""" """properly delete inode"""
if os.path.isfile(path): if os.path.isfile(path):
_overwrite_file(path, 1) _overwrite_file(path, passes)
new_path = _rename_inode(path) new_path = _rename_inode(path)
os.remove(new_path) os.remove(new_path)
else: else:
@ -51,15 +55,14 @@ def _nuke(path: str):
shutil.rmtree(new_path) shutil.rmtree(new_path)
def clean_tree(directory: str): def clean_tree(directory: str, passes: int = DEFAULT_PASSES):
"""securely delete dir tree""" """securely delete dir tree"""
for root, dirs, files in os.walk(directory, topdown=False): for root, dirs, files in os.walk(directory, topdown=False):
for name in files: for name in files:
_nuke(os.path.join(root, name)) _nuke(os.path.join(root, name))
for name in dirs: for name in dirs:
_nuke(os.path.join(root, name)) _nuke(os.path.join(root, name))
_nuke(root) _nuke(directory)
def clean(path: str): def clean(path: str):

View File

View File

@ -1,6 +0,0 @@
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --generate-hashes --output-file=requirements.txt requirements.in
#