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
Changes from 1 commit
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
21 changes: 21 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 All @@ -48,6 +50,15 @@ parser.add_argument('--yaml', action='store_true',
help='Use YAML as output format instead of JSON')
opts = parser.parse_args()

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 @@ -158,6 +169,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