annotate makeitso/python.py @ 200:5a2edca13b36

wip
author Jeff Hammel <k0scist@gmail.com>
date Tue, 24 Jun 2014 11:44:43 -0700
parents 9b78f52afe4e
children 65684aae6bfe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
c3f719824948 more allowable
Jeff Hammel <k0scist@gmail.com>
parents: 168
diff changeset
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
179
5bd1c50c6f61 works weirdly but kinda works
Jeff Hammel <k0scist@gmail.com>
parents: 178
diff changeset
31 class SetupPy(MakeItSoTemplate):
5bd1c50c6f61 works weirdly but kinda works
Jeff Hammel <k0scist@gmail.com>
parents: 178
diff changeset
32 """template for setup.py"""
180
9b78f52afe4e STUB: makeitso/python.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents: 179
diff changeset
33 templates = [('python_package', 'setup.py')]
163
Jeff Hammel <jhammel@mozilla.com>
parents: 162
diff changeset
34
200
Jeff Hammel <k0scist@gmail.com>
parents: 180
diff changeset
35 class Unittest(MakeItSoTemplate):
Jeff Hammel <k0scist@gmail.com>
parents: 180
diff changeset
36 """template for python unittest"""
Jeff Hammel <k0scist@gmail.com>
parents: 180
diff changeset
37 templates = [('python_package', 'tests', 'test_{{package}}.py')]
Jeff Hammel <k0scist@gmail.com>
parents: 180
diff changeset
38
151
e70421c50d0a move to ABC
Jeff Hammel <jhammel@mozilla.com>
parents: 131
diff changeset
39 class PythonTemplate(MakeItSoTemplate):
e70421c50d0a move to ABC
Jeff Hammel <jhammel@mozilla.com>
parents: 131
diff changeset
40 """abstract base class for python-type templates"""
160
Jeff Hammel <jhammel@mozilla.com>
parents: 159
diff changeset
41 vars = [Variable('description'),
Jeff Hammel <jhammel@mozilla.com>
parents: 159
diff changeset
42 Variable('author', 'author of the package'),
Jeff Hammel <jhammel@mozilla.com>
parents: 159
diff changeset
43 Variable('email', "author's email"),
Jeff Hammel <jhammel@mozilla.com>
parents: 159
diff changeset
44 Variable('url', 'project url'),
Jeff Hammel <jhammel@mozilla.com>
parents: 159
diff changeset
45 Variable('repo', 'project repository'),
Jeff Hammel <jhammel@mozilla.com>
parents: 159
diff changeset
46 ]
151
e70421c50d0a move to ABC
Jeff Hammel <jhammel@mozilla.com>
parents: 131
diff changeset
47
161
Jeff Hammel <jhammel@mozilla.com>
parents: 160
diff changeset
48 def output2name(self, path):
Jeff Hammel <jhammel@mozilla.com>
parents: 160
diff changeset
49 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
50
162
Jeff Hammel <jhammel@mozilla.com>
parents: 161
diff changeset
51
155
386a44a52139 moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents: 151
diff changeset
52 class PythonScriptTemplate(PythonTemplate):
386a44a52139 moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents: 151
diff changeset
53 """template for a single python script"""
159
Jeff Hammel <jhammel@mozilla.com>
parents: 155
diff changeset
54 templates = [('python_package', '{{package}}', '{{main}}.py')]
160
Jeff Hammel <jhammel@mozilla.com>
parents: 159
diff changeset
55 vars = [Variable('description')]
155
386a44a52139 moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents: 151
diff changeset
56
162
Jeff Hammel <jhammel@mozilla.com>
parents: 161
diff changeset
57
151
e70421c50d0a move to ABC
Jeff Hammel <jhammel@mozilla.com>
parents: 131
diff changeset
58 class PythonModuleTemplate(PythonTemplate):
131
4922bee3d080 add single module to registered templates
Jeff Hammel <jhammel@mozilla.com>
parents: 122
diff changeset
59 """single module python package"""
155
386a44a52139 moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents: 151
diff changeset
60 # TODO: this should use the same files as PythonPackageTemplate
159
Jeff Hammel <jhammel@mozilla.com>
parents: 155
diff changeset
61 templates = ['python_module',
Jeff Hammel <jhammel@mozilla.com>
parents: 155
diff changeset
62 ('python_package', '{{package}}', '{{main}}.py')]
Jeff Hammel <jhammel@mozilla.com>
parents: 155
diff changeset
63 vars = [Variable('description')]
Jeff Hammel <jhammel@mozilla.com>
parents: 155
diff changeset
64 look = False
131
4922bee3d080 add single module to registered templates
Jeff Hammel <jhammel@mozilla.com>
parents: 122
diff changeset
65
159
Jeff Hammel <jhammel@mozilla.com>
parents: 155
diff changeset
66 def pre(self, variables, output):
162
Jeff Hammel <jhammel@mozilla.com>
parents: 161
diff changeset
67 variables['project'] = variables['module'] = variables['main'] = self.output2name(output)
Jeff Hammel <jhammel@mozilla.com>
parents: 161
diff changeset
68
168
54f42aa651bc fix single module interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 166
diff changeset
69 def post(self, variables, output):
54f42aa651bc fix single module interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 166
diff changeset
70 shutil.move(os.path.join(output, '{{main.py}}'),
54f42aa651bc fix single module interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 166
diff changeset
71 os.path.join(output, variables['main']))
155
386a44a52139 moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents: 151
diff changeset
72
151
e70421c50d0a move to ABC
Jeff Hammel <jhammel@mozilla.com>
parents: 131
diff changeset
73 class PythonPackageTemplate(PythonTemplate):
78
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74 """
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
75 python package template
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
76 """
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
77 name = 'python-package'
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
78 templates = ['python_package']
108
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
79 vars = [Variable('description'),
115
7dbc3cdadffe add a test for the python-package template
Jeff Hammel <jhammel@mozilla.com>
parents: 114
diff changeset
80 Variable('author', 'author of the package'),
7dbc3cdadffe add a test for the python-package template
Jeff Hammel <jhammel@mozilla.com>
parents: 114
diff changeset
81 Variable('email', "author's email"),
159
Jeff Hammel <jhammel@mozilla.com>
parents: 155
diff changeset
82 Variable('url', 'project url'),
108
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
83 Variable('repo', 'project repository'),
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
84 ]
111
bad70fb08b15 minor syntax corrections
Jeff Hammel <jhammel@mozilla.com>
parents: 109
diff changeset
85 look = False
78
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
86
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
87 # things that go in setup.py
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
88 dependencies = {'web.py': ['webob'],
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
89 'template.py': ['MakeItSo']}
166
0c95d7f25d9b makeitso/python.py
Jeff Hammel <jhammel@mozilla.com>
parents: 165
diff changeset
90 console_scripts = {'main.py': '{{project}} = {{package}}.{{main}}:main',
0c95d7f25d9b makeitso/python.py
Jeff Hammel <jhammel@mozilla.com>
parents: 165
diff changeset
91 'template.py': '{{package}}-template = {{package}}.template:main'
78
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
92 }
151
e70421c50d0a move to ABC
Jeff Hammel <jhammel@mozilla.com>
parents: 131
diff changeset
93
78
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
94 def __init__(self, **kw):
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
95 MakeItSoTemplate.__init__(self, **kw)
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
96
109
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
97 # 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
98
108
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
99 def pre(self, variables, output):
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
100 """
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
101 sanitize some variables
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
102 """
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
103
109
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
104 # get project from output directory
161
Jeff Hammel <jhammel@mozilla.com>
parents: 160
diff changeset
105 variables['project'] = self.output2name(output)
109
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
106
114
b8d5d2041fe0 stub out project name -> package name
Jeff Hammel <jhammel@mozilla.com>
parents: 111
diff changeset
107 # get package name from project
177
c3f719824948 more allowable
Jeff Hammel <k0scist@gmail.com>
parents: 168
diff changeset
108 allowable = set(string.letters + string.digits + '_')
c3f719824948 more allowable
Jeff Hammel <k0scist@gmail.com>
parents: 168
diff changeset
109 if not set(variables['project']).issubset(allowable):
c3f719824948 more allowable
Jeff Hammel <k0scist@gmail.com>
parents: 168
diff changeset
110 raise AssertionError("Illegal fields in package name")
114
b8d5d2041fe0 stub out project name -> package name
Jeff Hammel <jhammel@mozilla.com>
parents: 111
diff changeset
111 variables['package'] = variables['project'].lower()
b8d5d2041fe0 stub out project name -> package name
Jeff Hammel <jhammel@mozilla.com>
parents: 111
diff changeset
112
159
Jeff Hammel <jhammel@mozilla.com>
parents: 155
diff changeset
113 # name of CLI main file
165
Jeff Hammel <jhammel@mozilla.com>
parents: 164
diff changeset
114 variables['main'] = 'main'
159
Jeff Hammel <jhammel@mozilla.com>
parents: 155
diff changeset
115
108
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
116 # dependencies
109
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
117 dependencies = set([])
111
bad70fb08b15 minor syntax corrections
Jeff Hammel <jhammel@mozilla.com>
parents: 109
diff changeset
118 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
119 dependencies.update(dependency)
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
120 dependencies = list(dependencies)
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
121 variables['dependencies'] = dependencies
155
386a44a52139 moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents: 151
diff changeset
122
108
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
123 # console_scripts
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
124 console_scripts = []
111
bad70fb08b15 minor syntax corrections
Jeff Hammel <jhammel@mozilla.com>
parents: 109
diff changeset
125 for template, console_script in self.console_scripts.items():
bad70fb08b15 minor syntax corrections
Jeff Hammel <jhammel@mozilla.com>
parents: 109
diff changeset
126 console_scripts.append(console_script)
109
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
127 if console_scripts:
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
128 s = 'setup(' # placeholder string
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
129 script_strings = ['[console_scripts]']
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
130 for console_script in console_scripts:
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
131 template = ContentTemplate(console_script)
178
d6ef91d49609 hopefully do away with some extra variable reentry
Jeff Hammel <k0scist@gmail.com>
parents: 177
diff changeset
132 output = template.substitute(project=variables['project'],
d6ef91d49609 hopefully do away with some extra variable reentry
Jeff Hammel <k0scist@gmail.com>
parents: 177
diff changeset
133 package=variables['package'],
d6ef91d49609 hopefully do away with some extra variable reentry
Jeff Hammel <k0scist@gmail.com>
parents: 177
diff changeset
134 main=variables['main'])
109
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
135 script_strings.append(output)
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
136 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
137 for i in script_strings])
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
138 else:
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
139 variables['console_scripts'] = ''
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
140
108
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
141
102
ad5fd3eb6674 template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
142 class PythonPackageCLI(MakeItSoCLI):
ad5fd3eb6674 template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
143 """
ad5fd3eb6674 template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
144 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
145 """
122
b2152efec89a get the description from the docstring if applicable
Jeff Hammel <jhammel@mozilla.com>
parents: 116
diff changeset
146 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
147
102
ad5fd3eb6674 template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
148 def main(args=sys.argv[1:]):
ad5fd3eb6674 template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
149 cli = PythonPackageCLI(PythonPackageTemplate)
ad5fd3eb6674 template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
150 cli(*args)
78
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
151
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
152 if __name__ == '__main__':
155
386a44a52139 moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents: 151
diff changeset
153 main()