summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/swr/rasterizer/scripts/mako/cmd.py
blob: 1a9ca56637cf99311716a1ee55b9e6177b884022 (plain)
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
56
57
58
59
60
61
62
# mako/cmd.py
# Copyright (C) 2006-2015 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
from argparse import ArgumentParser
from os.path import isfile, dirname
import sys
from mako.template import Template
from mako.lookup import TemplateLookup
from mako import exceptions

def varsplit(var):
    if "=" not in var:
        return (var, "")
    return var.split("=", 1)

def _exit():
    sys.stderr.write(exceptions.text_error_template().render())
    sys.exit(1)

def cmdline(argv=None):

    parser = ArgumentParser("usage: %prog [FILENAME]")
    parser.add_argument("--var", default=[], action="append",
                  help="variable (can be used multiple times, use name=value)")
    parser.add_argument("--template-dir", default=[], action="append",
                  help="Directory to use for template lookup (multiple "
                    "directories may be provided). If not given then if the "
                    "template is read from stdin, the value defaults to be "
                    "the current directory, otherwise it defaults to be the "
                    "parent directory of the file provided.")
    parser.add_argument('input', nargs='?', default='-')

    options = parser.parse_args(argv)
    if options.input == '-':
        lookup_dirs = options.template_dir or ["."]
        lookup = TemplateLookup(lookup_dirs)
        try:
            template = Template(sys.stdin.read(), lookup=lookup)
        except:
            _exit()
    else:
        filename = options.input
        if not isfile(filename):
            raise SystemExit("error: can't find %s" % filename)
        lookup_dirs = options.template_dir or [dirname(filename)]
        lookup = TemplateLookup(lookup_dirs)
        try:
            template = Template(filename=filename, lookup=lookup)
        except:
            _exit()

    kw = dict([varsplit(var) for var in options.var])
    try:
        print(template.render(**kw))
    except:
        _exit()


if __name__ == "__main__":
    cmdline()