add cleanup function, some fixes

This commit is contained in:
Jakobus Schürz 2020-02-12 00:19:51 +01:00
parent 2cc6f83fb9
commit 84e7a8b3d7

View file

@ -9,6 +9,8 @@ import errno
from datetime import datetime, timedelta from datetime import datetime, timedelta
import time import time
import fileinput
try: try:
from fuse import FUSE, FuseOSError, Operations from fuse import FUSE, FuseOSError, Operations
except: except:
@ -23,18 +25,25 @@ class Passthrough(Operations):
def __init__(self, root): def __init__(self, root):
self.root = root self.root = root
self.timeoffset = 2 #hours self.timeoffset = 2 #hours
self.yearlydir = True
self.monthlydir = True
# Helpers # Helpers
# ======= # =======
def _full_path(self, partial): def _full_path(self, partial):
today = datetime.now() - timedelta(hours=self.timeoffset)
if self.yearlydir:
path = os.path.join(self.root,"workdir",today.strftime("%Y"))
if self.monthlydir:
path = os.path.join(path, today.strftime("%m"))
else:
path = os.path.join(self.root, "workdir")
if partial.startswith("/"): if partial.startswith("/"):
partial = partial[1:] partial = partial[1:]
today = datetime.now() - timedelta(hours=self.timeoffset)
path = os.path.join(check_dir(os.path.join(self.root, path = os.path.join(check_dir(os.path.join(path, today.strftime("%Y-%m-%d"))), partial)
"workdir",
today.strftime("%Y-%m-%d"))),
partial)
return path return path
@ -156,36 +165,53 @@ class Passthrough(Operations):
return self.flush(path, fh) return self.flush(path, fh)
def cleanup_dirs(root): def cleanup_dirs(root):
for d in os.listdir(root): today = datetime.now() - timedelta(hours=2)
if not os.listdir(os.path.join(root, d)): today = today.strftime("%Y-%m-%d")
print("Directory is empty -> remove it",
os.listdir(os.path.join(root, d))) for root, dirs, files in os.walk(root, topdown=False):
for _dir in dirs:
print("cleanup",os.path.join(root, _dir))
if not _dir == today and not os.listdir(os.path.join(root, _dir)):
print("Directory is empty -> remove it",
os.path.join(root, _dir))
def check_dir(path, cleanup=False): def check_dir(path):
checkdir = os.path.isdir(path) checkdir = os.path.isdir(path)
if not checkdir: if not checkdir:
try: try:
os.makedirs(path, exist_ok=True) os.makedirs(path, exist_ok=True)
print("Created directory %s", (path), flush=True) print("Created directory {}".format(path), flush=True)
if cleanup:
cleanup_dirs(path)
except: except:
print("[-] Makedir error") print("[-] Makedir error")
return path return path
def main(root, mountpoint): def main(root, mountpoint):
#FUSE(Passthrough(root), mountpoint, nothreads=True, foreground=True) #FUSE(Passthrough(root), mountpoint, nothreads=True, foreground=True)
check_dir(root)
check_dir(mountpoint) check_dir(mountpoint)
with fileinput.input(os.environ['HOME']+'/.config/user-dirs.dirs.test', cleanup_dirs(root)
# first search if configuration exists for xdg-userdirs
# to use it with alias gowork and goarchive
foundarchive=False
foundwork=False
with fileinput.input(os.environ['HOME']+'/.config/user-dirs.dirs',
inplace=True) as fh: inplace=True) as fh:
for line in fh: for line in fh:
if line.startswith('XDG_WORK_DIR'): if line.startswith('XDG_ARCHIVE_DIR'):
print("XDG_WORK_DIR=$HOME/"+mountpoint, end='') print("XDG_ARCHIVE_DIR=\""+root+'"')
break foundarchive=True
else: elif line.startswith('XDG_WORK_DIR'):
print("XDG_WORK_DIR=$HOME/"+mountpoint, end='') print("XDG_WORK_DIR=\""+mountpoint+'"')
foundwork=True
else:
print(line, end='')
if not foundarchive:
with open(os.environ['HOME']+'/.config/user-dirs.dirs', 'a') as fh:
fh.write("XDG_ARCHIVE_DIR=\""+root+'"')
if not foundwork:
with open(os.environ['HOME']+'/.config/user-dirs.dirs', 'a') as fh:
fh.write("XDG_WORK_DIR=\""+mountpoint+'"')
FUSE(Passthrough(root), mountpoint, nothreads=True, foreground=True) FUSE(Passthrough(root), mountpoint, nothreads=True, foreground=True)