8
8
import urllib .request
9
9
from pathlib import Path
10
10
from shutil import which
11
- from typing import List , NoReturn , Optional
11
+ from typing import List , NoReturn , Optional , Union
12
12
13
13
from packaging .requirements import InvalidRequirement , Requirement
14
14
42
42
{app_lines}"""
43
43
44
44
45
- def maybe_script_content (app : str , is_path : bool ) -> Optional [str ]:
45
+ def maybe_script_content (app : str , is_path : bool ) -> Optional [Union [ str , Path ] ]:
46
46
# If the app is a script, return its content.
47
47
# Return None if it should be treated as a package name.
48
48
49
49
# Look for a local file first.
50
50
app_path = Path (app )
51
51
if app_path .is_file ():
52
- return app_path . read_text ( encoding = "utf-8" )
52
+ return app_path
53
53
elif is_path :
54
54
raise PipxError (f"The specified path { app } does not exist" )
55
55
@@ -71,7 +71,7 @@ def maybe_script_content(app: str, is_path: bool) -> Optional[str]:
71
71
72
72
73
73
def run_script (
74
- content : str ,
74
+ content : Union [ str , Path ] ,
75
75
app_args : List [str ],
76
76
python : str ,
77
77
pip_args : List [str ],
@@ -81,7 +81,7 @@ def run_script(
81
81
) -> NoReturn :
82
82
requirements = _get_requirements_from_script (content )
83
83
if requirements is None :
84
- exec_app ([ python , "-c" , content , * app_args ] )
84
+ python_path = Path ( python )
85
85
else :
86
86
# Note that the environment name is based on the identified
87
87
# requirements, and *not* on the script name. This is deliberate, as
@@ -99,7 +99,12 @@ def run_script(
99
99
venv = Venv (venv_dir , python = python , verbose = verbose )
100
100
venv .create_venv (venv_args , pip_args )
101
101
venv .install_unmanaged_packages (requirements , pip_args )
102
- exec_app ([venv .python_path , "-c" , content , * app_args ])
102
+ python_path = venv .python_path
103
+
104
+ if isinstance (content , Path ):
105
+ exec_app ([python_path , content , * app_args ])
106
+ else :
107
+ exec_app ([python_path , "-c" , content , * app_args ])
103
108
104
109
105
110
def run_package (
@@ -323,11 +328,14 @@ def _http_get_request(url: str) -> str:
323
328
INLINE_SCRIPT_METADATA = re .compile (r"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\s(?P<content>(^#(| .*)$\s)+)^# ///$" )
324
329
325
330
326
- def _get_requirements_from_script (content : str ) -> Optional [List [str ]]:
331
+ def _get_requirements_from_script (content : Union [ str , Path ] ) -> Optional [List [str ]]:
327
332
"""
328
333
Supports inline script metadata.
329
334
"""
330
335
336
+ if isinstance (content , Path ):
337
+ content = content .read_text (encoding = "utf-8" )
338
+
331
339
name = "script"
332
340
333
341
# Windows is currently getting un-normalized line endings, so normalize
0 commit comments