Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pip: Add support for pyproject.toml #332

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pip/flatpak-pip-generator
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ parser.add_argument('--cleanup', choices=['scripts', 'all'],
help='Select what to clean up after build')
parser.add_argument('--requirements-file', '-r',
help='Specify requirements.txt file')
parser.add_argument('--pyproject-file',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rebase and update README.md to include this option.

help='Specify pyproject.toml file')
parser.add_argument('--build-only', action='store_const',
dest='cleanup', const='all',
help='Clean up all files after build')
Expand Down Expand Up @@ -57,6 +59,18 @@ parser.add_argument('--ignore-errors', action='store_true',
help='Ignore errors when downloading packages')
opts = parser.parse_args()

if opts.requirements_file and opts.pyproject_file:
exit('Can\'t use both requirements and pyproject files at the same time')

if opts.pyproject_file:
try:
from tomllib import load as toml_load
except ModuleNotFoundError:
try:
from tomli import load as toml_load
except ModuleNotFoundError:
exit('tomli modules is not installed. Run "pip install tomli"')

if opts.yaml:
try:
import yaml
Expand Down Expand Up @@ -167,6 +181,16 @@ if opts.requirements_file:
except FileNotFoundError:
pass

elif opts.pyproject_file:
Copy link
Member

@TingPing TingPing Jan 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it should error if both --requirements-file and --pyproject-file are given.

pyproject_file = os.path.expanduser(opts.pyproject_file)
with open(pyproject_file, "rb") as f:
pyproject_data = toml_load(f)
dependencies = pyproject_data.get("project", {}).get("dependencies", [])
packages = list(requirements.parse('\n'.join(dependencies)))
with tempfile.NamedTemporaryFile('w', delete=False, prefix='requirements.') as req_file:
req_file.write('\n'.join(dependencies))
requirements_file = req_file.name

elif opts.packages:
packages = list(requirements.parse('\n'.join(opts.packages)))
with tempfile.NamedTemporaryFile('w', delete=False, prefix='requirements.') as req_file:
Expand Down
3 changes: 2 additions & 1 deletion pip/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ You can use that in your manifest like
* `--python2`: Build with Python 2. Note that you will have to build [the Python 2 shared-module](https://github.com/flathub/shared-modules/tree/master/python2.7) as it is not in any runtime.
* `--cleanup=(scripts|all)`: Add `cleanup` to the manifest. This is used when the packages installed are only used at build time.
* `--build-only`: Alias to `--cleanup=all`.
* `--requirements-file=`, `-r`: Reads the list of packages from `requirements.txt` file.
* `--requirements-file=`, `-r`: Reads the list of packages from `requirements.txt` file. Mutually exclusive with `--pyproject-file`.
* `--pyproject-file=`: Reads the list of packages from `pyproject.toml` file. Mutually exclusive with `--requirements-file` or `r`.
* `--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.
* `--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`.
* `--ignore-errors=`: Allow the generation of empty or otherwise broken files when downloading packages fails.
Expand Down