Example #1
0
def get_user_data(src_path, force, quiet):
    """Query to user for information needed as per the template's ``vooodoo.json``.
    """
    json_path = os.path.join(src_path, VOODOO_JSON_FILE)
    if not os.path.exists(json_path):
        return {}
    json_src = read_file(json_path)
    try:
        # Load the default user data in order
        def_user_data = json.loads(json_src, object_pairs_hook=OrderedDict)
    except ValueError as e:
        if not quiet:
            print_format('Invalid `voodoo.json`', color=COLOR_WARNING)
            print(e)
            def_user_data = {}

    user_data = {}
    if force:
        return def_user_data
    print('\n' + '-' * 50 + '\n')
    for key, value in def_user_data.items():
        resp = prompt('{0}?'.format(key), value)
        user_data[key] = to_unicode(resp).decode('utf8')
    print('\n' + '-' * 50 + '\n')

    try:
        os.remove(json_path)
    except OSError:
        pass
    return user_data
Example #2
0
def render_file(dst_path, rel_folder, folder, src_name, dst_name, render_tmpl,
                pretend=False, force=False, skip=False, quiet=False):
    """Process or copy a file of the skeleton.
    """
    fullpath = os.path.join(folder, src_name)
    created_path = os.path.join(rel_folder, dst_name).lstrip('.').lstrip('/')

    if pretend:
        final_path = os.path.join(dst_path, rel_folder, dst_name)
    else:
        final_path = make_dirs(dst_path, rel_folder, dst_name)

    if not os.path.exists(final_path):
        if not quiet:
            print_format('create', created_path, color=COLOR_OK)
        if not pretend:
            make_file(src_name, render_tmpl, fullpath, final_path)
        return

    # A file with this name already exists
    content = None
    if src_name.endswith('.tmpl'):
        content = render_tmpl(fullpath)
        identical = file_has_this_content(final_path, content)
    else:
        identical = files_are_identical(fullpath, final_path)

    # The existing file is identical.
    if identical:
        if not quiet:
            print_format('identical', created_path, color=COLOR_IGNORE, bright=None)
        return

    # The existing file is different.
    if not quiet:
        print_format('conflict', created_path, color=COLOR_DANGER)
    if force:
        overwrite = True
    elif skip:
        overwrite = False
    else:
        msg = '  Overwrite %s? (y/n)' % final_path
        overwrite = prompt_bool(msg, default=True)

    if not quiet:
        print_format('force' if overwrite else 'skip', created_path, color=COLOR_WARNING)

    if overwrite and not pretend:
        if content is None:
            copy_file(fullpath, final_path)
        else:
            create_file(final_path, content)