-
Notifications
You must be signed in to change notification settings - Fork 3
/
__init__.py
127 lines (105 loc) · 3.03 KB
/
__init__.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# SPDX-License-Identifier: MIT
import atexit
import logging
import os
import pathlib
import shutil
import subprocess
import sys
import tempfile
from collections.abc import Collection, Mapping, Sequence
from typing import NamedTuple, Optional, Tuple
class Package(NamedTuple):
srcdir: pathlib.Path
module_path: pathlib.Path
module_sources: Collection[str]
ROOT = pathlib.Path(__file__).parent.parent
WORKING_DIR = ROOT / '.bootstrap'
MODULES = WORKING_DIR / 'modules'
TMPDIR = WORKING_DIR / 'tmp'
EXTERNAL = ROOT / 'external'
PACKAGES = {
# what we need
'build': Package(
EXTERNAL / 'build',
EXTERNAL / 'build' / 'src',
{'build'},
),
'installer': Package(
EXTERNAL / 'installer',
EXTERNAL / 'installer' / 'src',
{'installer'},
),
# dependencies
'setuptools': Package(
EXTERNAL / 'setuptools',
EXTERNAL / 'setuptools',
{'setuptools', 'pkg_resources', '_distutils_hack'},
),
'flit_core': Package(
EXTERNAL / 'flit' / 'flit_core',
EXTERNAL / 'flit' / 'flit_core',
{'flit_core'},
),
'wheel': Package(
EXTERNAL / 'wheel',
EXTERNAL / 'wheel' / 'src',
{'wheel'},
),
'tomli': Package(
EXTERNAL / 'tomli',
EXTERNAL / 'tomli',
{'tomli'},
),
'pep517': Package(
EXTERNAL / 'pep517',
EXTERNAL / 'pep517',
{'pep517'},
),
}
EXTRA_PATH = [str(package.module_path) for package in PACKAGES.values()]
PACKAGE_PATH_ENV = {
'PYTHONPATH': os.path.pathsep.join(EXTRA_PATH),
}
# copy sources to module dir and inject it into sys.path
MODULES.mkdir(parents=True)
for package in PACKAGES.values():
for path in package.module_sources:
shutil.copytree(
package.module_path / path,
MODULES / path,
)
atexit.register(shutil.rmtree, MODULES)
sys.path.insert(0, str(MODULES))
# import what we need from the inject modules
import build # noqa: E402
import build.env # noqa: E402
import pep517 # noqa: E402
_logger = logging.getLogger(__name__)
def log(msg):
_logger.info(msg)
def custom_runner(
cmd: Sequence[str],
cwd: Optional[str] = None,
extra_environ: Optional[Mapping[str, str]] = None,
) -> None:
extra_environ = dict(extra_environ) if extra_environ else {}
extra_environ.update(PACKAGE_PATH_ENV)
pep517.default_subprocess_runner(cmd, cwd, extra_environ)
def install_package_egginfo(name: str) -> None:
package = PACKAGES[name]
subprocess.check_call(
[sys.executable, 'setup.py', 'egg_info'],
env=os.environ | PACKAGE_PATH_ENV,
cwd=package.srcdir,
)
shutil.copytree(
package.module_path / f'{name}.egg-info',
MODULES / f'{name}.egg-info',
)
def build_package(name: str, outdir: pathlib.Path) -> pathlib.Path:
log(f'Building {name}...')
srcdir = PACKAGES[name].srcdir
builder = build.ProjectBuilder(str(srcdir), runner=custom_runner)
wheel = builder.build('wheel', str(outdir))
return pathlib.Path(wheel)