Mercurial > hg > MakeItSo
annotate makeitso/script2package.py @ 181:7ac8571788e2
add script to make templates from files
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 23 Mar 2014 11:52:47 -0700 |
parents | 668b01d04457 |
children |
rev | line source |
---|---|
164
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 """ |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 make a package from a .py file |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 """ |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 ### STUB ### |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 # TODO: |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 # - thing to make a setup.py from a .py file |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 # - use makeitso templates -> directory structure |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 import optparse |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 import os |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 import subprocess |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 import sys |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 |
174
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
18 from .python import PythonModuleTemplate, PythonPackageTemplate |
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
19 |
176
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
20 ### name transformers.... -> ??? |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
21 |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
22 def scriptname2packagename(script): |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
23 return os.path.splitext(os.path.basename(script))[0] |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
24 |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
25 |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
26 |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
27 ### CLI parsing |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
28 |
164
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 def add_options(parser): |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
30 """add options to the OptionParser instance""" |
174
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
31 # TODO: replace with `configuration` package |
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
32 |
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
33 parser.add_option('-m', '--module', dest='py_module', |
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
34 action='store_true', default=False, |
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
35 help="create a single-module package with py_modules in setup.py") |
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
36 parser.add_option('-n', '--name', dest='name', |
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
37 help="Name of package; default taken from script name") |
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
38 parser.add_option('-o', '--output', dest='output', |
176
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
39 default=os.getcwd(), |
174
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
40 help="where to output the resulting package [DEFAULT: '.']") |
164
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
41 |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
42 def main(args=sys.argv[1:]): |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
43 |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
44 # parse command line options |
174
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
45 usage = '%prog [options] script.py' |
164
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
46 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
47 """description formatter for console script entry point""" |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
48 def format_description(self, description): |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
49 if description: |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
50 return description.strip() + '\n' |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
51 else: |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
52 return '' |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
53 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) |
174
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
54 add_options(parser) |
164
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
55 options, args = parser.parse_args(args) |
174
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
56 if len(args) != 1: |
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
57 parser.error("Please specify a source script") |
176
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
58 script = args[0] |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
59 |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
60 # Get package name from script |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
61 if not options.name: |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
62 options.name = scriptname2packagename(script) |
174
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
63 |
175
5fa35ff86644
STUB: makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
174
diff
changeset
|
64 # require a directory (for now) |
176
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
65 if os.path.exists(options.output): |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
66 if not os.path.isdir(options.output): |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
67 parser.error("'%s' is a file" % options.output) |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
68 options.output = os.path.join(options.output, options.name) |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
69 # XXX bad naming |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
70 else: |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
71 raise NotImplementedError("TODO") |
175
5fa35ff86644
STUB: makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
174
diff
changeset
|
72 |
174
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
73 # configure template |
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
74 template = PythonModuleTemplate if options.py_module else PythonPackageTemplate |
176
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
75 template = template() |
174
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
76 |
176
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
77 # get some variables: |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
78 # - author |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
79 # - description |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
80 # - email |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
81 # - repo |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
82 # - url |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
83 variables = {} |
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
84 |
174
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
85 # interpolate template |
176
668b01d04457
STUB: makeitso/makeitso.py makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
175
diff
changeset
|
86 template.substitute() |
175
5fa35ff86644
STUB: makeitso/script2package.py
Jeff Hammel <k0scist@gmail.com>
parents:
174
diff
changeset
|
87 import pdb; pdb.set_trace() |
174
aed8c4af5f26
STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents:
164
diff
changeset
|
88 # TODO |
164
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
89 |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
90 if __name__ == '__main__': |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
91 main() |