diff --git a/PRESUBMIT.py b/PRESUBMIT.py index c7f17e415d9..c49a42ede75 100644 --- a/PRESUBMIT.py +++ b/PRESUBMIT.py @@ -9,7 +9,8 @@ for more details about the presubmit API built into gcl. """ import datetime -import imp +import importlib.util +import importlib.machinery import os import os.path from typing import Callable @@ -70,10 +71,22 @@ def _CheckFormat(input_api, return unformatted_files +def load_source(modname, filename): + loader = importlib.machinery.SourceFileLoader(modname, filename) + spec = importlib.util.spec_from_file_location(modname, + filename, + loader=loader) + module = importlib.util.module_from_spec(spec) + # The module is always executed and not cached in sys.modules. + # Uncomment the following line to cache the module. + # sys.modules[module.__name__] = module + loader.exec_module(module) + return module + + def _CheckDartFormat(input_api, output_api): local_root = input_api.change.RepositoryRoot() - utils = imp.load_source('utils', - os.path.join(local_root, 'tools', 'utils.py')) + utils = load_source('utils', os.path.join(local_root, 'tools', 'utils.py')) dart = os.path.join(utils.CheckedInSdkPath(), 'bin', 'dart') @@ -155,8 +168,7 @@ def _CheckDartFormat(input_api, output_api): def _CheckStatusFiles(input_api, output_api): local_root = input_api.change.RepositoryRoot() - utils = imp.load_source('utils', - os.path.join(local_root, 'tools', 'utils.py')) + utils = load_source('utils', os.path.join(local_root, 'tools', 'utils.py')) dart = os.path.join(utils.CheckedInSdkPath(), 'bin', 'dart') lint = os.path.join(local_root, 'pkg', 'status_file', 'bin', 'lint.dart') @@ -231,12 +243,12 @@ def _CheckLayering(input_api, output_api): return [] local_root = input_api.change.RepositoryRoot() - compiler_layering_check = imp.load_source( + compiler_layering_check = load_source( 'compiler_layering_check', os.path.join(local_root, 'runtime', 'tools', 'compiler_layering_check.py')) errors = compiler_layering_check.DoCheck(local_root) - embedder_layering_check = imp.load_source( + embedder_layering_check = load_source( 'embedder_layering_check', os.path.join(local_root, 'runtime', 'tools', 'embedder_layering_check.py')) diff --git a/pkg/_fe_analyzer_shared/PRESUBMIT.py b/pkg/_fe_analyzer_shared/PRESUBMIT.py index 70592807da5..a5bcbc5ee2f 100644 --- a/pkg/_fe_analyzer_shared/PRESUBMIT.py +++ b/pkg/_fe_analyzer_shared/PRESUBMIT.py @@ -8,13 +8,27 @@ See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for more details about the presubmit API built into gcl. """ -import imp +import importlib.util +import importlib.machinery import os.path import subprocess USE_PYTHON3 = True +def load_source(modname, filename): + loader = importlib.machinery.SourceFileLoader(modname, filename) + spec = importlib.util.spec_from_file_location(modname, + filename, + loader=loader) + module = importlib.util.module_from_spec(spec) + # The module is always executed and not cached in sys.modules. + # Uncomment the following line to cache the module. + # sys.modules[module.__name__] = module + loader.exec_module(module) + return module + + def runSmokeTest(input_api, output_api): hasChangedFiles = False for git_file in input_api.AffectedTextFiles(): @@ -25,8 +39,8 @@ def runSmokeTest(input_api, output_api): if hasChangedFiles: local_root = input_api.change.RepositoryRoot() - utils = imp.load_source('utils', - os.path.join(local_root, 'tools', 'utils.py')) + utils = load_source('utils', + os.path.join(local_root, 'tools', 'utils.py')) dart = os.path.join(utils.CheckedInSdkPath(), 'bin', 'dart') smoke_test = os.path.join(local_root, 'pkg', '_fe_analyzer_shared', 'tool', 'smoke_test_quick.dart') diff --git a/pkg/front_end/PRESUBMIT.py b/pkg/front_end/PRESUBMIT.py index 0a0490477b1..5dae483e128 100644 --- a/pkg/front_end/PRESUBMIT.py +++ b/pkg/front_end/PRESUBMIT.py @@ -8,13 +8,27 @@ See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for more details about the presubmit API built into gcl. """ -import imp +import importlib.util +import importlib.machinery import os.path import subprocess USE_PYTHON3 = True +def load_source(modname, filename): + loader = importlib.machinery.SourceFileLoader(modname, filename) + spec = importlib.util.spec_from_file_location(modname, + filename, + loader=loader) + module = importlib.util.module_from_spec(spec) + # The module is always executed and not cached in sys.modules. + # Uncomment the following line to cache the module. + # sys.modules[module.__name__] = module + loader.exec_module(module) + return module + + def runSmokeTest(input_api, output_api): hasChangedFiles = False for git_file in input_api.AffectedTextFiles(): @@ -25,8 +39,8 @@ def runSmokeTest(input_api, output_api): if hasChangedFiles: local_root = input_api.change.RepositoryRoot() - utils = imp.load_source('utils', - os.path.join(local_root, 'tools', 'utils.py')) + utils = load_source('utils', + os.path.join(local_root, 'tools', 'utils.py')) dart = os.path.join(utils.CheckedInSdkPath(), 'bin', 'dart') smoke_test = os.path.join(local_root, 'pkg', 'front_end', 'tool', 'smoke_test_quick.dart') diff --git a/pkg/kernel/PRESUBMIT.py b/pkg/kernel/PRESUBMIT.py index 8630f2084e5..381237b37e4 100644 --- a/pkg/kernel/PRESUBMIT.py +++ b/pkg/kernel/PRESUBMIT.py @@ -8,13 +8,27 @@ See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for more details about the presubmit API built into gcl. """ -import imp +import importlib.util +import importlib.machinery import os.path import subprocess USE_PYTHON3 = True +def load_source(modname, filename): + loader = importlib.machinery.SourceFileLoader(modname, filename) + spec = importlib.util.spec_from_file_location(modname, + filename, + loader=loader) + module = importlib.util.module_from_spec(spec) + # The module is always executed and not cached in sys.modules. + # Uncomment the following line to cache the module. + # sys.modules[module.__name__] = module + loader.exec_module(module) + return module + + def runSmokeTest(input_api, output_api): hasChangedFiles = False for git_file in input_api.AffectedTextFiles(): @@ -25,8 +39,8 @@ def runSmokeTest(input_api, output_api): if hasChangedFiles: local_root = input_api.change.RepositoryRoot() - utils = imp.load_source('utils', - os.path.join(local_root, 'tools', 'utils.py')) + utils = load_source('utils', + os.path.join(local_root, 'tools', 'utils.py')) dart = os.path.join(utils.CheckedInSdkPath(), 'bin', 'dart') smoke_test = os.path.join(local_root, 'pkg', 'kernel', 'tool', 'smoke_test_quick.dart') diff --git a/sdk/lib/PRESUBMIT.py b/sdk/lib/PRESUBMIT.py index 21b779b5991..691fd78fb12 100644 --- a/sdk/lib/PRESUBMIT.py +++ b/sdk/lib/PRESUBMIT.py @@ -8,13 +8,27 @@ See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for more details about the presubmit API built into gcl. """ -import imp +import importlib.util +import importlib.machinery import os.path import subprocess USE_PYTHON3 = True +def load_source(modname, filename): + loader = importlib.machinery.SourceFileLoader(modname, filename) + spec = importlib.util.spec_from_file_location(modname, + filename, + loader=loader) + module = importlib.util.module_from_spec(spec) + # The module is always executed and not cached in sys.modules. + # Uncomment the following line to cache the module. + # sys.modules[module.__name__] = module + loader.exec_module(module) + return module + + def runSmokeTest(input_api, output_api): hasChangedFiles = False for git_file in input_api.AffectedTextFiles(): @@ -26,8 +40,8 @@ def runSmokeTest(input_api, output_api): if hasChangedFiles: local_root = input_api.change.RepositoryRoot() - utils = imp.load_source('utils', - os.path.join(local_root, 'tools', 'utils.py')) + utils = load_source('utils', + os.path.join(local_root, 'tools', 'utils.py')) dart = os.path.join(utils.CheckedInSdkPath(), 'bin', 'dart') yaml2json = os.path.join(local_root, 'tools', 'yaml2json.dart') libYaml = os.path.join(local_root, 'sdk', 'lib', 'libraries.yaml') diff --git a/tools/bots/bot_utils.py b/tools/bots/bot_utils.py index ff92e00fd3b..819e9c532c7 100755 --- a/tools/bots/bot_utils.py +++ b/tools/bots/bot_utils.py @@ -5,7 +5,8 @@ # BSD-style license that can be found in the LICENSE file. import hashlib -import imp +import importlib.util +import importlib.machinery import os import subprocess import sys @@ -14,9 +15,22 @@ DART_DIR = os.path.abspath( os.path.normpath(os.path.join(__file__, '..', '..', '..'))) +def load_source(modname, filename): + loader = importlib.machinery.SourceFileLoader(modname, filename) + spec = importlib.util.spec_from_file_location(modname, + filename, + loader=loader) + module = importlib.util.module_from_spec(spec) + # The module is always executed and not cached in sys.modules. + # Uncomment the following line to cache the module. + # sys.modules[module.__name__] = module + loader.exec_module(module) + return module + + def GetUtils(): '''Dynamically load the tools/utils.py python module.''' - return imp.load_source('utils', os.path.join(DART_DIR, 'tools', 'utils.py')) + return load_source('utils', os.path.join(DART_DIR, 'tools', 'utils.py')) def run(command, env=None, shell=False, throw_on_error=True): diff --git a/tools/utils.py b/tools/utils.py index e9bb2f92635..13b584389c4 100644 --- a/tools/utils.py +++ b/tools/utils.py @@ -11,7 +11,8 @@ import contextlib import datetime from functools import total_ordering import glob -import imp +import importlib.util +import importlib.machinery import json import os import platform @@ -111,16 +112,29 @@ def GetBaseDir(): return BASE_DIR +def load_source(modname, filename): + loader = importlib.machinery.SourceFileLoader(modname, filename) + spec = importlib.util.spec_from_file_location(modname, + filename, + loader=loader) + module = importlib.util.module_from_spec(spec) + # The module is always executed and not cached in sys.modules. + # Uncomment the following line to cache the module. + # sys.modules[module.__name__] = module + loader.exec_module(module) + return module + + def GetBotUtils(repo_path=DART_DIR): '''Dynamically load the tools/bots/bot_utils.py python module.''' - return imp.load_source( - 'bot_utils', os.path.join(repo_path, 'tools', 'bots', 'bot_utils.py')) + return load_source('bot_utils', + os.path.join(repo_path, 'tools', 'bots', 'bot_utils.py')) def GetMinidumpUtils(repo_path=DART_DIR): '''Dynamically load the tools/minidump.py python module.''' - return imp.load_source('minidump', - os.path.join(repo_path, 'tools', 'minidump.py')) + return load_source('minidump', + os.path.join(repo_path, 'tools', 'minidump.py')) @total_ordering