-
Notifications
You must be signed in to change notification settings - Fork 24.9k
[BE] add a linter to check consistency for cmake minimum version in requirements #156961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
b69b402
Update
XuehaiPan 531410b
Update
XuehaiPan 8c05e82
Update
XuehaiPan 57e3012
Update
XuehaiPan 50b3241
Update
XuehaiPan cc945a9
Update
XuehaiPan 4f0019e
Update
XuehaiPan 5bebecf
Update
XuehaiPan 376cf59
Update
XuehaiPan ac9d8d1
Update
XuehaiPan fd891bb
Update
XuehaiPan b8cb4c5
Update
XuehaiPan b9d904b
Update
XuehaiPan 6a05fb5
Update
XuehaiPan 17664fa
Update
XuehaiPan 45a5517
Update
XuehaiPan bddbc27
Update
XuehaiPan 1b0c79e
Update
XuehaiPan e474ef7
Update
XuehaiPan bdeef82
Update
XuehaiPan ab189b4
Update
XuehaiPan 993e599
Update
XuehaiPan 9d65d13
Update
XuehaiPan b7ee0b9
Update
XuehaiPan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
from __future__ import annotations | ||
|
||
import argparse | ||
import concurrent.futures | ||
import fnmatch | ||
import json | ||
import logging | ||
import os | ||
import re | ||
import sys | ||
from enum import Enum | ||
from pathlib import Path | ||
from typing import NamedTuple | ||
|
||
from packaging.requirements import Requirement | ||
from packaging.version import Version | ||
|
||
|
||
if sys.version_info >= (3, 11): | ||
import tomllib | ||
else: | ||
import tomli as tomllib # type: ignore[import-not-found] | ||
|
||
|
||
REPO_ROOT = Path(__file__).absolute().parents[3] | ||
sys.path.insert(0, str(REPO_ROOT)) | ||
|
||
from tools.setup_helpers.env import CMAKE_MINIMUM_VERSION_STRING | ||
|
||
|
||
sys.path.remove(str(REPO_ROOT)) | ||
|
||
|
||
LINTER_CODE = "CMAKE_MINIMUM_REQUIRED" | ||
CMAKE_MINIMUM_VERSION = Version(CMAKE_MINIMUM_VERSION_STRING) | ||
|
||
|
||
class LintSeverity(str, Enum): | ||
ERROR = "error" | ||
WARNING = "warning" | ||
ADVICE = "advice" | ||
DISABLED = "disabled" | ||
|
||
|
||
class LintMessage(NamedTuple): | ||
path: str | None | ||
line: int | None | ||
char: int | None | ||
code: str | ||
severity: LintSeverity | ||
name: str | ||
original: str | None | ||
replacement: str | None | ||
description: str | None | ||
|
||
|
||
def format_error_message( | ||
filename: str, | ||
error: Exception | None = None, | ||
*, | ||
line: int | None = None, | ||
message: str | None = None, | ||
) -> LintMessage: | ||
if message is None and error is not None: | ||
message = f"Failed due to {error.__class__.__name__}:\n{error}" | ||
return LintMessage( | ||
path=filename, | ||
line=line, | ||
char=None, | ||
code=LINTER_CODE, | ||
severity=LintSeverity.ERROR, | ||
name="CMake minimum version", | ||
original=None, | ||
replacement=None, | ||
description=message, | ||
) | ||
|
||
|
||
CMAKE_MINIMUM_REQUIRED_PATTERN = re.compile( | ||
r"cmake_minimum_required\(VERSION\s+(?P<version>\d+\.\d+(\.\d+)?)\b.*\)", | ||
flags=re.IGNORECASE, | ||
) | ||
|
||
|
||
def check_cmake(path: Path) -> list[LintMessage]: | ||
with path.open(encoding="utf-8") as f: | ||
for i, line in enumerate(f, start=1): | ||
if match := CMAKE_MINIMUM_REQUIRED_PATTERN.search(line): | ||
version = match.group("version") | ||
if path.samefile(REPO_ROOT / "CMakeLists.txt"): | ||
if Version(version) != CMAKE_MINIMUM_VERSION: | ||
return [ | ||
format_error_message( | ||
str(path), | ||
line=i, | ||
message=( | ||
f"CMake minimum version must be {CMAKE_MINIMUM_VERSION}, " | ||
f"but found {version}." | ||
), | ||
) | ||
] | ||
elif Version(version) > CMAKE_MINIMUM_VERSION: | ||
return [ | ||
format_error_message( | ||
str(path), | ||
line=i, | ||
message=( | ||
f"The environment can only provide CMake {CMAKE_MINIMUM_VERSION}, " | ||
f"but found requiring {version}." | ||
), | ||
) | ||
] | ||
return [] | ||
|
||
|
||
def check_requirement( | ||
requirement: Requirement, | ||
path: Path, | ||
*, | ||
line: int | None = None, | ||
) -> LintMessage | None: | ||
if requirement.name.lower() != "cmake": | ||
return None | ||
|
||
for spec in requirement.specifier: | ||
if ( | ||
spec.operator in ("==", ">=") | ||
and Version(spec.version.removesuffix(".*")) < CMAKE_MINIMUM_VERSION | ||
): | ||
return format_error_message( | ||
str(path), | ||
line=line, | ||
message=( | ||
f"CMake minimum version must be at least {CMAKE_MINIMUM_VERSION}, " | ||
f"but found {spec}." | ||
), | ||
) | ||
|
||
return None | ||
|
||
|
||
def check_pyproject(path: Path) -> list[LintMessage]: | ||
try: | ||
pyproject = tomllib.loads(path.read_text(encoding="utf-8")) | ||
except (tomllib.TOMLDecodeError, OSError) as err: | ||
return [format_error_message(str(path), err)] | ||
|
||
if not isinstance(pyproject, dict): | ||
return [] | ||
if not isinstance(pyproject.get("build-system"), dict): | ||
return [] | ||
|
||
build_system = pyproject["build-system"] | ||
requires = build_system.get("requires") | ||
if not isinstance(requires, list): | ||
return [] | ||
return list( | ||
filter( | ||
None, | ||
(check_requirement(Requirement(req), path=path) for req in requires), | ||
) | ||
) | ||
|
||
|
||
def check_requirements(path: Path) -> list[LintMessage]: | ||
try: | ||
with path.open(encoding="utf-8") as f: | ||
lines = f.readlines() | ||
except OSError as err: | ||
return [format_error_message(str(path), err)] | ||
|
||
lint_messages = [] | ||
for i, line in enumerate(lines, start=1): | ||
line = line.strip() | ||
if not line or line.startswith(("#", "-")): | ||
continue | ||
try: | ||
requirement = Requirement(line) | ||
except Exception: | ||
continue | ||
lint_message = check_requirement(requirement, path=path, line=i) | ||
if lint_message is not None: | ||
lint_messages.append(lint_message) | ||
|
||
return lint_messages | ||
|
||
|
||
def check_file(filename: str) -> list[LintMessage]: | ||
path = Path(filename).absolute() | ||
basename = path.name.lower() | ||
if basename in ("cmakelists.txt", "cmakelists.txt.in") or basename.endswith( | ||
(".cmake", ".cmake.in") | ||
): | ||
return check_cmake(path) | ||
if basename == "pyproject.toml": | ||
return check_pyproject(path) | ||
if fnmatch.fnmatch(basename, "*requirements*.txt") or fnmatch.fnmatch( | ||
basename, "*requirements*.in" | ||
): | ||
return check_requirements(path) | ||
return [] | ||
|
||
|
||
def main() -> None: | ||
parser = argparse.ArgumentParser( | ||
description="Check consistency of cmake minimum version in requirement files.", | ||
fromfile_prefix_chars="@", | ||
) | ||
parser.add_argument( | ||
"--verbose", | ||
action="store_true", | ||
help="verbose logging", | ||
) | ||
parser.add_argument( | ||
"filenames", | ||
nargs="+", | ||
help="paths to lint", | ||
) | ||
args = parser.parse_args() | ||
|
||
logging.basicConfig( | ||
format="<%(processName)s:%(levelname)s> %(message)s", | ||
level=logging.NOTSET | ||
if args.verbose | ||
else logging.DEBUG | ||
if len(args.filenames) < 1000 | ||
else logging.INFO, | ||
stream=sys.stderr, | ||
) | ||
|
||
with concurrent.futures.ProcessPoolExecutor( | ||
max_workers=os.cpu_count(), | ||
) as executor: | ||
futures = {executor.submit(check_file, x): x for x in args.filenames} | ||
for future in concurrent.futures.as_completed(futures): | ||
try: | ||
for lint_message in future.result(): | ||
print(json.dumps(lint_message._asdict()), flush=True) | ||
except Exception: | ||
logging.critical('Failed at "%s".', futures[future]) | ||
raise | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: attach logging config to specific logger instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is copied from other files.