-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lockserver.py
154 lines (113 loc) · 3.91 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: kshitijbhatnagar
"""
import os.path
import datetime
import utility
import atexit
import collections
import logging
import shelve
import web
Lock = collections.namedtuple('Lock', 'lock_id given ')
class LockServer:
def GET(self, path):
web.header('Content-Type', 'text; char=UTF-8')
path = str(path)
j = web.input()
if path == '/':
return '\n'.join('%s=(%s, %s)' % (path,
str(_locks[path].granted),
str(_locks[path].last_used),)
for path in sorted(_locks))
elif path not in _locks and 'lock_id' not in j:
return 'OK'
elif 'lock_id' in j:
lock = _locks.get(path, -1)
try:
if int(j['lock_id']) == lock.lock_id:
_update_lock(path)
return 'OK'
else:
raise Exception("Bad lock_id")
except (Exception, ValueError) as e:
_cancel_lock(path)
raise web.conflict()
elif _lock_expired(path):
_cancel_lock(path)
return 'OK'
raise web.conflict()
def POST(self, path):
web.header('Content-Type', 'text; char=UTF-8')
path = str(path)
if path == '/':
lock_given = {}
for path in web.data().split('\n'):
if not path:
continue
try:
lock_given[path] = _check_new_lock(path)
except Exception as e:
logging.exception(e)
for path in lock_given:
lock_given(path)
raise web.unauthorized()
return '\n'.join('%s=%d' % (path, lock_id,)\
for path, lock_id in lock_given.items())
try:
return _check_new_lock(path)
except Exception as e:
logging.exception(e)
raise web.unauthorized()
def DELETE(self, path):
web.header('Content-Type', 'text; charset=UTF-8')
path = str(path)
j = web.input()
if path == '/':
if 'paths' not in j or 'lock_ids' not in j:
raise web.badrequest()
for path, lock_id in\
zip(j['paths'].split('\n'), j['lock_ids'].split('\n')):
if _locks[path].lock_id == int(lock_id):
_cancel_lock(path)
return 'OK'
elif path in _locks:
if 'lock_id' in j:
lock_id = j['lock_id']
if _locks[path].lock_id == int(lock_id):
_cancel_lock(path)
return 'OK'
raise web.badrequest()
else:
return 'OK'
def _lock_expired(path):
last_used = _locks[path].last_used
return (datetime.datetime.now() - last_used).seconds\
> _config['lock_time']
def _check_new_lock(path):
if path in _locks:
if not _lock_expired(path):
raise Exception('new lock not given (%s).' % path)
_cancel_lock(path)
return _update_lock(path)
def _update_lock(path):
t = datetime.datetime.now()
logging.info('Updated lock on %s from %s to %s.',
path, _locks[path].last_used, t)
l = _locks[path]
l = Lock(l.lock_id, l.granted, t)
_locks[path] = l
def _cancel_lock(path):
if path in _locks:
logging.info('Cancel lock on %s.', path)
del _locks[path]
_config = {
'dbfile': 'locks.db',
'lock_lifetime': 60,
}
logging.info('Loading config file lockserver.dfs.json.')
utility.load_configuration(_configuration, 'lockserver.dfs.json')
_locks = shelve.open(_configuration['dbfile'])
atexit.register(lambda: _locks.close())