80 lines
2.5 KiB
Python
Executable File
80 lines
2.5 KiB
Python
Executable File
#!/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)
|
|
|
|
|
|
|