Skip to content

Commit 2ce0f0d

Browse files
committed
fix bps bug and ensure running var has value
1 parent 9d93398 commit 2ce0f0d

File tree

10 files changed

+132
-216
lines changed

10 files changed

+132
-216
lines changed

.github/workflows/precommit.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
- uses: actions/checkout@v5
1212
- uses: actions/setup-python@v6
1313
with:
14-
python-version: "3.10"
14+
python-version: "3.11"
1515
- name: Set up uv
1616
uses: astral-sh/setup-uv@v7
1717
- name: Install dependencies

batchtools/batchtools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def build_parser(self) -> None:
3232
"--verbose",
3333
"-v",
3434
action="count",
35+
default=0,
3536
help="Increase verbosity of output",
3637
)
3738

batchtools/bps.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111

1212

1313
class ListPodsCommandArgs(argparse.Namespace):
14-
verbose: int = 0
14+
verbose: int | None = 0
1515
node_names: list[str] = []
1616

1717

1818
class ListPodsCommand(Command):
1919
"""
20-
List active GPU pods per node. By default prints only BUSY nodes.
21-
With -v/--verbose, prints FREE for nodes seen with Running pods but 0 GPUs.
20+
batchtools [-v] bps [-h] [node-name [node-name ...]]
21+
22+
List active GPU pods per node. By default prints only BUSY nodes. So, it all nodes are FREE, it will return empty nodes/
23+
With -v/--verbose, prints FREE for nodes that have Running pods but 0 GPUs.
2224
"""
2325

2426
name: str = "bps"
@@ -35,43 +37,45 @@ def build_parser(cls, subparsers: SubParserFactory):
3537
@override
3638
def run(args: argparse.Namespace):
3739
args = cast(ListPodsCommandArgs, args)
40+
v = args.verbose or 0 # treat None as 0
41+
3842
try:
3943
with oc.timeout(120):
4044
all_pods = oc.selector("pods", all_namespaces=True).objects()
4145

4246
if args.node_names:
43-
# get individual nodes without repeats
47+
# filter to Running pods on requested nodes
4448
node_set = set(args.node_names)
45-
# Filter to Running pods on requested nodes
4649
pods_for_nodes = [
4750
p
4851
for p in all_pods
4952
if getattr(p.model.status, "phase", None) == "Running"
5053
and (getattr(p.model.spec, "nodeName", None) or "") in node_set
5154
]
55+
5256
# Group by node
53-
pods_by_node = defaultdict(list)
57+
pods_by_node: dict[str, list] = defaultdict(list)
5458
for p in pods_for_nodes:
5559
n = getattr(p.model.spec, "nodeName", None) or ""
5660
pods_by_node[n].append(p)
5761

62+
# Emit per requested node (only those requested)
5863
for node in node_set:
59-
lines = summarize_gpu_pods(
60-
pods_by_node.get(node, []), args.verbose > 0
61-
)
62-
if not lines and args.verbose:
64+
lines = summarize_gpu_pods(pods_by_node.get(node, []), v > 0)
65+
if not lines and v > 0:
6366
print(f"{node}: FREE")
6467
else:
6568
for ln in lines:
6669
print(ln)
70+
6771
else:
68-
# One global summary over all Running pods
72+
# Global summary over all Running pods
6973
running = [
7074
p
7175
for p in all_pods
7276
if getattr(p.model.status, "phase", None) == "Running"
7377
]
74-
for ln in summarize_gpu_pods(running, args.verbose > 0):
78+
for ln in summarize_gpu_pods(running, v > 0):
7579
print(ln)
7680

7781
except oc.OpenShiftPythonException as e:
@@ -105,7 +109,7 @@ def summarize_gpu_pods(pods, verbose: bool) -> list[str]:
105109
except Exception:
106110
continue
107111

108-
lines = []
112+
lines: list[str] = []
109113
nodes = sorted(seen_nodes or totals.keys())
110114
for node in nodes:
111115
total = totals.get(node, 0)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Metadata-Version: 2.4
2+
Name: python-batchtools
3+
Version: 0.1.0
4+
Summary: Add your description here
5+
Requires-Python: >=3.11
6+
Description-Content-Type: text/markdown
7+
Requires-Dist: openshift-client>=2.0.5
8+
Requires-Dist: typing-extensions>=4.15.0
9+
10+
# python-batchtools
11+
12+
## Set up your development environment
13+
14+
### Install tools
15+
16+
1. Start by installing `uv`. Depending on your distribution, this may be as simple as:
17+
18+
```sh
19+
sudo dnf -y install uv
20+
```
21+
22+
If you would like to run the latest version, you can install the command using `pipx`. First, install `pipx`:
23+
24+
```
25+
sudo dnf -y install pipx
26+
```
27+
28+
And then use `pipx` to install `uv`:
29+
30+
```
31+
pipx install uv
32+
```
33+
34+
2. Next, install `pre-commit`. As with `uv`, you can install this using your system package manager:
35+
36+
```
37+
sudo dnf -y install pre-commit
38+
```
39+
40+
Or you can install a possibly more recent version using `pipx`:
41+
42+
```
43+
pipx install pre-commit
44+
```
45+
46+
47+
### Activate pre-commit
48+
49+
Activate `pre-commit` for your working copy of this repository by running:
50+
51+
```
52+
pre-commit install
53+
```
54+
55+
This will configure `.git/hooks/pre-commit` to run the `pre-commit` tool every time you make a commit. Running these tests locally ensures that your code is clean and that tests are passing before you share your code with others. To manually run all the checks:
56+
57+
```
58+
pre-commit run --all-files
59+
```
60+
61+
62+
### Install dependencies
63+
64+
To install the project dependencies, run:
65+
66+
```
67+
uv sync --all-extras
68+
```
69+
70+
### Run tests
71+
72+
To run just the unit tests:
73+
74+
```
75+
uv run pytest
76+
```
77+
78+
This will generate a test coverage report in `htmlcov/index.html`.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
README.md
2+
pyproject.toml
3+
batchtools/__init__.py
4+
batchtools/basecommand.py
5+
batchtools/batchtools.py
6+
batchtools/bd.py
7+
batchtools/bj.py
8+
batchtools/bl.py
9+
batchtools/bp.py
10+
batchtools/bps.py
11+
batchtools/bq.py
12+
batchtools/br.py
13+
batchtools/build_yaml.py
14+
batchtools/file_setup.py
15+
batchtools/helpers.py
16+
python_batchtools.egg-info/PKG-INFO
17+
python_batchtools.egg-info/SOURCES.txt
18+
python_batchtools.egg-info/dependency_links.txt
19+
python_batchtools.egg-info/entry_points.txt
20+
python_batchtools.egg-info/requires.txt
21+
python_batchtools.egg-info/top_level.txt
22+
tests/test_batchtools.py
23+
tests/test_bd.py
24+
tests/test_bj.py
25+
tests/test_bl.py
26+
tests/test_bp.py
27+
tests/test_bps.py
28+
tests/test_br.py

python_batchtools.egg-info/dependency_links.txt

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[console_scripts]
2+
batchtools = batchtools.batchtools:main
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
openshift-client>=2.0.5
2+
typing-extensions>=4.15.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
batchtools

0 commit comments

Comments
 (0)