comparison supervisoradmin/add.py @ 0:e58db0f06ed8

initial commit (stub)
author Jeff Hammel <k0scist@gmail.com>
date Tue, 27 Jan 2015 14:01:52 -0800
parents
children ca51502dc8d3
comparison
equal deleted inserted replaced
-1:000000000000 0:e58db0f06ed8
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 add supervisor jobs
6 """
7
8 # imports
9 import argparse
10 import os
11 import subprocess
12 import sys
13 import time
14
15 # module globals
16 __all__ = ['main', 'Parser']
17 here = os.path.dirname(os.path.realpath(__file__))
18 string = (str, unicode)
19
20 def ensure_dir(directory):
21 """ensure a directory exists"""
22 if os.path.exists(directory):
23 if not os.path.isdir(directory):
24 raise OSError("Not a directory: '{}'".format(directory))
25 return directory
26 os.makedirs(directory)
27 return directory
28
29
30 class Parser(argparse.ArgumentParser):
31 """CLI option parser"""
32 def __init__(self, **kwargs):
33 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
34 kwargs.setdefault('description', __doc__)
35 argparse.ArgumentParser.__init__(self, **kwargs)
36 self.add_argument('--monitor', dest='monitor',
37 type=float, metavar='SLEEP',
38 help="run in monitor mode")
39 self.options = None
40
41 def parse_args(self, *args, **kw):
42 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
43 self.validate(options)
44 self.options = options
45 return options
46
47 def validate(self, options):
48 """validate options"""
49
50 def main(args=sys.argv[1:]):
51 """CLI"""
52
53 # parse command line options
54 parser = Parser()
55 options = parser.parse_args(args)
56
57 try:
58 while True:
59 if options.monitor:
60 time.sleep(options.monitor)
61 else:
62 break
63 except KeyboardInterrupt:
64 pass
65
66 if __name__ == '__main__':
67 main()
68
69