Mercurial > hg > MakeItSo
annotate makeitso/python_package/{{package}}/{{main}}.py @ 251:b4a3e8bbe095
py 3 + version bump
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 31 Mar 2017 18:16:36 -0700 |
parents | ab726b2f3143 |
children |
rev | line source |
---|---|
79 | 1 #!/usr/bin/env python |
159 | 2 # -*- coding: utf-8 -*- |
79 | 3 |
4 """ | |
5 {{description}} | |
6 """ | |
7 | |
187
74f41d53b057
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
161
diff
changeset
|
8 # imports |
188 | 9 import argparse |
159 | 10 import os |
11 import subprocess | |
79 | 12 import sys |
207 | 13 import time |
159 | 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__)) |
251 | 30 |
31 try: | |
32 # python 2 | |
33 string = (str, unicode) | |
34 except NameError: | |
35 # python 3 | |
36 string = (str, ) | |
37 | |
187
74f41d53b057
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
161
diff
changeset
|
38 |
196
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
39 def ensure_dir(directory): |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
40 """ensure a directory exists""" |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
41 if os.path.exists(directory): |
205 | 42 if not os.path.isdir(directory): |
43 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
|
44 return directory |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
45 os.makedirs(directory) |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
46 return directory |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
47 |
188 | 48 |
49 class Parser(argparse.ArgumentParser): | |
50 """CLI option parser""" | |
251 | 51 |
198
be7e2e336d7a
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
197
diff
changeset
|
52 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
|
53 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter) |
198
be7e2e336d7a
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
197
diff
changeset
|
54 kwargs.setdefault('description', __doc__) |
be7e2e336d7a
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
197
diff
changeset
|
55 argparse.ArgumentParser.__init__(self, **kwargs) |
207 | 56 self.add_argument('--monitor', dest='monitor', |
57 type=float, metavar='SLEEP', | |
58 help="run in monitor mode") | |
196
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
59 self.options = None |
188 | 60 |
196
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
61 def parse_args(self, *args, **kw): |
197 | 62 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
|
63 self.validate(options) |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
64 self.options = options |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
65 return options |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
66 |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
67 def validate(self, options): |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
68 """validate options""" |
79 | 69 |
251 | 70 |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
149
diff
changeset
|
71 def main(args=sys.argv[1:]): |
188 | 72 """CLI""" |
130
d9201f911458
better description formatting
Jeff Hammel <jhammel@mozilla.com>
parents:
79
diff
changeset
|
73 |
139 | 74 # parse command line options |
197 | 75 parser = Parser() |
188 | 76 options = parser.parse_args(args) |
79 | 77 |
207 | 78 try: |
79 while True: | |
80 if options.monitor: | |
81 time.sleep(options.monitor) | |
82 else: | |
83 break | |
84 except KeyboardInterrupt: | |
85 pass | |
86 | |
251 | 87 |
79 | 88 if __name__ == '__main__': |
196
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
89 main() |
207 | 90 |