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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
#! /usr/bin/env python
# Copyright 2015 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 cStringIO
import functools
import imp
import inspect
import itertools
import operator
import os
import re
import sys
from catapult_base import refactor
from catapult_base.refactor_util import move
from telemetry.internal.util import command_line
from telemetry.internal.util import path
_RELATIVE_BASE_DIRS = (
('chrome', 'test', 'telemetry'),
('content', 'test', 'gpu'),
('tools', 'bisect-manual-test.py'),
('tools', 'chrome_proxy'),
('tools', 'perf'),
('tools', 'profile_chrome', 'perf_controller.py'),
('tools', 'run-bisect-manual-test.py'),
('third_party', 'skia', 'tools', 'skp', 'page_sets'),
('third_party', 'trace-viewer'),
)
# All folders dependent on Telemetry, found using a code search.
# Note that this is not the same as the directory that imports are relative to.
BASE_DIRS = [path.GetTelemetryDir()] + [
os.path.join(path.GetChromiumSrcDir(), *dir_path)
for dir_path in _RELATIVE_BASE_DIRS]
def SortImportGroups(module_path):
"""Sort each group of imports in the given Python module.
A group is a collection of adjacent import statements, with no non-import
lines in between. Groups are sorted according to the Google Python Style
Guide: "lexicographically, ignoring case, according to each module's full
package path."
"""
_TransformImportGroups(module_path, _SortImportGroup)
def _SortImportGroup(import_group):
def _ImportComparator(import1, import2):
_, root1, module1, _, _ = import1
_, root2, module2, _, _ = import2
full_module1 = (root1 + '.' + module1 if root1 else module1).lower()
full_module2 = (root2 + '.' + module2 if root2 else module2).lower()
return cmp(full_module1, full_module2)
return sorted(import_group, cmp=_ImportComparator)
def _TransformImportGroups(module_path, transformation):
"""Apply a transformation to each group of imports in the given module.
An import is a tuple of (indent, root, module, alias, suffix),
serialized as <indent>from <root> import <module> as <alias><suffix>.
Args:
module_path: The module to apply transformations on.
transformation: A function that takes in an import group and returns a
modified import group. An import group is a list of import tuples.
Returns:
True iff the module was modified, and False otherwise.
"""
def _WriteImports(output_stream, import_group):
for indent, root, module, alias, suffix in transformation(import_group):
output_stream.write(indent)
if root:
output_stream.write('from ')
output_stream.write(root)
output_stream.write(' ')
output_stream.write('import ')
output_stream.write(module)
if alias:
output_stream.write(' as ')
output_stream.write(alias)
output_stream.write(suffix)
output_stream.write('\n')
# Read the file so we can diff it later to determine if we made any changes.
with open(module_path, 'r') as module_file:
original_file = module_file.read()
# Locate imports using regex, group them, and transform each one.
# This regex produces a tuple of (indent, root, module, alias, suffix).
regex = (r'(\s*)(?:from ((?:[a-z0-9_]+\.)*[a-z0-9_]+) )?'
r'import ((?:[a-z0-9_]+\.)*[A-Za-z0-9_]+)(?: as ([A-Za-z0-9_]+))?(.*)')
pattern = re.compile(regex)
updated_file = cStringIO.StringIO()
with open(module_path, 'r') as module_file:
import_group = []
for line in module_file:
import_match = pattern.match(line)
if import_match:
import_group.append(list(import_match.groups()))
continue
if not import_group:
updated_file.write(line)
continue
_WriteImports(updated_file, import_group)
import_group = []
updated_file.write(line)
if import_group:
_WriteImports(updated_file, import_group)
import_group = []
if original_file == updated_file.getvalue():
return False
with open(module_path, 'w') as module_file:
module_file.write(updated_file.getvalue())
return True
def _CountInterfaces(module):
return (len(list(module.FindChildren(refactor.Class))) +
len(list(module.FindChildren(refactor.Function))))
def _IsSourceDir(dir_name):
return dir_name[0] != '.' and dir_name != 'third_party'
def _IsPythonModule(file_name):
_, ext = os.path.splitext(file_name)
return ext == '.py'
class Count(command_line.Command):
"""Print the number of public modules."""
@classmethod
def AddCommandLineArgs(cls, parser):
parser.add_argument('type', nargs='?', choices=('interfaces', 'modules'),
default='modules')
def Run(self, args):
module_paths = path.ListFiles(
path.GetTelemetryDir(), self._IsPublicApiDir, self._IsPublicApiFile)
if args.type == 'modules':
print len(module_paths)
elif args.type == 'interfaces':
print reduce(operator.add, refactor.Transform(_CountInterfaces, module_paths))
return 0
@staticmethod
def _IsPublicApiDir(dir_name):
return (dir_name[0] != '.' and dir_name[0] != '_' and
dir_name != 'internal' and dir_name != 'third_party')
@staticmethod
def _IsPublicApiFile(file_name):
root, ext = os.path.splitext(file_name)
return (file_name[0] != '.' and
not root.endswith('_unittest') and ext == '.py')
def _TelemetryFiles():
list_files = functools.partial(path.ListFiles,
should_include_dir=_IsSourceDir,
should_include_file=_IsPythonModule)
return sorted(itertools.chain(*map(list_files, BASE_DIRS)))
class Mv(command_line.Command):
"""Move modules or packages."""
@classmethod
def AddCommandLineArgs(cls, parser):
parser.add_argument('source', nargs='+')
parser.add_argument('target')
@classmethod
def ProcessCommandLineArgs(cls, parser, args):
# Check source file paths.
for source_path in args.source:
# Ensure source path exists.
if not os.path.exists(source_path):
parser.error('"%s" not found.' % source_path)
# Ensure source path is in one of the BASE_DIRS.
for base_dir in BASE_DIRS:
if path.IsSubpath(source_path, base_dir):
break
else:
parser.error('"%s" is not in any of the base dirs.')
# Ensure target directory exists.
if not (os.path.exists(args.target) or
os.path.exists(os.path.dirname(args.target))):
parser.error('"%s" not found.' % args.target)
# Ensure target path is in one of the BASE_DIRS.
for base_dir in BASE_DIRS:
if path.IsSubpath(args.target, base_dir):
break
else:
parser.error('"%s" is not in any of the base dirs.')
# If there are multiple source paths, ensure target is a directory.
if len(args.source) > 1 and not os.path.isdir(args.target):
parser.error('Target "%s" is not a directory.' % args.target)
# Ensure target is not in any of the source paths.
for source_path in args.source:
if path.IsSubpath(args.target, source_path):
parser.error('Cannot move "%s" to a subdirectory of itself, "%s".' %
(source_path, args.target))
def Run(self, args):
move.Run(args.source, args.target, _TelemetryFiles())
for module_path in _TelemetryFiles():
SortImportGroups(module_path)
return 0
class Sort(command_line.Command):
"""Sort imports."""
@classmethod
def AddCommandLineArgs(cls, parser):
parser.add_argument('target', nargs='*')
@classmethod
def ProcessCommandLineArgs(cls, parser, args):
for target in args.target:
if not os.path.exists(target):
parser.error('"%s" not found.' % target)
def Run(self, args):
if args.target:
targets = args.target
else:
targets = BASE_DIRS
for base_dir in targets:
for module_path in path.ListFiles(base_dir, _IsSourceDir, _IsPythonModule):
SortImportGroups(module_path)
return 0
class RefactorCommand(command_line.SubcommandCommand):
commands = (Count, Mv, Sort,)
if __name__ == '__main__':
sys.exit(RefactorCommand.main())
|