Skip to content

Commit 5f56825

Browse files
JakobDevENDrain
authored andcommitted
pip: Add support for pyproject.toml
Co-authored-by: ENDrain <[email protected]>
1 parent 3c915c8 commit 5f56825

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

pip/flatpak-pip-generator

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ parser.add_argument('--cleanup', choices=['scripts', 'all'],
2929
help='Select what to clean up after build')
3030
parser.add_argument('--requirements-file', '-r',
3131
help='Specify requirements.txt file')
32+
parser.add_argument('--pyproject-file',
33+
help='Specify pyproject.toml file')
3234
parser.add_argument('--build-only', action='store_const',
3335
dest='cleanup', const='all',
3436
help='Clean up all files after build')
@@ -60,6 +62,18 @@ parser.add_argument('--ignore-pkg', nargs='*',
6062
help='Ignore a package when generating the manifest. Can only be used with a requirements file')
6163
opts = parser.parse_args()
6264

65+
if opts.requirements_file and opts.pyproject_file:
66+
exit('Can\'t use both requirements and pyproject files at the same time')
67+
68+
if opts.pyproject_file:
69+
try:
70+
from tomllib import load as toml_load
71+
except ModuleNotFoundError:
72+
try:
73+
from tomli import load as toml_load
74+
except ModuleNotFoundError:
75+
exit('tomli modules is not installed. Run "pip install tomli"')
76+
6377
if opts.yaml:
6478
try:
6579
import yaml
@@ -181,6 +195,16 @@ if opts.requirements_file:
181195
print(err)
182196
sys.exit(1)
183197

198+
elif opts.pyproject_file:
199+
pyproject_file = os.path.expanduser(opts.pyproject_file)
200+
with open(pyproject_file, "rb") as f:
201+
pyproject_data = toml_load(f)
202+
dependencies = pyproject_data.get("project", {}).get("dependencies", [])
203+
packages = list(requirements.parse('\n'.join(dependencies)))
204+
with tempfile.NamedTemporaryFile('w', delete=False, prefix='requirements.') as req_file:
205+
req_file.write('\n'.join(dependencies))
206+
requirements_file_output = req_file.name
207+
184208
elif opts.packages:
185209
packages = list(requirements.parse('\n'.join(opts.packages)))
186210
with tempfile.NamedTemporaryFile('w', delete=False, prefix='requirements.') as req_file:

pip/readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ You can use that in your manifest like
4444
* `--build-isolation`: Enable build isolation with pip (recommended but not always work).
4545
* `--cleanup=(scripts|all)`: Add `cleanup` to the manifest. This is used when the packages installed are only used at build time.
4646
* `--build-only`: Alias to `--cleanup=all`.
47-
* `--requirements-file=`, `-r`: Reads the list of packages from `requirements.txt` file.
47+
* `--requirements-file=`, `-r`: Reads the list of packages from `requirements.txt` file. Mutually exclusive with `--pyproject-file`.
48+
* `--pyproject-file=`: Reads the list of packages from `pyproject.toml` file. Mutually exclusive with `--requirements-file` or `r`.
4849
* `--ignore-pkg=`: Ignore a specific package name in a requirements-file, otherwise ignored.
4950
* `--checker-data`: This adds `x-checker-data` to modules so you will be notified when new releases happen. See [flatpak-external-data-checker](https://github.com/flathub/flatpak-external-data-checker) for more details.
5051
* `--runtime=`: Runs `pip` inside of a specific Flatpak runtime instead of on your host. Highly recommended for reproducability and portability. Examples would be `org.freedesktop.Sdk//22.08` or `org.gnome.Sdk/aarch64/43`.

0 commit comments

Comments
 (0)