Skip to content

Commit a60685b

Browse files
committed
Factor out dependency extraction from pyproject into separate script
1 parent f052165 commit a60685b

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ jobs:
108108
109109
# Add homogeneous TOML support (Python >= 3.12 has standard tomllib)
110110
$CONDA install -y -n "$CONDA_ENV" tomli
111-
$CONDA run --no-capture-output -n "$CONDA_ENV" python -c \
112-
"import tomli; from functools import reduce; fp = open('pyproject.toml', 'rb'); p = tomli.load(fp); fp.close(); print(' '.join(reduce(lambda x, y: x + y, list(p['project']['optional-dependencies'].values()), p['project']['dependencies'])))" > requires.txt
111+
$CONDA run --no-capture-output -n "$CONDA_ENV" python scripts/extract_dependencies_from_pyproject_toml.py -f "pyproject.toml" > requires.txt
113112
$CONDA install -y -n "$CONDA_ENV" `cat requires.txt`
114113
rm -f requires.txt
115114
done
@@ -265,8 +264,7 @@ jobs:
265264
# Python versioneer fails to compute the current version correctly otherwise
266265
git config --global --add safe.directory $(Resolve-Path '.' | % {$_.toString()})
267266
python -m pip install setuptools
268-
python -c `
269-
"import tomllib; from functools import reduce; fp = open('pyproject.toml', 'rb'); p = tomllib.load(fp); fp.close(); print('\n'.join(reduce(lambda x, y: x + y, list(p['project']['optional-dependencies'].values()), p['project']['dependencies'])))" > requires.txt
267+
python scripts/extract_dependencies_from_pyproject_toml.py -f "pyproject.toml" -s "\n" > requires.txt
270268
271269
# Install the Python requirements
272270
Get-Content .\requires.txt `
@@ -349,8 +347,7 @@ jobs:
349347
git config --global --add safe.directory $(realpath .)
350348
# Install tomli for Python < 3.11
351349
pip install tomli
352-
python -c \
353-
"import tomli; from functools import reduce; fp = open('pyproject.toml', 'rb'); p = tomli.load(fp); fp.close(); print(' '.join(reduce(lambda x, y: x + y, list(p['project']['optional-dependencies'].values()), p['project']['dependencies'])))" > requires.txt
350+
python scripts/extract_dependencies_from_pyproject_toml.py -f "pyproject.toml" > requires.txt
354351
pip install `cat requires.txt`
355352
rm -f requires.txt
356353
- name: Setup and Install Test Requirements
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
######################################################################################
2+
# Copyright (c) 2023-2025 Orange. All rights reserved. #
3+
# This software is distributed under the BSD 3-Clause-clear License, the text of #
4+
# which is available at https://spdx.org/licenses/BSD-3-Clause-Clear.html or #
5+
# see the "LICENSE.md" file for more details. #
6+
######################################################################################
7+
8+
"""Extract dependencies from pyproject.toml file"""
9+
10+
import argparse
11+
12+
try:
13+
import tomllib as tomli
14+
except ModuleNotFoundError:
15+
import tomli
16+
17+
18+
def main(args):
19+
with open(args.pyproject_file, "rb") as pyproject_file_descriptor:
20+
project_metadata = tomli.load(pyproject_file_descriptor)["project"]
21+
dependencies = project_metadata["dependencies"]
22+
23+
# Optional dependencies listed as per-group dependencies
24+
dependencies += list(project_metadata["optional_dependencies"].values())
25+
26+
print(args.dependency_separator.join(dependencies))
27+
28+
29+
if __name__ == "__main__":
30+
parser = argparse.ArgumentParser(
31+
prog="python extract_dependencies_from_pyproject_toml.py",
32+
formatter_class=argparse.RawTextHelpFormatter,
33+
description="Extracts the dependencies from a pyproject.toml file",
34+
)
35+
parser.add_argument(
36+
"-f",
37+
"--pyproject-file",
38+
metavar="FILE",
39+
help="Path of the pyproject.toml file",
40+
)
41+
parser.add_argument(
42+
"-s",
43+
"--dependency-separator",
44+
default=" ",
45+
help="Dependency separator (default: ' ')",
46+
)
47+
main(parser.parse_args())

0 commit comments

Comments
 (0)