1 |
""" |
---|
2 |
Delete glider from configuration file. |
---|
3 |
|
---|
4 |
Usage: |
---|
5 |
|
---|
6 |
delete-glider glider |
---|
7 |
|
---|
8 |
or |
---|
9 |
|
---|
10 |
python delete_glider.py path/to/config/file glider |
---|
11 |
""" |
---|
12 |
|
---|
13 |
import os |
---|
14 |
import sys |
---|
15 |
import errno |
---|
16 |
import lockfile as lf |
---|
17 |
import ConfigParser as cp |
---|
18 |
|
---|
19 |
def _main(): |
---|
20 |
"""Delete a glider section from the configuration file. |
---|
21 |
|
---|
22 |
These doctests require write permissions to tests/etc. |
---|
23 |
|
---|
24 |
Too few arguments: |
---|
25 |
|
---|
26 |
>>> import shutil |
---|
27 |
>>> cfg = os.path.join(os.path.dirname(__file__), |
---|
28 |
... "tests", "etc", "autopilot.cfg") |
---|
29 |
>>> shutil.copy2(cfg + ".test", cfg) |
---|
30 |
>>> sys.argv = [__file__, |
---|
31 |
... cfg,] |
---|
32 |
>>> _main() |
---|
33 |
<BLANKLINE> |
---|
34 |
Delete glider from configuration file. |
---|
35 |
<BLANKLINE> |
---|
36 |
Usage: |
---|
37 |
<BLANKLINE> |
---|
38 |
delete-glider glider |
---|
39 |
<BLANKLINE> |
---|
40 |
or |
---|
41 |
<BLANKLINE> |
---|
42 |
python delete_glider.py path/to/config/file glider |
---|
43 |
<BLANKLINE> |
---|
44 |
|
---|
45 |
Too many arguments: |
---|
46 |
|
---|
47 |
>>> import shutil |
---|
48 |
>>> cfg = os.path.join(os.path.dirname(__file__), |
---|
49 |
... "tests", "etc", "autopilot.cfg") |
---|
50 |
>>> shutil.copy2(cfg + ".test", cfg) |
---|
51 |
>>> sys.argv = [__file__, |
---|
52 |
... cfg, |
---|
53 |
... "Cthulhu", |
---|
54 |
... "Kraken",] |
---|
55 |
>>> _main() |
---|
56 |
<BLANKLINE> |
---|
57 |
Delete glider from configuration file. |
---|
58 |
<BLANKLINE> |
---|
59 |
Usage: |
---|
60 |
<BLANKLINE> |
---|
61 |
delete-glider glider |
---|
62 |
<BLANKLINE> |
---|
63 |
or |
---|
64 |
<BLANKLINE> |
---|
65 |
python delete_glider.py path/to/config/file glider |
---|
66 |
<BLANKLINE> |
---|
67 |
|
---|
68 |
Missing configuration file: |
---|
69 |
|
---|
70 |
>>> from StringIO import StringIO |
---|
71 |
>>> cfg = os.path.join(os.path.dirname(__file__), |
---|
72 |
... "tests", "etc", "autopilot.cfg") |
---|
73 |
>>> try: |
---|
74 |
... os.remove(cfg) |
---|
75 |
... except: |
---|
76 |
... pass |
---|
77 |
>>> sys.argv = [__file__, |
---|
78 |
... cfg, |
---|
79 |
... "Cthulhu",] |
---|
80 |
>>> output = StringIO() |
---|
81 |
>>> save = sys.stdout |
---|
82 |
>>> sys.stdout = output |
---|
83 |
>>> _main() |
---|
84 |
>>> sys.stdout = save |
---|
85 |
>>> output.getvalue() == "Glider cthulhu does not exist in {0}.\\n". \\ |
---|
86 |
... format(os.path.abspath(cfg)) |
---|
87 |
True |
---|
88 |
>>> output.close() |
---|
89 |
|
---|
90 |
Empty configuration file: |
---|
91 |
|
---|
92 |
>>> from StringIO import StringIO |
---|
93 |
>>> cfg = os.path.join(os.path.dirname(__file__), |
---|
94 |
... "tests", "etc", "autopilot.cfg") |
---|
95 |
>>> try: |
---|
96 |
... os.remove(cfg) |
---|
97 |
... open(cfg, 'w').close() |
---|
98 |
... except: |
---|
99 |
... pass |
---|
100 |
>>> sys.argv = [__file__, |
---|
101 |
... cfg, |
---|
102 |
... "Cthulhu",] |
---|
103 |
>>> output = StringIO() |
---|
104 |
>>> save = sys.stdout |
---|
105 |
>>> sys.stdout = output |
---|
106 |
>>> _main() |
---|
107 |
>>> sys.stdout = save |
---|
108 |
>>> output.getvalue() == "Glider cthulhu does not exist in {0}.\\n". \\ |
---|
109 |
... format(os.path.abspath(cfg)) |
---|
110 |
True |
---|
111 |
>>> output.close() |
---|
112 |
|
---|
113 |
Configuration does not contain glider: |
---|
114 |
|
---|
115 |
>>> import shutil |
---|
116 |
>>> from StringIO import StringIO |
---|
117 |
>>> cfg = os.path.join(os.path.dirname(__file__), |
---|
118 |
... "tests", "etc", "autopilot.cfg") |
---|
119 |
>>> shutil.copy2(cfg + ".test", cfg) |
---|
120 |
>>> sys.argv = [__file__, |
---|
121 |
... cfg, |
---|
122 |
... "Cthulhu",] |
---|
123 |
>>> output = StringIO() |
---|
124 |
>>> save = sys.stdout |
---|
125 |
>>> sys.stdout = output |
---|
126 |
>>> _main() |
---|
127 |
>>> sys.stdout = save |
---|
128 |
>>> output.getvalue() == "Glider cthulhu does not exist in {0}.\\n". \\ |
---|
129 |
... format(os.path.abspath(cfg)) |
---|
130 |
True |
---|
131 |
>>> output.close() |
---|
132 |
>>> |
---|
133 |
>>> import autopilot.list_gliders |
---|
134 |
>>> sys.argv = [__file__, |
---|
135 |
... cfg,] |
---|
136 |
>>> autopilot.list_gliders._main() |
---|
137 |
ramses |
---|
138 |
pelagia |
---|
139 |
salacia |
---|
140 |
sandgnat |
---|
141 |
rusalka |
---|
142 |
|
---|
143 |
Regular configuration file: |
---|
144 |
|
---|
145 |
>>> import shutil |
---|
146 |
>>> from StringIO import StringIO |
---|
147 |
>>> cfg = os.path.join(os.path.dirname(__file__), |
---|
148 |
... "tests", "etc", "autopilot.cfg") |
---|
149 |
>>> shutil.copy2(cfg + ".test", cfg) |
---|
150 |
>>> sys.argv = [__file__, |
---|
151 |
... cfg, |
---|
152 |
... "Pelagia"] |
---|
153 |
>>> output = StringIO() |
---|
154 |
>>> save = sys.stdout |
---|
155 |
>>> sys.stdout = output |
---|
156 |
>>> _main() |
---|
157 |
>>> sys.stdout = save |
---|
158 |
>>> output.getvalue() == "Glider pelagia deleted from {0}.\\n". \\ |
---|
159 |
... format(os.path.abspath(cfg)) |
---|
160 |
True |
---|
161 |
>>> output.close() |
---|
162 |
>>> |
---|
163 |
>>> import autopilot.list_gliders |
---|
164 |
>>> sys.argv = [__file__, |
---|
165 |
... cfg,] |
---|
166 |
>>> autopilot.list_gliders._main() |
---|
167 |
ramses |
---|
168 |
salacia |
---|
169 |
sandgnat |
---|
170 |
rusalka |
---|
171 |
|
---|
172 |
""" |
---|
173 |
|
---|
174 |
num_args = len(sys.argv) - 1 |
---|
175 |
if num_args != 2: |
---|
176 |
print __doc__ |
---|
177 |
else: |
---|
178 |
cfg_path = os.path.abspath(sys.argv[1]) |
---|
179 |
glider = sys.argv[2].lower() |
---|
180 |
report = {"glider": glider, |
---|
181 |
"path": cfg_path, |
---|
182 |
} |
---|
183 |
try: |
---|
184 |
lock = lf.FileLock(cfg_path) |
---|
185 |
lock.acquire(5) |
---|
186 |
except lf.LockTimeout: |
---|
187 |
lock.break_lock() |
---|
188 |
lock.acquire(0) |
---|
189 |
config = cp.SafeConfigParser() |
---|
190 |
try: |
---|
191 |
with open(cfg_path) as handle: |
---|
192 |
config.readfp(handle) |
---|
193 |
except IOError as e: |
---|
194 |
if e.errno == errno.ENOENT: |
---|
195 |
print "Glider {glider} does not exist in {path}.". \ |
---|
196 |
format(**report) |
---|
197 |
else: |
---|
198 |
if config.remove_section(glider): |
---|
199 |
with open(cfg_path, "w") as handle: |
---|
200 |
config.write(handle) |
---|
201 |
print "Glider {glider} deleted from {path}.". \ |
---|
202 |
format(**report) |
---|
203 |
else: |
---|
204 |
print "Glider {glider} does not exist in {path}.". \ |
---|
205 |
format(**report) |
---|
206 |
finally: |
---|
207 |
lock.release() |
---|
208 |
|
---|
209 |
if __name__ == "__main__": |
---|
210 |
_main() |
---|