BitMover/_file_stuff.py

129 lines
3.4 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 configure import Configure, CONFIG_FILE
from _hashing import xx_hash
from _lumberjack import timber
def check_log_dir(d):
create_folder(d)
# c = Configure(CONFIG_FILE)
# config = c.load_config()
log = timber(__name__)
log.info("Starting")
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 """
log.debug(f'path_exists({p})')
pe = os.path.exists(p)
# print(f'Path Exists: {pe}')
return pe
def is_dir(d):
""" determine if object is a dir or not """
log.debug(f'is_dir({d})')
return os.path.isdir(d)
def is_file(f):
""" determine if object is a file or not """
log.debug(f'is_file({f})')
return os.path.isfile(f)
def cmp_files(f1,f2):
""" compare two files """
log.debug(f'cmp_files({f1},{f2})')
#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 """
log.debug(f'path_access_read({path})')
val = os.access(path, os.R_OK)
if val is False:
log.error(f'Can not read from {path}')
return val
def path_access_write(path):
""" make sure we can write to the path """
log.debug(f'path_access_write({path})')
val = os.access(path, os.W_OK)
if val is False:
log.error(f'Can not write to {path}')
return val
def create_folder(file):
""" Function to create folder """
log.debug(f'create_folder({file})')
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. """
log.debug(f'cleanup_sd({f})')
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:
log.debug(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:
log.debug(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 """
log.debug('validate_config_dir_access')
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