view examples/doctest.txt @ 208:f7f7831f7c8c

add UTF-8 disclaimer
author Jeff Hammel <k0scist@gmail.com>
date Tue, 10 Feb 2015 13:19:01 -0800
parents de1ecefe301c
children
line wrap: on
line source

MakeItSo!
=========

A filesystem templating system for rapid deploy of projects

Boilerplate::

    >>> import makeitso
    >>> import os
    >>> import shutil
    >>> import sys
    >>> import tempfile
    >>> from subprocess import call
    >>> from StringIO import StringIO

Basic functionality::

    >>> example = os.path.join(here, 'example.txt')
    >>> template = makeitso.PolyTemplate([example], interactive=False)
    >>> template.missing()
    set(['name'])
    >>> template.substitute(dict(name='foo'))
    Hello foo
    
Substitute to a buffer::

    >>> buffer = StringIO()
    >>> template = makeitso.PolyTemplate([example], interactive=False)
    >>> template.substitute(dict(name='bar'), output=buffer)
    >>> buffer.getvalue().strip()
    'Hello bar'

Substitute to a file::

    >>> buffer = tempfile.mktemp()
    >>> template = makeitso.PolyTemplate([example], interactive=False)
    >>> template.substitute(dict(name='fleem'), buffer)
    >>> file(buffer).read().strip()
    'Hello fleem'
    >>> os.remove(buffer)

Including a file::

    >>> include_example = os.path.join(here, 'include-example.txt')
    >>> buffer = tempfile.mktemp()
    >>> template = makeitso.PolyTemplate([include_example], interactive=False)
    >>> template.substitute({}, buffer)
    >>> os.remove(buffer)

Directory case::

    >>> exampledir = os.path.join(here, 'directory-example')
    >>> tempdir = tempfile.mkdtemp()
    >>> template = makeitso.PolyTemplate([exampledir], interactive=False)
    >>> sorted(template.missing())
    ['bar', 'foo', 'subdir']
    >>> template.substitute(dict(foo='It', bar='life', subdir='mysubdir'), output=tempdir)
    >>> sorted(os.listdir(tempdir))
    ['foo.txt', 'mysubdir']
    >>> file(os.path.join(tempdir, 'foo.txt')).read().strip()
    'It is a wonderful life'
    >>> os.listdir(os.path.join(tempdir, 'mysubdir'))
    ['life.txt']
    >>> file(os.path.join(tempdir, 'mysubdir', 'life.txt')).read().strip()
    'It'
    >>> shutil.rmtree(tempdir)

Mixed case::

    >>> template = makeitso.PolyTemplate([example, exampledir], interactive=False)
    >>> variables = sorted(template.missing())
    >>> variables
    ['bar', 'foo', 'name', 'subdir']

You need to provide output for mixing files and directory templates::

    >>> variables = dict([(i, i.title()) for i in variables])
    >>> try:
    ...    template.substitute(variables)
    ... except AssertionError, e:
    ...    pass
    >>> e
    AssertionError()

Provide an output::

    >>> template = makeitso.PolyTemplate([example, exampledir], interactive=False)
    >>> template.substitute(variables, tempdir)
    >>> sorted(os.listdir(tempdir))
    ['Subdir', 'example.txt', 'foo.txt']
    >>> shutil.rmtree(tempdir)

Test API templates::

    >>> from makeitso.template import MakeItSoTemplate, Variable
    >>> class MyTemplate(MakeItSoTemplate):
    ...    name = 'foo'
    ...    templates = ['example.txt']
    ...    vars = [Variable(name='name', default='bar')]
    >>> buffer = tempfile.mktemp()
    >>> apitemplate = MyTemplate(interactive=False)
    >>> apitemplate.missing()
    set([])
    >>> apitemplate.substitute({}, buffer)
    >>> file(buffer).read().strip()
    'Hello bar'
    >>> os.remove(buffer)

If you dont use the defaults, then you will get a missing variable::

    >>> apitemplate.usedefaults = False
    >>> apitemplate.missing()
    set(['name'])
    >>> try:
    ...     apitemplate.substitute({})
    ... except Exception, e:
    ...     pass
    >>> isinstance(e, makeitso.MissingVariablesException)
    True

Test CLI handler:

    >>> from makeitso.cli import MakeItSoCLI
    >>> cli = MakeItSoCLI(MyTemplate)
    >>> buffer = tempfile.mktemp()
    >>> cli(buffer)
    >>> os.remove(buffer)

Test to make sure permissions are preserved. This won't work on windows::

    >>> buffer = tempfile.mktemp()
    >>> f = file(buffer, 'w')
    >>> print >> f, '#!/bin/bash\necho foo'
    >>> f.close()
    >>> os.chmod(buffer, 0755)
    >>> uritemplate = makeitso.URITemplate(example, interactive=False)
    >>> uritemplate.substitute(dict(name='bar'), buffer)
    >>> ('%o' % os.stat(buffer).st_mode).endswith('755')
    True
    >>> os.remove(buffer)

Test python package template using the entry point::

    >>> 'python-package' in makeitso.get_entry_points()
    True
    >>> template = makeitso.PolyTemplate(['python-package'], interactive=False)
    >>> missing = template.missing()
    >>> sorted(missing)
    ['author', 'description', 'email', 'repo', 'url']
    >>> tempdir = tempfile.mkdtemp()
    >>> variables = dict([(i, i) for i in missing])
    >>> projdir = os.path.join(tempdir, 'MyProject')
    >>> template.substitute(variables, output=projdir)

Make sure you actually get a package::

    >>> cwd = os.getcwd()
    >>> os.chdir(projdir)
    >>> os.path.exists('setup.py')
    True
    >>> call([sys.executable, 'setup.py', 'egg_info'])
    0
    >>> call([sys.executable, 'setup.py', 'sdist'])
    0
    >>> os.path.exists('dist') and os.path.isdir('dist')
    True
    >>> len(os.listdir('dist')) > 0
    True
    >>> os.chdir(cwd)
    >>> shutil.rmtree(tempdir)