106 lines
3.1 KiB
Python
106 lines
3.1 KiB
Python
#!/usr/bin/env python
|
|
|
|
"""
|
|
dump the dictionary generated from findings into a yaml file for later inspection
|
|
"""
|
|
|
|
import os
|
|
import yaml
|
|
from tqdm import tqdm
|
|
|
|
### Local Imports
|
|
from _hashing import xx_hash
|
|
|
|
def check_log_dir(d):
|
|
create_folder(d)
|
|
|
|
def dump_yaml(dictionary,file):
|
|
""" dump dictionary to yaml file """
|
|
with open(file, 'w', encoding="utf-8") as f:
|
|
yaml.dump(dictionary, f)
|
|
f.close()
|
|
|
|
def path_exists(p):
|
|
""" determine if the path exists """
|
|
pe = os.path.exists(p)
|
|
return pe
|
|
|
|
def is_dir(d):
|
|
""" determine if object is a dir or not """
|
|
return os.path.isdir(d)
|
|
|
|
def is_file(f):
|
|
""" determine if object is a file or not """
|
|
return os.path.isfile(f)
|
|
|
|
def cmp_files(f1,f2):
|
|
""" compare two files """
|
|
#TODO: Determine if path is actually a file
|
|
#TODO: Determine if the hash has already been stored and use it if so
|
|
|
|
hash1 = xx_hash(f1)
|
|
hash2 = xx_hash(f2)
|
|
return hash1 == hash2
|
|
|
|
def path_access_read(path):
|
|
""" make sure we can read from the path """
|
|
val = os.access(path, os.R_OK)
|
|
|
|
if val is False:
|
|
print(f'path_access_read check: cannot read from {path}')
|
|
return val
|
|
|
|
def path_access_write(path):
|
|
""" make sure we can write to the path """
|
|
val = os.access(path, os.W_OK)
|
|
|
|
if val is False:
|
|
print(f'path_access_write check: cannot write to {path}')
|
|
return val
|
|
|
|
def create_folder(file):
|
|
""" Function to create folder """
|
|
if path_exists(file) is False:
|
|
os.makedirs(file)
|
|
elif is_dir(file) is False:
|
|
pass # this needs to turn into bailing out as there is a collision.
|
|
|
|
def cleanup_sd(f,config):
|
|
""" If we should clean up the SD, nuke the copied files. """
|
|
if config['cleanup_sd'] is True:
|
|
os.system('clear')
|
|
for file in tqdm(f, desc = "Cleaning Up SD:"):
|
|
if f[file]['source_cleanable'] is True:
|
|
print(f"Cleanup SD: Removing File -\n{os.path.join(f[file]['folders']['source_path'],f[file]['name'])}")
|
|
os.remove(os.path.join(f[file]['folders']['source_path'],f[file]['name']))
|
|
else:
|
|
print(f"Cleanup SD: Not Removing File -\n{os.path.join(f[file]['folders']['source_path'],f[file]['name'])}")
|
|
|
|
def validate_config_dir_access(config):
|
|
""" Validate we can operate in the defined directories """
|
|
check = path_access_write(config['folders']['destination']['base'])
|
|
if check is False:
|
|
accessible = False
|
|
else:
|
|
check = path_access_read(config['folders']['source']['base'])
|
|
if check is False:
|
|
accessible = False
|
|
else:
|
|
if config['store_backup'] is True:
|
|
check = path_access_write(config['folders']['backup'])
|
|
if check is False:
|
|
accessible = False
|
|
else:
|
|
accessible = True
|
|
else:
|
|
accessible = True
|
|
return accessible
|
|
|
|
def get_file_name(path_file_name):
|
|
return os.path.basename(path_file_name)
|
|
|
|
def get_dotted_file_ext(file_name):
|
|
return os.path.splitext(file_name)[1]
|
|
|
|
def get_file_ext(file_name):
|
|
return get_dotted_file_ext(file_name).split('.')[1] |