Revision 68
(checked in by cbc, 12 years ago)
|
File locking and task prioritization implemented.
|
Line | |
---|
1 |
""" |
---|
2 |
Delete glider from configuration file. |
---|
3 |
|
---|
4 |
Usage: |
---|
5 |
|
---|
6 |
python delete_glider.py path/to/config/file glider |
---|
7 |
""" |
---|
8 |
|
---|
9 |
import sys |
---|
10 |
import errno |
---|
11 |
import lockfile as lf |
---|
12 |
import ConfigParser as cp |
---|
13 |
|
---|
14 |
if __name__ == "__main__": |
---|
15 |
num_args = len(sys.argv) - 1 |
---|
16 |
if num_args != 2: |
---|
17 |
print __doc__ |
---|
18 |
else: |
---|
19 |
cfg_path = sys.argv[1] |
---|
20 |
glider = sys.argv[2].lower() |
---|
21 |
report = {"glider": glider, |
---|
22 |
"path": cfg_path, |
---|
23 |
} |
---|
24 |
try: |
---|
25 |
lock = lf.FileLock(cfg_path) |
---|
26 |
lock.acquire(5) |
---|
27 |
except lf.LockTimeout: |
---|
28 |
lock.break_lock() |
---|
29 |
lock.acquire(0) |
---|
30 |
config = cp.SafeConfigParser() |
---|
31 |
try: |
---|
32 |
with open(cfg_path) as handle: |
---|
33 |
config.readfp(handle) |
---|
34 |
except IOError as e: |
---|
35 |
if e.errno == errno.ENOENT: |
---|
36 |
print "Glider {glider} does not exist in {path}.". \ |
---|
37 |
format(**report) |
---|
38 |
else: |
---|
39 |
if config.remove_section(glider): |
---|
40 |
with open(cfg_path, "w") as handle: |
---|
41 |
config.write(handle) |
---|
42 |
print "Glider {glider} deleted from {path}.". \ |
---|
43 |
format(**report) |
---|
44 |
else: |
---|
45 |
print "Glider {glider} does not exist in {path}.". \ |
---|
46 |
format(**report) |
---|
47 |
finally: |
---|
48 |
lock.release() |
---|