File: utils.py

package info (click to toggle)
python-hatch-mypyc 0.16.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 232 kB
  • sloc: python: 1,025; makefile: 5
file content (60 lines) | stat: -rw-r--r-- 1,715 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# SPDX-FileCopyrightText: 2021-present Ofek Lev <oss@ofek.dev>
#
# SPDX-License-Identifier: MIT
from __future__ import annotations

import os


def construct_setup_file(package_source, *args, **kwargs):
    # Ensure that setuptools is imported first
    contents = """\
from setuptools import setup
from mypyc.build import mypycify

setup(
    name='mypyc_output',
    ext_modules=mypycify(
        [{}
        ],{}
    ),{}
)
"""

    package_dir_data = ''
    if package_source:
        package_source = package_source.replace(os.path.sep, '/')
        package_dir_data = f"\n    package_dir={{'': '{package_source}'}},"

    return contents.format(
        ''.join(f'\n            {arg!r},' for arg in args),
        ''.join(f'\n        {key}={value!r},' for key, value in kwargs.items()),
        package_dir_data,
    )


def installed_in_prefix() -> bool:  # no cov
    # pip always sets this
    python_path = os.environ.get('PYTHONPATH', '')
    if not python_path:
        return False

    paths = python_path.split(os.pathsep)

    # pip only sets one location, see:
    # https://github.com/pypa/pip/blob/21.3.1/src/pip/_internal/build_env.py#L137
    if len(paths) > 1:
        return False

    temp_build_dir = os.path.dirname(paths[0])

    # https://github.com/pypa/pip/blob/21.3.1/src/pip/_internal/build_env.py#L74
    # https://github.com/pypa/pip/blob/21.3.1/src/pip/_internal/utils/temp_dir.py#L164
    if not os.path.basename(temp_build_dir).startswith('pip-build-env-'):
        return False

    wheel_requirements_install_path = os.path.join(temp_build_dir, 'normal', 'lib', 'site-packages')
    if not os.path.isdir(wheel_requirements_install_path):
        return False

    return True