def parse_templates(cfg, templates, repo, next_custom, preview):
    for t in templates.split(' '):
        tpath = template_path(t)
        if os.path.exists(tpath):
            with open(tpath, 'r') as fp:
                lines = fp.readlines()

            if len(lines) < 2:
                term.err("The template \"" + t + "\" is not valid, aborting.")
                return

            if not lines[0].startswith('#'):
                term.err("The template \"" + t + "\" doesn't define any valid "
                                                 "output, aborting.")
                return

            output = str(lines[0]).strip(' #\n')

            # resolve relative paths to the project's root
            if not os.path.isabs(output):
                output = os.path.join(PRJ_ROOT, output)

            outdir = os.path.dirname(output)

            if not os.path.exists(outdir):
                term.err("The template output directory \"" + outdir +
                         "\" doesn't exists.")

            term.info("Processing template \"" + bold(t) + "\" for " + output +
                      "...")

            lines = lines[1:]
            xformed = Template("".join(lines))
            vstring = build_version_string(cfg, repo, False, next_custom)
            args = build_format_args(cfg, repo, next_custom)
            keywords = {
                'CURRENT_VERSION': vstring,
                'MAJOR': args['maj'],
                'MINOR': args['min'],
                'PATCH': args['patch'],
                'REV': args['rev'],
                'REV_PREFIX': args['rev_prefix'],
                'BUILD_ID': args['build_id'],
                'FULL_BUILD_ID': args['build_id_full'],
                'COMMIT_COUNT': args['commit_count'],
                'COMMIT_COUNT_STR':
                str(args['commit_count']) if args['commit_count'] > 0 else '',

                'COMMIT_COUNT_PREFIX': args['commit_count_prefix'],
                'META_PR': args['meta_pr'],
                'META_PR_PREFIX': args['meta_pr_prefix']
            }

            try:
                res = xformed.substitute(keywords)
            except KeyError as e:
                term.err("Unknown key \"" + e.message + "\" found, aborting.")
                sys.exit(1)

            if not preview:
                try:
                    fp = open(output, 'w')
                    fp.write(res)
                    fp.close()
                except IOError:
                    term.err("Couldn't write file \"" + output + "\"")
                    sys.exit(1)
            else:
                term.out(res)

            wrote_bytes = len(res) if preview else os.stat(output).st_size
            term.info("Done, " + str(wrote_bytes) + " bytes written.")
        else:
            term.err("Couldn't find the \"" + t + "\" template")
            sys.exit(1)
Parse one or more templates, substitute placeholder variables with
dataset/ETHPy150Open manuelbua/gitver/gitver/commands.py/parse_templates