-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.py
170 lines (150 loc) · 5.11 KB
/
bootstrap.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
:Version: 1.1
"""
import argparse
import logging
import os
import platform
import shutil
import subprocess
import sys
import venv
from pathlib import Path
from tempfile import TemporaryDirectory
from urllib.request import urlopen
BUILDOUT_VERSION = '3.0rc2'
PIP_VERSION = '22.0.4'
SETUPTOOLS_VERSION = '59.6.0'
WHEEL_VERSION = '0.37.1'
GET_PIP_URL = 'https://bootstrap.pypa.io/get-pip.py'
def main():
parser = argparse.ArgumentParser(description='Bootstrap zc.buildout')
parser.add_argument(
'-r, --recreate', dest='recreate', action='store_true',
help='Recreate virtual python if it exists.',
)
parser.add_argument(
'--buildout_version', default=BUILDOUT_VERSION,
help='Version of zc.buildout (default: %(default)).',
)
parser.add_argument(
'--pip_version', default=PIP_VERSION,
help='Version of pip (default: %(default)).',
)
parser.add_argument(
'--setuptools_version', default=SETUPTOOLS_VERSION,
help='Version of setuptools (default: %(default)).',
)
parser.add_argument(
'--wheel_version', default=WHEEL_VERSION,
help='Version of wheel (default: %(default)).',
)
args = parser.parse_args(sys.argv[1:])
logging.basicConfig(
stream=sys.stdout,
level=logging.DEBUG,
format='%(levelname)s: %(message)s'
)
logger = logging.getLogger('buildout.bootstrap')
project_dir = Path(os.getcwd()).absolute()
venv_dir = project_dir / '.venv'
if args.recreate and venv_dir.is_dir():
logger.info('Delete exists virtual python')
shutil.rmtree(venv_dir)
is_win = platform.system() == 'Windows'
if is_win:
venv_python_path = venv_dir / 'Scripts' / 'python.exe'
else:
venv_python_path = venv_dir / 'bin' / 'python'
if venv_python_path.is_file():
logger.debug('Checking version of exists virtual python...')
proc = subprocess.Popen(
[
str(venv_python_path),
'-c',
'import sys;print(sys.version)',
],
stdout=subprocess.PIPE
)
stdout, stderr = proc.communicate()
env_version = stdout.strip().decode()
if env_version != sys.version:
logger.info(
'Removing exists virtual python environment '
'due to incorrect version of Python in it...'
)
shutil.rmtree(venv_dir)
if not venv_python_path.is_file():
logger.info(f'Installing virtual python environment in directory {venv_dir}')
venv.create(
venv_dir,
symlinks=not is_win,
system_site_packages=False,
with_pip=False,
)
logger.info('Virtual python environment has installed.')
logger.info('Installing pip...')
with TemporaryDirectory() as tmp_dir:
get_pip_path = Path(tmp_dir) / 'get_pip.py'
with urlopen(GET_PIP_URL) as get_pip:
content: bytes = get_pip.read()
get_pip_path.write_bytes(content)
cmd = [
str(venv_python_path),
str(get_pip_path),
'--no-setuptools',
'--no-wheel',
]
if args.pip_version:
cmd.append(f'pip=={args.pip_version}')
_run_cmd(cmd)
logger.info(f'Installing setuptools=={args.setuptools_version} and wheel=={args.wheel_version}')
_run_cmd([
str(venv_python_path),
'-m', 'pip',
'install',
f'setuptools=={args.setuptools_version}',
f'wheel=={args.wheel_version}',
])
logger.info(f'Installing zc.buildout == {args.buildout_version}')
_run_cmd([
str(venv_python_path),
'-m', 'pip',
'install',
f'zc.buildout=={args.buildout_version}'
])
logger.info(f'Bootstrap zc.buildout')
is_win = platform.system() == 'Windows'
if is_win:
buildout_path = venv_dir / 'Scripts' / 'buildout.exe'
else:
buildout_path = venv_dir / 'bin' / 'buildout'
_run_cmd([
str(buildout_path),
'bootstrap',
])
# Delete zc.buildout.egg-link from list of develop eggs
buildout_link = project_dir / 'develop-eggs' / 'zc.buildout.egg-link'
if buildout_link.is_file():
os.remove(buildout_link)
# Fix buildout script
buildout_script_path = project_dir / 'bin' / 'buildout'
buildout_script = buildout_script_path.read_text()
buildout_script = buildout_script.replace(
'import zc.buildout.buildout',
'try:\n'
' from _distutils_hack import DistutilsMetaFinder\n'
' DistutilsMetaFinder.pip_imported_during_build = lambda cls: True\n'
'except ImportError:\n'
' pass\n\n'
'import zc.buildout.buildout\n'
'import warnings\n'
'from pkg_resources import PkgResourcesDeprecationWarning\n\n'
'warnings.filterwarnings("ignore", category=PkgResourcesDeprecationWarning)'
)
buildout_script_path.write_text(buildout_script)
def _run_cmd(cmd):
if subprocess.call(cmd) != 0:
raise Exception(f'Failed to execute command:\n {" ".join(cmd)}')
if __name__ == '__main__':
main()