skia2/tools/rewrite_includes.py
Kevin Lubick 2f576263e4 Reland "[infra] Use Python3 for our Presubmits"
This is a reland of commit 094bcdb9e5

Original change's description:
> [infra] Use Python3 for our Presubmits
>
> https://source.chromium.org/chromium/chromium/tools/depot_tools/+/main:presubmit_support.py;l=319;drc=443d9135cc33f3156d5fe25ebec33f9adffbab65
>
> This also makes any errors from `make train -C infra/bots`
> look well formatted because check_output returns a bytestring
> in Python3 and when that is printed, the newlines et al are not
> rendered correctly. Thus we want the output of check_output
> to be encoded to UTF-8.
>
> Without setting the USE_PYTHON3 = True in PRESUBMIT.py,
> it appears that `git cl upload` would try to run our
> infra_tests.py in Python2 mode, which does not have
> the encoding argument for check_output.
>
> Apparently cipd_bin_packages/vpython3 does not have the
> "six" package installed (but cipd_bin_packages/vpython does)
> so I replaced the six.StringIO with io.StringIO, which
> is where that is located in Python3.
>
> Change-Id: Ic8f61bf943531583ba3d110a85260d69bdbf5eb2
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/537677
> Reviewed-by: Ravi Mistry <rmistry@google.com>
> Reviewed-by: Eric Boren <borenet@google.com>

Change-Id: Ia7fb2f3b6d8b70ca95cf10763782d4a0122053e0
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/537978
Reviewed-by: Eric Boren <borenet@google.com>
Commit-Queue: Kevin Lubick <kjlubick@google.com>
2022-05-06 14:01:35 +00:00

125 lines
3.6 KiB
Python
Executable File

#!/usr/bin/python
#
# Copyright 2019 Google Inc.
#
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import argparse
import os
import sys
from io import StringIO
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',
'dm',
'docs',
'example',
'experimental',
'fuzz',
'gm',
'include',
'modules',
'platform_tools/android/apps',
'samplecode',
'src',
'tests',
'third_party/etc1',
'third_party/gif',
'tools'
]
ignorelist = [
# Don't count our local Vulkan headers as Skia headers;
# we don't want #include <vulkan/vulkan_foo.h> rewritten to point to them.
'include/third_party/vulkan',
# Some node_modules/ files (used by CanvasKit et al) have c++ code which we should ignore.
'node_modules',
]
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 fix_path(path) for snippet in ignorelist):
for file_name in files:
if file_name.endswith('.h'):
if file_name in headers:
message = ('Header filename is used more than once!\n- ' + path + '/' + file_name +
'\n- ' + headers[file_name])
assert file_name not in headers, message
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.
need_rewriting = []
for file_path in to_rewrite():
if ('/generated/' in file_path or
'tests/sksl/' in file_path or
'third_party/skcms' in file_path or
file_path.startswith('bazel/rbe')):
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('.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.
output = StringIO() if args.dry_run else open(file_path, 'w')
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 = 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):
output.write(inc.strip('\n') + '\n')
includes = []
output.write(line.strip('\n') + '\n')
if args.dry_run and output.getvalue() != open(file_path).read():
need_rewriting.append(file_path)
rc = 1
output.close()
if need_rewriting:
print('Some files need rewritten #includes:')
for path in need_rewriting:
print('\t' + path)
print('To do this automatically, run')
print('python tools/rewrite_includes.py ' + ' '.join(need_rewriting))
sys.exit(1)