forked from great-expectations/great_expectations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
733 lines (622 loc) · 22.2 KB
/
tasks.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
"""
PyInvoke developer task file
https://www.pyinvoke.org/
These tasks can be run using `invoke <NAME>` or `inv <NAME>` from the project root.
To show all available tasks `invoke --list`
To show task help page `invoke <NAME> --help`
"""
from __future__ import annotations
import json
import os
import pathlib
import shutil
import sys
from typing import TYPE_CHECKING, Union
import invoke
from typing_extensions import Final
from docs.sphinx_api_docs_source import check_public_api_docstrings, public_api_report
from docs.sphinx_api_docs_source.build_sphinx_api_docs import SphinxInvokeDocsBuilder
try:
from tests.integration.usage_statistics import usage_stats_utils
is_ge_installed: bool = True
except ModuleNotFoundError:
is_ge_installed = False
if TYPE_CHECKING:
from invoke.context import Context
GX_ROOT_DIR: Final = pathlib.Path(__file__).parent / "great_expectations"
_CHECK_HELP_DESC = "Only checks for needed changes without writing back. Exit with error code if changes needed."
_EXCLUDE_HELP_DESC = "Exclude files or directories"
_PATH_HELP_DESC = "Target path. (Default: .)"
# https://www.pyinvoke.org/faq.html?highlight=pty#why-is-my-command-behaving-differently-under-invoke-versus-being-run-by-hand
_PTY_HELP_DESC = "Whether or not to use a pseudo terminal"
@invoke.task(
help={
"check": _CHECK_HELP_DESC,
"exclude": _EXCLUDE_HELP_DESC,
"path": _PATH_HELP_DESC,
"isort": "Use `isort` to sort packages. Default behavior.",
"ruff": (
"Use `ruff` instead of `isort` to sort imports."
" This will eventually become the default."
),
"pty": _PTY_HELP_DESC,
}
)
def sort(
ctx: Context,
path: str = ".",
check: bool = False,
exclude: str | None = None,
ruff: bool = False, # isort is the current default
isort: bool = False,
pty: bool = True,
):
"""Sort module imports."""
if ruff and isort:
raise invoke.Exit("cannot use both `--ruff` and `--isort`", code=1)
if not isort:
cmds = [
"ruff",
path,
"--select I",
"--diff" if check else "--fix",
]
if exclude:
cmds.extend(["--extend-exclude", exclude])
else:
cmds = ["isort", path]
if check:
cmds.append("--check-only")
if exclude:
cmds.extend(["--skip", exclude])
ctx.run(" ".join(cmds), echo=True, pty=pty)
@invoke.task(
help={
"check": _CHECK_HELP_DESC,
"exclude": _EXCLUDE_HELP_DESC,
"path": _PATH_HELP_DESC,
"sort": "Disable import sorting. Runs by default.",
"pty": _PTY_HELP_DESC,
}
)
def fmt(
ctx: Context,
path: str = ".",
sort_: bool = True,
check: bool = False,
exclude: str | None = None,
pty: bool = True,
):
"""
Run code formatter.
"""
if sort_:
sort(ctx, path, check=check, exclude=exclude, pty=pty)
cmds = ["black", path]
if check:
cmds.append("--check")
if exclude:
cmds.extend(["--exclude", exclude])
ctx.run(" ".join(cmds), echo=True, pty=pty)
@invoke.task(
help={
"path": _PATH_HELP_DESC,
"fix": "Attempt to automatically fix lint violations.",
"watch": "Run in watch mode by re-running whenever files change.",
"pty": _PTY_HELP_DESC,
}
)
def lint(
ctx: Context,
path: str = ".",
fix: bool = False,
watch: bool = False,
pty: bool = True,
):
"""Run formatter (black) and linter (ruff)"""
fmt(ctx, path, check=not fix, pty=pty)
# Run code linter (ruff)
cmds = ["ruff", path]
if fix:
cmds.append("--fix")
if watch:
cmds.append("--watch")
ctx.run(" ".join(cmds), echo=True, pty=pty)
@invoke.task(help={"path": _PATH_HELP_DESC})
def fix(ctx: Context, path: str = "."):
"""Automatically fix all possible code issues."""
lint(ctx, path=path, fix=True)
fmt(ctx, path=path, sort_=True)
@invoke.task(help={"path": _PATH_HELP_DESC})
def upgrade(ctx: Context, path: str = "."):
"""Run code syntax upgrades."""
cmds = ["ruff", path, "--select", "UP", "--fix"]
ctx.run(" ".join(cmds), echo=True, pty=True)
@invoke.task(
help={
"all_files": "Run hooks against all files, not just the current changes.",
"diff": "Show the diff of changes on hook failure.",
"sync": "Re-install the latest git hooks.",
}
)
def hooks(
ctx: Context, all_files: bool = False, diff: bool = False, sync: bool = False
):
"""Run and manage pre-commit hooks."""
cmds = ["pre-commit", "run"]
if diff:
cmds.append("--show-diff-on-failure")
if all_files:
cmds.extend(["--all-files"])
else:
# used in CI - runs faster and only checks files that have changed
cmds.extend(["--from-ref", "origin/HEAD", "--to-ref", "HEAD"])
ctx.run(" ".join(cmds))
if sync:
print(" Re-installing hooks ...")
ctx.run(" ".join(["pre-commit", "uninstall"]), echo=True)
ctx.run(" ".join(["pre-commit", "install"]), echo=True)
@invoke.task(aliases=("docstring",), iterable=("paths",))
def docstrings(ctx: Context, paths: list[str] | None = None):
"""
Check public API docstrings.
Optionally pass a directory or file.
To pass multiple items:
invoke docstrings -p=great_expectations/core -p=great_expectations/util.py
"""
if paths:
select_paths = [pathlib.Path(p) for p in paths]
else:
select_paths = None
try:
check_public_api_docstrings.main(select_paths=select_paths)
except AssertionError as err:
raise invoke.Exit(
message=f"{err}\n\nGenerated with {check_public_api_docstrings.__file__}",
code=1,
)
@invoke.task(
aliases=["types"],
iterable=["packages"],
help={
"packages": "One or more `great_expectatations` sub-packages to type-check with mypy.",
"install-types": "Automatically install any needed types from `typeshed`.",
"daemon": "Run mypy in daemon mode with faster analysis."
" The daemon will be started and re-used for subsequent calls."
" For detailed usage see `dmypy --help`.",
"clear-cache": "Clear the local mypy cache directory.",
"check-stub-sources": "Check the implementation `.py` files for any `.pyi`"
" stub files in `great_expectations`."
" By default `mypy` will not check implementation files if a `.pyi` stub file exists."
" This should be run in CI in addition to the normal type-checking step.",
"python-version": "Type check as if running a specific python version. Default 3.8",
},
)
def type_check(
ctx: Context,
packages: list[str],
install_types: bool = False,
pretty: bool = False,
warn_unused_ignores: bool = False,
daemon: bool = False,
clear_cache: bool = False,
report: bool = False,
check_stub_sources: bool = False,
ci: bool = False,
python_version: str = "3.8",
):
"""Run mypy static type-checking on select packages."""
mypy_cache = pathlib.Path(".mypy_cache")
if ci:
mypy_cache.mkdir(exist_ok=True)
print(f" mypy cache {mypy_cache.absolute()}")
type_check(
ctx,
packages,
install_types=True,
pretty=pretty,
warn_unused_ignores=True,
daemon=daemon,
clear_cache=clear_cache,
report=report,
check_stub_sources=check_stub_sources,
ci=False,
python_version=python_version,
)
return # don't run twice
if clear_cache:
print(f" Clearing {mypy_cache} ... ", end="")
try:
shutil.rmtree(mypy_cache)
print("✅"),
except FileNotFoundError as exc:
print(f"❌\n {exc}")
if daemon:
bin = "dmypy run --"
else:
bin = "mypy"
ge_pkgs = [f"great_expectations.{p}" for p in packages]
if check_stub_sources:
# see --help docs for explanation of this flag
for stub_file in GX_ROOT_DIR.glob("**/*.pyi"):
source_file = stub_file.with_name( # TODO:py3.9 .with_stem()
f"{stub_file.name[:-1]}"
)
relative_path = source_file.relative_to(GX_ROOT_DIR.parent)
ge_pkgs.append(str(relative_path))
cmds = [
bin,
*ge_pkgs,
]
if install_types:
cmds.extend(["--install-types", "--non-interactive"])
if daemon:
# see related issue https://github.com/python/mypy/issues/9475
cmds.extend(["--follow-imports=normal"])
if report:
cmds.extend(["--txt-report", "type_cov", "--html-report", "type_cov"])
if pretty:
cmds.extend(["--pretty"])
if warn_unused_ignores:
cmds.extend(["--warn-unused-ignores"])
if python_version:
cmds.extend(["--python-version", python_version])
# use pseudo-terminal for colorized output
ctx.run(" ".join(cmds), echo=True, pty=True)
@invoke.task(aliases=["get-stats"])
def get_usage_stats_json(ctx: Context):
"""
Dump usage stats event examples to json file
"""
if not is_ge_installed:
raise invoke.Exit(
message="This invoke task requires Great Expecations to be installed in the environment. Please try again.",
code=1,
)
events = usage_stats_utils.get_usage_stats_example_events()
version = usage_stats_utils.get_gx_version()
outfile = f"v{version}_example_events.json"
with open(outfile, "w") as f:
json.dump(events, f)
print(f"File written to '{outfile}'.")
@invoke.task(pre=[get_usage_stats_json], aliases=["move-stats"])
def mv_usage_stats_json(ctx: Context):
"""
Use databricks-cli lib to move usage stats event examples to dbfs:/
"""
version = usage_stats_utils.get_gx_version()
outfile = f"v{version}_example_events.json"
cmd = "databricks fs cp --overwrite {0} dbfs:/schemas/{0}"
cmd = cmd.format(outfile)
ctx.run(cmd)
print(f"'{outfile}' copied to dbfs.")
UNIT_TEST_DEFAULT_TIMEOUT: float = 2.0
@invoke.task(
aliases=["test"],
help={
"unit": "Runs tests marked with the 'unit' marker. Default behavior.",
"integration": "Runs integration tests and exclude unit-tests. By default only unit tests are run.",
"ignore-markers": "Don't exclude any test by not passing any markers to pytest.",
"slowest": "Report on the slowest n number of tests",
"ci": "execute tests assuming a CI environment. Publish XML reports for coverage reporting etc.",
"timeout": f"Fails unit-tests if calls take longer than this value. Default {UNIT_TEST_DEFAULT_TIMEOUT} seconds",
"html": "Create html coverage report",
"package": "Run tests on a specific package. Assumes there is a `tests/<PACKAGE>` directory of the same name.",
"full-cov": "Show coverage report on the entire `great_expectations` package regardless of `--package` param.",
},
)
def tests(
ctx: Context,
unit: bool = True,
integration: bool = False,
ignore_markers: bool = False,
ci: bool = False,
html: bool = False,
cloud: bool = True,
slowest: int = 5,
timeout: float = UNIT_TEST_DEFAULT_TIMEOUT,
package: str | None = None,
full_cov: bool = False,
):
"""
Run tests. Runs unit tests by default.
Use `invoke tests -p=<TARGET_PACKAGE>` to run tests on a particular package and measure coverage (or lack thereof).
"""
markers = []
if integration:
markers += ["integration"]
unit = False
markers += ["unit" if unit else "not unit"]
marker_text = " and ".join(markers)
cov_param = "--cov=great_expectations"
if package and not full_cov:
cov_param += f"/{package.replace('.', '/')}"
cmds = [
"pytest",
f"--durations={slowest}",
cov_param,
"--cov-report term",
"-vv",
]
if not ignore_markers:
cmds += ["-m", f"'{marker_text}'"]
if unit and not ignore_markers:
try:
import pytest_timeout # noqa: F401
cmds += [f"--timeout={timeout}"]
except ImportError:
print("`pytest-timeout` is not installed, cannot use --timeout")
if cloud:
cmds += ["--cloud"]
if ci:
cmds += ["--cov-report", "xml"]
if html:
cmds += ["--cov-report", "html"]
if package:
cmds += [f"tests/{package.replace('.', '/')}"] # allow `foo.bar`` format
ctx.run(" ".join(cmds), echo=True, pty=True)
PYTHON_VERSION_DEFAULT: float = 3.8
@invoke.task(
help={
"name": "Docker image name.",
"tag": "Docker image tag.",
"build": "If True build the image, otherwise run it. Defaults to False.",
"detach": "Run container in background and print container ID. Defaults to False.",
"py": f"version of python to use. Default is {PYTHON_VERSION_DEFAULT}",
"cmd": "Command for docker image. Default is bash.",
"target": "Set the target build stage to build.",
}
)
def docker(
ctx: Context,
name: str = "gx38local",
tag: str = "latest",
build: bool = False,
detach: bool = False,
cmd: str = "bash",
py: float = PYTHON_VERSION_DEFAULT,
target: str | None = None,
):
"""
Build or run gx docker image.
"""
_exit_with_error_if_not_in_repo_root(task_name="docker")
filedir = os.path.realpath(
os.path.dirname(os.path.realpath(__file__)) # noqa: PTH120
)
cmds = ["docker"]
if build:
cmds.extend(
[
"buildx",
"build",
"-f",
"docker/Dockerfile.tests",
f"--tag {name}:{tag}",
*[
f"--build-arg {arg}"
for arg in ["SOURCE=local", f"PYTHON_VERSION={py}"]
],
".",
]
)
if target:
cmds.extend(["--target", target])
else:
cmds.append("run")
if detach:
cmds.append("--detach")
cmds.extend(
[
"-it",
"--rm",
"--mount",
f"type=bind,source={filedir},target=/great_expectations",
"-w",
"/great_expectations",
f"{name}:{tag}",
f"{cmd}",
]
)
ctx.run(" ".join(cmds), echo=True, pty=True)
@invoke.task(
aliases=("schema", "schemas"),
help={
"sync": "Update the json schemas at `great_expectations/datasource/fluent/schemas`",
"indent": "Indent size for nested json objects. Default: 4",
"clean": "Delete all schema files and sub directories."
" Can be combined with `--sync` to reset the /schemas dir and remove stale schemas",
},
)
def type_schema(
ctx: Context,
sync: bool = False,
clean: bool = False,
indent: int = 4,
):
"""
Show all the json schemas for Fluent Datasources & DataAssets
Generate json schema for each Datasource & DataAsset with `--sync`.
"""
import pandas
from great_expectations.datasource.fluent import (
_PANDAS_SCHEMA_VERSION,
BatchRequest,
Datasource,
)
from great_expectations.datasource.fluent.sources import (
_iter_all_registered_types,
)
schema_dir_root: Final[pathlib.Path] = (
GX_ROOT_DIR / "datasource" / "fluent" / "schemas"
)
if clean:
file_count = len(list(schema_dir_root.glob("**/*.json")))
print(f"🗑️ removing schema directory and contents - {file_count} .json files")
shutil.rmtree(schema_dir_root)
schema_dir_root.mkdir(exist_ok=True)
datasource_dir: pathlib.Path = schema_dir_root
if not sync:
print("--------------------\nRegistered Fluent types\n--------------------\n")
name_model = [
("BatchRequest", BatchRequest),
(Datasource.__name__, Datasource),
*_iter_all_registered_types(),
]
for name, model in name_model:
if issubclass(model, Datasource):
datasource_dir = schema_dir_root.joinpath(model.__name__)
datasource_dir.mkdir(exist_ok=True)
schema_dir = schema_dir_root
print("-" * shutil.get_terminal_size()[0])
else:
schema_dir = datasource_dir
print(" ", end="")
if not sync:
print(f"{name} - {model.__name__}.json")
continue
if (
datasource_dir.name.startswith("Pandas")
and _PANDAS_SCHEMA_VERSION != pandas.__version__
):
print(
f"🙈 {name} - was generated with pandas"
f" {_PANDAS_SCHEMA_VERSION} but you have {pandas.__version__}; skipping"
)
continue
try:
schema_path = schema_dir.joinpath(f"{model.__name__}.json")
json_str: str = model.schema_json(indent=indent) + "\n"
if schema_path.exists():
if json_str == schema_path.read_text():
print(f"✅ {name} - {schema_path.name} unchanged")
continue
schema_path.write_text(json_str)
print(f"🔃 {name} - {schema_path.name} schema updated")
except TypeError as err:
print(f"❌ {name} - Could not sync schema - {type(err).__name__}:{err}")
raise invoke.Exit(code=0)
def _exit_with_error_if_not_in_repo_root(task_name: str):
"""Exit if the command was not run from the repository root."""
filedir = os.path.realpath(
os.path.dirname(os.path.realpath(__file__)) # noqa: PTH120
)
curdir = os.path.realpath(os.getcwd()) # noqa: PTH109
exit_message = f"The {task_name} task must be invoked from the same directory as the tasks.py file at the top of the repo."
if filedir != curdir:
raise invoke.Exit(
exit_message,
code=1,
)
@invoke.task
def api_docs(ctx: Context):
"""Build api documentation."""
repo_root = pathlib.Path(__file__).parent
_exit_with_error_if_not_run_from_correct_dir(
task_name="docs", correct_dir=repo_root
)
sphinx_api_docs_source_dir = repo_root / "docs" / "sphinx_api_docs_source"
doc_builder = SphinxInvokeDocsBuilder(
ctx=ctx, api_docs_source_path=sphinx_api_docs_source_dir, repo_root=repo_root
)
doc_builder.build_docs()
@invoke.task(
name="docs",
help={
"build": "Build docs via yarn build instead of serve via yarn start. Default False.",
"clean": "Remove directories and files from versioned docs and code. Default False.",
"start": "Only run yarn start, do not process versions. For example if you have already run invoke docs and just want to serve docs locally for editing.",
"lint": "Run the linter",
},
)
def docs(
ctx: Context,
build: bool = False,
clean: bool = False,
start: bool = False,
lint: bool = False,
):
"""Build documentation site, including api documentation and earlier doc versions. Note: Internet access required to download earlier versions."""
repo_root = pathlib.Path(__file__).parent
_exit_with_error_if_not_run_from_correct_dir(
task_name="docs", correct_dir=repo_root
)
print("Running invoke docs from:", repo_root)
old_pwd = pathlib.Path.cwd()
docusaurus_dir = repo_root / "docs/docusaurus"
os.chdir(docusaurus_dir)
if clean:
rm_cmds = ["rm", "-f", "oss_docs_versions.zip", "versions.json"]
ctx.run(" ".join(rm_cmds), echo=True)
rm_rf_cmds = [
"rm",
"-rf",
"versioned_code",
"versioned_docs",
"versioned_sidebars",
]
ctx.run(" ".join(rm_rf_cmds), echo=True)
elif lint:
ctx.run(" ".join(["yarn lint"]), echo=True)
else:
if start:
ctx.run(" ".join(["yarn start"]), echo=True)
else:
print("Making sure docusaurus dependencies are installed.")
ctx.run(" ".join(["yarn install"]), echo=True)
if build:
build_docs_cmd = "../build_docs"
else:
build_docs_cmd = "../build_docs_locally.sh"
print(f"Running {build_docs_cmd} from:", docusaurus_dir)
ctx.run(build_docs_cmd, echo=True)
os.chdir(old_pwd)
@invoke.task(
name="public-api",
help={
"write_to_file": "Write items to be addressed to public_api_report.txt, default False",
},
)
def public_api_task(
ctx: Context,
write_to_file: bool = False,
):
"""Generate a report to determine the state of our Public API. Lists classes, methods and functions that are used in examples in our documentation, and any manual includes or excludes (see public_api_report.py). Items listed when generating this report need the @public_api decorator (and a good docstring) or to be excluded from consideration if they are not applicable to our Public API."""
repo_root = pathlib.Path(__file__).parent
_exit_with_error_if_not_run_from_correct_dir(
task_name="public-api", correct_dir=repo_root
)
# Docs folder is not reachable from install of Great Expectations
api_docs_dir = repo_root / "docs" / "sphinx_api_docs_source"
sys.path.append(str(api_docs_dir.resolve()))
public_api_report.generate_public_api_report(write_to_file=write_to_file)
def _exit_with_error_if_not_run_from_correct_dir(
task_name: str, correct_dir: Union[pathlib.Path, None] = None
) -> None:
"""Exit if the command was not run from the correct directory."""
if not correct_dir:
correct_dir = pathlib.Path(__file__).parent
curdir = pathlib.Path.cwd()
exit_message = f"The {task_name} task must be invoked from the same directory as the tasks.py file."
if correct_dir != curdir:
raise invoke.Exit(
exit_message,
code=1,
)
@invoke.task(
aliases=("links",),
help={"skip_external": "Skip external link checks (is slow), default is True"},
)
def link_checker(ctx: Context, skip_external: bool = True):
"""Checks the Docusaurus docs for broken links"""
import docs.checks.docs_link_checker as checker
path: str = "docs/docusaurus/docs"
docs_root: str = "docs/docusaurus/docs"
site_prefix: str = "docs"
code, message = checker.scan_docs(
path=path,
docs_root=docs_root,
site_prefix=site_prefix,
skip_external=skip_external,
)
raise invoke.Exit(message, code)