Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: tools/pretty_gyp.py

Issue 1454433002: Python 3 compatibility Base URL: https://chromium.googlesource.com/external/gyp.git@master
Patch Set: Rebase with master (4ec6c4e3a94bd04a6da2858163d40b2429b8aad1) Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 # Copyright (c) 2012 Google Inc. All rights reserved. 3 # Copyright (c) 2012 Google Inc. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be 4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file. 5 # found in the LICENSE file.
6 6
7 """Pretty-prints the contents of a GYP file.""" 7 """Pretty-prints the contents of a GYP file."""
8 8
9 from __future__ import print_function
10
9 import sys 11 import sys
10 import re 12 import re
11 13
12 14
13 # Regex to remove comments when we're counting braces. 15 # Regex to remove comments when we're counting braces.
14 COMMENT_RE = re.compile(r'\s*#.*') 16 COMMENT_RE = re.compile(r'\s*#.*')
15 17
16 # Regex to remove quoted strings when we're counting braces. 18 # Regex to remove quoted strings when we're counting braces.
17 # It takes into account quoted quotes, and makes sure that the quotes match. 19 # It takes into account quoted quotes, and makes sure that the quotes match.
18 # NOTE: It does not handle quotes that span more than one line, or 20 # NOTE: It does not handle quotes that span more than one line, or
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 basic_offset = 2 120 basic_offset = 2
119 last_line = "" 121 last_line = ""
120 for line in lines: 122 for line in lines:
121 line = line.strip('\r\n\t ') # Otherwise doesn't strip \r on Unix. 123 line = line.strip('\r\n\t ') # Otherwise doesn't strip \r on Unix.
122 if len(line) > 0: 124 if len(line) > 0:
123 brace_diff = 0 125 brace_diff = 0
124 if not COMMENT_RE.match(line): 126 if not COMMENT_RE.match(line):
125 (brace_diff, after) = count_braces(line) 127 (brace_diff, after) = count_braces(line)
126 if brace_diff != 0: 128 if brace_diff != 0:
127 if after: 129 if after:
128 print " " * (basic_offset * indent) + line 130 print(" " * (basic_offset * indent) + line)
129 indent += brace_diff 131 indent += brace_diff
130 else: 132 else:
131 indent += brace_diff 133 indent += brace_diff
132 print " " * (basic_offset * indent) + line 134 print(" " * (basic_offset * indent) + line)
133 else: 135 else:
134 print " " * (basic_offset * indent) + line 136 print(" " * (basic_offset * indent) + line)
135 else: 137 else:
136 print "" 138 print("")
137 last_line = line 139 last_line = line
138 140
139 141
140 def main(): 142 def main():
141 if len(sys.argv) > 1: 143 if len(sys.argv) > 1:
142 data = open(sys.argv[1]).read().splitlines() 144 data = open(sys.argv[1]).read().splitlines()
143 else: 145 else:
144 data = sys.stdin.read().splitlines() 146 data = sys.stdin.read().splitlines()
145 # Split up the double braces. 147 # Split up the double braces.
146 lines = split_double_braces(data) 148 lines = split_double_braces(data)
147 149
148 # Indent and print the output. 150 # Indent and print the output.
149 prettyprint_input(lines) 151 prettyprint_input(lines)
150 return 0 152 return 0
151 153
152 154
153 if __name__ == '__main__': 155 if __name__ == '__main__':
154 sys.exit(main()) 156 sys.exit(main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698