/
loop
executable file
·45 lines (34 loc) · 1.1 KB
/
loop
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
#!/usr/bin/env python
# encoding: utf-8
"""
Loop script runner.
Usage:
loop <loop-name>|<ad-hoc-loop> <parameters>
"""
from itertools import imap
from os.path import dirname, isfile, join, realpath
from pipes import quote
from subprocess import call
from sys import argv, stderr
from loopozorg import Loop, exit_on_ctrl_c
__author__ = 'Maciej Konieczny <hello@narf.pl>'
def _main():
# read arguments
if len(argv) < 2:
print >> stderr, __doc__[1:-1]
exit(1)
first_arg = argv[1]
other_args = argv[2:]
# suppose first argument is a loop name
loops_dir = join(dirname(realpath(__file__)), '..')
loop_path = realpath(join(loops_dir, first_arg + '.py'))
if isfile(loop_path):
parameters = ' '.join(imap(quote, other_args))
with exit_on_ctrl_c(quiet=True):
call('python {0} {1}'.format(loop_path, parameters), shell=True)
else:
# assume first argument is an ad hoc loop
Loop(parameters=other_args).run(command=first_arg,
enable_autotemplate=False)
if __name__ == '__main__':
_main()