Introduce wspath and fix inline_file stacktraces to show filepath.

This commit is contained in:
Aleksey Khoroshilov
2023-12-19 18:04:36 +07:00
parent 7a4e9e3a87
commit 9d947d6f7b
5 changed files with 48 additions and 63 deletions
+3 -3
View File
@@ -281,8 +281,8 @@ def CheckLicense(input_api, output_api):
# This call inlines Chromium checks into current scope from src/PRESUBMIT.py. We
# do this to have the right order of checks, so all `--fix`-aware checks are
# executed first.
chromium_presubmit_overrides.inline_presubmit_from_src('PRESUBMIT.py',
globals(), locals())
chromium_presubmit_overrides.inline_presubmit('//PRESUBMIT.py', globals(),
locals())
_BANNED_CPP_FUNCTIONS += (
BanRule(
@@ -441,4 +441,4 @@ To remove unused imports: ./tools/android/checkstyle/remove_unused_imports.sh"""
ret.append(output_api.PresubmitError(msg))
return ret
# DON'T ADD NEW CHECKS HERE, ADD THEM BEFORE FIRST inline_presubmit_from_src().
# DON'T ADD NEW CHECKS HERE, ADD THEM BEFORE FIRST inline_presubmit().
+2 -2
View File
@@ -1,7 +1,7 @@
{
"disabled_checks": {
// Checks defined in the //PRESUBMIT.py global scope.
"PRESUBMIT.py": [
"//PRESUBMIT.py": [
// We don't use OWNERS files.
"CheckSecurityOwners",
// This validates added strings with screenshot tests which we don't use.
@@ -35,7 +35,7 @@
// Checks that should trigger presubmit errors instead of warnings.
"checks_to_force_presubmit_errors": {
"PRESUBMIT.py": [
"//PRESUBMIT.py": [
"CheckBuildConfigMacrosWithoutInclude",
"CheckForSuperfluousStlIncludesInHeaders",
"CheckForIncludeGuards",
+7 -16
View File
@@ -10,7 +10,6 @@
import inspect
import os
import re
import sys
import traceback
import override_utils
@@ -23,19 +22,12 @@ CANNED_CHECKS_KEY = 'canned'
# Helper to load json5 presubmit config.
def load_presubmit_config():
try:
json5_path = import_inline.join_src_dir('third_party', 'pyjson5',
'src')
sys.path.append(json5_path)
with import_inline.sys_path('//third_party/pyjson5/src'):
# pylint: disable=import-outside-toplevel,import-error
import json5
return json5.load(
open(
import_inline.join_src_dir('brave',
'chromium_presubmit_config.json5')))
finally:
# Restore sys.path to what it was before.
sys.path.remove(json5_path)
open(import_inline.wspath(
'//brave/chromium_presubmit_config.json5')))
config = load_presubmit_config()
@@ -90,7 +82,7 @@ def override_canned_checks(canned_checks):
src_filter = lambda f: input_api.FilterSourceFile(
f, files_to_check=files_to_check, files_to_skip=files_to_skip)
return [
f.LocalPath()
f.AbsoluteLocalPath()
for f in input_api.AffectedSourceFiles(src_filter)
]
@@ -192,11 +184,10 @@ def setup_per_check_file_filter(input_api):
# Inlines presubmit file as if it was run from the dir where it's located.
def inline_presubmit_from_src(filename, _globals, _locals):
def inline_presubmit(filename, _globals, _locals):
class State:
def __init__(self, filename):
self.presubmit_dir = os.path.dirname(
import_inline.join_src_dir(filename))
self.presubmit_dir = os.path.dirname(import_inline.wspath(filename))
self.orig_cwd = os.getcwd()
self.orig_presubmit_dir = ''
@@ -220,7 +211,7 @@ def inline_presubmit_from_src(filename, _globals, _locals):
assert pre_check_name not in _globals
_globals[pre_check_name] = PreRunChecks
import_inline.inline_file_from_src(filename, _globals, _locals)
import_inline.inline_file(filename, _globals, _locals)
apply_generic_check_overrides(_globals, filename, True)
def PostRunChecks(input_api, _output_api):
+32 -31
View File
@@ -4,59 +4,60 @@
# You can obtain one at http://mozilla.org/MPL/2.0/. */
import contextlib
import importlib.util
import functools
import os.path
import sys
@functools.lru_cache(maxsize=None)
def get_src_dir():
"""Searches for src/ dir which includes brave/ dir."""
current_file = globals().get('__file__')
if current_file and os.path.isabs(current_file):
path = os.path.dirname(current_file)
if current_file:
current_dir = os.path.dirname(os.path.abspath(current_file))
else:
path = os.getcwd()
current_dir = os.getcwd()
while True:
if os.path.basename(path) == 'src' and os.path.isdir(
os.path.join(path, 'brave')):
return path
parent_dir = os.path.dirname(path)
if parent_dir == path:
if os.path.basename(current_dir) == 'src' and os.path.isdir(
os.path.join(current_dir, 'brave')):
return current_dir
parent_dir = os.path.dirname(current_dir)
if parent_dir == current_dir:
# We hit the system root directory.
raise RuntimeError("Can't find src/ directory")
path = parent_dir
current_dir = parent_dir
def join_src_dir(*args):
return os.path.join(get_src_dir(), *args)
# Returns OS path from workspace path (//brave/path/file.py).
def wspath(path):
assert isinstance(path, str)
if path.startswith('//'):
path = os.path.join(get_src_dir(), path[2:])
# Normalize path separators.
return os.path.normpath(path)
def _inline_file(location, _globals, _locals):
"""Inlines file by executing it using passed scopes."""
with open(location, "r") as f:
# Inline file by executing it using passed scopes.
def inline_file(path, _globals, _locals):
path = wspath(path)
with open(path, "r") as f:
# Compile first to set the location explicitly. This makes stacktrace to
# show the actual filename instead of '<string>'.
code = compile(f.read(), path, 'exec')
# pylint: disable=exec-used
exec(f.read(), _globals, _locals)
exec(code, _globals, _locals)
def inline_module(module_name, _globals, _locals):
"""Finds module and inlines it by executing using passed scopes."""
module_spec = importlib.util.find_spec(module_name)
if not module_spec:
raise ModuleNotFoundError(
f"Can't find module to inline: {module_name}")
# pylint: disable=exec-used
exec(module_spec.loader.get_data(module_spec.loader.path), _globals,
_locals)
def inline_file_from_src(location, _globals, _locals):
"""Locates src/ dir and inlines relative file by executing it using passed
scopes."""
_inline_file(join_src_dir(location), _globals, _locals)
# Locate src/ dir and inline relative file by executing it using passed scopes.
def inline_file_from_src(path, _globals, _locals):
inline_file(f"//{path}", _globals, _locals)
@contextlib.contextmanager
def sys_path(path, position=None):
path = wspath(path)
path_exists = path in sys.path
if not path_exists:
if position is None:
+4 -11
View File
@@ -3,23 +3,16 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.
import sys
from argparse import ArgumentParser
from os import getcwd
from os.path import basename, dirname
from subprocess import check_call
from import_inline import join_src_dir
from import_inline import sys_path
_upstream_dir = join_src_dir('chrome', 'tools', 'build', 'win')
sys.path.append(_upstream_dir)
# pylint: disable=import-error, wrong-import-position
import create_installer_archive as upstream_impl
sys.path.remove(_upstream_dir)
del _upstream_dir
with sys_path('//chrome/tools/build/win'):
# pylint: disable=import-error, wrong-import-position
import create_installer_archive as upstream_impl
def main():