Example #1
0
def render_project(directory, project_name, license, version='0.0.1.dev0', py_versions=('3', '3.5'), **kwargs):

    # Create project directory
    try:
        project_dir = Path(directory) / project_name
        project_dir.mkdir()
    except FileExistsError:
        print('Directory "{}" already exits in "{}"!!!'.format(project_name, directory))
        sys.exit(1)

    license_variables = process_license(license)
    license_dir = license_variables.pop('license_dir')

    # Render the templates
    variables = {'project_name': project_name,
                 'py_versions': py_versions,
                 'version': version,
                 'author': kwargs.get('user_name'),
                 'email': kwargs.get('user_email'),
                 **license_variables
                 }

    templates_dir = utils.get_data_dir() / 'templates'
    lookup = TemplateLookup(directories=[templates_dir.as_posix(), license_dir.name],
                            module_directory='/tmp/autopilot_mako_modules',
                            output_encoding='utf-8', input_encoding='utf-8',
                            encoding_errors='replace')

    for level, (path, subdirs, files) in enumerate(os.walk(templates_dir.as_posix())):
        path = Path(path)
        if '__pycache__' in subdirs:
            subdirs.remove('__pycache__')

        if level == 0:   # Inject license, it was generated
            files.append('LICENSE')
        for name in files:
            if should_skip_file(name):
                continue

            in_path = (path / name).relative_to(templates_dir)
            # Get output path to render the template
            out_path = Template((project_dir / in_path).as_posix(),
                            #output_encoding='utf-8', input_encoding='utf-8',
                            encoding_errors='replace').render_unicode(**variables)
            out_path = Path(out_path)
            #logging.info('Render {}'.format(out_path.relative_to(project_dir)))
            print('Render {}'.format(out_path.relative_to(project_dir)))

            out_path.parent.mkdir(parents=True, exist_ok=True)

            template = lookup.get_template(in_path.as_posix())
            with out_path.open(mode='xb') as out:
                out.write(template.render(**variables))

    license_dir.cleanup()
    return project_dir