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

Supporting running scripts directly

上级 f73c6432
Supporting running scripts with the nearest `__pypackages__` loaded.
......@@ -11,6 +11,7 @@ from pdm.cli.commands.base import BaseCommand
from pdm.exceptions import PdmUsageError
from pdm.iostream import stream
from pdm.project import Project
from pdm.utils import find_project_root
class Command(BaseCommand):
......@@ -40,27 +41,30 @@ class Command(BaseCommand):
shell=False,
env=None,
) -> None:
if env:
os.environ.update(env)
if shell:
sys.exit(subprocess.call(os.path.expandvars(args), shell=True))
with project.environment.activate():
if env:
os.environ.update(env)
if shell:
sys.exit(subprocess.call(os.path.expandvars(args), shell=True))
command, *args = args
expanded_command = project.environment.which(command)
if not expanded_command:
raise PdmUsageError(
"Command {} is not found on your PATH.".format(
stream.green(f"'{command}'")
command, *args = args
expanded_command = project.environment.which(command)
if not expanded_command:
raise PdmUsageError(
"Command {} is not found on your PATH.".format(
stream.green(f"'{command}'")
)
)
)
expanded_command = os.path.expanduser(os.path.expandvars(expanded_command))
expanded_args = [os.path.expandvars(arg) for arg in [expanded_command] + args]
if os.name == "nt" or "CI" in os.environ:
# In order to make sure pytest is playing well,
# don't hand over the process under a testing environment.
sys.exit(subprocess.call(expanded_args))
else:
os.execv(expanded_command, expanded_args)
expanded_command = os.path.expanduser(os.path.expandvars(expanded_command))
expanded_args = [
os.path.expandvars(arg) for arg in [expanded_command] + args
]
if os.name == "nt" or "CI" in os.environ:
# In order to make sure pytest is playing well,
# don't hand over the process under a testing environment.
sys.exit(subprocess.call(expanded_args))
else:
os.execv(expanded_command, expanded_args)
def _normalize_script(self, script):
if not getattr(script, "items", None):
......@@ -125,8 +129,14 @@ class Command(BaseCommand):
def handle(self, project: Project, options: argparse.Namespace) -> None:
if options.list:
return self._show_list(project)
with project.environment.activate():
if project.scripts and options.command in project.scripts:
self._run_script(project, options.command, options.args)
else:
self._run_command(project, [options.command] + options.args)
if project.scripts and options.command in project.scripts:
self._run_script(project, options.command, options.args)
elif os.path.isfile(options.command) and options.command.endswith(".py"):
# Allow executing py scripts like `pdm run my_script.py`.
# In this case, the nearest `__pypackages__` will be loaded as
# the library source.
new_root = find_project_root(os.path.abspath(options.command))
project = Project(new_root) if new_root else project
self._run_command(project, ["python", options.command] + options.args)
else:
self._run_command(project, [options.command] + options.args)
......@@ -169,3 +169,4 @@ colorama = []
filterwarnings = [
"ignore::DeprecationWarning"
]
markers = ["pypi: Tests that connect to the real PyPI"]
......@@ -239,12 +239,14 @@ def test_import_other_format_file(project, invoke, filename):
assert result.exit_code == 0
@pytest.mark.pypi
def test_search_package(project, invoke):
result = invoke(["search", "requests"], obj=project)
assert result.exit_code == 0
assert len(result.output.splitlines()) > 0
@pytest.mark.pypi
def test_show_package_on_pypi(invoke):
result = invoke(["show", "ipython"])
assert result.exit_code == 0
......
......@@ -2,6 +2,8 @@ import json
import os
import textwrap
import pytest
from pdm.utils import cd, temp_environ
......@@ -134,7 +136,6 @@ def test_run_show_list_of_scripts(project, invoke):
}
project.write_pyproject()
result = invoke(["run", "--list"], obj=project)
print(result.output, result.stderr)
result_lines = result.output.splitlines()[2:]
assert result_lines[0].strip() == "test_cmd cmd flask db upgrade"
assert (
......@@ -142,3 +143,21 @@ def test_run_show_list_of_scripts(project, invoke):
== "test_script call test_script:main call a python function"
)
assert result_lines[2].strip() == "test_shell shell echo $FOO shell command"
@pytest.mark.pypi
def test_run_script_with_pep582(project, invoke, capfd):
project.tool_settings["python_requires"] = ">=3.7"
project.write_pyproject()
(project.root / "test_script.py").write_text(
"import requests\nprint(requests.__version__)\n"
)
result = invoke(["add", "requests==2.24.0"], obj=project)
assert result.exit_code == 0
capfd.readouterr()
with cd(os.path.expanduser("~")):
result = invoke(["run", str(project.root / "test_script.py")], obj=project)
assert result.exit_code == 0
out, _ = capfd.readouterr()
assert out.strip() == "2.24.0"
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册