annotate makeitso/python.py @ 228:a0a86aa9a031

one more time
author Jeff Hammel <k0scist@gmail.com>
date Mon, 14 Dec 2015 14:09:01 -0800
parents 65684aae6bfe
children a1a615aae3d1
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
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
31
179
5bd1c50c6f61 works weirdly but kinda works
Jeff Hammel <k0scist@gmail.com>
parents: 178
diff changeset
32 class SetupPy(MakeItSoTemplate):
5bd1c50c6f61 works weirdly but kinda works
Jeff Hammel <k0scist@gmail.com>
parents: 178
diff changeset
33 """template for setup.py"""
180
9b78f52afe4e STUB: makeitso/python.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents: 179
diff changeset
34 templates = [('python_package', 'setup.py')]
163
Jeff Hammel <jhammel@mozilla.com>
parents: 162
diff changeset
35
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
36
200
Jeff Hammel <k0scist@gmail.com>
parents: 180
diff changeset
37 class Unittest(MakeItSoTemplate):
Jeff Hammel <k0scist@gmail.com>
parents: 180
diff changeset
38 """template for python unittest"""
Jeff Hammel <k0scist@gmail.com>
parents: 180
diff changeset
39 templates = [('python_package', 'tests', 'test_{{package}}.py')]
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
40 def pre(self, variables, output):
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
41 if 'package' not in variables:
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
42 package = os.path.splitext(os.path.basename(output.rstrip(os.path.sep)))[0]
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
43 indicator = 'test_'
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
44 if package.startswith(indicator):
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
45 package = package[len(indicator):]
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
46 variables['package'] = package
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
47
200
Jeff Hammel <k0scist@gmail.com>
parents: 180
diff changeset
48
151
e70421c50d0a move to ABC
Jeff Hammel <jhammel@mozilla.com>
parents: 131
diff changeset
49 class PythonTemplate(MakeItSoTemplate):
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
50 """abstract base class for python-type templates"""
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
51 vars = [Variable('description'),
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
52 Variable('author', 'author of the package'),
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
53 Variable('email', "author's email"),
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
54 Variable('url', 'project url'),
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
55 Variable('repo', 'project repository'),
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
56 ]
151
e70421c50d0a move to ABC
Jeff Hammel <jhammel@mozilla.com>
parents: 131
diff changeset
57
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
58 def output2name(self, path):
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
59 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
60
162
Jeff Hammel <jhammel@mozilla.com>
parents: 161
diff changeset
61
155
386a44a52139 moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents: 151
diff changeset
62 class PythonScriptTemplate(PythonTemplate):
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
63 """template for a single python script"""
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
64 templates = [('python_package', '{{package}}', '{{main}}.py')]
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
65 vars = [Variable('description')]
155
386a44a52139 moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents: 151
diff changeset
66
162
Jeff Hammel <jhammel@mozilla.com>
parents: 161
diff changeset
67
151
e70421c50d0a move to ABC
Jeff Hammel <jhammel@mozilla.com>
parents: 131
diff changeset
68 class PythonModuleTemplate(PythonTemplate):
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
69 """single module python package"""
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
70
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
71 templates = ['python_module',
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
72 ('python_package', '{{package}}', '{{main}}.py')]
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
73 vars = [Variable('description')]
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
74 look = False
131
4922bee3d080 add single module to registered templates
Jeff Hammel <jhammel@mozilla.com>
parents: 122
diff changeset
75
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
76 def pre(self, variables, output):
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
77 variables['project'] = variables['module'] = variables['main'] = self.output2name(output)
162
Jeff Hammel <jhammel@mozilla.com>
parents: 161
diff changeset
78
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
79 def post(self, variables, output):
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
80 shutil.move(os.path.join(output, '{{main.py}}'),
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
81 os.path.join(output, variables['main']))
155
386a44a52139 moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents: 151
diff changeset
82
151
e70421c50d0a move to ABC
Jeff Hammel <jhammel@mozilla.com>
parents: 131
diff changeset
83 class PythonPackageTemplate(PythonTemplate):
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
84 """
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
85 python package template
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
86 """
109
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
87 # 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
88
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
89 name = 'python-package'
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
90 templates = ['python_package']
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
91 vars = [Variable('description'),
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
92 Variable('author', 'author of the package'),
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
93 Variable('email', "author's email"),
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
94 Variable('url', 'project url'),
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
95 Variable('repo', 'project repository'),
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
96 ]
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
97 look = False
109
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
98
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
99 # things that go in setup.py
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
100 dependencies = {'web.py': ['webob'],
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
101 'template.py': ['MakeItSo']}
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
102 console_scripts = {'main.py': '{{project}} = {{package}}.{{main}}:main',
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
103 'template.py': '{{package}}-template = {{package}}.template:main'
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
104 }
114
b8d5d2041fe0 stub out project name -> package name
Jeff Hammel <jhammel@mozilla.com>
parents: 111
diff changeset
105
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
106 def pre(self, variables, output):
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
107 """
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
108 sanitize some variables
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
109 """
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
110
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
111 # get project from output directory
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
112 variables['project'] = self.output2name(output)
159
Jeff Hammel <jhammel@mozilla.com>
parents: 155
diff changeset
113
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
114 # get package name from project
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
115 allowable = set(string.letters + string.digits + '_')
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
116 if not set(variables['project']).issubset(allowable):
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
117 raise AssertionError("Illegal fields in package name")
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
118 variables['package'] = variables['project'].lower()
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
119
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
120 # name of CLI main file
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
121 variables['main'] = 'main'
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
122
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
123 # package dependencies
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
124 dependencies = set([])
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
125 for template, dependency in self.dependencies.items():
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
126 dependencies.update(dependency)
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
127 variables['dependencies'] = list(dependencies)
155
386a44a52139 moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents: 151
diff changeset
128
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
129 # console_scripts
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
130 console_scripts = []
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
131 for template, console_script in self.console_scripts.items():
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
132 console_scripts.append(console_script)
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
133 if console_scripts:
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
134 s = 'setup(' # placeholder string
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
135 script_strings = ['[console_scripts]']
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
136 for console_script in console_scripts:
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
137 template = ContentTemplate(console_script)
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
138 output = template.substitute(project=variables['project'],
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
139 package=variables['package'],
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
140 main=variables['main'])
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
141 script_strings.append(output)
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
142 variables['console_scripts'] = '\n'.join([' ' * len(s) + i
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
143 for i in script_strings])
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
144 else:
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
145 variables['console_scripts'] = ''
109
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
146
108
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
147
102
ad5fd3eb6674 template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
148 class PythonPackageCLI(MakeItSoCLI):
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
149 """
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
150 CLI front end for the python package template
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
151 """
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
152 usage = '%prog [options] project'
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
153
95
e74baa8e6df4 fix CLI interface a bit....write a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 78
diff changeset
154
102
ad5fd3eb6674 template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
155 def main(args=sys.argv[1:]):
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
156 """CLI"""
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
157 cli = PythonPackageCLI(PythonPackageTemplate)
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
158 cli(*args)
78
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
159
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
160 if __name__ == '__main__':
201
65684aae6bfe make unittest template smarter
Jeff Hammel <k0scist@gmail.com>
parents: 200
diff changeset
161 main()