add a fast subset option and --dry-run

You can now pass files to rewrite, still defaulting
to all sources.  This speeds things up and is nice
for a Git or PRESUBMIT hook, added here too.

Passing --dry-run or -n will just check that the
files we would have written is the same, printing
the names of any mismatches and failing if any.

Cq-Include-Trybots: skia.primary:Housekeeper-PerCommit-CheckGeneratedFiles
Tricium: no
Change-Id: I94218f49071b8634841b04e4b536ad1ae5d9d5fd
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/230143
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Eric Boren <borenet@google.com>
This commit is contained in:
Mike Klein 2019-07-26 11:55:40 -05:00 committed by Skia Commit-Bot
parent 6397fa30ab
commit bb413430e4
2 changed files with 66 additions and 34 deletions

View File

@ -192,6 +192,14 @@ def _CheckGNFormatted(input_api, output_api):
'`%s` failed, try\n\t%s' % (' '.join(cmd), fix)))
return results
def _CheckIncludesFormatted(input_api, output_api):
"""Make sure #includes in files we're changing have been formatted."""
cmd = ['python',
'tools/rewrite_includes.py',
'--dry-run'] + map(str, input_api.AffectedFiles())
if 0 != subprocess.call(cmd):
return [output_api.PresubmitError('`%s` failed' % ' '.join(cmd))]
return []
def _CheckCompileIsolate(input_api, output_api):
"""Ensure that gen_compile_isolate.py does not change compile.isolate."""
@ -271,6 +279,7 @@ def _CommonChecks(input_api, output_api):
results.extend(_ToolFlags(input_api, output_api))
results.extend(_CheckCompileIsolate(input_api, output_api))
results.extend(_CheckDEPSValid(input_api, output_api))
results.extend(_CheckIncludesFormatted(input_api, output_api))
return results

View File

@ -5,7 +5,17 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import StringIO
import argparse
import os
import sys
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--dry-run', action='store_true',
help='Just check there is nothing to rewrite.')
parser.add_argument('sources', nargs='*',
help='Source files to rewrite, or all if empty.')
args = parser.parse_args()
roots = [
'bench',
@ -42,41 +52,54 @@ for root in roots:
assert file_name not in headers
headers[file_name] = os.path.abspath(os.path.join(path, file_name))
def to_rewrite():
if args.sources:
for path in args.sources:
yield path
else:
for root in roots:
for path, _, files in os.walk(root):
for file_name in files:
yield os.path.join(path, file_name)
# Rewrite any #includes relative to Skia's top-level directory.
for root in roots:
for path, _, files in os.walk(root):
if 'generated' in path:
continue
for file_name in files:
if (file_name.endswith('.h') or
file_name.endswith('.c') or
file_name.endswith('.m') or
file_name.endswith('.mm') or
file_name.endswith('.inc') or
file_name.endswith('.fp') or
file_name.endswith('.cc') or
file_name.endswith('.cpp')):
# Read the whole file into memory.
file_path = os.path.join(path, file_name)
lines = open(file_path).readlines()
rc = 0
for file_path in to_rewrite():
if 'generated' in file_path:
continue
if (file_path.endswith('.h') or
file_path.endswith('.c') or
file_path.endswith('.m') or
file_path.endswith('.mm') or
file_path.endswith('.inc') or
file_path.endswith('.fp') or
file_path.endswith('.cc') or
file_path.endswith('.cpp')):
# Read the whole file into memory.
lines = open(file_path).readlines()
# Write it back out again line by line with substitutions for #includes.
with open(file_path, 'w') as output:
includes = []
# Write it back out again line by line with substitutions for #includes.
output = StringIO.StringIO() if args.dry_run else open(file_path, 'w')
for line in lines:
parts = line.replace('<', '"').replace('>', '"').split('"')
if (len(parts) == 3
and '#' in parts[0]
and 'include' in parts[0]
and os.path.basename(parts[1]) in headers):
header = headers[os.path.basename(parts[1])]
includes.append(parts[0] +
'"%s"' % os.path.relpath(header, '.') +
parts[2])
else:
for inc in sorted(includes):
print >>output, inc.strip('\n')
includes = []
print >>output, line.strip('\n')
includes = []
for line in lines:
parts = line.replace('<', '"').replace('>', '"').split('"')
if (len(parts) == 3
and '#' in parts[0]
and 'include' in parts[0]
and os.path.basename(parts[1]) in headers):
header = headers[os.path.basename(parts[1])]
includes.append(parts[0] +
'"%s"' % os.path.relpath(header, '.') +
parts[2])
else:
for inc in sorted(includes):
print >>output, inc.strip('\n')
includes = []
print >>output, line.strip('\n')
if args.dry_run and output.getvalue() != open(file_path).read():
print file_path, 'has #includes that need rewriting.'
rc = 1
output.close()
sys.exit(rc)