| 1 |
# SPDX-FileCopyrightText: 2021-present Ofek Lev <[email protected]> |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: MIT |
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import os |
| 7 |
|
| 8 |
|
| 9 |
def construct_setup_file(package_source, *args, **kwargs): |
| 10 |
# Ensure that setuptools is imported first |
| 11 |
contents = """\ |
| 12 |
from setuptools import setup |
| 13 |
from mypyc.build import mypycify |
| 14 |
|
| 15 |
setup( |
| 16 |
name='mypyc_output', |
| 17 |
ext_modules=mypycify( |
| 18 |
[{} |
| 19 |
],{} |
| 20 |
),{} |
| 21 |
) |
| 22 |
""" |
| 23 |
|
| 24 |
package_dir_data = '' |
| 25 |
if package_source: |
| 26 |
package_source = package_source.replace(os.path.sep, '/') |
| 27 |
package_dir_data = f"\n package_dir={{'': '{package_source}'}}," |
| 28 |
|
| 29 |
return contents.format( |
| 30 |
''.join(f'\n {arg!r},' for arg in args), |
| 31 |
''.join(f'\n {key}={value!r},' for key, value in kwargs.items()), |
| 32 |
package_dir_data, |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
def installed_in_prefix() -> bool: # no cov |
| 37 |
# pip always sets this |
| 38 |
python_path = os.environ.get('PYTHONPATH', '') |
| 39 |
if not python_path: |
| 40 |
return False |
| 41 |
|
| 42 |
paths = python_path.split(os.pathsep) |
| 43 |
|
| 44 |
# pip only sets one location, see: |
| 45 |
# https://github.com/pypa/pip/blob/21.3.1/src/pip/_internal/build_env.py#L137 |
| 46 |
if len(paths) > 1: |
| 47 |
return False |
| 48 |
|
| 49 |
temp_build_dir = os.path.dirname(paths[0]) |
| 50 |
|
| 51 |
# https://github.com/pypa/pip/blob/21.3.1/src/pip/_internal/build_env.py#L74 |
| 52 |
# https://github.com/pypa/pip/blob/21.3.1/src/pip/_internal/utils/temp_dir.py#L164 |
| 53 |
if not os.path.basename(temp_build_dir).startswith('pip-build-env-'): |
| 54 |
return False |
| 55 |
|
| 56 |
wheel_requirements_install_path = os.path.join(temp_build_dir, 'normal', 'lib', 'site-packages') |
| 57 |
if not os.path.isdir(wheel_requirements_install_path): |
| 58 |
return False |
| 59 |
|
| 60 |
return True |