annotate makeitso/python_package/{{package}}/{{main}}.py @ 233:ceb5c8ee5bbe

foo
author Jeff Hammel <k0scist@gmail.com>
date Mon, 14 Dec 2015 14:51:27 -0800
parents ab726b2f3143
children b4a3e8bbe095
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
79
8c326f764cfe add a CLI template
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
159
Jeff Hammel <jhammel@mozilla.com>
parents: 155
diff changeset
2 # -*- coding: utf-8 -*-
79
8c326f764cfe add a CLI template
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3
8c326f764cfe add a CLI template
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 """
8c326f764cfe add a CLI template
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 {{description}}
8c326f764cfe add a CLI template
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 """
8c326f764cfe add a CLI template
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7
187
74f41d53b057 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 161
diff changeset
8 # imports
188
1bcf60bcc455 update to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 187
diff changeset
9 import argparse
159
Jeff Hammel <jhammel@mozilla.com>
parents: 155
diff changeset
10 import os
Jeff Hammel <jhammel@mozilla.com>
parents: 155
diff changeset
11 import subprocess
79
8c326f764cfe add a CLI template
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 import sys
207
04c5464355b8 monitor
Jeff Hammel <k0scist@gmail.com>
parents: 206
diff changeset
13 import time
159
Jeff Hammel <jhammel@mozilla.com>
parents: 155
diff changeset
14
210
ab726b2f3143 arbitrary python requirements, the bad way
Jeff Hammel <k0scist@gmail.com>
parents: 207
diff changeset
15 # python requirements
ab726b2f3143 arbitrary python requirements, the bad way
Jeff Hammel <k0scist@gmail.com>
parents: 207
diff changeset
16 # (key, value) = (module, PyPI name)
ab726b2f3143 arbitrary python requirements, the bad way
Jeff Hammel <k0scist@gmail.com>
parents: 207
diff changeset
17 requirements = ()
ab726b2f3143 arbitrary python requirements, the bad way
Jeff Hammel <k0scist@gmail.com>
parents: 207
diff changeset
18 for module, package in requirements:
ab726b2f3143 arbitrary python requirements, the bad way
Jeff Hammel <k0scist@gmail.com>
parents: 207
diff changeset
19 try:
ab726b2f3143 arbitrary python requirements, the bad way
Jeff Hammel <k0scist@gmail.com>
parents: 207
diff changeset
20 globals()[module] = __import__(module)
ab726b2f3143 arbitrary python requirements, the bad way
Jeff Hammel <k0scist@gmail.com>
parents: 207
diff changeset
21 except ImportError:
ab726b2f3143 arbitrary python requirements, the bad way
Jeff Hammel <k0scist@gmail.com>
parents: 207
diff changeset
22 # install requirement and try again
ab726b2f3143 arbitrary python requirements, the bad way
Jeff Hammel <k0scist@gmail.com>
parents: 207
diff changeset
23 subprocess.check_call(['pip', 'install', package])
ab726b2f3143 arbitrary python requirements, the bad way
Jeff Hammel <k0scist@gmail.com>
parents: 207
diff changeset
24 args = [sys.executable] + sys.argv
ab726b2f3143 arbitrary python requirements, the bad way
Jeff Hammel <k0scist@gmail.com>
parents: 207
diff changeset
25 os.execl(sys.executable, *args)
ab726b2f3143 arbitrary python requirements, the bad way
Jeff Hammel <k0scist@gmail.com>
parents: 207
diff changeset
26
195
3cb66c9e5ce8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 188
diff changeset
27 # module globals
3cb66c9e5ce8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 188
diff changeset
28 __all__ = ['main', 'Parser']
187
74f41d53b057 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 161
diff changeset
29 here = os.path.dirname(os.path.realpath(__file__))
188
1bcf60bcc455 update to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 187
diff changeset
30 string = (str, unicode)
187
74f41d53b057 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 161
diff changeset
31
196
d25ca7099df8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 195
diff changeset
32 def ensure_dir(directory):
d25ca7099df8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 195
diff changeset
33 """ensure a directory exists"""
d25ca7099df8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 195
diff changeset
34 if os.path.exists(directory):
205
0a991b8fe839 better check
Jeff Hammel <k0scist@gmail.com>
parents: 198
diff changeset
35 if not os.path.isdir(directory):
0a991b8fe839 better check
Jeff Hammel <k0scist@gmail.com>
parents: 198
diff changeset
36 raise OSError("Not a directory: '{}'".format(directory))
196
d25ca7099df8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 195
diff changeset
37 return directory
d25ca7099df8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 195
diff changeset
38 os.makedirs(directory)
d25ca7099df8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 195
diff changeset
39 return directory
d25ca7099df8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 195
diff changeset
40
188
1bcf60bcc455 update to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 187
diff changeset
41
1bcf60bcc455 update to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 187
diff changeset
42 class Parser(argparse.ArgumentParser):
1bcf60bcc455 update to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 187
diff changeset
43 """CLI option parser"""
198
be7e2e336d7a STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 197
diff changeset
44 def __init__(self, **kwargs):
206
d9d7bfdb54db http://stackoverflow.com/questions/4375327/python-argparse-preformatted-help-text
Jeff Hammel <k0scist@gmail.com>
parents: 205
diff changeset
45 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
198
be7e2e336d7a STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 197
diff changeset
46 kwargs.setdefault('description', __doc__)
be7e2e336d7a STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 197
diff changeset
47 argparse.ArgumentParser.__init__(self, **kwargs)
207
04c5464355b8 monitor
Jeff Hammel <k0scist@gmail.com>
parents: 206
diff changeset
48 self.add_argument('--monitor', dest='monitor',
04c5464355b8 monitor
Jeff Hammel <k0scist@gmail.com>
parents: 206
diff changeset
49 type=float, metavar='SLEEP',
04c5464355b8 monitor
Jeff Hammel <k0scist@gmail.com>
parents: 206
diff changeset
50 help="run in monitor mode")
196
d25ca7099df8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 195
diff changeset
51 self.options = None
188
1bcf60bcc455 update to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 187
diff changeset
52
196
d25ca7099df8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 195
diff changeset
53 def parse_args(self, *args, **kw):
197
bb57bec165b7 fix errors with template
Jeff Hammel <k0scist@gmail.com>
parents: 196
diff changeset
54 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
196
d25ca7099df8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 195
diff changeset
55 self.validate(options)
d25ca7099df8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 195
diff changeset
56 self.options = options
d25ca7099df8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 195
diff changeset
57 return options
d25ca7099df8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 195
diff changeset
58
d25ca7099df8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 195
diff changeset
59 def validate(self, options):
d25ca7099df8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 195
diff changeset
60 """validate options"""
79
8c326f764cfe add a CLI template
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61
155
386a44a52139 moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents: 149
diff changeset
62 def main(args=sys.argv[1:]):
188
1bcf60bcc455 update to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 187
diff changeset
63 """CLI"""
130
d9201f911458 better description formatting
Jeff Hammel <jhammel@mozilla.com>
parents: 79
diff changeset
64
139
23652cbfdfad whitespace
Jeff Hammel <jhammel@mozilla.com>
parents: 130
diff changeset
65 # parse command line options
197
bb57bec165b7 fix errors with template
Jeff Hammel <k0scist@gmail.com>
parents: 196
diff changeset
66 parser = Parser()
188
1bcf60bcc455 update to argparse
Jeff Hammel <k0scist@gmail.com>
parents: 187
diff changeset
67 options = parser.parse_args(args)
79
8c326f764cfe add a CLI template
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68
207
04c5464355b8 monitor
Jeff Hammel <k0scist@gmail.com>
parents: 206
diff changeset
69 try:
04c5464355b8 monitor
Jeff Hammel <k0scist@gmail.com>
parents: 206
diff changeset
70 while True:
04c5464355b8 monitor
Jeff Hammel <k0scist@gmail.com>
parents: 206
diff changeset
71 if options.monitor:
04c5464355b8 monitor
Jeff Hammel <k0scist@gmail.com>
parents: 206
diff changeset
72 time.sleep(options.monitor)
04c5464355b8 monitor
Jeff Hammel <k0scist@gmail.com>
parents: 206
diff changeset
73 else:
04c5464355b8 monitor
Jeff Hammel <k0scist@gmail.com>
parents: 206
diff changeset
74 break
04c5464355b8 monitor
Jeff Hammel <k0scist@gmail.com>
parents: 206
diff changeset
75 except KeyboardInterrupt:
04c5464355b8 monitor
Jeff Hammel <k0scist@gmail.com>
parents: 206
diff changeset
76 pass
04c5464355b8 monitor
Jeff Hammel <k0scist@gmail.com>
parents: 206
diff changeset
77
79
8c326f764cfe add a CLI template
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
78 if __name__ == '__main__':
196
d25ca7099df8 STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents: 195
diff changeset
79 main()
207
04c5464355b8 monitor
Jeff Hammel <k0scist@gmail.com>
parents: 206
diff changeset
80