# HG changeset patch # User Jeff Hammel # Date 1375222838 25200 # Node ID cfd4f1e910903e9e4180ba8228d9b1af2483733e # Parent ff1f41a18da9847b564c4dd60e5c2ebc9b669518 wip diff -r ff1f41a18da9 -r cfd4f1e91090 makeitso/makeitso.py --- a/makeitso/makeitso.py Tue Jul 30 11:08:01 2013 -0700 +++ b/makeitso/makeitso.py Tue Jul 30 15:20:38 2013 -0700 @@ -91,6 +91,7 @@ f, headers = urllib.urlretrieve(uri) # XXX -> urllib2 for timeout return file(f).read() + ### things that deal with variables class MissingVariablesException(Exception): @@ -118,7 +119,7 @@ class ContentTemplate(tempita.Template): """MakeItSo's extension of tempita's Template class""" - + defaults = {'include': include} def __init__(self, content, name=None, interactive=True, variables=None): @@ -129,7 +130,7 @@ # TODO: automagically tell if the program is interactive or not self.interactive = interactive - + tempita.Template.__init__(self, content, name=name) def get_variables(self, **variables): @@ -163,7 +164,7 @@ vars.update(self.read_variables(missing)) else: raise MissingVariablesException(missing) - + def variables(self): """return the variables needed for a template""" return self.missing() @@ -188,7 +189,7 @@ def __init__(self, uri, interactive=True, variables=None): content = include(uri) - + # remove makeitso shebang if it has one if shebang_re.match(content): content = os.linesep.join(content.splitlines()[1:]) @@ -198,7 +199,7 @@ variables['here'] = parent_uri(uri) # TODO: could add other metadata about the uri, # such as last modification time' - + ContentTemplate.__init__(self, content, name=uri, interactive=interactive, variables=variables) @@ -228,7 +229,7 @@ class DirectoryTemplate(ContentTemplate): """template for a directory structure""" - + def __init__(self, directory, interactive=True, variables=None): """ - output : output directory; if None will render in place @@ -246,7 +247,7 @@ assert output # must provide output if os.path.exists(output): assert os.path.isdir(output), "%s: Must be a directory" % self.name - + def missing(self, **variables): vars = self.defaults.copy() vars.update(variables) @@ -264,7 +265,7 @@ missed = ContentTemplate(f).missing(**vars) missing.update(missed) variables.update(dict([(i, '') for i in missed])) - + path = os.path.join(dirpath, f) template = URITemplate(path, interactive=self.interactive) missed = template.missing(**vars) @@ -284,15 +285,15 @@ # make output directory if necessary if output and not os.path.exists(output): os.makedirs(output) - + for dirname, dirnames, filenames in os.walk(self.name): - + # interpolate directory names for d in dirnames: path = os.path.join(dirname, d) interpolated = ContentTemplate(path).substitute(**vars) target = os.path.join(output, interpolated.split(self.name, 1)[-1].strip(os.path.sep)) - + if os.path.exists(target): # ensure its a directory # TODO: check this first before interpolation is in progress diff -r ff1f41a18da9 -r cfd4f1e91090 makeitso/python.py --- a/makeitso/python.py Tue Jul 30 11:08:01 2013 -0700 +++ b/makeitso/python.py Tue Jul 30 15:20:38 2013 -0700 @@ -27,17 +27,24 @@ class PythonTemplate(MakeItSoTemplate): """abstract base class for python-type templates""" + def path2name(self, path): + return os.path.basename(path.rstrip(os.path.sep)) class PythonScriptTemplate(PythonTemplate): """template for a single python script""" - templates = [os.path.join('python_package', '{{package}}', 'main.py')] + templates = [('python_package', '{{package}}', '{{main}}.py')] class PythonModuleTemplate(PythonTemplate): """single module python package""" # TODO: this should use the same files as PythonPackageTemplate - templates = ['python_module'] + templates = ['python_module', + ('python_package', '{{package}}', '{{main}}.py')] + vars = [Variable('description')] + look = False + def pre(self, variables, output): + variables['module'] = variables['main'] = path2name(output) class PythonPackageTemplate(PythonTemplate): """ @@ -48,7 +55,7 @@ vars = [Variable('description'), Variable('author', 'author of the package'), Variable('email', "author's email"), - Variable('url'), + Variable('url', 'project url'), Variable('repo', 'project repository'), ] look = False @@ -56,7 +63,7 @@ # things that go in setup.py dependencies = {'web.py': ['webob'], 'template.py': ['MakeItSo']} - console_scripts = {'main.py': '{{project}} = {{project}}.main:main', + console_scripts = {'main.py': '{{project}} = {{project}}.{{main}}:main', 'template.py': '{{project}}-template = {{project}}.template:main' } @@ -71,13 +78,16 @@ """ # get project from output directory - variables['project'] = os.path.basename(output) + variables['project'] = self.path2name(output) # get package name from project # XXX could have looser restrictions with transforms assert variables['project'].isalnum(), 'Project name must be just letters, you gave %s' % variables['project'] variables['package'] = variables['project'].lower() + # name of CLI main file + variables.setdefault('main', 'main') + # dependencies dependencies = set([]) for template, dependency in self.dependencies.items(): diff -r ff1f41a18da9 -r cfd4f1e91090 makeitso/python_package/{{package}}/__init__.py --- a/makeitso/python_package/{{package}}/__init__.py Tue Jul 30 11:08:01 2013 -0700 +++ b/makeitso/python_package/{{package}}/__init__.py Tue Jul 30 15:20:38 2013 -0700 @@ -1,2 +1,2 @@ # -from main import * +from {{main}} import * diff -r ff1f41a18da9 -r cfd4f1e91090 makeitso/python_package/{{package}}/main.py --- a/makeitso/python_package/{{package}}/main.py Tue Jul 30 11:08:01 2013 -0700 +++ b/makeitso/python_package/{{package}}/main.py Tue Jul 30 15:20:38 2013 -0700 @@ -1,16 +1,22 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """ {{description}} """ +import optparse +import os +import subprocess import sys -import optparse + +def add_options(parser): + """add {{package}} options to an OptionParser instance""" def main(args=sys.argv[1:]): # parse command line options - usage = '%prog [options]' + usage = '%prog [options] ...' class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): """description formatter for console script entry point""" def format_description(self, description): diff -r ff1f41a18da9 -r cfd4f1e91090 makeitso/template.py --- a/makeitso/template.py Tue Jul 30 11:08:01 2013 -0700 +++ b/makeitso/template.py Tue Jul 30 15:20:38 2013 -0700 @@ -16,7 +16,7 @@ class Variable(object): """variable object for MakeItSo templates""" - + def __init__(self, name, description=None, default=Undefined, cast=None): self.name = name @@ -116,6 +116,8 @@ # ensure all of these templates exist self._templates = [] for template in self.templates: + if not isinstance(template, basestring): + template = os.path.join(*template) if template.startswith('http://') or template.startswith('https://'): self._templates.append(template) continue @@ -192,7 +194,7 @@ def post(self, variables, output): """do stuff after interpolation""" - + def read_variables(self, variables): """read variables from stdin""" retval = {} diff -r ff1f41a18da9 -r cfd4f1e91090 setup.py --- a/setup.py Tue Jul 30 11:08:01 2013 -0700 +++ b/setup.py Tue Jul 30 15:20:38 2013 -0700 @@ -36,5 +36,6 @@ [makeitso.templates] python-package = makeitso.python:PythonPackageTemplate python-module = makeitso.python:PythonModuleTemplate + python-script = makeitso.python:PythonScriptTemplate """, )