projects-jenz/file_mover/remote_local_dir.py
2021-10-30 23:52:37 +02:00

56 lines
2.0 KiB
Python
Executable File

from pathlib import Path
import contextlib
import os
from datetime import timedelta, datetime
from zipfile import ZipFile
class local_dir_remote:
def __init__(self, config):
self.config = config
self.path = config["path"]
def list_dir(self, recursive_search = False):
path = Path(self.path)
file_list = []
delta = timedelta(minutes=2)
glob_pattern = '*'
glob_pattern_recursive = '**'
if not recursive_search:
for file_demo in list(path.glob(glob_pattern)):
mtime = datetime.fromtimestamp(os.stat(file_demo).st_mtime)
if mtime < datetime.now() - delta:
file_list.append(file_demo)
else:
for file_directory in list(path.glob(glob_pattern_recursive)):
try:
for files in list(file_directory.glob(glob_pattern)):
if '/maps' in str(files):
continue
file_list.append(files)
except PermissionError:
continue
return file_list
def zip_local_directory(self, job):
source_files = self.list_dir(True)
zipname = f'{job["zipname"]}-{str(datetime.now()).split(" ")[0]}.zip'
with ZipFile(zipname, 'w') as myzip:
for files in source_files:
try:
myzip.write(files)
except PermissionError:
continue
return str(Path.cwd()) + '/' + zipname
def delete_local_demo(self, pathfile):
with contextlib.suppress(FileNotFoundError):
if str(pathfile).endswith('.dem'):
os.remove(pathfile)
print('removed demo: ', pathfile)
def delete_local_zip(self, zip_file_path):
with contextlib.suppress(FileNotFoundError):
if (str(zip_file_path).endswith('.zip')):
os.remove(zip_file_path)
print('removed zip: ', zip_file_path)