-
Notifications
You must be signed in to change notification settings - Fork 2
/
ctrl.py
222 lines (181 loc) · 4.8 KB
/
ctrl.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""
<Program Name>
ctrl.py
<Authors>
Team Totolly Secure
<Started>
December 2015.
<Purpose>
Command line tool to edit permissions in JSON file.
Tool can be used independently if necessary.
"""
import json, sys, os.path, getopt, re
acl = {}
def __read_json(name):
"""
<Purpose>
Reads JSON file and returns it as a dictionary.
<Arguments>
name of input file
<Exceptions>
None
<Returns>
Returns dictionary
"""
with open(name) as f:
data = json.load(f)
return data
def __write_json(name, data):
"""
<Purpose>
Writes dictionary to JSON file.
<Arguments>
name of output file
data in the form of dictionary to write in file
<Exceptions>
None
<Returns>
None
"""
with open(name, "w") as ofs :
ofs.write(json.dumps(data, indent = 4))
def __load_json(file_name):
"""
<Purpose>
Reads JSON file and fills global acl dictionary.
<Arguments>
file_name or name of input file
<Exceptions>
None
<Returns>
None
"""
global acl
if not os.path.isfile(file_name):
__write_json(file_name, acl)
else:
acl = __read_json(file_name)
def compare_time(start, end):
"""
<Purpose>
Manually compares two times.
Returns True if end time is more recent than start time.
Returns False otherwise.
<Arguments>
start time
end time
<Exceptions>
None
<Returns>
Bool
"""
s = re.split('-|\+|:| ', start)
e = re.split('-|\+|:| ', end)
if s[0] > e[0]:
return False
if s[1] > e[1]:
return False
if s[2] > e[2]:
return False
if s[3] > e[3]:
return False
if s[4] > e[4]:
return False
if s[5] > e[5]:
return False
return True
def main(argv):
global acl
if len(argv) < 2:
print "Invalid command - too few arguments"
sys.exit()
__load_json("acl.json")
delete = ''
user = ''
permission = ''
start = ''
end = ''
layer = []
branch = []
try:
opts, args = getopt.getopt(argv[1:], "d:u:p:s:e:l:b:hf", ["delete=","help=","user=","permission=","start=","end=","layer=","branch=","flush="])
except getopt.GetoptError:
print "crtl.py -u <username> -p <permission> -s <start-time> -e <end-time> -l <layers> -b <branches>\nctrl.py -d <username>\nctrl.py -d <username> -p <permission>\nctrl -f"
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
print "crtl.py -u <username> -p <permission> -s <start-time> -e <end-time> -l <layers> -b <branches>\nctrl.py -d <username>\nctrl.py -d <username> -p <permission>\nctrl.py -f"
sys.exit()
elif opt in ("-u", "--user"):
user = arg
elif opt in ("-p", "--permission"):
permission = arg
elif opt in ("-s", "--start"):
start = arg
elif opt in ("-e", "--end"):
end = arg
elif opt in ("-l", "--layer"):
if arg in ("True", "true", "t", "T"):
layer = True
elif arg in ("False", "false", "f", "F"):
layer = False
else:
layer = re.split(',|;| ', arg)
elif opt in ("-b", "--branch"):
if arg in ("True", "true", "t", "T"):
branch = True
elif arg in ("False", "false", "f", "F"):
branch = False
else:
branch = re.split(',|;| ', arg)
elif opt in ("-d", "--delete"):
delete = arg
elif opt in ("-f", "--flush"):
acl = {}
__write_json("acl.json", acl)
sys.exit()
if delete == '':
if user not in acl:
if user == '':
print "Invalid command - username required"
sys.exit()
acl[user] = {}
if permission == '' or permission not in ("commit","merge","write"):
print "Invalid command - commit/merge/write permission required"
sys.exit()
elif permission != '':
acl[user][permission] = {}
if start != '' and end != '':
if compare_time(start, end) == False:
print "Invalid command - end time must be greater than start time"
sys.exit()
if start in ("True", "true", "t", "T"):
acl[user][permission]["start"] = True
elif start in ("False", "false", "f", "F"):
acl[user][permission]["start"] = False
elif start != '':
acl[user][permission]["start"] = start
if end in ("True", "true", "t", "T"):
acl[user][permission]["end"] = True
elif start in ("False", "false", "f", "F"):
acl[user][permission]["end"] = False
elif end != '':
acl[user][permission]["end"] = end
if layer in (True, False):
acl[user][permission]["layer"] = layer
elif len(layer) != 0:
acl[user][permission]["layer"] = layer
if branch in (True, False):
acl[user][permission]["branch"] = branch
elif len(branch) != 0:
acl[user][permission]["branch"] = branch
else:
if permission == '':
if delete in acl:
del acl[delete]
else:
if delete in acl:
if permission in acl[delete]:
del acl[delete][permission]
__write_json("acl.json", acl)
main(sys.argv)