未验证 提交 fd143544 编写于 作者: F Frost Ming 提交者: GitHub

Merge pull request #52 from frostming/feature/init-python

improve init command to add more options
Allow specifying Python interpreter via `--python` option in `pdm init`.
Set `python_requires` when initializing and defaults to `>={current_version}`.
......@@ -415,6 +415,7 @@ def do_init(
license: str = "MIT",
author: str = "",
email: str = "",
python_requires: str = "",
) -> None:
"""Bootstrap the project and create a pyproject.toml"""
data = {
......@@ -432,6 +433,8 @@ def do_init(
},
"build-system": {"requires": ["pdm"], "build-backend": "pdm.builders.api"},
}
if python_requires and python_requires != "*":
data["tool"]["pdm"]["python_requires"] = python_requires
if not project.pyproject:
project._pyproject = data
else:
......
......@@ -15,7 +15,7 @@ from pdm.cli.options import (
)
from pdm.context import context
from pdm.project import Project
from pdm.utils import get_user_email_from_git
from pdm.utils import get_python_version, get_user_email_from_git
pass_project = click.make_pass_decorator(Project, ensure=True)
context_settings = {"ignore_unknown_options": True, "allow_extra_args": True}
......@@ -283,10 +283,19 @@ def build(project, sdist, wheel, dest, clean):
actions.do_build(project, sdist, wheel, dest, clean)
@cli.command(help="Initialize a pyproject.toml for PDM.")
@cli.command()
@verbose_option
@click.option(
"-p",
"--python",
help="Specify the Python interperter version or path to use.",
metavar="PYTHON",
)
@pass_project
def init(project):
def init(project, python):
"""Initialize a pyproject.toml for PDM."""
if python:
actions.do_use(project, python)
if project.pyproject_file.exists():
context.io.echo(
"{}".format(
......@@ -304,14 +313,21 @@ def init(project):
git_user, git_email = get_user_email_from_git()
author = click.prompt(f"Author name", default=git_user)
email = click.prompt(f"Author email", default=git_email)
actions.do_init(project, name, version, license, author, email)
python_version = ".".join(
map(str, get_python_version(project.environment.python_executable)[:2])
)
python_requires = click.prompt(
"Python requires('*' to allow any)", default=f">={python_version}"
)
actions.do_init(project, name, version, license, author, email, python_requires)
@cli.command()
@click.argument("python")
@pass_project
def use(project, python):
"""Use the given python version as base interpreter."""
"""Use the given python version or path as base interpreter."""
actions.do_use(project, python)
......
import functools
import os
import shutil
import sys
from pathlib import Path
import pytest
......@@ -8,6 +9,7 @@ from click.testing import CliRunner
from pdm.cli import actions, commands
from pdm.models.requirements import parse_requirement
from pdm.utils import get_python_version
@pytest.fixture()
......@@ -139,3 +141,49 @@ def test_install_with_lockfile(project, invoke, working_set, repository):
assert "Lock file hash doesn't match" in result.output
assert "pytz" in project.get_locked_candidates()
assert project.is_lockfile_hash_match()
def test_init_command(project_no_init, invoke, mocker):
mocker.patch(
"pdm.cli.commands.get_user_email_from_git",
return_value=("Testing", "me@example.org"),
)
do_init = mocker.patch.object(actions, "do_init")
result = invoke(["init"], input="test-project\n\n\n\n\n\n", obj=project_no_init)
assert result.exit_code == 0
python_version = ".".join(
map(str, get_python_version(project_no_init.environment.python_executable)[:2])
)
do_init.assert_called_with(
project_no_init,
"test-project",
"0.0.0",
"MIT",
"Testing",
"me@example.org",
f">={python_version}",
)
def test_init_command_python_option(project_no_init, invoke, mocker):
mocker.patch(
"pdm.cli.commands.get_user_email_from_git",
return_value=("Testing", "me@example.org"),
)
do_init = mocker.patch.object(actions, "do_init")
result = invoke(
["init", "--python", sys.executable],
input="test-project\n\n\n\n\n\n",
obj=project_no_init,
)
assert result.exit_code == 0
python_version = ".".join(map(str, sys.version_info[:2]))
do_init.assert_called_with(
project_no_init,
"test-project",
"0.0.0",
"MIT",
"Testing",
"me@example.org",
f">={python_version}",
)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册