Skip to content

Commit f87b040

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

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
import codecs
12+
13+
try:
14+
import tomllib as tomli
15+
except ModuleNotFoundError:
16+
import tomli
17+
18+
19+
def main(args):
20+
with open(args.pyproject_file, "rb") as pyproject_file_descriptor:
21+
project_metadata = tomli.load(pyproject_file_descriptor)["project"]
22+
dependencies = project_metadata["dependencies"]
23+
24+
# Optional dependencies listed as per-group dependencies
25+
for dependency_group in project_metadata["optional-dependencies"].values():
26+
dependencies += list(dependency_group)
27+
print(args.dependency_separator.join(dependencies))
28+
29+
30+
if __name__ == "__main__":
31+
parser = argparse.ArgumentParser(
32+
prog="python extract_dependencies_from_pyproject_toml.py",
33+
formatter_class=argparse.RawTextHelpFormatter,
34+
description="Extracts the dependencies from a pyproject.toml file",
35+
)
36+
parser.add_argument(
37+
"-f",
38+
"--pyproject-file",
39+
metavar="FILE",
40+
help="Path of the pyproject.toml file",
41+
)
42+
43+
parser.add_argument(
44+
"-s",
45+
"--dependency-separator",
46+
default=" ",
47+
type=lambda s: codecs.decode(s, "unicode_escape") if s == "\\n" else s,
48+
help="Dependency separator (default: ' ')",
49+
)
50+
main(parser.parse_args())

0 commit comments

Comments
 (0)