forked from scikit-learn/scikit-learn
/
update_environments_and_lock_files.py
655 lines (580 loc) · 22.6 KB
/
update_environments_and_lock_files.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
"""Script to update CI environment files and associated lock files.
To run it you need to be in the root folder of the scikit-learn repo:
python build_tools/update_environments_and_lock_files.py
Two scenarios where this script can be useful:
- make sure that the latest versions of all the dependencies are used in the CI.
We can run this script regularly and open a PR with the changes to the lock
files. This workflow will eventually be automated with a bot in the future.
- bump minimum dependencies in sklearn/_min_dependencies.py. Running this
script will update both the CI environment files and associated lock files.
You can then open a PR with the changes.
- pin some packages to an older version by adding them to the
default_package_constraints variable. This is useful when regressions are
introduced in our dependencies, this has happened for example with pytest 7
and coverage 6.3.
Environments are conda environment.yml or pip requirements.txt. Lock files are
conda-lock lock files or pip-compile requirements.txt.
pip requirements.txt are used when we install some dependencies (e.g. numpy and
scipy) with apt-get and the rest of the dependencies (e.g. pytest and joblib)
with pip.
To run this script you need:
- conda-lock. The version should match the one used in the CI in
sklearn/_min_dependencies.py
- pip-tools
"""
import re
import subprocess
import sys
from pathlib import Path
import shlex
import json
import logging
from importlib.metadata import version
import click
from conda_lock import __version__ as conda_lock_version
from jinja2 import Environment
from packaging.version import Version
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
logger.addHandler(handler)
common_dependencies_without_coverage = [
"python",
"numpy",
"blas",
"scipy",
"cython",
"joblib",
"threadpoolctl",
"matplotlib",
"pandas",
"pyamg",
"pytest",
"pytest-xdist",
"pillow",
]
common_dependencies = common_dependencies_without_coverage + [
"pytest-cov",
"coverage",
]
docstring_test_dependencies = ["sphinx", "numpydoc"]
default_package_constraints = {
# XXX: pin pytest-xdist to workaround:
# https://github.com/pytest-dev/pytest-xdist/issues/840
"pytest-xdist": "2.5.0",
}
def remove_from(alist, to_remove):
return [each for each in alist if each not in to_remove]
conda_build_metadata_list = [
{
"build_name": "pylatest_conda_forge_mkl_linux-64",
"folder": "build_tools/azure",
"platform": "linux-64",
"channel": "conda-forge",
"conda_dependencies": common_dependencies + [
"ccache",
"pytorch",
"pytorch-cpu",
"array-api-compat",
],
"package_constraints": {
"blas": "[build=mkl]",
"pytorch": "1.13",
},
},
{
"build_name": "pylatest_conda_forge_mkl_osx-64",
"folder": "build_tools/azure",
"platform": "osx-64",
"channel": "conda-forge",
"conda_dependencies": common_dependencies + [
"ccache",
"compilers",
"llvm-openmp",
],
"package_constraints": {
"blas": "[build=mkl]",
},
},
{
"build_name": "pylatest_conda_mkl_no_openmp",
"folder": "build_tools/azure",
"platform": "osx-64",
"channel": "defaults",
# TODO work-around to get cython>=0.29.33 via PyPi until it is in conda defaults
# See: https://github.com/ContinuumIO/anaconda-issues/issues/13120
"conda_dependencies": remove_from(common_dependencies, ["cython"]) + ["ccache"],
# TODO work-around to get cython>=0.29.33 via PyPi until it is in conda defaults
# See: https://github.com/ContinuumIO/anaconda-issues/issues/13120
"pip_dependencies": ["cython"],
"package_constraints": {
"blas": "[build=mkl]",
# 2022-06-09 currently mamba install 1.23 and scipy 1.7 which
# should be compatible but actually are not. This pin can be
# removed when scipy 1.8 is available in conda defaults channel.
# For more details, see
# https://github.com/scikit-learn/scikit-learn/pull/24363#issuecomment-1236927660
# and https://github.com/scipy/scipy/issues/16964
"numpy": "1.22",
# XXX: coverage is temporary pinned to 6.2 because 6.3 is not
# fork-safe and 6.4 is not available yet (July 2022) in conda
# defaults channel. For more details, see:
# https://github.com/nedbat/coveragepy/issues/1310
"coverage": "6.2",
},
},
{
"build_name": "pylatest_conda_forge_mkl_no_coverage",
"folder": "build_tools/azure",
"platform": "linux-64",
"channel": "conda-forge",
"conda_dependencies": common_dependencies_without_coverage + ["ccache"],
"package_constraints": {
"blas": "[build=mkl]",
},
},
{
"build_name": "py38_conda_defaults_openblas",
"folder": "build_tools/azure",
"platform": "linux-64",
"channel": "defaults",
# TODO work-around to get cython>=0.29.33 via PyPi until it is in conda defaults
# See: https://github.com/ContinuumIO/anaconda-issues/issues/13120
"conda_dependencies": remove_from(common_dependencies, ["cython"]) + ["ccache"],
# TODO work-around to get cython>=0.29.33 via PyPi until it is in conda defaults
# See: https://github.com/ContinuumIO/anaconda-issues/issues/13120
"pip_dependencies": ["cython"],
"package_constraints": {
"python": "3.8",
"blas": "[build=openblas]",
"numpy": "min",
"scipy": "min",
"matplotlib": "min",
"threadpoolctl": "2.2.0",
# XXX: coverage is temporary pinned to 6.2 because 6.3 is not
# fork-safe and 6.4 is not available yet (July 2022) in conda
# defaults channel. For more details, see:
# https://github.com/nedbat/coveragepy/issues/1310
"coverage": "6.2",
},
},
{
"build_name": "py38_conda_forge_openblas_ubuntu_2204",
"folder": "build_tools/azure",
"platform": "linux-64",
"channel": "conda-forge",
"conda_dependencies": common_dependencies_without_coverage + ["ccache"],
"package_constraints": {"python": "3.8", "blas": "[build=openblas]"},
},
{
"build_name": "pylatest_pip_openblas_pandas",
"folder": "build_tools/azure",
"platform": "linux-64",
"channel": "defaults",
# sphinx in conda_dependencies as a temporary work-around for
# https://github.com/conda-incubator/conda-lock/issues/309
"conda_dependencies": ["python", "ccache", "sphinx"],
"pip_dependencies": (
remove_from(common_dependencies, ["python", "blas"])
+ remove_from(docstring_test_dependencies, ["sphinx"])
+ ["lightgbm", "scikit-image"]
),
"package_constraints": {
"python": "3.9",
# TODO: remove the following pin when we have a solution since
# pinning a pandas version partially defeats the purpose of the
# `pylastest` CI configurations which are meant to test
# scikit-learn against the most recently released versions of
# its dependencies.
# pandas 2.0 depends on tzdata as well as python installed via conda
# We currently have a mix of conda/pip and run into the following issue:
# https://github.com/conda/conda-lock/issues/179
"pandas": "1.5.3",
},
},
{
"build_name": "pylatest_pip_scipy_dev",
"folder": "build_tools/azure",
"platform": "linux-64",
"channel": "defaults",
# sphinx in conda_dependencies as a temporary work-around for
# https://github.com/conda-incubator/conda-lock/issues/309
"conda_dependencies": ["python", "ccache", "sphinx"],
"pip_dependencies": (
remove_from(
common_dependencies,
[
"python",
"blas",
"matplotlib",
"pyamg",
# all the dependencies below have a development version
# installed in the CI, so they can be removed from the
# environment.yml
"numpy",
"scipy",
"pandas",
"cython",
"joblib",
"pillow",
],
)
+ ["pooch"]
+ remove_from(docstring_test_dependencies, ["sphinx"])
# python-dateutil is a dependency of pandas and pandas is removed from
# the environment.yml. Adding python-dateutil so it is pinned
+ ["python-dateutil"]
),
},
{
"build_name": "pypy3",
"folder": "build_tools/azure",
"platform": "linux-64",
"channel": "conda-forge",
"conda_dependencies": (
["pypy", "python"]
+ remove_from(
common_dependencies_without_coverage, ["python", "pandas", "pillow"]
)
+ ["ccache"]
),
"package_constraints": {
"blas": "[build=openblas]",
"python": "3.9",
},
},
{
"build_name": "py38_conda_forge_mkl",
"folder": "build_tools/azure",
"platform": "win-64",
"channel": "conda-forge",
"conda_dependencies": remove_from(common_dependencies, ["pandas", "pyamg"]) + [
"wheel",
"pip",
],
"package_constraints": {
"python": "3.8",
"blas": "[build=mkl]",
},
},
{
"build_name": "doc_min_dependencies",
"folder": "build_tools/circle",
"platform": "linux-64",
"channel": "conda-forge",
"conda_dependencies": common_dependencies_without_coverage + [
"scikit-image",
"seaborn",
"memory_profiler",
"compilers",
"sphinx",
"sphinx-gallery",
"numpydoc",
"sphinx-prompt",
"plotly",
"pooch",
],
"pip_dependencies": ["sphinxext-opengraph"],
"package_constraints": {
"python": "3.8",
"numpy": "min",
"scipy": "min",
"matplotlib": "min",
"cython": "min",
"scikit-image": "min",
"sphinx": "min",
"pandas": "min",
"sphinx-gallery": "min",
"numpydoc": "min",
"sphinx-prompt": "min",
"sphinxext-opengraph": "min",
"plotly": "min",
},
},
{
"build_name": "doc",
"folder": "build_tools/circle",
"platform": "linux-64",
"channel": "conda-forge",
"conda_dependencies": common_dependencies_without_coverage + [
"scikit-image",
"seaborn",
"memory_profiler",
"compilers",
"sphinx",
"sphinx-gallery",
"numpydoc",
"sphinx-prompt",
"plotly",
"pooch",
"sphinxext-opengraph",
],
"pip_dependencies": ["jupyterlite-sphinx", "jupyterlite-pyodide-kernel"],
"package_constraints": {
"python": "3.9",
# XXX: sphinx > 6.0 does not correctly generate searchindex.js
"sphinx": "6.0.0",
},
},
{
"build_name": "py39_conda_forge",
"folder": "build_tools/cirrus",
"platform": "linux-aarch64",
"channel": "conda-forge",
"conda_dependencies": remove_from(
common_dependencies_without_coverage, ["pandas", "pyamg"]
) + ["pip", "ccache"],
"package_constraints": {
"python": "3.9",
},
},
]
pip_build_metadata_list = [
{
"build_name": "debian_atlas_32bit",
"folder": "build_tools/azure",
"pip_dependencies": [
"cython",
"joblib",
"threadpoolctl",
"pytest",
"pytest-cov",
],
"package_constraints": {
"joblib": "min",
"threadpoolctl": "2.2.0",
"pytest": "min",
"pytest-cov": "min",
# no pytest-xdist because it causes issue on 32bit
},
# same Python version as in debian-32 build
"python_version": "3.9.2",
},
{
"build_name": "ubuntu_atlas",
"folder": "build_tools/azure",
"pip_dependencies": [
"cython",
"joblib",
"threadpoolctl",
"pytest",
"pytest-xdist",
],
"package_constraints": {"joblib": "min", "threadpoolctl": "min"},
"python_version": "3.10.4",
},
]
def execute_command(command_list):
proc = subprocess.Popen(
command_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
out, err = proc.communicate()
out, err = out.decode(), err.decode()
if proc.returncode != 0:
command_str = " ".join(command_list)
raise RuntimeError(
"Command exited with non-zero exit code.\n"
"Exit code: {}\n"
"Command:\n{}\n"
"stdout:\n{}\n"
"stderr:\n{}\n".format(proc.returncode, command_str, out, err)
)
return out
def get_package_with_constraint(package_name, build_metadata, uses_pip=False):
build_package_constraints = build_metadata.get("package_constraints")
if build_package_constraints is None:
constraint = None
else:
constraint = build_package_constraints.get(package_name)
constraint = constraint or default_package_constraints.get(package_name)
if constraint is None:
return package_name
comment = ""
if constraint == "min":
constraint = execute_command(
[sys.executable, "sklearn/_min_dependencies.py", package_name]
).strip()
comment = " # min"
if re.match(r"\d[.\d]*", constraint):
equality = "==" if uses_pip else "="
constraint = equality + constraint
return f"{package_name}{constraint}{comment}"
environment = Environment(trim_blocks=True, lstrip_blocks=True)
environment.filters["get_package_with_constraint"] = get_package_with_constraint
def get_conda_environment_content(build_metadata):
template = environment.from_string("""
# DO NOT EDIT: this file is generated from the specification found in the
# following script to centralize the configuration for CI builds:
# build_tools/update_environments_and_lock_files.py
channels:
- {{ build_metadata['channel'] }}
dependencies:
{% for conda_dep in build_metadata['conda_dependencies'] %}
- {{ conda_dep | get_package_with_constraint(build_metadata) }}
{% endfor %}
{% if build_metadata['pip_dependencies'] %}
- pip
- pip:
{% for pip_dep in build_metadata.get('pip_dependencies', []) %}
- {{ pip_dep | get_package_with_constraint(build_metadata, uses_pip=True) }}
{% endfor %}
{% endif %}""".strip())
return template.render(build_metadata=build_metadata)
def write_conda_environment(build_metadata):
content = get_conda_environment_content(build_metadata)
build_name = build_metadata["build_name"]
folder_path = Path(build_metadata["folder"])
output_path = folder_path / f"{build_name}_environment.yml"
output_path.write_text(content)
def write_all_conda_environments(build_metadata_list):
for build_metadata in build_metadata_list:
write_conda_environment(build_metadata)
def conda_lock(environment_path, lock_file_path, platform):
command = (
f"conda-lock lock --mamba --kind explicit --platform {platform} "
f"--file {environment_path} --filename-template {lock_file_path}"
)
logger.debug("conda-lock command: %s", command)
execute_command(shlex.split(command))
def create_conda_lock_file(build_metadata):
build_name = build_metadata["build_name"]
folder_path = Path(build_metadata["folder"])
environment_path = folder_path / f"{build_name}_environment.yml"
platform = build_metadata["platform"]
lock_file_basename = build_name
if not lock_file_basename.endswith(platform):
lock_file_basename = f"{lock_file_basename}_{platform}"
lock_file_path = folder_path / f"{lock_file_basename}_conda.lock"
conda_lock(environment_path, lock_file_path, platform)
def write_all_conda_lock_files(build_metadata_list):
for build_metadata in build_metadata_list:
logger.info(build_metadata["build_name"])
create_conda_lock_file(build_metadata)
def get_pip_requirements_content(build_metadata):
template = environment.from_string("""
# DO NOT EDIT: this file is generated from the specification found in the
# following script to centralize the configuration for CI builds:
# build_tools/update_environments_and_lock_files.py
{% for pip_dep in build_metadata['pip_dependencies'] %}
{{ pip_dep | get_package_with_constraint(build_metadata, uses_pip=True) }}
{% endfor %}""".strip())
return template.render(build_metadata=build_metadata)
def write_pip_requirements(build_metadata):
build_name = build_metadata["build_name"]
content = get_pip_requirements_content(build_metadata)
folder_path = Path(build_metadata["folder"])
output_path = folder_path / f"{build_name}_requirements.txt"
output_path.write_text(content)
def write_all_pip_requirements(build_metadata_list):
for build_metadata in build_metadata_list:
logger.info(build_metadata["build_name"])
write_pip_requirements(build_metadata)
def pip_compile(pip_compile_path, requirements_path, lock_file_path):
command = f"{pip_compile_path} --upgrade {requirements_path} -o {lock_file_path}"
logger.debug("pip-compile command: %s", command)
execute_command(shlex.split(command))
def write_pip_lock_file(build_metadata):
build_name = build_metadata["build_name"]
python_version = build_metadata["python_version"]
environment_name = f"pip-tools-python{python_version}"
# To make sure that the Python used to create the pip lock file is the same
# as the one used during the CI build where the lock file is used, we first
# create a conda environment with the correct Python version and
# pip-compile and run pip-compile in this environment
command = (
"conda create -c conda-forge -n"
f" pip-tools-python{python_version} python={python_version} pip-tools -y"
)
execute_command(shlex.split(command))
json_output = execute_command(shlex.split("conda info --json"))
conda_info = json.loads(json_output)
environment_folder = [
each for each in conda_info["envs"] if each.endswith(environment_name)
][0]
environment_path = Path(environment_folder)
pip_compile_path = environment_path / "bin" / "pip-compile"
folder_path = Path(build_metadata["folder"])
requirement_path = folder_path / f"{build_name}_requirements.txt"
lock_file_path = folder_path / f"{build_name}_lock.txt"
pip_compile(pip_compile_path, requirement_path, lock_file_path)
def write_all_pip_lock_files(build_metadata_list):
for build_metadata in build_metadata_list:
write_pip_lock_file(build_metadata)
def remove_unnecessary_package_from_lock_file(build_metadata_list, build_name, package):
# find the build metadata from the list
build_metadata = None
for metadata in build_metadata_list:
if metadata["build_name"] == build_name:
build_metadata = metadata
break
if build_metadata is None:
logger.warning(
f"Could not find {build_name} in build_metadata_list. This may be expected"
" if --select-build is used"
)
return
folder_path = Path(build_metadata["folder"])
platform = build_metadata["platform"]
lock_file_basename = build_name
if not lock_file_basename.endswith(platform):
lock_file_basename = f"{lock_file_basename}_{platform}"
lock_file_path = folder_path / f"{lock_file_basename}_conda.lock"
with open(lock_file_path, "r") as f_read_only:
lock_file_content = f_read_only.readlines()
with open(lock_file_path, "w") as f_write:
for line in lock_file_content:
if package in line:
# simple heuristic to remove the package
# it will not work if package that have partial string in common
logger.debug(f"Removing {package} from {build_name} lock file")
logger.debug(f"Removed line: {line}")
continue
f_write.write(line)
def check_conda_lock_version():
# Check that the installed conda-lock version is consistent with _min_dependencies.
expected_conda_lock_version = execute_command(
[sys.executable, "sklearn/_min_dependencies.py", "conda-lock"]
).strip()
installed_conda_lock_version = version("conda-lock")
if installed_conda_lock_version != expected_conda_lock_version:
raise RuntimeError(
f"Expected conda-lock version: {expected_conda_lock_version}, got:"
f" {installed_conda_lock_version}"
)
@click.command()
@click.option(
"--select-build",
default="",
help="Regex to restrict the builds we want to update environment and lock files",
)
def main(select_build):
check_conda_lock_version()
filtered_conda_build_metadata_list = [
each
for each in conda_build_metadata_list
if re.search(select_build, each["build_name"])
]
logger.info("Writing conda environments")
write_all_conda_environments(filtered_conda_build_metadata_list)
logger.info("Writing conda lock files")
write_all_conda_lock_files(filtered_conda_build_metadata_list)
if Version(conda_lock_version) < Version("1.5"):
# In conda-lock < 1.5, `os_name` was not considered when creating the
# dependency trees:
# https://github.com/conda/conda-lock/issues/293
# Here, we need to manually remove the package `pywinpty` from the `doc`
# build which is only needed on Windows required by `jupyter-server`.
remove_unnecessary_package_from_lock_file(
filtered_conda_build_metadata_list, "doc", "pywinpty"
)
filtered_pip_build_metadata_list = [
each
for each in pip_build_metadata_list
if re.search(select_build, each["build_name"])
]
logger.info("Writing pip requirements")
write_all_pip_requirements(filtered_pip_build_metadata_list)
logger.info("Writing pip lock files")
write_all_pip_lock_files(filtered_pip_build_metadata_list)
if __name__ == "__main__":
main()