forked from scipy/scipy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.py
1202 lines (1026 loc) · 38.7 KB
/
dev.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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python3
'''
Developer CLI: building (meson), tests, benchmark, etc.
This file contains tasks definitions for doit (https://pydoit.org).
And also a CLI interface using click (https://click.palletsprojects.com).
The CLI is ideal for project contributors while,
doit interface is better suited for authoring the development tasks.
REQUIREMENTS:
--------------
- see environment.yml: doit, pydevtool, click, rich-click
# USAGE:
## 1 - click API
Commands can added using default Click API. i.e.
```
@cli.command()
@click.argument('extra_argv', nargs=-1)
@click.pass_obj
def python(ctx_obj, extra_argv):
"""Start a Python shell with PYTHONPATH set"""
```
## 2 - class based Click command definition
`CliGroup` provides an alternative class based API to create Click commands.
Just use the `cls_cmd` decorator. And define a `run()` method
```
@cli.cls_cmd('test')
class Test():
"""Run tests"""
@classmethod
def run(cls):
print('Running tests...')
```
- Command may make use a Click.Group context defining a `ctx` class attribute
- Command options are also define as class attributes
```
@cli.cls_cmd('test')
class Test():
"""Run tests"""
ctx = CONTEXT
verbose = Option(
['--verbose', '-v'], default=False, is_flag=True, help="verbosity")
@classmethod
def run(cls, **kwargs): # kwargs contains options from class and CONTEXT
print('Running tests...')
```
## 3 - class based interface can be run as a doit task by subclassing from Task
- Extra doit task metadata can be defined as class attribute TASK_META.
- `run()` method will be used as python-action by task
```
@cli.cls_cmd('test')
class Test(Task): # Task base class, doit will create a task
"""Run tests"""
ctx = CONTEXT
TASK_META = {
'task_dep': ['build'],
}
@classmethod
def run(cls, **kwargs):
pass
```
## 4 - doit tasks with cmd-action "shell" or dynamic metadata
Define method `task_meta()` instead of `run()`:
```
@cli.cls_cmd('refguide-check')
class RefguideCheck(Task):
@classmethod
def task_meta(cls, **kwargs):
return {
```
'''
import os
import subprocess
import sys
import warnings
import shutil
import json
import datetime
import time
import platform
import importlib.util
import errno
import contextlib
from sysconfig import get_path
# distutils is required to infer meson install path
# if this needs to be replaced for Python 3.12 support and there's no
# stdlib alternative, use CmdAction and the hack discussed in gh-16058
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
from distutils import dist
from distutils.command.install import INSTALL_SCHEMES
from pathlib import Path
from collections import namedtuple
from types import ModuleType as new_module
from dataclasses import dataclass
import click
from click import Option, Argument
from doit.cmd_base import ModuleTaskLoader
from doit.reporter import ZeroReporter
from doit.exceptions import TaskError
from doit.api import run_tasks
from pydevtool.cli import UnifiedContext, CliGroup, Task
from rich.console import Console
from rich.panel import Panel
from rich.theme import Theme
from rich_click import rich_click
DOIT_CONFIG = {
'verbosity': 2,
'minversion': '0.36.0',
}
console_theme = Theme({
"cmd": "italic gray50",
})
if sys.platform == 'win32':
class EMOJI:
cmd = ">"
else:
class EMOJI:
cmd = ":computer:"
rich_click.STYLE_ERRORS_SUGGESTION = "yellow italic"
rich_click.SHOW_ARGUMENTS = True
rich_click.GROUP_ARGUMENTS_OPTIONS = False
rich_click.SHOW_METAVARS_COLUMN = True
rich_click.USE_MARKDOWN = True
rich_click.OPTION_GROUPS = {
"dev.py": [
{
"name": "Options",
"options": [
"--help", "--build-dir", "--no-build", "--install-prefix"],
},
],
"dev.py test": [
{
"name": "Options",
"options": ["--help", "--verbose", "--parallel", "--coverage",
"--durations"],
},
{
"name": "Options: test selection",
"options": ["--submodule", "--tests", "--mode"],
},
],
}
rich_click.COMMAND_GROUPS = {
"dev.py": [
{
"name": "build & testing",
"commands": ["build", "test"],
},
{
"name": "static checkers",
"commands": ["lint", "mypy"],
},
{
"name": "environments",
"commands": ["shell", "python", "ipython"],
},
{
"name": "documentation",
"commands": ["doc", "refguide-check"],
},
{
"name": "release",
"commands": ["notes", "authors"],
},
{
"name": "benchmarking",
"commands": ["bench"],
},
]
}
class ErrorOnlyReporter(ZeroReporter):
desc = """Report errors only"""
def runtime_error(self, msg):
console = Console()
console.print("[red bold] msg")
def add_failure(self, task, fail_info):
console = Console()
if isinstance(fail_info, TaskError):
console.print(f'[red]Task Error - {task.name}'
f' => {fail_info.message}')
if fail_info.traceback:
console.print(Panel(
"".join(fail_info.traceback),
title=f"{task.name}",
subtitle=fail_info.message,
border_style="red",
))
CONTEXT = UnifiedContext({
'build_dir': Option(
['--build-dir'], metavar='BUILD_DIR',
default='build', show_default=True,
help=':wrench: Relative path to the build directory.'),
'no_build': Option(
["--no-build", "-n"], default=False, is_flag=True,
help=(":wrench: Do not build the project"
" (note event python only modification require build).")),
'install_prefix': Option(
['--install-prefix'], default=None, metavar='INSTALL_DIR',
help=(":wrench: Relative path to the install directory."
" Default is <build-dir>-install.")),
})
def run_doit_task(tasks):
"""
:param tasks: (dict) task_name -> {options}
"""
loader = ModuleTaskLoader(globals())
doit_config = {
'verbosity': 2,
'reporter': ErrorOnlyReporter,
}
return run_tasks(loader, tasks, extra_config={'GLOBAL': doit_config})
class CLI(CliGroup):
context = CONTEXT
run_doit_task = run_doit_task
@click.group(cls=CLI)
@click.pass_context
def cli(ctx, **kwargs):
"""Developer Tool for SciPy
\bCommands that require a built/installed instance are marked with :wrench:.
\b**python dev.py --build-dir my-build test -s stats**
""" # noqa: E501
CLI.update_context(ctx, kwargs)
PROJECT_MODULE = "scipy"
PROJECT_ROOT_FILES = ['scipy', 'LICENSE.txt', 'meson.build']
@dataclass
class Dirs:
"""
root:
Directory where scr, build config and tools are located
(and this file)
build:
Directory where build output files (i.e. *.o) are saved
install:
Directory where .so from build and .py from src are put together.
site:
Directory where the built SciPy version was installed.
This is a custom prefix, followed by a relative path matching
the one the system would use for the site-packages of the active
Python interpreter.
"""
# all paths are absolute
root: Path
build: Path
installed: Path
site: Path # <install>/lib/python<version>/site-packages
def __init__(self, args=None):
""":params args: object like Context(build_dir, install_prefix)"""
self.root = Path(__file__).parent.absolute()
if not args:
return
self.build = Path(args.build_dir).resolve()
if args.install_prefix:
self.installed = Path(args.install_prefix).resolve()
else:
self.installed = self.build.parent / (self.build.stem + "-install")
# relative path for site-package with py version
# i.e. 'lib/python3.10/site-packages'
self.site = self.get_site_packages()
def add_sys_path(self):
"""Add site dir to sys.path / PYTHONPATH"""
site_dir = str(self.site)
sys.path.insert(0, site_dir)
os.environ['PYTHONPATH'] = \
os.pathsep.join((site_dir, os.environ.get('PYTHONPATH', '')))
def get_site_packages(self):
"""
Depending on whether we have debian python or not,
return dist_packages path or site_packages path.
"""
if 'deb_system' in INSTALL_SCHEMES:
# debian patched python in use
install_cmd = dist.Distribution().get_command_obj('install')
install_cmd.select_scheme('deb_system')
install_cmd.finalize_options()
plat_path = Path(install_cmd.install_platlib)
else:
plat_path = Path(get_path('platlib'))
return self.installed / plat_path.relative_to(sys.exec_prefix)
@contextlib.contextmanager
def working_dir(new_dir):
current_dir = os.getcwd()
try:
os.chdir(new_dir)
yield
finally:
os.chdir(current_dir)
def import_module_from_path(mod_name, mod_path):
"""Import module with name `mod_name` from file path `mod_path`"""
spec = importlib.util.spec_from_file_location(mod_name, mod_path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
def get_test_runner(project_module):
"""
get Test Runner from locally installed/built project
"""
__import__(project_module)
# scipy._lib._testutils:PytestTester
test = sys.modules[project_module].test
version = sys.modules[project_module].__version__
mod_path = sys.modules[project_module].__file__
mod_path = os.path.abspath(os.path.join(os.path.dirname(mod_path)))
return test, version, mod_path
############
@cli.cls_cmd('build')
class Build(Task):
""":wrench: Build & install package on path.
\b
```python
Examples:
$ python dev.py build --asan ;
ASAN_OPTIONS=detect_leaks=0:symbolize=1:strict_init_order=true
LD_PRELOAD=$(gcc --print-file-name=libasan.so)
python dev.py test -v -t
./scipy/ndimage/tests/test_morphology.py -- -s
```
"""
ctx = CONTEXT
werror = Option(
['--werror'], default=False, is_flag=True,
help="Treat warnings as errors")
gcov = Option(
['--gcov'], default=False, is_flag=True,
help="enable C code coverage via gcov (requires GCC)."
"gcov output goes to build/**/*.gc*")
asan = Option(
['--asan'], default=False, is_flag=True,
help=("Build and run with AddressSanitizer support. "
"Note: the build system doesn't check whether "
"the project is already compiled with ASan. "
"If not, you need to do a clean build (delete "
"build and build-install directories)."))
debug = Option(
['--debug', '-d'], default=False, is_flag=True, help="Debug build")
parallel = Option(
['--parallel', '-j'], default=None, metavar='N_JOBS',
help=("Number of parallel jobs for building. "
"This defaults to 2 * n_cpus + 2."))
show_build_log = Option(
['--show-build-log'], default=False, is_flag=True,
help="Show build output rather than using a log file")
win_cp_openblas = Option(
['--win-cp-openblas'], default=False, is_flag=True,
help=("If set, and on Windows, copy OpenBLAS lib to install directory "
"after meson install. "
"Note: this argument may be removed in the future once a "
"`site.cfg`-like mechanism to select BLAS/LAPACK libraries is "
"implemented for Meson"))
@classmethod
def setup_build(cls, dirs, args):
"""
Setting up meson-build
"""
for fn in PROJECT_ROOT_FILES:
if not (dirs.root / fn).exists():
print("To build the project, run dev.py in "
"git checkout or unpacked source")
sys.exit(1)
env = dict(os.environ)
cmd = ["meson", "setup", dirs.build, "--prefix", dirs.installed]
build_dir = dirs.build
run_dir = Path()
if build_dir.exists() and not (build_dir / 'meson-info').exists():
if list(build_dir.iterdir()):
raise RuntimeError("Can't build into non-empty directory "
f"'{build_dir.absolute()}'")
if sys.platform == "cygwin":
# Cygwin only has netlib lapack, but can link against
# OpenBLAS rather than netlib blas at runtime. There is
# no libopenblas-devel to enable linking against
# openblas-specific functions or OpenBLAS Lapack
cmd.extend(["-Dlapack=lapack", "-Dblas=blas"])
build_options_file = (
build_dir / "meson-info" / "intro-buildoptions.json")
if build_options_file.exists():
with open(build_options_file) as f:
build_options = json.load(f)
installdir = None
for option in build_options:
if option["name"] == "prefix":
installdir = option["value"]
break
if installdir != str(dirs.installed):
run_dir = build_dir
cmd = ["meson", "setup", "--reconfigure",
"--prefix", str(dirs.installed)]
else:
return
if args.werror:
cmd += ["--werror"]
if args.gcov:
cmd += ['-Db_coverage=true']
if args.asan:
cmd += ['-Db_sanitize=address,undefined']
# Setting up meson build
cmd_str = ' '.join([str(p) for p in cmd])
cls.console.print(f"{EMOJI.cmd} [cmd] {cmd_str}")
ret = subprocess.call(cmd, env=env, cwd=run_dir)
if ret == 0:
print("Meson build setup OK")
else:
print("Meson build setup failed!")
sys.exit(1)
return env
@classmethod
def build_project(cls, dirs, args, env):
"""
Build a dev version of the project.
"""
cmd = ["ninja", "-C", str(dirs.build)]
if args.parallel is not None:
cmd += ["-j", str(args.parallel)]
# Building with ninja-backend
cmd_str = ' '.join([str(p) for p in cmd])
cls.console.print(f"{EMOJI.cmd} [cmd] {cmd_str}")
ret = subprocess.call(cmd, env=env, cwd=dirs.root)
if ret == 0:
print("Build OK")
else:
print("Build failed!")
sys.exit(1)
@classmethod
def install_project(cls, dirs, args):
"""
Installs the project after building.
"""
if dirs.installed.exists():
non_empty = len(os.listdir(dirs.installed))
if non_empty and not dirs.site.exists():
raise RuntimeError("Can't install in non-empty directory: "
f"'{dirs.installed}'")
cmd = ["meson", "install", "-C", args.build_dir, "--only-changed"]
log_filename = dirs.root / 'meson-install.log'
start_time = datetime.datetime.now()
cmd_str = ' '.join([str(p) for p in cmd])
cls.console.print(f"{EMOJI.cmd} [cmd] {cmd_str}")
if args.show_build_log:
ret = subprocess.call(cmd, cwd=dirs.root)
else:
print("Installing, see meson-install.log...")
with open(log_filename, 'w') as log:
p = subprocess.Popen(cmd, stdout=log, stderr=log,
cwd=dirs.root)
try:
# Wait for it to finish, and print something to indicate the
# process is alive, but only if the log file has grown (to
# allow continuous integration environments kill a hanging
# process accurately if it produces no output)
last_blip = time.time()
last_log_size = os.stat(log_filename).st_size
while p.poll() is None:
time.sleep(0.5)
if time.time() - last_blip > 60:
log_size = os.stat(log_filename).st_size
if log_size > last_log_size:
elapsed = datetime.datetime.now() - start_time
print(" ... installation in progress ({} "
"elapsed)".format(elapsed))
last_blip = time.time()
last_log_size = log_size
ret = p.wait()
except: # noqa: E722
p.terminate()
raise
elapsed = datetime.datetime.now() - start_time
if ret != 0:
if not args.show_build_log:
with open(log_filename) as f:
print(f.read())
print(f"Installation failed! ({elapsed} elapsed)")
sys.exit(1)
# ignore everything in the install directory.
with open(dirs.installed / ".gitignore", "w") as f:
f.write("*")
if sys.platform == "cygwin":
rebase_cmd = ["/usr/bin/rebase", "--database", "--oblivious"]
rebase_cmd.extend(Path(dirs.installed).glob("**/*.dll"))
subprocess.check_call(rebase_cmd)
print("Installation OK")
return
@classmethod
def copy_openblas(cls, dirs):
"""
Copies OpenBLAS DLL to the SciPy install dir, and also overwrites the
default `_distributor_init.py` file with the one
we use for wheels uploaded to PyPI so that DLL gets loaded.
Assumes pkg-config is installed and aware of OpenBLAS.
The "dirs" parameter is typically a "Dirs" object with the
structure as the following, say, if dev.py is run from the
folder "repo":
dirs = Dirs(
root=WindowsPath('C:/.../repo'),
build=WindowsPath('C:/.../repo/build'),
installed=WindowsPath('C:/.../repo/build-install'),
site=WindowsPath('C:/.../repo/build-install/Lib/site-packages'
)
"""
# Get OpenBLAS lib path from pkg-config
cmd = ['pkg-config', '--variable', 'libdir', 'openblas']
result = subprocess.run(cmd, capture_output=True, text=True)
# pkg-config does not return any meaningful error message if fails
if result.returncode != 0:
print('"pkg-config --variable libdir openblas" '
'command did not manage to find OpenBLAS '
'succesfully. Try running manually on the '
'command prompt for more information.')
return result.returncode
# Skip the drive letter of the path -> /c to get Windows drive
# to be appended correctly to avoid "C:\c\..." from stdout.
openblas_lib_path = Path(result.stdout.strip()[2:]).resolve()
if not openblas_lib_path.stem == 'lib':
raise RuntimeError('"pkg-config --variable libdir openblas" '
'command did not return a path ending with'
' "lib" folder. Instead it returned '
f'"{openblas_lib_path}"')
# Look in bin subdirectory for OpenBLAS binaries.
bin_path = openblas_lib_path.parent / 'bin'
# Locate, make output .libs directory in Scipy install directory.
scipy_path = dirs.site / 'scipy'
libs_path = scipy_path / '.libs'
libs_path.mkdir(exist_ok=True)
# Copy DLL files from OpenBLAS install to scipy install .libs subdir.
for dll_fn in bin_path.glob('*.dll'):
out_fname = libs_path / dll_fn.name
print(f'Copying {dll_fn} ----> {out_fname}')
out_fname.write_bytes(dll_fn.read_bytes())
# Write _distributor_init.py to scipy install dir;
# this ensures the .libs file is on the DLL search path at run-time,
# so OpenBLAS gets found
openblas_support = import_module_from_path(
'openblas_support',
dirs.root / 'tools' / 'openblas_support.py')
openblas_support.make_init(scipy_path)
return 0
@classmethod
def run(cls, add_path=False, **kwargs):
kwargs.update(cls.ctx.get(kwargs))
Args = namedtuple('Args', [k for k in kwargs.keys()])
args = Args(**kwargs)
cls.console = Console(theme=console_theme)
dirs = Dirs(args)
if args.no_build:
print("Skipping build")
else:
env = cls.setup_build(dirs, args)
cls.build_project(dirs, args, env)
cls.install_project(dirs, args)
if args.win_cp_openblas and platform.system() == 'Windows':
if cls.copy_openblas(dirs) == 0:
print('OpenBLAS copied')
else:
print("OpenBLAS copy failed!")
sys.exit(1)
# add site to sys.path
if add_path:
dirs.add_sys_path()
@cli.cls_cmd('test')
class Test(Task):
""":wrench: Run tests.
\b
```python
Examples:
$ python dev.py test -s {SAMPLE_SUBMODULE}
$ python dev.py test -t scipy.optimize.tests.test_minimize_constrained
$ python dev.py test -s cluster -m full --durations 20
$ python dev.py test -s stats -- --tb=line # `--` passes next args to pytest
```
""" # noqa: E501
ctx = CONTEXT
verbose = Option(
['--verbose', '-v'], default=False, is_flag=True,
help="more verbosity")
# removed doctests as currently not supported by _lib/_testutils.py
# doctests = Option(['--doctests'], default=False)
coverage = Option(
['--coverage', '-c'], default=False, is_flag=True,
help=("report coverage of project code. "
"HTML output goes under build/coverage"))
durations = Option(
['--durations', '-d'], default=None, metavar="NUM_TESTS",
help="Show timing for the given number of slowest tests"
)
submodule = Option(
['--submodule', '-s'], default=None, metavar='MODULE_NAME',
help="Submodule whose tests to run (cluster, constants, ...)")
tests = Option(
['--tests', '-t'], default=None, multiple=True, metavar='TESTS',
help='Specify tests to run')
mode = Option(
['--mode', '-m'], default='fast', metavar='MODE', show_default=True,
help=("'fast', 'full', or something that could be passed to "
"`pytest -m` as a marker expression"))
parallel = Option(
['--parallel', '-j'], default=1, metavar='N_JOBS',
help="Number of parallel jobs for testing"
)
# Argument can't have `help=`; used to consume all of `-- arg1 arg2 arg3`
pytest_args = Argument(
['pytest_args'], nargs=-1, metavar='PYTEST-ARGS', required=False
)
TASK_META = {
'task_dep': ['build'],
}
@classmethod
def scipy_tests(cls, args, pytest_args):
dirs = Dirs(args)
dirs.add_sys_path()
print(f"SciPy from development installed path at: {dirs.site}")
# FIXME: support pos-args with doit
extra_argv = pytest_args[:] if pytest_args else []
if extra_argv and extra_argv[0] == '--':
extra_argv = extra_argv[1:]
if args.coverage:
dst_dir = dirs.root / args.build_dir / 'coverage'
fn = dst_dir / 'coverage_html.js'
if dst_dir.is_dir() and fn.is_file():
shutil.rmtree(dst_dir)
extra_argv += ['--cov-report=html:' + str(dst_dir)]
shutil.copyfile(dirs.root / '.coveragerc',
dirs.site / '.coveragerc')
if args.durations:
extra_argv += ['--durations', args.durations]
# convert options to test selection
if args.submodule:
tests = [PROJECT_MODULE + "." + args.submodule]
elif args.tests:
tests = args.tests
else:
tests = None
runner, version, mod_path = get_test_runner(PROJECT_MODULE)
# FIXME: changing CWD is not a good practice
with working_dir(dirs.site):
print("Running tests for {} version:{}, installed at:{}".format(
PROJECT_MODULE, version, mod_path))
# runner verbosity - convert bool to int
verbose = int(args.verbose) + 1
result = runner( # scipy._lib._testutils:PytestTester
args.mode,
verbose=verbose,
extra_argv=extra_argv,
doctests=False,
coverage=args.coverage,
tests=tests,
parallel=args.parallel)
return result
@classmethod
def run(cls, pytest_args, **kwargs):
"""run unit-tests"""
kwargs.update(cls.ctx.get())
Args = namedtuple('Args', [k for k in kwargs.keys()])
args = Args(**kwargs)
return cls.scipy_tests(args, pytest_args)
@cli.cls_cmd('bench')
class Bench(Task):
""":wrench: Run benchmarks.
\b
```python
Examples:
$ python dev.py bench -t integrate.SolveBVP
$ python dev.py bench -t linalg.Norm
$ python dev.py bench --compare main
```
"""
ctx = CONTEXT
TASK_META = {
'task_dep': ['build'],
}
submodule = Option(
['--submodule', '-s'], default=None, metavar='SUBMODULE',
help="Submodule whose tests to run (cluster, constants, ...)")
tests = Option(
['--tests', '-t'], default=None, multiple=True,
metavar='TESTS', help='Specify tests to run')
compare = Option(
['--compare', '-c'], default=None, metavar='COMPARE', multiple=True,
help=(
"Compare benchmark results of current HEAD to BEFORE. "
"Use an additional --bench COMMIT to override HEAD with COMMIT. "
"Note that you need to commit your changes first!"))
@staticmethod
def run_asv(dirs, cmd):
EXTRA_PATH = ['/usr/lib/ccache', '/usr/lib/f90cache',
'/usr/local/lib/ccache', '/usr/local/lib/f90cache']
bench_dir = dirs.root / 'benchmarks'
sys.path.insert(0, str(bench_dir))
# Always use ccache, if installed
env = dict(os.environ)
env['PATH'] = os.pathsep.join(EXTRA_PATH +
env.get('PATH', '').split(os.pathsep))
# Control BLAS/LAPACK threads
env['OPENBLAS_NUM_THREADS'] = '1'
env['MKL_NUM_THREADS'] = '1'
# Limit memory usage
from benchmarks.common import set_mem_rlimit
try:
set_mem_rlimit()
except (ImportError, RuntimeError):
pass
try:
return subprocess.call(cmd, env=env, cwd=bench_dir)
except OSError as err:
if err.errno == errno.ENOENT:
cmd_str = " ".join(cmd)
print(f"Error when running '{cmd_str}': {err}\n")
print("You need to install Airspeed Velocity "
"(https://airspeed-velocity.github.io/asv/)")
print("to run Scipy benchmarks")
return 1
raise
@classmethod
def scipy_bench(cls, args):
dirs = Dirs(args)
dirs.add_sys_path()
print(f"SciPy from development installed path at: {dirs.site}")
with working_dir(dirs.site):
runner, version, mod_path = get_test_runner(PROJECT_MODULE)
extra_argv = []
if args.tests:
extra_argv.append(args.tests)
if args.submodule:
extra_argv.append([args.submodule])
bench_args = []
for a in extra_argv:
bench_args.extend(['--bench', ' '.join(str(x) for x in a)])
if not args.compare:
print("Running benchmarks for Scipy version %s at %s"
% (version, mod_path))
cmd = ['asv', 'run', '--dry-run', '--show-stderr',
'--python=same'] + bench_args
retval = cls.run_asv(dirs, cmd)
sys.exit(retval)
else:
if len(args.compare) == 1:
commit_a = args.compare[0]
commit_b = 'HEAD'
elif len(args.compare) == 2:
commit_a, commit_b = args.compare
else:
print("Too many commits to compare benchmarks for")
# Check for uncommitted files
if commit_b == 'HEAD':
r1 = subprocess.call(['git', 'diff-index', '--quiet',
'--cached', 'HEAD'])
r2 = subprocess.call(['git', 'diff-files', '--quiet'])
if r1 != 0 or r2 != 0:
print("*" * 80)
print("WARNING: you have uncommitted changes --- "
"these will NOT be benchmarked!")
print("*" * 80)
# Fix commit ids (HEAD is local to current repo)
p = subprocess.Popen(['git', 'rev-parse', commit_b],
stdout=subprocess.PIPE)
out, err = p.communicate()
commit_b = out.strip()
p = subprocess.Popen(['git', 'rev-parse', commit_a],
stdout=subprocess.PIPE)
out, err = p.communicate()
commit_a = out.strip()
cmd_compare = [
'asv', 'continuous', '--show-stderr', '--factor', '1.05',
commit_a, commit_b
] + bench_args
cls.run_asv(dirs, cmd_compare)
sys.exit(1)
@classmethod
def run(cls, **kwargs):
"""run benchmark"""
kwargs.update(cls.ctx.get())
Args = namedtuple('Args', [k for k in kwargs.keys()])
args = Args(**kwargs)
cls.scipy_bench(args)
###################
# linters
def emit_cmdstr(cmd):
"""Print the command that's being run to stdout
Note: cannot use this in the below tasks (yet), because as is these command
strings are always echoed to the console, even if the command isn't run
(but for example the `build` command is run).
"""
console = Console(theme=console_theme)
# The [cmd] square brackets controls the font styling, typically in italics
# to differentiate it from other stdout content
console.print(f"{EMOJI.cmd} [cmd] {cmd}")
def task_lint():
# Lint just the diff since branching off of main using a
# stricter configuration.
# emit_cmdstr(os.path.join('tools', 'lint.py') + ' --diff-against main')
return {
'basename': 'lint',
'actions': [str(Dirs().root / 'tools' / 'lint.py') +
' --diff-against=main'],
'doc': 'Lint only files modified since last commit (stricter rules)',
}
def task_unicode_check():
# emit_cmdstr(os.path.join('tools', 'unicode-check.py'))
return {
'basename': 'unicode-check',
'actions': [str(Dirs().root / 'tools' / 'unicode-check.py')],
'doc': 'Check for disallowed Unicode characters in the SciPy Python '
'and Cython source code.',
}
def task_check_test_name():
# emit_cmdstr(os.path.join('tools', 'check_test_name.py'))
return {
"basename": "check-testname",
"actions": [str(Dirs().root / "tools" / "check_test_name.py")],
"doc": "Check tests are correctly named so that pytest runs them."
}
@cli.cls_cmd('lint')
class Lint():
""":dash: Run linter on modified files and check for
disallowed Unicode characters and possibly-invalid test names."""
def run():
run_doit_task({
'lint': {},
'unicode-check': {},
'check-testname': {},
})
@cli.cls_cmd('mypy')
class Mypy(Task):
""":wrench: Run mypy on the codebase."""
ctx = CONTEXT
TASK_META = {
'task_dep': ['build'],
}
@classmethod
def run(cls, **kwargs):
kwargs.update(cls.ctx.get())
Args = namedtuple('Args', [k for k in kwargs.keys()])
args = Args(**kwargs)
dirs = Dirs(args)
try:
import mypy.api
except ImportError as e:
raise RuntimeError(
"Mypy not found. Please install it by running "
"pip install -r mypy_requirements.txt from the repo root"
) from e
config = dirs.root / "mypy.ini"
check_path = PROJECT_MODULE
with working_dir(dirs.site):
# By default mypy won't color the output since it isn't being
# invoked from a tty.
os.environ['MYPY_FORCE_COLOR'] = '1'
# Change to the site directory to make sure mypy doesn't pick
# up any type stubs in the source tree.
emit_cmdstr(f"mypy.api.run --config-file {config} {check_path}")
report, errors, status = mypy.api.run([
"--config-file",
str(config),
check_path,
])
print(report, end='')
print(errors, end='', file=sys.stderr)
return status == 0
##########################################
# DOC