-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncd-add
executable file
·86 lines (74 loc) · 2.5 KB
/
syncd-add
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
import json
import argparse
import os
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(description="add a new syncd rule")
parser.add_argument("source", help="file/location to sync from")
parser.add_argument("destination", help="file/location to sync to")
parser.add_argument("--rules", help="Specify an alternative rules file to use")
parser.add_argument("-v", "--verbose", action="store_true", default=False, help="Specify an increased verbosity level")
directionGroup = parser.add_mutually_exclusive_group()
directionGroup.add_argument("--monodirectional", action="store_true")
parser.add_argument("--norestart", action="store_true", default=False, help="Do not restart daemon after making changes") #FIXME should be --no-restart
args = parser.parse_args()
#first, pick a rules.json to use
#this really should be a function in a utility class
if args.rules:
rulesFile = args.rules
else:
userConfigDir = os.getenv('XDG_CONFIG_HOME', default=os.path.join(os.path.expanduser('~'), '.config'))
syncdConfigDir = os.path.join(userConfigDir, "syncd")
#rulesFile = "/tmp/rules.json"
rulesFile = os.path.join(syncdConfigDir, "rules.json")
if os.path.exists(rulesFile):
pass
else:
if args.verbose:
print("no rules file, running syncd for initial setup")
os.system("syncd")
if args.verbose:
print("Using", rulesFile, "as rules file")
#get the new rule from the command line
newSrc = args.source
newDest = args.destination
#newDests = [args.destination]
#handle raw filesystem location
if newSrc[0] == "/": #FIXME It would be nice if this worked on Windows
newSrc = "file://" + newSrc
if args.verbose:
print("Adding new rule:")
if args.monodirectional:
print(newSrc, "->", [newDest])
print("monodirectionally")
else:
print(newSrc, "->", [newDest])
print(newDest, "->", [newSrc])
print("bidirectionally")
#load existing rules
oldRulesFile = open(rulesFile)
myJsonArray= json.loads(oldRulesFile.read())
oldRulesFile.close()
if args.verbose:
print("Original rules:")
print(myJsonArray)
#make our alteration
if args.monodirectional:
myJsonArray[newSrc] = [newDest]
else:
myJsonArray[newSrc] = [newDest]
myJsonArray[newDest] = [newSrc]
if args.verbose:
print("New rules:")
print(myJsonArray)
#output new rules file
#open("testOut.json").write(json.dumps(myJsonArray))
newRulesFile = open(rulesFile, "w")
newRulesFile.write(json.dumps(myJsonArray, indent=3))
newRulesFile.close()
if args.norestart:
pass
else:
os.system("syncd-restart")
#announce our victory
print("All done!")