Skip to content
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

[Feat]: Add lint-requirements command #545

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ jobs:
- extra_metadata
- elfdeps
- prebuilt_wheel_hook
- lint_requirements
os:
- ubuntu-latest
- macos-latest
Expand Down
3 changes: 3 additions & 0 deletions .mergify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pull_request_rules:
- check-success=e2e (3.11, 1.75, prebuilt_wheels_alt_server, ubuntu-latest)
- check-success=e2e (3.11, 1.75, report_missing_dependency, ubuntu-latest)
- check-success=e2e (3.11, 1.75, rust_vendor, ubuntu-latest)
- check-success=e2e (3.11, 1.75, lint_requirements, ubuntu-latest)
- check-success=e2e (3.12, 1.75, bootstrap, macos-latest)
- check-success=e2e (3.12, 1.75, bootstrap, ubuntu-latest)
- check-success=e2e (3.12, 1.75, bootstrap_build_tags, macos-latest)
Expand Down Expand Up @@ -92,6 +93,8 @@ pull_request_rules:
- check-success=e2e (3.12, 1.75, report_missing_dependency, ubuntu-latest)
- check-success=e2e (3.12, 1.75, rust_vendor, macos-latest)
- check-success=e2e (3.12, 1.75, rust_vendor, ubuntu-latest)
- check-success=e2e (3.12, 1.75, lint_requirements, macos-latest)
- check-success=e2e (3.12, 1.75, lint_requirements, ubuntu-latest)
- "-draft"

# At least 1 reviewer
Expand Down
32 changes: 32 additions & 0 deletions e2e/test_lint_requirements.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
# -*- indent-tabs-mode: nil; tab-width: 2; sh-indentation: 2; -*-

# This script acts as an e2e test for lint_requirements command of fromager

SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$SCRIPTDIR/common.sh"

pass=true

# Test to demonstrate that command works as expected when input files are valid

if ! fromager lint-requirements "$SCRIPTDIR/validate_inputs/constraints.txt" "$SCRIPTDIR/validate_inputs/requirements.txt"; then
echo "the input files should have been recognized as correctly formatted" 1>&2
pass=false
fi;

# Test to demonstrate failing of command due to missing input args

if fromager lint-requirements; then
echo "missing input files should have been recognized by the command" 1>&2
pass=false
fi;

# Test to demonstrate that command reports error for invalid / bad input files

if fromager lint-requirements "$SCRIPTDIR/validate_inputs/constraints.txt" "$SCRIPTDIR/validate_inputs/invalid-requirements.txt"; then
echo "invalid input files should have been recognized by the command" 1>&2
pass=false
fi;

$pass
5 changes: 5 additions & 0 deletions e2e/validate_inputs/constraints.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This requirements.txt is used for testing lint_requirements
# command of fromager and contains examples of packages

gast==0.5.5
torch==2.3.1
5 changes: 5 additions & 0 deletions e2e/validate_inputs/invalid-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This requirements file is invalid on purpose to test the lint-requirements
# command of fromager. Do not attempt to fix this file

foo==
bar<=
5 changes: 5 additions & 0 deletions e2e/validate_inputs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This requirements.txt is used for testing lint_requirements
# command of fromager and contains examples of packages
stevedore==5.2.0
kfp==2.11.0

2 changes: 2 additions & 0 deletions src/fromager/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
download_sequence,
graph,
lint,
lint_requirements,
list_overrides,
migrate_config,
server,
Expand All @@ -26,4 +27,5 @@
download_sequence.download_sequence,
server.wheel_server,
migrate_config.migrate_config,
lint_requirements.lint_requirements,
]
39 changes: 39 additions & 0 deletions src/fromager/commands/lint_requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import logging
import sys

import click
from packaging.requirements import InvalidRequirement, Requirement

from fromager import requirements_file

logger = logging.getLogger(__name__)


@click.command()
@click.argument(
"input_files_path", nargs=-1, required=True, type=click.Path(exists=False)
)
def lint_requirements(input_files_path: list[click.Path]) -> None:
"""
Command to lint the constraints.txt and requirements.txt files
This command takes a single wildcard path string for constraints.txt and requirements.txt.
It checks the formatting of these files and reports issues if found.
"""

if len(input_files_path) == 0:
logger.error("no constraints.txt or requirements.txt found in given paths")
sys.exit(1)

flag = True

for path in input_files_path:
parsed_lines = requirements_file.parse_requirements_file(str(path))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to convert this to string because the function parse_requirements_file only accepts str | pathlib.Path

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine. For future reference, https://github.com/python-wheel-build/fromager/blob/main/src/fromager/clickext.py#L10 defines a class that can convert click.Path() to pathlib.Path(). We use that in a few other places.

for line in parsed_lines:
try:
Requirement(line)
except InvalidRequirement as err:
logger.error(f"{path}: {line}: {err}")
flag = False

if not flag:
sys.exit(1)
Loading