Skip to content

Commit eee418f

Browse files
authored
Rewrite Pylint Check Without Tox (#42757)
* add pylint check * minor fix * remove creating new package installation * clean imports * clean mypy imports * merge updates * merge updates and remove package install * use pip_install
1 parent 20f7d18 commit eee418f

File tree

3 files changed

+108
-12
lines changed

3 files changed

+108
-12
lines changed

eng/tools/azure-sdk-tools/azpysdk/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from .whl import whl
1616
from .import_all import import_all
1717
from .mypy import mypy
18+
from .pylint import pylint
1819

1920
from ci_tools.logging import configure_logging, logger
2021

@@ -72,6 +73,7 @@ def build_parser() -> argparse.ArgumentParser:
7273
whl().register(subparsers, [common])
7374
import_all().register(subparsers, [common])
7475
mypy().register(subparsers, [common])
76+
pylint().register(subparsers, [common])
7577

7678
return parser
7779

eng/tools/azure-sdk-tools/azpysdk/mypy.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,6 @@ def run(self, args: argparse.Namespace) -> int:
5353

5454
executable, staging_directory = self.get_executable(args.isolate, args.command, sys.executable, package_dir)
5555
logger.info(f"Processing {package_name} for mypy check")
56-
57-
create_package_and_install(
58-
distribution_directory=staging_directory,
59-
target_setup=package_dir,
60-
skip_install=False,
61-
cache_dir=None,
62-
work_dir=staging_directory,
63-
force_create=False,
64-
package_type="wheel",
65-
pre_download_disabled=False,
66-
python_executable=executable
67-
)
6856

6957
# install mypy
7058
try:
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import argparse
2+
import os
3+
import sys
4+
5+
from typing import Optional, List
6+
from subprocess import CalledProcessError, check_call
7+
8+
from .Check import Check
9+
from ci_tools.functions import pip_install
10+
from ci_tools.variables import discover_repo_root, in_ci, set_envvar_defaults, in_ci, set_envvar_defaults
11+
from ci_tools.environment_exclusions import is_check_enabled
12+
from ci_tools.logging import logger
13+
14+
REPO_ROOT = discover_repo_root()
15+
PYLINT_VERSION = "3.2.7"
16+
17+
class pylint(Check):
18+
def __init__(self) -> None:
19+
super().__init__()
20+
21+
def register(self, subparsers: "argparse._SubParsersAction", parent_parsers: Optional[List[argparse.ArgumentParser]] = None) -> None:
22+
"""Register the pylint check. The pylint check installs pylint and runs pylint against the target package.
23+
"""
24+
parents = parent_parsers or []
25+
p = subparsers.add_parser("pylint", parents=parents, help="Run the pylint check")
26+
p.set_defaults(func=self.run)
27+
28+
p.add_argument(
29+
"--next",
30+
default=False,
31+
help="Next version of pylint is being tested.",
32+
required=False,
33+
)
34+
35+
def run(self, args: argparse.Namespace) -> int:
36+
"""Run the pylint check command."""
37+
logger.info("Running pylint check...")
38+
39+
set_envvar_defaults()
40+
targeted = self.get_targeted_directories(args)
41+
42+
results: List[int] = []
43+
44+
for parsed in targeted:
45+
package_dir = parsed.folder
46+
package_name = parsed.name
47+
executable, staging_directory = self.get_executable(args.isolate, args.command, sys.executable, package_dir)
48+
logger.info(f"Processing {package_name} for pylint check")
49+
50+
# install dependencies
51+
try:
52+
pip_install([
53+
"azure-pylint-guidelines-checker==0.5.6", "--index-url=https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-python/pypi/simple/"
54+
], True, executable, package_dir)
55+
except CalledProcessError as e:
56+
logger.error("Failed to install dependencies:", e)
57+
return e.returncode
58+
59+
# install pylint
60+
try:
61+
if args.next:
62+
# use latest version of pylint
63+
pip_install(["pylint"], True, executable, package_dir)
64+
else:
65+
pip_install([f"pylint=={PYLINT_VERSION}"], True, executable, package_dir)
66+
except CalledProcessError as e:
67+
logger.error("Failed to install pylint:", e)
68+
return e.returncode
69+
70+
top_level_module = parsed.namespace.split(".")[0]
71+
72+
if in_ci():
73+
if not is_check_enabled(package_dir, "pylint"):
74+
logger.info(
75+
f"Package {package_name} opts-out of pylint check."
76+
)
77+
continue
78+
79+
rcFileLocation = os.path.join(REPO_ROOT, "eng/pylintrc") if args.next else os.path.join(REPO_ROOT, "pylintrc")
80+
81+
try:
82+
results.append(check_call(
83+
[
84+
executable,
85+
"-m",
86+
"pylint",
87+
"--rcfile={}".format(rcFileLocation),
88+
"--output-format=parseable",
89+
os.path.join(package_dir, top_level_module),
90+
]
91+
))
92+
except CalledProcessError as e:
93+
logger.error(
94+
"{} exited with linting error {}. Please see this link for more information https://aka.ms/azsdk/python/pylint-guide".format(package_name, e.returncode)
95+
)
96+
if args.next and in_ci():
97+
from gh_tools.vnext_issue_creator import create_vnext_issue
98+
create_vnext_issue(package_dir, "pylint")
99+
100+
results.append(e.returncode)
101+
102+
if args.next and in_ci():
103+
from gh_tools.vnext_issue_creator import close_vnext_issue
104+
close_vnext_issue(package_name, "pylint")
105+
106+
return max(results) if results else 0

0 commit comments

Comments
 (0)