Exemple #1
0
def init():
    """
    Initialize a new python project with default files.  Default values from herring.conf and directory name.
    """
    defaults = _project_defaults()

    if Project.prompt:
        defaults['name'] = prompt("Enter the project's name:", defaults['name'])
        defaults['package'] = prompt("Enter the project's package:", defaults['package'])
        defaults['author'] = prompt("Enter the project's author:", defaults['author'])
        defaults['author_email'] = prompt("Enter the project's author's email:", defaults['author_email'])
        defaults['description'] = prompt("Enter the project's description:", defaults['description'])

    # print("defaults:\n{defaults}".format(defaults=pformat(defaults)))

    if Project.use_templates:

        template = Template()

        for template_dir in [os.path.abspath(os.path.join(herringlib, 'herringlib', 'templates'))
                             for herringlib in HerringFile.herringlib_paths]:

            info("template directory: %s" % template_dir)
            # noinspection PyArgumentEqualDefault
            template.generate(template_dir, defaults, overwrite=False)
def update():
    """
    Regenerate files (except herringfile) from current templates.
    Delete the file(s) you want to update, then run this task.
    """
    defaults = _project_defaults()

    template = Template()

    for template_dir in [os.path.abspath(os.path.join(herringlib, 'herringlib', 'templates'))
                         for herringlib in HerringFile.herringlib_paths]:

        info("template directory: %s" % template_dir)
        # noinspection PyArgumentEqualDefault
        template.generate(template_dir, defaults, overwrite=False)
def test_template_rendering():
    """
    The goal when rendering is to never lose previous content and to only create a backup when necessary.
    So we test creating backup files as needed:

    * render file with param dict 0 verify no backup
    * render file with param dict 0 verify no backup
    * render file with param dict 1 verify one backup, ~ backup is file rendered with param dict 0
    * render file with param dict 1 verify one backup, ~ backup is file rendered with param dict 0
    * render file with param dict 2 verify two backups, ~ backup is file rendered with param dict 0,
      ~1 backup is file rendered with param dict 1
    * render file with param dict 2 verify two backups, ~ backup is file rendered with param dict 0,
      ~1 backup is file rendered with param dict 1

    The dummy.py.template renders to a script that prints the params hash so we can easily verify content.
    This gives us a round trip:

    * params[n] dict is rendered to dummy.py
    * running dummy.py then eval'ing the output should give a dict equal to the original params[n] dict
    """
    params = [
        {
            'name': 'Foo',
            'package': 'foo',
            'author': 'joe bob',
            'author_email': '*****@*****.**',
            'description': 'Just another foo',
        },
        {
            'name': 'FooBar',
            'package': 'foo',
            'author': 'joe bob',
            'author_email': '*****@*****.**',
            'description': 'Just another foobar',
        },
        {
            'name': 'FooBar',
            'package': 'foobar',
            'author': 'joe bob',
            'author_email': '*****@*****.**',
            'description': 'Just another foobar',
        },
    ]
    dest_dir = os.path.join(os.path.dirname(__file__), 'output')
    if os.path.isdir(dest_dir):
        shutil.rmtree(dest_dir)
    mkdir_p(dest_dir)
    source_name = os.path.join(os.path.dirname(__file__), 'dummy.py.template')
    dest_name = os.path.join(dest_dir, 'dummy.py')

    assert len(backup_files(dest_dir)) == 0

    template = Template()
    with LocalShell() as local:
        template._create_from_template(src_filename=source_name, dest_filename=dest_name, **params[0])
        # dist_dir: ['test.py']
        assert len(backup_files(dest_dir)) == 0
        assert eval(local.run("python {file}".format(file=dest_name))) == params[0]

        template._create_from_template(src_filename=source_name, dest_filename=dest_name, **params[0])
        # dist_dir: ['test.py']
        assert len(backup_files(dest_dir)) == 0
        assert eval(local.run("python {file}".format(file=dest_name))) == params[0]

        template._create_from_template(src_filename=source_name, dest_filename=dest_name, **params[1])
        # dist_dir: ['test.py', 'test.py~']
        assert 'dummy.py~' in backup_files(dest_dir)
        assert len(backup_files(dest_dir)) == 1
        assert eval(local.run("python {file}".format(file=dest_name))) == params[1]
        assert eval(local.run("python {file}~".format(file=dest_name))) == params[0]

        template._create_from_template(src_filename=source_name, dest_filename=dest_name, **params[1])
        # dist_dir: ['test.py', 'test.py~']
        assert 'dummy.py~' in backup_files(dest_dir)
        assert len(backup_files(dest_dir)) == 1
        assert eval(local.run("python {file}".format(file=dest_name))) == params[1]
        assert eval(local.run("python {file}~".format(file=dest_name))) == params[0]

        template._create_from_template(src_filename=source_name, dest_filename=dest_name, **params[2])
        # dist_dir: ['test.py', 'test.py~', 'test.py1~']
        assert 'dummy.py~' in backup_files(dest_dir)
        assert 'dummy.py1~' in backup_files(dest_dir)
        assert len(backup_files(dest_dir)) == 2
        assert eval(local.run("python {file}".format(file=dest_name))) == params[2]
        assert eval(local.run("python {file}1~".format(file=dest_name))) == params[1]
        assert eval(local.run("python {file}~".format(file=dest_name))) == params[0]

        template._create_from_template(src_filename=source_name, dest_filename=dest_name, **params[2])
        # dist_dir: ['test.py', 'test.py~', 'test.py1~']
        assert 'dummy.py~' in backup_files(dest_dir)
        assert 'dummy.py1~' in backup_files(dest_dir)
        assert len(backup_files(dest_dir)) == 2
        assert eval(local.run("python {file}".format(file=dest_name))) == params[2]
        assert eval(local.run("python {file}1~".format(file=dest_name))) == params[1]
        assert eval(local.run("python {file}~".format(file=dest_name))) == params[0]