use commandline-parameter

This commit is contained in:
Jakobus Schürz 2020-02-12 16:28:31 +01:00
parent 34de25cdb3
commit 3f8f32e658

View file

@ -11,6 +11,8 @@ import time
import fileinput import fileinput
import argparse
try: try:
from fuse import FUSE, FuseOSError, Operations from fuse import FUSE, FuseOSError, Operations
except: except:
@ -22,23 +24,20 @@ except:
class WorkdirFS(Operations): class WorkdirFS(Operations):
def __init__(self, root): def __init__(self, args):
self.root = root self.args = args
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) today = datetime.now() - timedelta(hours=self.args.timeoffset)
if self.yearlydir: if self.args.yearlydir:
path = os.path.join(self.root,"workdir",today.strftime("%Y")) path = os.path.join(self.args.archive,"workdir",today.strftime("%Y"))
if self.monthlydir: if self.args.monthlydir:
path = os.path.join(path, today.strftime("%m")) path = os.path.join(path, today.strftime("%m"))
else: else:
path = os.path.join(self.root, "workdir") path = os.path.join(self.args.archive, "workdir")
if partial.startswith("/"): if partial.startswith("/"):
partial = partial[1:] partial = partial[1:]
@ -88,7 +87,7 @@ class WorkdirFS(Operations):
pathname = os.readlink(self._full_path(path)) pathname = os.readlink(self._full_path(path))
if pathname.startswith("/"): if pathname.startswith("/"):
# Path name is absolute, sanitize it. # Path name is absolute, sanitize it.
return os.path.relpath(pathname, self.root) return os.path.relpath(pathname, self.args.archive)
else: else:
return pathname return pathname
@ -165,6 +164,7 @@ class WorkdirFS(Operations):
return self.flush(path, fh) return self.flush(path, fh)
def cleanup_dirs(root): def cleanup_dirs(root):
today = datetime.now() - timedelta(hours=2) today = datetime.now() - timedelta(hours=2)
today = today.strftime("%Y-%m-%d") today = today.strftime("%Y-%m-%d")
@ -172,7 +172,8 @@ def cleanup_dirs(root):
for _dir in dirs: for _dir in dirs:
print("cleanup",os.path.join(root, _dir)) print("cleanup",os.path.join(root, _dir))
if not _dir == today and not os.listdir(os.path.join(root, _dir)): if not _dir == today and not os.listdir(os.path.join(root, _dir)):
print("Directory is empty -> remove it", print("Directory is empty -> remove it (not now implemented for
testpurpose)",
os.path.join(root, _dir)) os.path.join(root, _dir))
@ -186,11 +187,11 @@ def check_dir(path):
print("[-] Makedir error") print("[-] Makedir error")
return path return path
def main(root, mountpoint): def main(args):
#FUSE(WorkdirFS(root), mountpoint, nothreads=True, foreground=True) #FUSE(WorkdirFS(root), mountpoint, nothreads=True, foreground=True)
check_dir(root) check_dir(args.archive)
check_dir(mountpoint) check_dir(args.mountpoint)
cleanup_dirs(root) cleanup_dirs(args.archive)
# first search if configuration exists for xdg-userdirs # first search if configuration exists for xdg-userdirs
# to use it with alias gowork and goarchive # to use it with alias gowork and goarchive
foundarchive=False foundarchive=False
@ -199,37 +200,38 @@ def main(root, mountpoint):
inplace=True) as fh: inplace=True) as fh:
for line in fh: for line in fh:
if line.startswith('XDG_ARCHIVE_DIR'): if line.startswith('XDG_ARCHIVE_DIR'):
print("XDG_ARCHIVE_DIR=\""+root+'"\n') print("XDG_ARCHIVE_DIR=\""+args.archive+'"\n')
foundarchive=True foundarchive=True
elif line.startswith('XDG_WORK_DIR'): elif line.startswith('XDG_WORK_DIR'):
print("XDG_WORK_DIR=\""+mountpoint+'"\n') print("XDG_WORK_DIR=\""+args.mountpoint+'"\n')
foundwork=True foundwork=True
else: else:
print(line, end='') print(line, end='')
if not foundarchive: if not foundarchive:
with open(os.environ['HOME']+'/.config/user-dirs.dirs', 'a') as fh: with open(os.environ['HOME']+'/.config/user-dirs.dirs', 'a') as fh:
fh.write("XDG_ARCHIVE_DIR=\""+root+'"\n') fh.write("XDG_ARCHIVE_DIR=\""+args.archive+'"\n')
if not foundwork: if not foundwork:
with open(os.environ['HOME']+'/.config/user-dirs.dirs', 'a') as fh: with open(os.environ['HOME']+'/.config/user-dirs.dirs', 'a') as fh:
fh.write("XDG_WORK_DIR=\""+mountpoint+'"\n') fh.write("XDG_WORK_DIR=\""+args.mountpoint+'"\n')
FUSE(WorkdirFS(root), mountpoint, nothreads=True, foreground=True) # start FUSE filesystem
FUSE(WorkdirFS(args), args.mountpoint, nothreads=True, foreground=True)
if __name__ == '__main__': if __name__ == '__main__':
#main(sys.argv[2], sys.argv[1]) #main(sys.argv[2], sys.argv[1])
root = os.environ['HOME']+'/archive' parser = argparse.ArgumentParser()
mountpoint = os.environ['HOME']+'/Work' parser.add_argument("-a", "--archive",
print(len(sys.argv)) default=os.environ['HOME']+'/archive', help="Path to archivedir-base")
print(sys.argv) parser.add_argument("-m", "--mountpoint",
if len(sys.argv) == 1: default=os.environ['HOME']+'/Work', help='Path to Workdir')
print("use default locations") parser.add_argument("-t", "--timeoffset", type=int, default=2, help="""If you're working
elif len(sys.argv) == 2: all day till 3 o'clock in the morning, set it to 4, so next day
mountpoint = sys.argv[1] archive-dir will be created 4 hours after midnight. You have 1h
elif len(sys.argv) == 3: tolerance, if you're working one day a little bit longer""")
mountpoint = sys.argv[2] parser.add_argument("-y", "--yearlydir", action="store_false")
root = sys.argv[1] parser.add_argument("-M", "--monthlydir", action="store_false")
else: args = parser.parse_args()
print("not correct count of arguments") print(args)
raise #root = os.environ['HOME']+'/archive'
print(root, mountpoint) #mountpoint = os.environ['HOME']+'/Work'
main(root, mountpoint) main(args)