| 140 | |
| 141 | |
| 142 | def 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 |