Mercurial > hg > MakeItSo
annotate makeitso/python.py @ 178:d6ef91d49609
hopefully do away with some extra variable reentry
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 02 Mar 2014 18:45:20 -0800 |
parents | c3f719824948 |
children | 5bd1c50c6f61 |
rev | line source |
---|---|
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 """ |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 python package templates for makeitso |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 Several components are included. |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 [TODO] You may use these subtemplates in any combination. |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 * README.txt : a README in restructured text |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 * examples : examples for your package |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 * setup.py : setup utility for the full package |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 * ./main.py : CLI handler for your webapp |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 * ./model.py : model of a persisted object |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 * ./template.py : a MakeItSo template for project creation |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 * ./tests : doctest suite for the package |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 * ./web.py : a webob web handler |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 """ |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 |
164
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
163
diff
changeset
|
19 # TODO: console_scripts for all of these |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
163
diff
changeset
|
20 |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
21 import os |
168
54f42aa651bc
fix single module interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
166
diff
changeset
|
22 import shutil |
177 | 23 import string |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 import sys |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 from cli import MakeItSoCLI |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
26 from makeitso import ContentTemplate |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 from optparse import OptionParser |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
28 from template import MakeItSoTemplate |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
29 from template import Variable |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
30 |
163 | 31 |
151 | 32 class PythonTemplate(MakeItSoTemplate): |
33 """abstract base class for python-type templates""" | |
160 | 34 vars = [Variable('description'), |
35 Variable('author', 'author of the package'), | |
36 Variable('email', "author's email"), | |
37 Variable('url', 'project url'), | |
38 Variable('repo', 'project repository'), | |
39 ] | |
151 | 40 |
161 | 41 def output2name(self, path): |
42 return os.path.splitext(os.path.basename(path.rstrip(os.path.sep)))[0] | |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
43 |
162 | 44 |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
45 class PythonScriptTemplate(PythonTemplate): |
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
46 """template for a single python script""" |
159 | 47 templates = [('python_package', '{{package}}', '{{main}}.py')] |
160 | 48 vars = [Variable('description')] |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
49 |
162 | 50 |
151 | 51 class PythonModuleTemplate(PythonTemplate): |
131
4922bee3d080
add single module to registered templates
Jeff Hammel <jhammel@mozilla.com>
parents:
122
diff
changeset
|
52 """single module python package""" |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
53 # TODO: this should use the same files as PythonPackageTemplate |
159 | 54 templates = ['python_module', |
55 ('python_package', '{{package}}', '{{main}}.py')] | |
56 vars = [Variable('description')] | |
57 look = False | |
131
4922bee3d080
add single module to registered templates
Jeff Hammel <jhammel@mozilla.com>
parents:
122
diff
changeset
|
58 |
159 | 59 def pre(self, variables, output): |
162 | 60 variables['project'] = variables['module'] = variables['main'] = self.output2name(output) |
61 | |
168
54f42aa651bc
fix single module interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
166
diff
changeset
|
62 def post(self, variables, output): |
54f42aa651bc
fix single module interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
166
diff
changeset
|
63 shutil.move(os.path.join(output, '{{main.py}}'), |
54f42aa651bc
fix single module interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
166
diff
changeset
|
64 os.path.join(output, variables['main'])) |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
65 |
151 | 66 class PythonPackageTemplate(PythonTemplate): |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
67 """ |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
68 python package template |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
69 """ |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
70 name = 'python-package' |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
71 templates = ['python_package'] |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
72 vars = [Variable('description'), |
115
7dbc3cdadffe
add a test for the python-package template
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
73 Variable('author', 'author of the package'), |
7dbc3cdadffe
add a test for the python-package template
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
74 Variable('email', "author's email"), |
159 | 75 Variable('url', 'project url'), |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
76 Variable('repo', 'project repository'), |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
77 ] |
111 | 78 look = False |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
79 |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
80 # things that go in setup.py |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
81 dependencies = {'web.py': ['webob'], |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
82 'template.py': ['MakeItSo']} |
166 | 83 console_scripts = {'main.py': '{{project}} = {{package}}.{{main}}:main', |
84 'template.py': '{{package}}-template = {{package}}.template:main' | |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
85 } |
151 | 86 |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
87 def __init__(self, **kw): |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
88 MakeItSoTemplate.__init__(self, **kw) |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
89 |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
90 # TODO: get the templates you actually care about [maybe from the CLI?] |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
91 |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
92 def pre(self, variables, output): |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
93 """ |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
94 sanitize some variables |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
95 """ |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
96 |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
97 # get project from output directory |
161 | 98 variables['project'] = self.output2name(output) |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
99 |
114
b8d5d2041fe0
stub out project name -> package name
Jeff Hammel <jhammel@mozilla.com>
parents:
111
diff
changeset
|
100 # get package name from project |
177 | 101 allowable = set(string.letters + string.digits + '_') |
102 if not set(variables['project']).issubset(allowable): | |
103 raise AssertionError("Illegal fields in package name") | |
114
b8d5d2041fe0
stub out project name -> package name
Jeff Hammel <jhammel@mozilla.com>
parents:
111
diff
changeset
|
104 variables['package'] = variables['project'].lower() |
b8d5d2041fe0
stub out project name -> package name
Jeff Hammel <jhammel@mozilla.com>
parents:
111
diff
changeset
|
105 |
159 | 106 # name of CLI main file |
165 | 107 variables['main'] = 'main' |
159 | 108 |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
109 # dependencies |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
110 dependencies = set([]) |
111 | 111 for template, dependency in self.dependencies.items(): |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
112 dependencies.update(dependency) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
113 dependencies = list(dependencies) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
114 variables['dependencies'] = dependencies |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
115 |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
116 # console_scripts |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
117 console_scripts = [] |
111 | 118 for template, console_script in self.console_scripts.items(): |
119 console_scripts.append(console_script) | |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
120 if console_scripts: |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
121 s = 'setup(' # placeholder string |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
122 script_strings = ['[console_scripts]'] |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
123 for console_script in console_scripts: |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
124 template = ContentTemplate(console_script) |
178
d6ef91d49609
hopefully do away with some extra variable reentry
Jeff Hammel <k0scist@gmail.com>
parents:
177
diff
changeset
|
125 output = template.substitute(project=variables['project'], |
d6ef91d49609
hopefully do away with some extra variable reentry
Jeff Hammel <k0scist@gmail.com>
parents:
177
diff
changeset
|
126 package=variables['package'], |
d6ef91d49609
hopefully do away with some extra variable reentry
Jeff Hammel <k0scist@gmail.com>
parents:
177
diff
changeset
|
127 main=variables['main']) |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
128 script_strings.append(output) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
129 variables['console_scripts'] = '\n'.join([' ' * len(s) + i |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
130 for i in script_strings]) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
131 else: |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
132 variables['console_scripts'] = '' |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
133 |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
134 |
102
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
135 class PythonPackageCLI(MakeItSoCLI): |
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
136 """ |
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
137 CLI front end for the python package template |
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
138 """ |
122
b2152efec89a
get the description from the docstring if applicable
Jeff Hammel <jhammel@mozilla.com>
parents:
116
diff
changeset
|
139 usage = '%prog [options] project' |
95
e74baa8e6df4
fix CLI interface a bit....write a test for it
Jeff Hammel <jhammel@mozilla.com>
parents:
78
diff
changeset
|
140 |
102
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
141 def main(args=sys.argv[1:]): |
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
142 cli = PythonPackageCLI(PythonPackageTemplate) |
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
143 cli(*args) |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
144 |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
145 if __name__ == '__main__': |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
146 main() |