-
Notifications
You must be signed in to change notification settings - Fork 0
/
lockserver.py
161 lines (123 loc) · 4.92 KB
/
lockserver.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
import web
import shelve
import datetime
import logging
import random
urls = (
'/filepath/(.*)', 'NameServer'
)
class LockServer:
def GET(self, filepath):
web.header('Content-Type', 'text/plain; charset=UTF-8')
filepath = str(filepath)
i = web.input()
if filepath == '/':
p = lock[filepath]
# just a list of file=(granted, last_used)
return '\n'.join('%s=(%s, %s)' % (filepath,
str(p.granted),
str(p.last_used),)
for filepath in sorted(lock))
elif filepath not in lock and 'lock_id' not in i:
return 'OK'
elif 'lock_id' in i:
locks = lock.get(filepath, -1)
try:
if int(i['lock_id']) == locks.lock_id:
t = datetime.datetime.now()
logging.info('Update lock on %s from %s to %s.',
filepath, lock[filepath].last_used, t)
l = lock[filepath]
l = lock(l.lock_id, l.granted, t)
lock[filepath] = l
return 'OK'
else:
raise Exception("Bad lock_id")
except (Exception, ValueError) as e:
# logging.exception(e)
if filepath in lock:
logging.info('Revoking lock on %s.', filepath)
del lock[filepath]
raise web.conflict()
raise web.conflict()
def POST(self, filepath):
web.header('Content-Type', 'text/plain; charset=UTF-8')
filepath = str(filepath)
if filepath == '/':
granted_locks = {}
for filepath in web.data().split('\n'):
if not filepath:
# to allow an empty line at the end of the request data
continue
try:
lock_id = random.randrange(0, 32768)
logging.info('Granting lock (%d) on %s.', lock_id, filepath)
t = datetime.datetime.now()
lock[filepath] = lock(lock_id, t, t)
return lock_id
except Exception as e:
logging.exception(e)
raise web.unauthorized()
# list of filename=lock_id
return '\n'.join('%s=%d' % (filepath, lock_id,) \
for filepath, lock_id in granted_locks.items())
raise web.unauthorized()
def DELETE(self, filepath):
web.header('Content-Type', 'text/plain; charset=UTF-8')
filepath = str(filepath)
i = web.input()
# allow deletion of multiple locks
# so it'll be easier to add transactions
if filepath == '/':
if 'filepaths' not in i or 'lock_ids' not in i:
raise web.badrequest()
for filepath, lock_id in \
zip(i['filepaths'].split('\n'), i['lock_ids'].split('\n')):
if lock[filepath].lock_id == int(lock_id):
if filepath in lock:
logging.info('Revoking lock on %s.', filepath)
del lock[filepath]
# return OK even if some lock_ids were wrong
# because they wanted to revoke them, so we don't need
# to bother them with some lock_ids they want to revoke
# are no longer valid
return 'OK'
elif filepath in lock:
if 'lock_id' in i:
lock_id = i['lock_id']
if lock[filepath].lock_id == int(lock_id):
if filepath in lock:
logging.info('Revoking lock on %s.', filepath)
del lock[filepath]
# see above for why always ok
return 'OK'
raise web.badrequest()
else:
return 'OK'
def new_lock(filepath):
"""Create a new lock for filepath, and return its id."""
lock_id = random.randrange(0, 32768)
logging.info('Granting lock (%d) on %s.', lock_id, filepath)
t = datetime.datetime.now()
lock[filepath] = lock(lock_id, t, t)
return lock_id
def update_lock(filepath):
t = datetime.datetime.now()
logging.info('Update lock on %s from %s to %s.',
filepath, lock[filepath].last_used, t)
l = lock[filepath]
l = lock(l.lock_id, l.granted, t)
lock[filepath] = l
def revoke_lock(filepath):
"""Revoke the lock associated to filepath."""
if filepath in lock:
logging.info('Revoking lock on %s.', filepath)
del lock[filepath]
_config = {
'dbfile': 'locks.db',
'lock_lifetime': 60,
}
lock = shelve.open(_config['dbfile'])
if __name__ == '__main__':
app = web.application(urls, globals())
app.run()