~ubuntu-branches/ubuntu/oneiric/mozc/oneiric

« back to all changes in this revision

Viewing changes to mozc_build_tools/gyp/tools/pretty_gyp.py

  • Committer: Bazaar Package Importer
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2010-07-14 03:26:47 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100714032647-13qjisj6m8cm8jdx
Tags: 0.12.410.102-1
* New upstream release (Closes: #588971).
  - Add mozc-server, mozc-utils-gui and scim-mozc packages.
* Update debian/rules.
  Add --gypdir option to build_mozc.py.
* Update debian/control.
  - Bumped standards-version to 3.9.0.
  - Update description.
* Add mozc icon (Closes: #588972).
* Add patch which revises issue 18.
  ibus_mozc_issue18.patch
* kFreeBSD build support.
  support_kfreebsd.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# Copyright (c) 2009 The Chromium Authors. All rights reserved.
 
3
# Use of this source code is governed by a BSD-style license that can be
 
4
# found in the LICENSE file.
 
5
 
 
6
# This file pretty-prints the contents of a GYP file.
 
7
 
 
8
import sys
 
9
import re
 
10
 
 
11
input = []
 
12
if len(sys.argv) > 1:
 
13
  input_file = open(sys.argv[1])
 
14
  input = input_file.read().splitlines()
 
15
  input_file.close()
 
16
else:
 
17
  input = sys.stdin.read().splitlines()
 
18
 
 
19
# This is used to remove comments when we're counting braces.
 
20
comment_re = re.compile(r'\s*#.*')
 
21
 
 
22
# This is used to remove quoted strings when we're counting braces.
 
23
# It takes into account quoted quotes, and makes sure that the quotes
 
24
# match.
 
25
# NOTE: It does not handle quotes that span more than one line, or
 
26
# cases where an escaped quote is preceeded by an escaped backslash.
 
27
quote_re_str = r'(?P<q>[\'"])(.*?)(?<![^\\][\\])(?P=q)'
 
28
quote_re = re.compile(quote_re_str)
 
29
 
 
30
def comment_replace(matchobj):
 
31
  return matchobj.group(1) + matchobj.group(2) + '#' * len(matchobj.group(3))
 
32
 
 
33
def mask_comments(input):
 
34
  # This is used to mask the quoted strings so we skip braces inside
 
35
  # quoted strings.
 
36
  search_re = re.compile(r'(.*?)(#)(.*)')
 
37
  return [search_re.sub(comment_replace, line) for line in input]
 
38
 
 
39
def quote_replace(matchobj):
 
40
  return "%s%s%s%s" % (matchobj.group(1),
 
41
                       matchobj.group(2),
 
42
                       'x'*len(matchobj.group(3)),
 
43
                       matchobj.group(2))
 
44
 
 
45
def mask_quotes(input):
 
46
  # This is used to mask the quoted strings so we skip braces inside
 
47
  # quoted strings.
 
48
  search_re = re.compile(r'(.*?)' + quote_re_str)
 
49
  return [search_re.sub(quote_replace, line) for line in input]
 
50
 
 
51
def do_split(input, masked_input, search_re):
 
52
  output = []
 
53
  mask_output = []
 
54
  for (line, masked_line) in zip(input, masked_input):
 
55
    m = search_re.match(masked_line)
 
56
    while m:
 
57
      split = len(m.group(1))
 
58
      line = line[:split] + r'\n' + line[split:]
 
59
      masked_line = masked_line[:split] + r'\n' + masked_line[split:]
 
60
      m = search_re.match(masked_line)
 
61
    output.extend(line.split(r'\n'))
 
62
    mask_output.extend(masked_line.split(r'\n'))
 
63
  return (output, mask_output)
 
64
 
 
65
# This masks out the quotes and comments, and then splits appropriate
 
66
# lines (lines that matche the double_*_brace re's above) before
 
67
# indenting them below.
 
68
def split_double_braces(input):
 
69
  # These are used to split lines which have multiple braces on them, so
 
70
  # that the indentation looks prettier when all laid out (e.g. closing
 
71
  # braces make a nice diagonal line).
 
72
  double_open_brace_re = re.compile(r'(.*?[\[\{\(,])(\s*)([\[\{\(])')
 
73
  double_close_brace_re = re.compile(r'(.*?[\]\}\)],?)(\s*)([\]\}\)])')
 
74
 
 
75
  masked_input = mask_quotes(input)
 
76
  masked_input = mask_comments(masked_input)
 
77
 
 
78
  (output, mask_output) = do_split(input, masked_input, double_open_brace_re)
 
79
  (output, mask_output) = do_split(output, mask_output, double_close_brace_re)
 
80
 
 
81
  return output
 
82
 
 
83
# This keeps track of the number of braces on a given line and returns
 
84
# the result.  It starts at zero and subtracts for closed braces, and
 
85
# adds for open braces.
 
86
def count_braces(line):
 
87
  open_braces = ['[', '(', '{']
 
88
  close_braces = [']', ')', '}']
 
89
  closing_prefix_re = re.compile(r'(.*?[^\s\]\}\)]+.*?)([\]\}\)],?)\s*$')
 
90
  cnt = 0
 
91
  stripline = comment_re.sub(r'', line)
 
92
  stripline = quote_re.sub(r"''", stripline)
 
93
  for char in stripline:
 
94
    for brace in open_braces:
 
95
      if char == brace:
 
96
        cnt += 1
 
97
    for brace in close_braces:
 
98
      if char == brace:
 
99
        cnt -= 1
 
100
 
 
101
  after = False
 
102
  if cnt > 0:
 
103
    after = True
 
104
 
 
105
  # This catches the special case of a closing brace having something
 
106
  # other than just whitespace ahead of it -- we don't want to
 
107
  # unindent that until after this line is printed so it stays with
 
108
  # the previous indentation level.
 
109
  if cnt < 0 and closing_prefix_re.match(stripline):
 
110
    after = True
 
111
  return (cnt, after)
 
112
 
 
113
# This does the main work of indenting the input based on the brace counts.
 
114
def prettyprint_input(lines):
 
115
  indent = 0
 
116
  basic_offset = 2
 
117
  last_line = ""
 
118
  for line in lines:
 
119
    if comment_re.match(line):
 
120
      print line
 
121
    else:
 
122
      line = line.strip('\r\n\t ')  # Otherwise doesn't strip \r on Unix.
 
123
      if len(line) > 0:
 
124
        (brace_diff, after) = count_braces(line)
 
125
        if brace_diff != 0:
 
126
          if after:
 
127
            print " " * (basic_offset * indent) + line
 
128
            indent += brace_diff
 
129
          else:
 
130
            indent += brace_diff
 
131
            print " " * (basic_offset * indent) + line
 
132
        else:
 
133
          print " " * (basic_offset * indent) + line
 
134
      else:
 
135
        print ""
 
136
      last_line = line
 
137
 
 
138
# Split up the double braces.
 
139
lines = split_double_braces(input)
 
140
 
 
141
# Indent and print the output.
 
142
prettyprint_input(lines)