summaryrefslogtreecommitdiffstats
path: root/native_client_sdk/src/build_tools/easy_template.py
blob: d439ea0304e034e39c1866a27bf65e3d3f0796fd (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import copy
import cStringIO
import optparse
import os
import re
import sys


STATEMENT_RE = "\[\[(.*?)\]\]"  # [[...]]
EXPR_RE = "\{\{(.*?)\}\}"  # {{...}}

def TemplateToPython(template, statement_re, expr_re):
  output = cStringIO.StringIO()
  indent_re = re.compile(r'\s*')
  indent_string = ''
  for line in template.splitlines(1):  # 1 => keep line ends
    m = statement_re.match(line)
    if m:
      statement = m.group(1)
      indent_string = indent_re.match(statement).group()
      if statement.rstrip()[-1:] == ':':
        indent_string += '  '
      output.write(statement + '\n')
    else:
      line_ending = ''
      while line and line[-1] in '\\"\n\r':
        line_ending = line[-1] + line_ending
        line = line[:-1]

      m = expr_re.search(line)
      if m:
        line = line.replace('%', '%%')
        subst_line = r'r"""%s""" %% (%s,)' % (
            re.sub(expr_re, '%s', line),
            ', '.join(re.findall(expr_re, line)))
      else:
        subst_line = r'r"""%s"""' % line

      out_string = r'%s__outfile__.write(%s + %s)' % (
          indent_string,
          subst_line,
          repr(line_ending))
      output.write(out_string + '\n')

  return output.getvalue()


def RunTemplate(srcfile, dstfile, template_dict, statement_re=None,
                expr_re=None):
  statement_re = statement_re or re.compile(STATEMENT_RE)
  expr_re = expr_re or re.compile(EXPR_RE)
  script = TemplateToPython(srcfile.read(), statement_re, expr_re)
  template_dict = copy.copy(template_dict)
  template_dict['__outfile__'] = dstfile
  exec script in template_dict


def RunTemplateFile(srcpath, dstpath, template_dict, statement_re=None,
                    expr_re=None):
  with open(srcpath) as srcfile:
    with open(dstpath, 'w') as dstfile:
      RunTemplate(srcfile, dstfile, template_dict, statement_re, expr_re)


def RunTemplateFileIfChanged(srcpath, dstpath, replace):
  dststr = cStringIO.StringIO()
  with open(srcpath) as srcfile:
    RunTemplate(srcfile, dststr, replace)

  if os.path.exists(dstpath):
    with open(dstpath) as dstfile:
      if dstfile.read() == dststr.getvalue():
        return

  with open(dstpath, 'w') as dstfile:
    dstfile.write(dststr.getvalue())


def RunTemplateString(src, template_dict, statement_re=None, expr_re=None):
  srcstr = cStringIO.StringIO(src)
  dststr = cStringIO.StringIO()
  RunTemplate(srcstr, dststr, template_dict, statement_re, expr_re)
  return dststr.getvalue()


def main(args):
  parser = optparse.OptionParser()
  _, args = parser.parse_args(args)
  if not args:
    return

  with open(args[0]) as f:
    print TemplateToPython(
        f.read(), re.compile(STATEMENT_RE), re.compile(EXPR_RE))

if __name__ == '__main__':
  sys.exit(main(sys.argv[1:]))