rewrite_includes.py: make it work on windows

Also:
    Revert "don't try to format includes on windows"
    This reverts commit e68b5bd4e6.

Change-Id: I93dc4dba4808c07882e977ac0693c93e282bbad5
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/230878
Commit-Queue: Hal Canary <halcanary@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
This commit is contained in:
Hal Canary 2019-07-30 13:49:45 -04:00 committed by Skia Commit-Bot
parent 71c34240f4
commit 4df3d5340e
2 changed files with 11 additions and 10 deletions

View File

@ -198,8 +198,7 @@ def _CheckIncludesFormatted(input_api, output_api):
cmd = ['python',
'tools/rewrite_includes.py',
'--dry-run'] + files
# TODO(mtklein): tools/rewrite_includes.py doesn't work on Windows.
if 'win32' not in sys.platform and 0 != subprocess.call(cmd):
if 0 != subprocess.call(cmd):
return [output_api.PresubmitError('`%s` failed' % ' '.join(cmd))]
return []

View File

@ -40,11 +40,15 @@ roots = [
# we don't want #include <vulkan/vulkan_foo.h> rewritten to point to them.
blacklist = ['include/third_party/vulkan']
assert '/' in [os.sep, os.altsep]
def fix_path(p):
return p.replace(os.sep, os.altsep) if os.altsep else p
# Map short name -> absolute path for all Skia headers.
headers = {}
for root in roots:
for path, _, files in os.walk(root):
if not any(snippet in path for snippet in blacklist):
if not any(snippet in fix_path(path) for snippet in blacklist):
for file_name in files:
if file_name.endswith('.h'):
if file_name in headers:
@ -79,7 +83,7 @@ for file_path in to_rewrite():
lines = open(file_path).readlines()
# Write it back out again line by line with substitutions for #includes.
output = StringIO.StringIO() if args.dry_run else open(file_path, 'w')
output = StringIO.StringIO() if args.dry_run else open(file_path, 'wb')
includes = []
for line in lines:
@ -88,15 +92,13 @@ for file_path in to_rewrite():
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])
header = fix_path(os.path.relpath(headers[os.path.basename(parts[1])], '.'))
includes.append(parts[0] + '"%s"' % header + parts[2])
else:
for inc in sorted(includes):
print >>output, inc.strip('\n')
output.write(inc.strip('\n') + '\n')
includes = []
print >>output, line.strip('\n')
output.write(line.strip('\n') + '\n')
if args.dry_run and output.getvalue() != open(file_path).read():
need_rewriting.append(file_path)