def test_generate_file_with_true_conditional(env):
    infile = "tests/files/{% if generate_file == 'y' %}cheese.txt{% endif %}"
    generate.generate_file(project_dir=".", infile=infile, context={"generate_file": "y"}, env=env)
    assert os.path.isfile("tests/files/cheese.txt")
    with open("tests/files/cheese.txt", "rt") as f:
        generated_text = f.read()
        assert generated_text == "Testing that generate_file was y"
def test_generate_file(env):
    infile = "tests/files/{{generate_file}}.txt"
    generate.generate_file(project_dir=".", infile=infile, context={"generate_file": "cheese"}, env=env)
    assert os.path.isfile("tests/files/cheese.txt")
    with open("tests/files/cheese.txt", "rt") as f:
        generated_text = f.read()
        assert generated_text == "Testing cheese"
Exemplo n.º 3
0
def test_generate_file_with_false_condition(env):
    infile = 'tests/files/{% if generate_file == \'y\' %}cheese.txt{% endif %}'
    generate.generate_file(project_dir=".",
                           infile=infile,
                           context={'generate_file': 'n'},
                           env=env)
    assert not os.path.exists('tests/files/cheese.txt')
Exemplo n.º 4
0
def test_generate_file_with_false_condition(env):
    infile = "tests/files/{% if generate_file == 'y' %}cheese.txt{% endif %}"
    generate.generate_file(project_dir=".",
                           infile=infile,
                           context={"generate_file": "n"},
                           env=env)
    assert not os.path.exists("tests/files/cheese.txt")
Exemplo n.º 5
0
 def test_generate_file(self):
     env = Environment()
     env.loader = FileSystemLoader(".")
     infile = "tests/files/{{generate_file}}.txt"
     generate.generate_file(project_dir=".", infile=infile, context={"generate_file": "cheese"}, env=env)
     self.assertTrue(os.path.isfile("tests/files/cheese.txt"))
     with open("tests/files/cheese.txt", "rt") as f:
         generated_text = f.read()
         self.assertEqual(generated_text, "Testing cheese")
Exemplo n.º 6
0
def test_generate_file_with_false_condition(env):
    infile = 'tests/files/{% if generate_file == \'y\' %}cheese.txt{% endif %}'
    generate.generate_file(
        project_dir=".",
        infile=infile,
        context={'generate_file': 'n'},
        env=env
    )
    assert not os.path.exists('tests/files/cheese.txt')
Exemplo n.º 7
0
def test_generate_file_with_false_condition(env):
    """Verify correct work of boolean condition in file name on file generation.

    This test has negative answer, so file should not be rendered.
    """
    infile = 'tests/files/{% if generate_file == \'y\' %}cheese.txt{% endif %}'
    generate.generate_file(
        project_dir=".", infile=infile, context={'generate_file': 'n'}, env=env
    )
    assert not os.path.isfile('tests/files/cheese.txt')
def test_generate_file_random_ascii_string(env, length, punctuation):
    """Verify correct work of random_ascii_string extension on file generation."""
    infile = 'tests/files/{{cookiecutter.random_string_file}}.txt'
    data = {'random_string_file': 'cheese'}
    context = {"cookiecutter": data, "length": length, "punctuation": punctuation}
    generate.generate_file(project_dir=".", infile=infile, context=context, env=env)
    assert os.path.isfile('tests/files/cheese.txt')
    with open('tests/files/cheese.txt', 'rt') as f:
        generated_text = f.read()
        assert len(generated_text) == length
Exemplo n.º 9
0
def test_generate_file(env):
    """Verify simple file is generated with rendered context data."""
    infile = 'tests/files/{{generate_file}}.txt'
    generate.generate_file(
        project_dir=".", infile=infile, context={'generate_file': 'cheese'}, env=env
    )
    assert os.path.isfile('tests/files/cheese.txt')
    with open('tests/files/cheese.txt', 'rt') as f:
        generated_text = f.read()
        assert generated_text == 'Testing cheese'
Exemplo n.º 10
0
def test_generate_file_with_true_conditional(env):
    infile = "tests/files/{% if generate_file == 'y' %}cheese.txt{% endif %}"
    generate.generate_file(project_dir=".",
                           infile=infile,
                           context={"generate_file": "y"},
                           env=env)
    assert os.path.isfile("tests/files/cheese.txt")
    with open("tests/files/cheese.txt", "rt") as f:
        generated_text = f.read()
        assert generated_text == "Testing that generate_file was y"
Exemplo n.º 11
0
def test_generate_file_with_true_conditional(env):
    infile = 'tests/files/{% if generate_file == \'y\' %}cheese.txt{% endif %}'
    generate.generate_file(project_dir=".",
                           infile=infile,
                           context={'generate_file': 'y'},
                           env=env)
    assert os.path.isfile('tests/files/cheese.txt')
    with open('tests/files/cheese.txt', 'rt') as f:
        generated_text = f.read()
        assert generated_text == 'Testing that generate_file was y'
Exemplo n.º 12
0
def test_generate_file_verbose_template_syntax_error(env, expected_msg_regex):
    """Verify correct exception raised on syntax error in file before generation."""
    with pytest.raises(TemplateSyntaxError) as exception:
        generate.generate_file(
            project_dir=".",
            infile='tests/files/syntax_error.txt',
            context={'syntax_error': 'syntax_error'},
            env=env,
        )
    assert expected_msg_regex.match(str(exception.value))
Exemplo n.º 13
0
def test_generate_file(env):
    infile = 'tests/files/{{generate_file}}.txt'
    generate.generate_file(project_dir=".",
                           infile=infile,
                           context={'generate_file': 'cheese'},
                           env=env)
    assert os.path.isfile('tests/files/cheese.txt')
    with open('tests/files/cheese.txt', 'rt') as f:
        generated_text = f.read()
        assert generated_text == 'Testing cheese'
def test_generate_file_verbose_template_syntax_error(env, expected_msg):
    try:
        generate.generate_file(
            project_dir=".", infile="tests/files/syntax_error.txt", context={"syntax_error": "syntax_error"}, env=env
        )
    except TemplateSyntaxError as exception:
        assert str(exception) == expected_msg
    except Exception as exception:
        pytest.fail("Unexpected exception thrown: {0}".format(exception))
    else:
        pytest.fail("TemplateSyntaxError not thrown")
Exemplo n.º 15
0
 def test_generate_conditional_file_no_create(self):
     env = Environment()
     env.loader = FileSystemLoader('.')
     infile = 'tests/files/{% if cheese %}{{generate_file}}.txt{% endif %}'
     generate.generate_file(
         project_dir=".",
         infile=infile,
         context={'cheese': False, 'generate_file': 'cheese'},
         env=env
     )
     self.assertFalse(os.path.isfile('tests/files/cheese.txt'))
Exemplo n.º 16
0
def recut():
    """
    Recreate setup.py so that we can edit keywords
    Remove unnecessary code examples
    """
    # template location
    try:
        # cutting cookie from directory with template
        temp_dir = find.find_template('..')
    except NonTemplatedInputDirException as e:
        # template coming from Github
        # Hooks are passed through jinja2. raw will
        # Make sure `cookiecutter.project` isn't replaced
        {% raw %}
        temp_dir = os.path.join(config['cookiecutters_dir'],
                                'cookiecutter-ckan-extension',
                                '{{cookiecutter.project}}')
        {% endraw %}

    # Location for resulting file
    destination = os.getcwd()
    # name of template
    setup_template = 'setup.py'

    # get context
    context = {{ cookiecutter | jsonify }}

    # Process keywords
    keywords = context['keywords'].strip().split()
    keywords = [keyword for keyword in keywords
                if keyword not in ('ckan', 'CKAN', 'A', 'space',
                                   'seperated', 'list', 'of', 'keywords')]
    keywords.insert(0, 'CKAN')
    keywords = u' '.join(keywords)
    context['keywords'] = keywords

    # Double check 'project_shortname' and 'plugin_class_name'
    short_name = context['project'][8:].replace('-','_')
    if context['project_shortname'] != short_name:
        context['project_shortname'] = short_name

    plugin_class_name = '{}Plugin'.format(context['project_shortname']
                        .title().replace('_', ''))
    if context['plugin_class_name'] != plugin_class_name:
        context['plugin_class_name'] = plugin_class_name
    # Recut cookie
    env = StrictEnvironment()
    env.loader = jinja2.FileSystemLoader(temp_dir)
    gen.generate_file(project_dir=destination,
                      infile=setup_template,
                      context={'cookiecutter': context},
                      env=env)
    if not asbool(context['include_examples']):
        remove_code_examples(os.path.join(destination, 'ckanext', short_name))
Exemplo n.º 17
0
def test_generate_file_jsonify_filter(env):
    infile = 'tests/files/{{cookiecutter.jsonify_file}}.txt'
    data = {'jsonify_file': 'cheese', 'type': 'roquefort'}
    generate.generate_file(project_dir=".",
                           infile=infile,
                           context={'cookiecutter': data},
                           env=env)
    assert os.path.isfile('tests/files/cheese.txt')
    with open('tests/files/cheese.txt', 'rt') as f:
        generated_text = f.read()
        assert json.loads(generated_text) == data
Exemplo n.º 18
0
 def insert_license_file():
     """
     Given `context["open_source_license"]`, insert the corresponding
     license file into the generated project's root.
     """
     generate_file(
         project_dir="..",
         infile="LICENSE",
         context=self.context,
         env=self.env
     )
Exemplo n.º 19
0
def test_generate_file_jsonify_filter(env):
    infile = "tests/files/{{cookiecutter.jsonify_file}}.txt"
    data = {"jsonify_file": "cheese", "type": "roquefort"}
    generate.generate_file(project_dir=".",
                           infile=infile,
                           context={"cookiecutter": data},
                           env=env)
    assert os.path.isfile("tests/files/cheese.txt")
    with open("tests/files/cheese.txt", "rt") as f:
        generated_text = f.read()
        assert json.loads(generated_text) == data
Exemplo n.º 20
0
def test_generate_file(env):
    infile = 'tests/files/{{generate_file}}.txt'
    generate.generate_file(
        project_dir=".",
        infile=infile,
        context={'generate_file': 'cheese'},
        env=env
    )
    assert os.path.isfile('tests/files/cheese.txt')
    with open('tests/files/cheese.txt', 'rt') as f:
        generated_text = f.read()
        assert generated_text == 'Testing cheese'
Exemplo n.º 21
0
def test_generate_file_verbose_template_syntax_error(env, expected_msg):
    try:
        generate.generate_file(project_dir=".",
                               infile='tests/files/syntax_error.txt',
                               context={'syntax_error': 'syntax_error'},
                               env=env)
    except TemplateSyntaxError as exception:
        assert str(exception) == expected_msg
    except Exception as exception:
        pytest.fail('Unexpected exception thrown: {0}'.format(exception))
    else:
        pytest.fail('TemplateSyntaxError not thrown')
Exemplo n.º 22
0
def test_generate_file(env):
    infile = "tests/files/{{generate_file}}.txt"
    generate.generate_file(
        project_dir=".",
        infile=infile,
        context={"generate_file": "cheese"},
        env=env,
    )
    assert os.path.isfile("tests/files/cheese.txt")
    with open("tests/files/cheese.txt", "rt") as f:
        generated_text = f.read()
        assert generated_text == "Testing cheese"
Exemplo n.º 23
0
def test_generate_file_with_true_conditional(env):
    infile = 'tests/files/{% if generate_file == \'y\' %}cheese.txt{% endif %}'
    generate.generate_file(
        project_dir=".",
        infile=infile,
        context={'generate_file': 'y'},
        env=env
    )
    assert os.path.isfile('tests/files/cheese.txt')
    with open('tests/files/cheese.txt', 'rt') as f:
        generated_text = f.read()
        assert generated_text == 'Testing that generate_file was y'
Exemplo n.º 24
0
 def test_generate_file(self):
     env = Environment()
     env.loader = FileSystemLoader('.')
     infile = 'tests/files/{{generate_file}}.txt'
     generate.generate_file(project_dir=".",
                            infile=infile,
                            context={'generate_file': 'cheese'},
                            env=env)
     self.assertTrue(os.path.isfile('tests/files/cheese.txt'))
     with open('tests/files/cheese.txt', 'rt') as f:
         generated_text = f.read()
         self.assertEqual(generated_text, 'Testing cheese')
Exemplo n.º 25
0
def test_generate_file_jsonify_filter(env):
    infile = 'tests/files/{{cookiecutter.jsonify_file}}.txt'
    data = {'jsonify_file': 'cheese', 'type': 'roquefort'}
    generate.generate_file(
        project_dir=".",
        infile=infile,
        context={'cookiecutter': data},
        env=env
    )
    assert os.path.isfile('tests/files/cheese.txt')
    with open('tests/files/cheese.txt', 'rt') as f:
        generated_text = f.read()
        assert json.loads(generated_text) == data
Exemplo n.º 26
0
 def test_generate_file(self):
     env = Environment()
     env.loader = FileSystemLoader('.')
     infile = 'tests/files/{{generate_file}}.txt'
     generate.generate_file(
         infile=infile,
         context={'generate_file': 'cheese'},
         env=env
     )
     self.assertTrue(os.path.isfile('tests/files/cheese.txt'))
     with open('tests/files/cheese.txt', 'rt') as f:
          generated_text = f.read()
          self.assertEqual(generated_text, 'Testing cheese')
Exemplo n.º 27
0
def test_generate_file_with_true_condition(env):
    """Verify correct work of boolean condition in file name on file generation.

    This test has positive answer, so file should be rendered.
    """
    infile = 'tests/files/{% if generate_file == \'y\' %}cheese.txt{% endif %}'
    generate.generate_file(
        project_dir=".", infile=infile, context={'generate_file': 'y'}, env=env
    )
    assert os.path.isfile('tests/files/cheese.txt')
    with open('tests/files/cheese.txt', 'rt') as f:
        generated_text = f.read()
        assert generated_text == 'Testing that generate_file was y'
Exemplo n.º 28
0
def test_generate_file_verbose_template_syntax_error(env, expected_msg):
    try:
        generate.generate_file(
            project_dir=".",
            infile="tests/files/syntax_error.txt",
            context={"syntax_error": "syntax_error"},
            env=env,
        )
    except TemplateSyntaxError as exception:
        assert str(exception) == expected_msg
    except Exception as exception:
        pytest.fail("Unexpected exception thrown: {0}".format(exception))
    else:
        pytest.fail("TemplateSyntaxError not thrown")
Exemplo n.º 29
0
 def test_generate_conditional_file(self):
     env = Environment()
     env.loader = FileSystemLoader('.')
     infile = 'tests/files/{% if cheese %}{{generate_file}}.txt{% endif %}'
     generate.generate_file(
         project_dir=".",
         infile=infile,
         context={'cheese': True, 'generate_file': 'cheese'},
         env=env
     )
     self.assertTrue(os.path.isfile('tests/files/cheese.txt'))
     with open('tests/files/cheese.txt', 'rt') as f:
         generated_text = f.read()
         self.assertEqual(generated_text, 'Testing cheese')
Exemplo n.º 30
0
def test_generate_file_random_ascii_string(env, length, punctuation):
    infile = "tests/files/{{cookiecutter.random_string_file}}.txt"
    data = {"random_string_file": "cheese"}
    context = {
        "cookiecutter": data,
        "length": length,
        "punctuation": punctuation,
    }
    generate.generate_file(project_dir=".",
                           infile=infile,
                           context=context,
                           env=env)
    assert os.path.isfile("tests/files/cheese.txt")
    with open("tests/files/cheese.txt", "rt") as f:
        generated_text = f.read()
        assert len(generated_text) == length
def test_generate_file_does_not_translate_crlf_newlines_to_lf(env):
    """Verify that file generation use same line ending, as in source file."""
    infile = 'tests/files/{{cookiecutter.generate_file}}_crlf_newlines.txt'
    generate.generate_file(
        project_dir=".",
        infile=infile,
        context={'cookiecutter': {'generate_file': 'cheese'}},
        env=env,
    )

    # this generated file should have a CRLF line ending
    gf = 'tests/files/cheese_crlf_newlines.txt'
    with open(gf, 'r', encoding='utf-8', newline='') as f:
        simple_text = f.readline()
    assert simple_text == 'newline is CRLF\r\n'
    assert f.newlines == '\r\n'
Exemplo n.º 32
0
 def test_generate_file_verbose_template_syntax_error(self):
     env = Environment()
     env.loader = FileSystemLoader('.')
     try:
         generate.generate_file(project_dir=".",
                                infile='tests/files/syntax_error.txt',
                                context={'syntax_error': 'syntax_error'},
                                env=env)
     except TemplateSyntaxError as exception:
         self.assertEquals(
             str(exception), 'Missing end of comment tag\n'
             '  File "./tests/files/syntax_error.txt", line 1\n'
             '    I eat {{ syntax_error }} {# this comment is not closed}')
     except exception:
         self.fail('Unexpected exception thrown:', exception)
     else:
         self.fail('TemplateSyntaxError not thrown')
Exemplo n.º 33
0
 def test_generate_file_verbose_template_syntax_error(self):
     env = Environment()
     env.loader = FileSystemLoader('.')
     try:
         generate.generate_file(
             project_dir=".",
             infile='tests/files/syntax_error.txt',
             context={'syntax_error': 'syntax_error'},
             env=env
         )
     except TemplateSyntaxError as exception:
         self.assertEquals(
             str(exception),
             'Missing end of comment tag\n'
             '  File "./tests/files/syntax_error.txt", line 1\n'
             '    I eat {{ syntax_error }} {# this comment is not closed}'
         )
     except exception:
         self.fail('Unexpected exception thrown:', exception)
     else:
         self.fail('TemplateSyntaxError not thrown')
Exemplo n.º 34
0
 def test_generate_file_verbose_template_syntax_error(self):
     env = Environment()
     env.loader = FileSystemLoader(".")
     try:
         generate.generate_file(
             project_dir=".",
             infile="tests/files/syntax_error.txt",
             context={"syntax_error": "syntax_error"},
             env=env,
         )
     except TemplateSyntaxError as exception:
         expected = (
             "Missing end of comment tag\n"
             '  File "./tests/files/syntax_error.txt", line 1\n'
             "    I eat {{ syntax_error }} {# this comment is not closed}"
         )
         expected = expected.replace("/", os.sep)
         self.assertEquals(str(exception), expected)
     except exception:
         self.fail("Unexpected exception thrown:", exception)
     else:
         self.fail("TemplateSyntaxError not thrown")
def test_generate_file_with_false_condition(env):
    infile = "tests/files/{% if generate_file == 'y' %}cheese.txt{% endif %}"
    generate.generate_file(project_dir=".", infile=infile, context={"generate_file": "n"}, env=env)
    assert not os.path.exists("tests/files/cheese.txt")
Exemplo n.º 36
0
def generate_files(repo_dir,
                   context=None,
                   output_dir='.',
                   overwrite_if_exists=False):  # pragma: no cover
    """Render the templates and saves them to files.
   
    :param repo_dir: Project template input directory.
    :param context: Dict for populating the template's variables.
    :param output_dir: Where to output the generated project dir into.
    :param overwrite_if_exists: Overwrite the contents of the output directory  if it exists.
    """
    template_dir = find_template(repo_dir)
    logger.debug('Generating project from {}...'.format(template_dir))
    context = context or OrderedDict([])

    unrendered_dir = os.path.split(template_dir)[1]
    ensure_dir_is_templated(unrendered_dir)
    env = StrictEnvironment(
        context=context,
        keep_trailing_newline=True,
    )
    try:
        project_dir, output_directory_created = render_and_create_dir(
            unrendered_dir, context, output_dir, env, overwrite_if_exists)
    except UndefinedError as err:
        msg = "Unable to create project directory '{}'".format(unrendered_dir)
        raise UndefinedVariableInTemplate(msg, err, context)

    # We want the Jinja path and the OS paths to match. Consequently, we'll:
    #   + CD to the template folder
    #   + Set Jinja's path to '.'
    #
    #  In order to build our files to the correct folder(s), we'll use an
    # absolute path for the target folder (project_dir)

    project_dir = os.path.abspath(project_dir)
    logger.debug('Project directory is {}'.format(project_dir))

    # if we created the output directory, then it's ok to remove it
    # if rendering fails
    delete_project_on_failure = output_directory_created

    with work_in(template_dir):
        env.loader = FileSystemLoader('.')

        for root, dirs, files in os.walk('.'):
            # We must separate the two types of dirs into different lists.
            # The reason is that we don't want ``os.walk`` to go through the
            # unrendered directories, since they will just be copied.
            copy_dirs = []
            render_dirs = []

            for d in dirs:
                d_ = os.path.normpath(os.path.join(root, d))
                # We check the full path, because that's how it can be
                # specified in the ``_copy_without_render`` setting, but
                # we store just the dir name
                if is_copy_only_path(d_, context):
                    copy_dirs.append(d)
                else:
                    render_dirs.append(d)

            for copy_dir in copy_dirs:
                indir = os.path.normpath(os.path.join(root, copy_dir))
                outdir = os.path.normpath(os.path.join(project_dir, indir))
                logger.debug('Copying dir {} to {} without rendering'
                             ''.format(indir, outdir))
                shutil.copytree(indir, outdir)

            # We mutate ``dirs``, because we only want to go through these dirs
            # recursively
            dirs[:] = render_dirs
            for d in dirs:
                unrendered_dir = os.path.join(project_dir, root, d)
                try:
                    render_and_create_dir(unrendered_dir, context, output_dir,
                                          env, overwrite_if_exists)
                except UndefinedError as err:
                    if delete_project_on_failure:
                        rmtree(project_dir)
                    _dir = os.path.relpath(unrendered_dir, output_dir)
                    msg = "Unable to create directory '{}'".format(_dir)
                    raise UndefinedVariableInTemplate(msg, err, context)

            for f in files:
                infile = os.path.normpath(os.path.join(root, f))
                if is_copy_only_path(infile, context):
                    outfile_tmpl = env.from_string(infile)
                    outfile_rendered = outfile_tmpl.render(**context)
                    outfile = os.path.join(project_dir, outfile_rendered)
                    logger.debug('Copying file {} to {} without rendering'
                                 ''.format(infile, outfile))
                    shutil.copyfile(infile, outfile)
                    shutil.copymode(infile, outfile)
                    continue
                try:
                    generate_file(project_dir, infile, context, env)
                except UndefinedError as err:
                    if delete_project_on_failure:
                        rmtree(project_dir)
                    msg = "Unable to create file '{}'".format(infile)
                    raise UndefinedVariableInTemplate(msg, err, context)

    return project_dir