workdirfs/workdirfs.py

330 lines
12 KiB
Python
Raw Normal View History

2020-02-11 15:14:26 +01:00
#!/usr/bin/env -S python3 -u
2016-04-11 00:40:01 +02:00
from __future__ import with_statement
import os
import sys
import errno
2020-02-11 15:14:26 +01:00
from datetime import datetime, timedelta
import time
2020-02-12 00:19:51 +01:00
import fileinput
2020-02-12 16:28:31 +01:00
import argparse
2020-02-14 15:48:51 +01:00
import gzip
import shutil
2020-02-12 16:28:31 +01:00
2020-02-11 15:14:26 +01:00
try:
from fuse import FUSE, FuseOSError, Operations
except:
try:
from fusepy import FUSE, FuseOSError, Operations
except:
print("please install fusepy")
2020-02-12 19:20:02 +01:00
raise errno.ModuleNotFoundError
2016-04-11 00:40:01 +02:00
2020-02-12 01:27:35 +01:00
class WorkdirFS(Operations):
2020-02-12 16:28:31 +01:00
def __init__(self, args):
self.args = args
2020-02-12 20:23:27 +01:00
self.today = datetime.now() - timedelta(hours=self.args.timeoffset)
2020-02-13 10:12:53 +01:00
self.yesterday = datetime.now() - timedelta(hours=self.args.timeoffset)
2020-02-14 15:48:51 +01:00
self.confdir = os.path.join(os.environ['HOME'], '.local', 'workdirfs')
if os.path.exists(os.path.join(self.confdir, 'yesterpath')):
with open(os.path.join(self.confdir, 'yesterpath'), 'r') as fh:
self.yesterpath = fh.readline().strip()
else:
self.yesterpath = os.path.join(self.args.archive, 'workdir', self.yesterday.strftime("%Y-%m-%d"))
if not os.path.isdir(self.confdir):
os.mkdir(self.confdir)
with open(os.path.join(self.confdir, 'yesterpath'), 'w') as fh:
fh.write(self.yesterpath)
print("initial yesterpath is {}".format(self.yesterpath))
2016-04-11 00:40:01 +02:00
# Helpers
# =======
def _full_path(self, partial):
2020-02-12 20:23:27 +01:00
self.today = datetime.now() - timedelta(hours=self.args.timeoffset)
2020-02-12 16:28:31 +01:00
if self.args.yearlydir:
2020-02-12 20:23:27 +01:00
path = os.path.join(os.environ['HOME'],
self.args.archive,"workdir",self.today.strftime("%Y"))
2020-02-12 16:28:31 +01:00
if self.args.monthlydir:
2020-02-12 20:23:27 +01:00
path = os.path.join(path, self.today.strftime("%m"))
2020-02-12 00:19:51 +01:00
else:
2020-02-12 18:18:19 +01:00
path = os.path.join(os.environ['HOME'], self.args.archive, "workdir")
2020-02-12 00:19:51 +01:00
2016-04-11 00:40:01 +02:00
if partial.startswith("/"):
partial = partial[1:]
2020-02-12 00:19:51 +01:00
2020-02-14 15:48:51 +01:00
with open(os.path.join(self.confdir, 'yesterpath'), 'w') as fh:
fh.write(self.yesterpath)
2020-02-13 10:12:53 +01:00
path = os.path.join(check_dir(
os.path.join(path, self.today.strftime("%Y-%m-%d")),
2020-02-14 15:48:51 +01:00
self.yesterpath),
2020-02-13 10:12:53 +01:00
partial
)
if self.today > self.yesterday:
self.yesterday = self.today
2020-02-11 15:14:26 +01:00
2016-04-11 00:40:01 +02:00
return path
# Filesystem methods
# ==================
def access(self, path, mode):
full_path = self._full_path(path)
if not os.access(full_path, mode):
raise FuseOSError(errno.EACCES)
def chmod(self, path, mode):
full_path = self._full_path(path)
return os.chmod(full_path, mode)
def chown(self, path, uid, gid):
full_path = self._full_path(path)
return os.chown(full_path, uid, gid)
def getattr(self, path, fh=None):
full_path = self._full_path(path)
st = os.lstat(full_path)
2020-02-11 15:14:26 +01:00
return dict((key, getattr(st, key)) for key in ('st_atime',
'st_ctime',
'st_gid',
'st_mode',
'st_mtime',
'st_nlink',
'st_size',
'st_uid'))
2016-04-11 00:40:01 +02:00
def readdir(self, path, fh):
full_path = self._full_path(path)
dirents = ['.', '..']
if os.path.isdir(full_path):
dirents.extend(os.listdir(full_path))
for r in dirents:
yield r
def readlink(self, path):
pathname = os.readlink(self._full_path(path))
if pathname.startswith("/"):
# Path name is absolute, sanitize it.
2020-02-12 18:18:19 +01:00
return os.path.relpath(pathname, os.path.join(os.environ['HOME'],
self.args.archive))
2016-04-11 00:40:01 +02:00
else:
return pathname
def mknod(self, path, mode, dev):
return os.mknod(self._full_path(path), mode, dev)
def rmdir(self, path):
full_path = self._full_path(path)
return os.rmdir(full_path)
def mkdir(self, path, mode):
return os.mkdir(self._full_path(path), mode)
def statfs(self, path):
full_path = self._full_path(path)
stv = os.statvfs(full_path)
2020-02-11 15:14:26 +01:00
return dict((key, getattr(stv, key)) for key in ('f_bavail',
'f_bfree',
'f_blocks',
'f_bsize',
'f_favail',
'f_ffree',
'f_files',
'f_flag',
'f_frsize',
'f_namemax'))
2016-04-11 00:40:01 +02:00
def unlink(self, path):
return os.unlink(self._full_path(path))
def symlink(self, name, target):
return os.symlink(target, self._full_path(name))
2016-04-11 00:40:01 +02:00
def rename(self, old, new):
return os.rename(self._full_path(old), self._full_path(new))
def link(self, target, name):
return os.link(self._full_path(name), self._full_path(target))
2016-04-11 00:40:01 +02:00
def utimens(self, path, times=None):
return os.utime(self._full_path(path), times)
# File methods
# ============
def open(self, path, flags):
full_path = self._full_path(path)
return os.open(full_path, flags)
def create(self, path, mode, fi=None):
full_path = self._full_path(path)
return os.open(full_path, os.O_WRONLY | os.O_CREAT, mode)
def read(self, path, length, offset, fh):
os.lseek(fh, offset, os.SEEK_SET)
return os.read(fh, length)
def write(self, path, buf, offset, fh):
os.lseek(fh, offset, os.SEEK_SET)
return os.write(fh, buf)
def truncate(self, path, length, fh=None):
full_path = self._full_path(path)
with open(full_path, 'r+') as f:
f.truncate(length)
def flush(self, path, fh):
return os.fsync(fh)
def release(self, path, fh):
return os.close(fh)
def fsync(self, path, fdatasync, fh):
return self.flush(path, fh)
2020-02-11 15:14:26 +01:00
def cleanup_dirs(root):
2020-02-12 16:28:31 +01:00
2020-02-12 00:19:51 +01:00
today = datetime.now() - timedelta(hours=2)
today = today.strftime("%Y-%m-%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)):
2020-02-12 17:16:53 +01:00
print("""Directory is empty -> remove it (not now implemented for
testpurpose)""",
2020-02-12 00:19:51 +01:00
os.path.join(root, _dir))
2020-02-11 15:14:26 +01:00
2020-02-13 10:12:53 +01:00
def check_dir(path, yesterpath=None):
2020-02-14 15:48:51 +01:00
if not os.path.exists(path) and not os.path.isdir(path):
#try:
2020-02-11 15:14:26 +01:00
os.makedirs(path, exist_ok=True)
2020-02-12 00:19:51 +01:00
print("Created directory {}".format(path), flush=True)
2020-02-14 15:48:51 +01:00
print("yesterpath: {} todaypath: {}".format(yesterpath, path))
if yesterpath != None:
if path != yesterpath:
_zipfiles(yesterpath)
#Create .yesterpath in archive with yesterpath inside
wdconfigdir = os.path.join(os.environ['HOME'], '.local', 'workdirfs')
#with open(os.path.join(wdconfigdir, 'yesterpath'), 'w+') as f:
# f.write(path)
f = open(os.path.join(wdconfigdir, 'yesterpath'), 'w+')
f.write(path)
f.flush()
f.close()
print("update yesterpath")
#except Exception as e:
# print("[-] Error while check dir and zip files: ", e)
2020-02-11 15:14:26 +01:00
return path
2020-02-14 08:16:05 +01:00
def _zipfiles(path):
2020-02-13 10:12:53 +01:00
print("Zip files in yesterdays archivdir {}".format(path))
2020-02-14 15:48:51 +01:00
zip_fileext=".gz"
#zip_compression=zipfile.ZIP_DEFLATED
2020-02-13 15:28:45 +01:00
zip_compressionlevel=5
2020-02-13 10:12:53 +01:00
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
for file in f:
2020-02-14 15:48:51 +01:00
if zip_fileext not in file:
2020-02-13 10:12:53 +01:00
files.append(os.path.join(r, file))
for f in files:
2020-02-13 15:28:45 +01:00
print("file to zip: {} -> {}".format(os.path.basename(f), f+'.bzip'))
try:
2020-02-14 15:48:51 +01:00
with open(f, 'rb') as f_in:
with gzip.open(
f+zip_fileext,
'wb',
compresslevel=zip_compressionlevel) as f_out:
shutil.copyfileobj(f_in, f_out)
# with ZipFile(
# f+zip_fileext,
# 'w',
# allowZip64=True,
# compression=zip_compression,
# compresslevel=zip_compressionlevel
# ) as zf:
# zf.write(f, os.path.basename(f))
2020-02-13 15:28:45 +01:00
except Exception as e:
print("Error during zipping file {}".format(f), e)
else:
os.remove(f)
2020-02-13 10:12:53 +01:00
2020-02-12 16:28:31 +01:00
def main(args):
2020-02-12 01:27:35 +01:00
#FUSE(WorkdirFS(root), mountpoint, nothreads=True, foreground=True)
2020-02-12 18:18:19 +01:00
check_dir(os.path.join(os.environ['HOME'], args.mountpoint))
2020-02-14 15:48:51 +01:00
if args.archive[:0] == "/":
# Archive-Path is absolute path -> use it as is
check_dir(args.archive)
cleanup_dirs(args.archive)
xdg_entry = '"' + args.archive + '"'
else:
# Archive-path is relative, so use is as relative path from users
# homedir
check_dir(os.path.join(os.environ['HOME'], args.archive))
cleanup_dirs(os.path.join(os.environ['HOME'], args.archive))
xdg_entry = '"$HOME/' + args.archive + '"'
2020-02-12 00:19:51 +01:00
# first search if configuration exists for xdg-userdirs
# to use it with alias gowork and goarchive
foundarchive=False
foundwork=False
try:
with fileinput.input(os.environ['HOME']+'/.config/user-dirs.dirs',
inplace=True) as fh:
for line in fh:
if line.startswith('XDG_ARCHIVE_DIR'):
2020-02-14 15:48:51 +01:00
print("XDG_ARCHIVE_DIR=" + xdg_entry, end='\n')
foundarchive=True
elif line.startswith('XDG_WORK_DIR'):
print("XDG_WORK_DIR=\"$HOME/"+args.mountpoint+'"', end='\n')
foundwork=True
else:
print(line, end='')
except:
print("File not existing, create it: {}".format(os.environ['HOME']+'/.config/user-dirs.dirs'))
2020-02-12 00:19:51 +01:00
if not foundarchive:
2020-02-12 00:29:40 +01:00
with open(os.environ['HOME']+'/.config/user-dirs.dirs', 'a') as fh:
2020-02-14 15:48:51 +01:00
fh.write("XDG_ARCHIVE_DIR=" + xdg_entry + '\n')
2020-02-12 00:19:51 +01:00
if not foundwork:
2020-02-12 00:29:40 +01:00
with open(os.environ['HOME']+'/.config/user-dirs.dirs', 'a') as fh:
2020-02-14 15:48:51 +01:00
fh.write('XDG_WORK_DIR=\"$HOME/' + args.mountpoint + '"\n')
2020-02-11 17:56:42 +01:00
2020-02-12 16:28:31 +01:00
# start FUSE filesystem
2020-02-12 18:18:19 +01:00
FUSE(WorkdirFS(args), os.path.join(os.environ['HOME'], args.mountpoint), nothreads=True, foreground=True)
2016-04-11 00:40:01 +02:00
if __name__ == '__main__':
2020-02-11 15:14:26 +01:00
#main(sys.argv[2], sys.argv[1])
2020-02-12 16:28:31 +01:00
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--archive",
2020-02-12 18:18:19 +01:00
default='archive', help="Path to archivedir-base")
2020-02-12 16:28:31 +01:00
parser.add_argument("-m", "--mountpoint",
2020-02-12 18:18:19 +01:00
default='Work', help='Path to Workdir')
2020-02-12 16:28:31 +01:00
parser.add_argument("-t", "--timeoffset", type=int, default=2, help="""If you're working
all day till 3 o'clock in the morning, set it to 4, so next day
archive-dir will be created 4 hours after midnight. You have 1h
tolerance, if you're working one day a little bit longer""")
parser.add_argument("-y", "--yearlydir", action="store_true")
parser.add_argument("-M", "--monthlydir", action="store_true")
2020-02-12 16:28:31 +01:00
args = parser.parse_args()
print(args)
#root = os.environ['HOME']+'/archive'
#mountpoint = os.environ['HOME']+'/Work'
main(args)