MCPcopy
hub / github.com/pex-tool/pex / is_python_script

Function is_python_script

pex/executables.py:142–177  ·  view source on GitHub ↗
(
    path,  # type: Text
    check_executable=True,  # type: bool
)

Source from the content-addressed store, hash-verified

140
141
142def is_python_script(
143 path, # type: Text
144 check_executable=True, # type: bool
145):
146 # type: (...) -> bool
147
148 path = os.path.realpath(path)
149 if is_script(
150 path,
151 pattern=_PYTHON_SHEBANG_RE,
152 check_executable=check_executable,
153 extra_check=lambda shebang, fp: shebang == b"/bin/sh" and fp.read(13) == b"'''': pshprs\n",
154 ):
155 return True
156
157 if WINDOWS:
158 # Check for the style of console scripts we create.
159 from pex import windows
160
161 if windows.is_script(path):
162 return True
163
164 # Check for the style of console scripts Pip creates.
165 if not zipfile.is_zipfile(path):
166 return False
167
168 from pex.ziputils import Zip
169
170 zip_script = Zip.load(path)
171 with open(os.devnull, "wb") as fp:
172 shebang = zip_script.isolate_header(fp, stop_at=_SHEBANG_MAGIC)
173 if shebang:
174 if bool(re.match(_PYTHON_SHEBANG_RE, shebang[len(_SHEBANG_MAGIC) :].strip())):
175 return True
176
177 return False

Callers 8

python_scriptMethod · 0.90
install_wheelFunction · 0.90
_inspectMethod · 0.90
test_is_python_scriptFunction · 0.90
check_is_python_scriptFunction · 0.90
_safe_argsFunction · 0.90

Calls 5

isolate_headerMethod · 0.80
stripMethod · 0.80
is_scriptFunction · 0.70
readMethod · 0.45
loadMethod · 0.45