Skip to content

Commit 150222d

Browse files
committed
[Feat]: Add lint-requirements command
1 parent fac7ee6 commit 150222d

File tree

7 files changed

+86
-0
lines changed

7 files changed

+86
-0
lines changed

.github/workflows/test.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ jobs:
101101
- extra_metadata
102102
- elfdeps
103103
- prebuilt_wheel_hook
104+
- lint_requirements
104105
os:
105106
- ubuntu-latest
106107
- macos-latest

.mergify.yml

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pull_request_rules:
4848
- check-success=e2e (3.11, 1.75, prebuilt_wheels_alt_server, ubuntu-latest)
4949
- check-success=e2e (3.11, 1.75, report_missing_dependency, ubuntu-latest)
5050
- check-success=e2e (3.11, 1.75, rust_vendor, ubuntu-latest)
51+
- check-success=e2e (3.11, 1.75, lint_requirements, ubuntu-latest)
5152
- check-success=e2e (3.12, 1.75, bootstrap, macos-latest)
5253
- check-success=e2e (3.12, 1.75, bootstrap, ubuntu-latest)
5354
- check-success=e2e (3.12, 1.75, bootstrap_build_tags, macos-latest)
@@ -92,6 +93,8 @@ pull_request_rules:
9293
- check-success=e2e (3.12, 1.75, report_missing_dependency, ubuntu-latest)
9394
- check-success=e2e (3.12, 1.75, rust_vendor, macos-latest)
9495
- check-success=e2e (3.12, 1.75, rust_vendor, ubuntu-latest)
96+
- check-success=e2e (3.12, 1.75, lint_requirements, macos-latest)
97+
- check-success=e2e (3.12, 1.75, lint_requirements, ubuntu-latest)
9598
- "-draft"
9699

97100
# At least 1 reviewer

e2e/test_lint_requirements.sh

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
# -*- indent-tabs-mode: nil; tab-width: 2; sh-indentation: 2; -*-
3+
4+
# This script acts as an e2e test for lint_requirements command of fromager
5+
6+
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7+
source "$SCRIPTDIR/common.sh"
8+
9+
pass=true
10+
11+
fromager lint-requirements "$SCRIPTDIR/validate_inputs/constraints.txt" "$SCRIPTDIR/validate_inputs/requirements.txt"
12+
13+
if [ $? -ne 0 ]; then
14+
echo "the input files are not correctly formatted."
15+
pass=false
16+
fi;
17+
18+
$pass

e2e/validate_inputs/constraints.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This requirements.txt is used for testing lint_requirements
2+
# command of fromager and contains examples of packages
3+
4+
gast==0.5.5
5+
torch==2.3.1

e2e/validate_inputs/requirements.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This requirements.txt is used for testing lint_requirements
2+
# command of fromager an contains examples of packages
3+
stevedore==5.2.0
4+
kfp==2.11.0
5+

src/fromager/commands/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
download_sequence,
77
graph,
88
lint,
9+
lint_requirements,
910
list_overrides,
1011
migrate_config,
1112
server,
@@ -26,4 +27,5 @@
2627
download_sequence.download_sequence,
2728
server.wheel_server,
2829
migrate_config.migrate_config,
30+
lint_requirements.lint_requirements,
2931
]
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import fileinput
2+
import glob
3+
import itertools
4+
import logging
5+
import sys
6+
7+
import click
8+
from packaging.requirements import InvalidRequirement, Requirement
9+
10+
logger = logging.getLogger(__name__)
11+
12+
13+
@click.command()
14+
@click.argument("constraints_wildcard", type=click.Path(exists=False))
15+
@click.argument("requirements_wildcard", type=click.Path(exists=False))
16+
def lint_requirements(constraints_wildcard: str, requirements_wildcard: str) -> None:
17+
"""
18+
Command to lint the constraints.txt and requirements.txt files
19+
This command takes two wildcard paths for constraints.txt and requirements.txt respectively.
20+
It checks the formatting of these files and reports issues if found.
21+
"""
22+
# Exit if user does not provide wildcard paths
23+
if not constraints_wildcard or not requirements_wildcard:
24+
logger.error("path for requirements.txt or constraints.txt is missing")
25+
sys.exit(1)
26+
27+
# Get all the files to check in a list
28+
files_to_check = list(
29+
itertools.chain.from_iterable(
30+
glob.glob(f) for f in [constraints_wildcard, requirements_wildcard]
31+
)
32+
)
33+
34+
if len(files_to_check) == 0:
35+
logger.error("no constraints.txt or requirements.txt found in given paths")
36+
sys.exit(1)
37+
38+
flag = True
39+
40+
for line in fileinput.input(files_to_check):
41+
string_to_check, _, _ = line.partition("#")
42+
if not string_to_check.strip():
43+
continue
44+
45+
try:
46+
Requirement(string_to_check)
47+
except InvalidRequirement as err:
48+
logger.error(f"{fileinput.filename()}: {fileinput.filelineno()}: {err}")
49+
flag = False
50+
51+
if not flag:
52+
sys.exit(1)

0 commit comments

Comments
 (0)