store yesterpath persistent
This commit is contained in:
parent
5a2d76280c
commit
73a6f034a5
1 changed files with 67 additions and 26 deletions
93
workdirfs.py
93
workdirfs.py
|
@ -12,7 +12,8 @@ import time
|
||||||
import fileinput
|
import fileinput
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import zipfile
|
import gzip
|
||||||
|
import shutil
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from fuse import FUSE, FuseOSError, Operations
|
from fuse import FUSE, FuseOSError, Operations
|
||||||
|
@ -29,6 +30,18 @@ class WorkdirFS(Operations):
|
||||||
self.args = args
|
self.args = args
|
||||||
self.today = datetime.now() - timedelta(hours=self.args.timeoffset)
|
self.today = datetime.now() - timedelta(hours=self.args.timeoffset)
|
||||||
self.yesterday = datetime.now() - timedelta(hours=self.args.timeoffset)
|
self.yesterday = datetime.now() - timedelta(hours=self.args.timeoffset)
|
||||||
|
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))
|
||||||
|
|
||||||
# Helpers
|
# Helpers
|
||||||
# =======
|
# =======
|
||||||
|
@ -46,10 +59,12 @@ class WorkdirFS(Operations):
|
||||||
if partial.startswith("/"):
|
if partial.startswith("/"):
|
||||||
partial = partial[1:]
|
partial = partial[1:]
|
||||||
|
|
||||||
|
with open(os.path.join(self.confdir, 'yesterpath'), 'w') as fh:
|
||||||
|
fh.write(self.yesterpath)
|
||||||
|
|
||||||
path = os.path.join(check_dir(
|
path = os.path.join(check_dir(
|
||||||
os.path.join(path, self.today.strftime("%Y-%m-%d")),
|
os.path.join(path, self.today.strftime("%Y-%m-%d")),
|
||||||
os.path.join(path, self.yesterday.strftime("%Y-%m-%d"))
|
self.yesterpath),
|
||||||
),
|
|
||||||
partial
|
partial
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -191,41 +206,58 @@ def cleanup_dirs(root):
|
||||||
|
|
||||||
|
|
||||||
def check_dir(path, yesterpath=None):
|
def check_dir(path, yesterpath=None):
|
||||||
checkdir = os.path.isdir(path)
|
if not os.path.exists(path) and not os.path.isdir(path):
|
||||||
if not checkdir:
|
#try:
|
||||||
try:
|
|
||||||
os.makedirs(path, exist_ok=True)
|
os.makedirs(path, exist_ok=True)
|
||||||
print("Created directory {}".format(path), flush=True)
|
print("Created directory {}".format(path), flush=True)
|
||||||
if yesterpath != None and path != yesterpath:
|
print("yesterpath: {} todaypath: {}".format(yesterpath, path))
|
||||||
_zipfiles(yesterpath)
|
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:
|
#except Exception as e:
|
||||||
print("[-] Makedir error")
|
# print("[-] Error while check dir and zip files: ", e)
|
||||||
return path
|
return path
|
||||||
|
|
||||||
def _zipfiles(path):
|
def _zipfiles(path):
|
||||||
print("Zip files in yesterdays archivdir {}".format(path))
|
print("Zip files in yesterdays archivdir {}".format(path))
|
||||||
zip_fileext=".zip"
|
zip_fileext=".gz"
|
||||||
zip_compression=zipfile.ZIP_DEFLATED
|
#zip_compression=zipfile.ZIP_DEFLATED
|
||||||
zip_compressionlevel=5
|
zip_compressionlevel=5
|
||||||
files = []
|
files = []
|
||||||
# r=root, d=directories, f = files
|
# r=root, d=directories, f = files
|
||||||
for r, d, f in os.walk(path):
|
for r, d, f in os.walk(path):
|
||||||
for file in f:
|
for file in f:
|
||||||
if '.gz' not in file:
|
if zip_fileext not in file:
|
||||||
files.append(os.path.join(r, file))
|
files.append(os.path.join(r, file))
|
||||||
|
|
||||||
for f in files:
|
for f in files:
|
||||||
print("file to zip: {} -> {}".format(os.path.basename(f), f+'.bzip'))
|
print("file to zip: {} -> {}".format(os.path.basename(f), f+'.bzip'))
|
||||||
try:
|
try:
|
||||||
with ZipFile(
|
with open(f, 'rb') as f_in:
|
||||||
f+zip_fileext,
|
with gzip.open(
|
||||||
'w',
|
f+zip_fileext,
|
||||||
allowZip64=True,
|
'wb',
|
||||||
compression=zip_compression,
|
compresslevel=zip_compressionlevel) as f_out:
|
||||||
compresslevel=zip_compressionlevel
|
shutil.copyfileobj(f_in, f_out)
|
||||||
) as zf:
|
|
||||||
zf.write(f, os.path.basename(f))
|
# with ZipFile(
|
||||||
|
# f+zip_fileext,
|
||||||
|
# 'w',
|
||||||
|
# allowZip64=True,
|
||||||
|
# compression=zip_compression,
|
||||||
|
# compresslevel=zip_compressionlevel
|
||||||
|
# ) as zf:
|
||||||
|
# zf.write(f, os.path.basename(f))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Error during zipping file {}".format(f), e)
|
print("Error during zipping file {}".format(f), e)
|
||||||
else:
|
else:
|
||||||
|
@ -237,9 +269,18 @@ def _zipfiles(path):
|
||||||
|
|
||||||
def main(args):
|
def main(args):
|
||||||
#FUSE(WorkdirFS(root), mountpoint, nothreads=True, foreground=True)
|
#FUSE(WorkdirFS(root), mountpoint, nothreads=True, foreground=True)
|
||||||
check_dir(os.path.join(os.environ['HOME'], args.archive))
|
|
||||||
check_dir(os.path.join(os.environ['HOME'], args.mountpoint))
|
check_dir(os.path.join(os.environ['HOME'], args.mountpoint))
|
||||||
cleanup_dirs(os.path.join(os.environ['HOME'], args.archive))
|
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 + '"'
|
||||||
# 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
|
||||||
|
@ -249,7 +290,7 @@ def main(args):
|
||||||
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=\"$HOME/"+args.archive+'"', end='\n')
|
print("XDG_ARCHIVE_DIR=" + xdg_entry, end='\n')
|
||||||
foundarchive=True
|
foundarchive=True
|
||||||
elif line.startswith('XDG_WORK_DIR'):
|
elif line.startswith('XDG_WORK_DIR'):
|
||||||
print("XDG_WORK_DIR=\"$HOME/"+args.mountpoint+'"', end='\n')
|
print("XDG_WORK_DIR=\"$HOME/"+args.mountpoint+'"', end='\n')
|
||||||
|
@ -260,10 +301,10 @@ def main(args):
|
||||||
print("File not existing, create it: {}".format(os.environ['HOME']+'/.config/user-dirs.dirs'))
|
print("File not existing, create it: {}".format(os.environ['HOME']+'/.config/user-dirs.dirs'))
|
||||||
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=\"$HOME/"+args.archive+'"\n')
|
fh.write("XDG_ARCHIVE_DIR=" + xdg_entry + '\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=\"$HOME/"+args.mountpoint+'"\n')
|
fh.write('XDG_WORK_DIR=\"$HOME/' + args.mountpoint + '"\n')
|
||||||
|
|
||||||
# start FUSE filesystem
|
# start FUSE filesystem
|
||||||
FUSE(WorkdirFS(args), os.path.join(os.environ['HOME'], args.mountpoint), nothreads=True, foreground=True)
|
FUSE(WorkdirFS(args), os.path.join(os.environ['HOME'], args.mountpoint), nothreads=True, foreground=True)
|
||||||
|
|
Loading…
Reference in a new issue