Repository navigation
Expand file tree
/
Copy pathflock
More file actions
executable file
·178 lines (138 loc) · 4.39 KB
/
Copy pathflock
File metadata and controls
executable file
·178 lines (138 loc) · 4.39 KB
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
#!/usr/bin/env python3
#
# Mac OS (and Solaris) friendly flock wrapper
# from https://snippets.bentasker.co.uk/posts/python3/flock-cli.html
#
# Author: B Tasker
#
'''
Usage:
./flock.py [-on] lockfile [-c] command
./flock.py [-un] lockfile
Note: does not support the following
-s
-w [timeout]
By default, if an unsupported argument is passed
the script will exit with an error (to prevent unexpected
behaviour).
However, to simply ignore these arguments, export an
environment variable:
FLOCK_IGNORE_UNSUPPORTED=1
'''
import fcntl
import os
import sys
class FLock:
''' Calls the OS's flock interface
'''
def __init__(self):
self.lock_f = False
self.downstream_command = False
self.args = False
self.lockfile = "lock"
self.ignore_unsupported = os.getenv("FLOCK_IGNORE_UNSUPPORTED", False)
self.unsupported = [
"-s",
"--shared",
"-w",
"--wait",
"--timeout"
]
def acquire_lock(self):
''' Acquire the lock
'''
if not self.lock_f:
self.open_lock_file()
op = fcntl.LOCK_EX
if "-n" in self.args or "--nb" in self.args:
op |= fcntl.LOCK_NB
r = fcntl.flock(self.lock_f, op)
def check_command(self):
''' Iterate through the commandline and throw an error
if unsupported args are provided (and not suppressed)
'''
if self.ignore_unsupported:
return
for arg in self.args:
if arg in self.unsupported:
print(f"Err: arg {arg} not supported")
sys.exit(1)
def exec(self, cmd = False):
''' Acquire a lock, execute the command
then release the lock
If cmd is not provided, the calculated command
in self.downstream_command will be used
'''
self.check_command()
if not cmd:
cmd = ' '.join(self.downstream_command)
if "-u" in self.args:
# User called the unlock command
self.open_lock_file()
self.release()
return
self.acquire_lock()
if "-o" in self.args or "--close" in self.args:
self.lock_f.close()
os.system(cmd)
self.release()
def open_lock_file(self):
''' Open a handle on the lockfile
'''
self.lock_f = open(self.lockfile, "w")
def process_command_line(self, cmdline):
''' Iterate through command line arguments in order to
identify what args (and value) have been provided to "flock"
and what's to be passed into the command we execute
'''
# Process command line flags
flock_flags = ["-s", "-x", "-e", "--exclusive", "-o", "-n", "-c", "--nb", "--close"]
flock_flag_w_arg = ["-w"]
accept_next = False
flock_end = False
last_arg = False
lockfile = False
built_command = []
flock_args = {}
for i in cmdline:
if i == cmdline[0]:
continue
if i in flock_flags:
flock_args[i] = False
continue
if i in flock_flag_w_arg:
flock_args[i] = False
accept_next = True
last_arg = i
continue
if accept_next:
flock_args[last_arg] = i
accept_next = False
continue
if not lockfile:
# We've reached the lockfile definition
lockfile = i
continue
if len(built_command) == 0 and i.startswith("-"):
# User forgot to provide the lockfile name and
# just provided the command.
#
# Lets not overwrite their command...
built_command.append(lockfile)
lockfile = "lock"
# We're now onto the command and its arguments
built_command.append(i)
self.args = flock_args
self.downstream_command = built_command
if lockfile:
self.lockfile = lockfile
def release(self):
''' Release the lock
'''
if not self.lock_f:
self.open_lock_file()
fcntl.flock(self.lock_f, fcntl.LOCK_UN)
if __name__ == "__main__":
flock = FLock()
flock.process_command_line(sys.argv)
flock.exec()