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