Require isort on all Python files. (#8420)

This commit is contained in:
Jiaming Yuan
2022-11-08 12:59:06 +08:00
committed by GitHub
parent bf8de227a9
commit 0d3da9869c
69 changed files with 290 additions and 187 deletions

View File

@@ -12,16 +12,31 @@ CURDIR = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
PROJECT_ROOT = os.path.normpath(os.path.join(CURDIR, os.path.pardir, os.path.pardir))
def run_formatter(rel_path: str) -> bool:
path = os.path.join(PROJECT_ROOT, rel_path)
isort_ret = subprocess.run(["isort", "--check", "--profile=black", path]).returncode
black_ret = subprocess.run(["black", "--check", rel_path]).returncode
if isort_ret != 0 or black_ret != 0:
msg = (
"Please run the following command on your machine to address the format"
f" errors:\n isort --profile=black {rel_path}\n black {rel_path}\n"
)
print(msg, file=sys.stdout)
def run_black(rel_path: str) -> bool:
cmd = ["black", "-q", "--check", rel_path]
ret = subprocess.run(cmd).returncode
if ret != 0:
subprocess.run(["black", "--version"])
msg = """
Please run the following command on your machine to address the formatting error:
"""
msg += " ".join(cmd)
print(msg, file=sys.stderr)
return False
return True
def run_isort(rel_path: str) -> bool:
cmd = ["isort", "--check", "--profile=black", rel_path]
ret = subprocess.run(cmd).returncode
if ret != 0:
msg = """
Please run the following command on your machine to address the formatting error:
"""
msg += " ".join(cmd)
print(msg, file=sys.stderr)
return False
return True
@@ -114,8 +129,8 @@ if __name__ == "__main__":
parser.add_argument("--pylint", type=int, choices=[0, 1], default=1)
args = parser.parse_args()
if args.format == 1:
if not all(
run_formatter(path)
black_results = [
run_black(path)
for path in [
# core
"python-package/xgboost/__init__.py",
@@ -141,7 +156,28 @@ if __name__ == "__main__":
"demo/guide-python/categorical.py",
"demo/guide-python/spark_estimator_examples.py",
]
):
]
if not all(black_results):
sys.exit(-1)
isort_results = [
run_isort(path)
for path in [
# core
"python-package/",
# tests
"tests/test_distributed/",
"tests/python/",
"tests/python-gpu/",
"tests/ci_build/",
# demo
"demo/",
# misc
"dev/",
"doc/",
]
]
if not all(black_results):
sys.exit(-1)
if args.type_check == 1: