From 6c67f54d8d4cc78616522a91caebeac7d4153e75 Mon Sep 17 00:00:00 2001 From: Ville Rantanen Date: Fri, 26 Jan 2018 11:13:44 +0200 Subject: [PATCH] share manager --- data/config.json.example | 8 +++- utils/create-share.py | 7 +++- utils/flees-manager.py | 79 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 4 deletions(-) create mode 100755 utils/flees-manager.py diff --git a/data/config.json.example b/data/config.json.example index 89bdb01..5eba648 100644 --- a/data/config.json.example +++ b/data/config.json.example @@ -1,9 +1,13 @@ { + "__comment": [ + "workers: number of parallel processes. single long upload reserves a process.", + "timeout: seconds for process to last. single long upload cant take longer than this.", + "uid: Docker runs as root, this changes owner of written files. -1 to skip chowning" + ], "workers": 8, "timeout": 3600, - "uid_comment": "Docker runs as root, this changes owner of written files.", "uid": 1000, - "__comment": "most likely you will not change anything after this line", + "__do_not_edit": "most likely you will not change anything after this line", "data_folder": "data", "shares_file": "data/shares.json", "date_format": "%Y-%m-%d %H:%M" diff --git a/utils/create-share.py b/utils/create-share.py index 42bf8a3..4c5c2da 100755 --- a/utils/create-share.py +++ b/utils/create-share.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -import hashlib,argparse,json,sys +import hashlib,argparse,json,sys,os from datetime import datetime from shutil import copyfile @@ -51,7 +51,10 @@ if opts.expire: }) if opts.shares: - shares = json.load(open(opts.shares,'rt')) + if os.path.exists(opts.shares): + shares = json.load(open(opts.shares,'rt')) + else: + shares = [] shares.append(share) if opts.insert: print("creating backup %s"%(opts.shares+".bkp",)) diff --git a/utils/flees-manager.py b/utils/flees-manager.py new file mode 100755 index 0000000..e173a04 --- /dev/null +++ b/utils/flees-manager.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python +import argparse,json,sys,os +from shutil import copyfile + + +def list_shares(shares): + print("\t".join( ('Name', 'Path') )) + for share in shares: + print("\t".join( (share['name'], share['path']) )) + + +def list_folders(shares,config): + folders = sorted(os.listdir(config['data_folder'])) + print("\t".join( ('Path','Share') )) + for folder in folders: + full_path = os.path.join(config['data_folder'], folder) + if not os.path.isdir(full_path): + continue + share_name = "[unused by any share]" + for share in shares: + share_path = os.path.join(config['data_folder'], share['path']) + if os.path.samefile(full_path, share_path): + share_name = share['name'] + break + print("\t".join( (folder, share_name) )) + +def remove_share(name,shares,config): + share = [share for share in shares if share['name'] == name] + for share_ in share: + print("Removing share: %s"%( name, )) + print(json.dumps(share_, indent = 2, sort_keys = True)) + + shares = [share for share in shares if share['name'] != name] + + share_file = config['shares_file'] + print("creating backup %s"%(share_file+".bkp",)) + copyfile(share_file, share_file+".bkp") + with open(share_file,'wt') as fp: + json.dump(shares, fp, indent = 2, sort_keys = True) + print("Removed %s from %s"%(name, share_file)) + + +parser = argparse.ArgumentParser(description='Flees share manager') +parser.add_argument('-c','--config', action="store", dest="config", default = "data/config.json", + help = "Your current config.json file") + +subparsers = parser.add_subparsers(help='sub-command help', dest='subparser_name') + +parser_list = subparsers.add_parser('list', help = "List shares") + +parser_folders = subparsers.add_parser('folders', help = "List folders and share names") + +parser_remove = subparsers.add_parser('remove', help = "Remove a share") +parser_remove.add_argument(dest="name") + + +opts = parser.parse_args() + +if os.path.exists(opts.config): + config = json.load(open(opts.config,'rt')) +else: + print("config file does not exist!") + sys.exit(1) + +if os.path.exists(config['shares_file']): + shares = json.load(open(config['shares_file'],'rt')) +else: + print("shares_file does not exist!") + sys.exit(1) + +if opts.subparser_name == 'list': + list_shares(shares) +elif opts.subparser_name == 'folders': + list_folders(shares,config) +elif opts.subparser_name == 'remove': + remove_share(opts.name,shares,config) + + +