File: expand_jinja.py

package info (click to toggle)
scap-security-guide 0.1.76-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 110,644 kB
  • sloc: xml: 241,883; sh: 73,777; python: 32,527; makefile: 27
file content (55 lines) | stat: -rw-r--r-- 1,387 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from __future__ import print_function

import os
import sys
import argparse

import ssg.jinja


SUBS_DICT = ssg.jinja.load_macros()


def expand_jinja(filepath):
    return ssg.jinja.process_file(filepath, SUBS_DICT)


def _get_output_filepath(outdir, filepath):
    out_filepath = None

    if filepath.endswith(".jinja"):
        out_filepath = filepath.split(".jinja")[0]
        filepath = out_filepath

    if outdir:
        filename = os.path.basename(filepath)
        out_filepath = os.path.join(outdir, filename)

    if not out_filepath:
        raise RuntimeError(
            "Don't know where to save expansion of '{0}', as "
            "the output directory has not been supplied, and "
            "the file doesn't end with '.jinja'."
            .format(filepath)
        )
    return out_filepath


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("filename", nargs="+")
    parser.add_argument("--outdir")

    args = parser.parse_args()

    for filepath in args.filename:
        try:
            out_filepath = _get_output_filepath(args.outdir, filepath)
        except RuntimeError as exc:
            print(str(exc), file=sys.stderr)
            sys.exit(1)

        expanded_contents = expand_jinja(filepath)
        with open(out_filepath, "w") as f:
            f.write(expanded_contents)
        print(out_filepath)