-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathbootstrap.py
More file actions
244 lines (209 loc) · 7.89 KB
/
Copy pathbootstrap.py
File metadata and controls
244 lines (209 loc) · 7.89 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/skeleton for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import itertools
import click
import utils_thirdparty
from utils_thirdparty import Environment
from utils_thirdparty import PypiPackage
@click.command()
@click.option(
"-r",
"--requirements-file",
type=click.Path(exists=True, readable=True, path_type=str, dir_okay=False),
metavar="FILE",
multiple=True,
default=["requirements.txt"],
show_default=True,
help="Path to the requirements file(s) to use for thirdparty packages.",
)
@click.option(
"-d",
"--thirdparty-dir",
type=click.Path(exists=True, readable=True, path_type=str, file_okay=False),
metavar="DIR",
default=utils_thirdparty.THIRDPARTY_DIR,
show_default=True,
help="Path to the thirdparty directory where wheels are built and "
"sources, ABOUT and LICENSE files fetched.",
)
@click.option(
"-p",
"--python-version",
type=click.Choice(utils_thirdparty.PYTHON_VERSIONS),
metavar="PYVER",
default=utils_thirdparty.PYTHON_VERSIONS,
show_default=True,
multiple=True,
help="Python version(s) to use for this build.",
)
@click.option(
"-o",
"--operating-system",
type=click.Choice(utils_thirdparty.PLATFORMS_BY_OS),
metavar="OS",
default=tuple(utils_thirdparty.PLATFORMS_BY_OS),
multiple=True,
show_default=True,
help="OS(ses) to use for this build: one of linux, mac or windows.",
)
@click.option(
"-l",
"--latest-version",
is_flag=True,
help="Get the latest version of all packages, ignoring version specifiers.",
)
@click.option(
"--sync-dejacode",
is_flag=True,
help="Synchronize packages with DejaCode.",
)
@click.option(
"--with-deps",
is_flag=True,
help="Also include all dependent wheels.",
)
@click.help_option("-h", "--help")
def bootstrap(
requirements_file,
thirdparty_dir,
python_version,
operating_system,
with_deps,
latest_version,
sync_dejacode,
build_remotely=False,
):
"""
Bootstrap a thirdparty Python packages directory from pip requirements.
Fetch or build to THIRDPARTY_DIR all the wheels and source distributions for
the pip ``--requirement-file`` requirements FILE(s). Build wheels compatible
with all the provided ``--python-version`` PYVER(s) and ```--operating_system``
OS(s) defaulting to all supported combinations. Create or fetch .ABOUT and
.LICENSE files.
Optionally ignore version specifiers and use the ``--latest-version``
of everything.
Sources and wheels are fetched with attempts first from PyPI, then our remote repository.
If missing wheels are built as needed.
"""
# rename variables for clarity since these are lists
requirements_files = requirements_file
python_versions = python_version
operating_systems = operating_system
# create the environments we need
evts = itertools.product(python_versions, operating_systems)
environments = [Environment.from_pyver_and_os(pyv, os) for pyv, os in evts]
# collect all packages to process from requirements files
# this will fail with an exception if there are packages we cannot find
required_name_versions = set()
for req_file in requirements_files:
nvs = utils_thirdparty.load_requirements(requirements_file=req_file, force_pinned=False)
required_name_versions.update(nvs)
if latest_version:
required_name_versions = set((name, None) for name, _ver in required_name_versions)
print(
f"PROCESSING {len(required_name_versions)} REQUIREMENTS in {len(requirements_files)} FILES"
)
# fetch all available wheels, keep track of missing
# start with local, then remote, then PyPI
print("==> COLLECTING ALREADY LOCALLY AVAILABLE REQUIRED WHEELS")
# list of all the wheel filenames either pre-existing, fetched or built
# updated as we progress
available_wheel_filenames = []
local_packages_by_namever = {
(p.name, p.version): p
for p in utils_thirdparty.get_local_packages(directory=thirdparty_dir)
}
# list of (name, version, environment) not local and to fetch
name_version_envt_to_fetch = []
# start with a local check
for (name, version), envt in itertools.product(required_name_versions, environments):
local_pack = local_packages_by_namever.get(
(
name,
version,
)
)
if local_pack:
supported_wheels = list(local_pack.get_supported_wheels(environment=envt))
if supported_wheels:
available_wheel_filenames.extend(w.filename for w in supported_wheels)
print(
f"====> No fetch or build needed. "
f"Local wheel already available for {name}=={version} "
f"on os: {envt.operating_system} for Python: {envt.python_version}"
)
continue
name_version_envt_to_fetch.append(
(
name,
version,
envt,
)
)
print(f"==> TRYING TO FETCH #{len(name_version_envt_to_fetch)} REQUIRED WHEELS")
# list of (name, version, environment) not fetch and to build
name_version_envt_to_build = []
# then check if the wheel can be fetched without building from remote and Pypi
for name, version, envt in name_version_envt_to_fetch:
fetched_fwn = utils_thirdparty.fetch_package_wheel(
name=name,
version=version,
environment=envt,
dest_dir=thirdparty_dir,
)
if fetched_fwn:
available_wheel_filenames.append(fetched_fwn)
else:
name_version_envt_to_build.append(
(
name,
version,
envt,
)
)
# At this stage we have all the wheels we could obtain without building
for name, version, envt in name_version_envt_to_build:
print(
f"====> Need to build wheels for {name}=={version} on os: "
f"{envt.operating_system} for Python: {envt.python_version}"
)
packages_and_envts_to_build = [
(PypiPackage(name, version), envt) for name, version, envt in name_version_envt_to_build
]
print(f"==> BUILDING #{len(packages_and_envts_to_build)} MISSING WHEELS")
package_envts_not_built, wheel_filenames_built = utils_thirdparty.build_missing_wheels(
packages_and_envts=packages_and_envts_to_build,
build_remotely=build_remotely,
with_deps=with_deps,
dest_dir=thirdparty_dir,
)
if wheel_filenames_built:
available_wheel_filenames.extend(available_wheel_filenames)
for pack, envt in package_envts_not_built:
print(
f"====> FAILED to build any wheel for {pack.name}=={pack.version} "
f"on os: {envt.operating_system} for Python: {envt.python_version}"
)
print(f"==> FETCHING SOURCE DISTRIBUTIONS")
# fetch all sources, keep track of missing
# This is a list of (name, version)
utils_thirdparty.fetch_missing_sources(dest_dir=thirdparty_dir)
print(f"==> FETCHING ABOUT AND LICENSE FILES")
utils_thirdparty.add_fetch_or_update_about_and_license_files(dest_dir=thirdparty_dir)
############################################################################
if sync_dejacode:
print(f"==> SYNC WITH DEJACODE")
# try to fetch from DejaCode any missing ABOUT
# create all missing DejaCode packages
pass
utils_thirdparty.find_problems(dest_dir=thirdparty_dir)
if __name__ == "__main__":
bootstrap()