-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example python lib w click - with resources, project
- Loading branch information
1 parent
4456db9
commit 055fded
Showing
2 changed files
with
31 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,47 @@ | ||
import click | ||
import os | ||
import sys | ||
from typing import Optional | ||
|
||
from dbt.cli.flags import Flags | ||
from dbt.cli.main import cli as dbt, deps | ||
from dbt.events.functions import setup_event_logger | ||
from dbt.tracking import initialize_from_flags, track_run | ||
from dbt.adapters.factory import adapter_management | ||
from dbt.profiler import profiler | ||
from dbt.config.runtime import load_project | ||
|
||
|
||
def make_context(args): | ||
def make_context(args, command=dbt) -> Optional[click.Context]: | ||
try: | ||
ctx = dbt.make_context('dbt', args) | ||
ctx = command.make_context(command.name, args) | ||
except click.exceptions.Exit: | ||
exit() | ||
return | ||
|
||
ctx.invoked_subcommand = ctx.protected_args[0] if ctx.protected_args else None | ||
ctx.obj = {} | ||
|
||
return ctx | ||
|
||
# python core/dbt/cli/example.py | ||
# python core/dbt/cli/example.py --version | ||
# python core/dbt/cli/example.py deps --project-dir <project-dir-path> | ||
# python core/dbt/cli/example.py run --project-dir <project-dir-path> | ||
if __name__ == "__main__": | ||
ctx = make_context(sys.argv[1:]) | ||
dbt.invoke(ctx) | ||
cli_args = sys.argv[1:] | ||
|
||
# Use cli group to configure context + call arbitrary command | ||
ctx = make_context(cli_args) | ||
if ctx: | ||
dbt.invoke(ctx) | ||
|
||
# Skips group-level context setting | ||
# dbt.commands['deps'].invoke(ctx) | ||
# deps.invoke(ctx) | ||
# Bypass cli group context configuration entirely and invoke deps directly | ||
# Note: This only really works because of the prior global initializations (logging, tracking) from dbt.invoke(ctx) | ||
click.echo("\n`dbt deps` called") | ||
ctx_deps = make_context([], deps) | ||
ctx_deps.with_resource(track_run(run_command='deps')) | ||
ctx_deps.with_resource(adapter_management()) | ||
ctx_deps.with_resource(profiler(enable=True, outfile='output.profile')) | ||
profile_dir_override = os.path.expanduser("~/src/jaffle_shop") | ||
ctx_deps.obj['project'] = load_project(profile_dir_override, True, None, None) | ||
deps.invoke(ctx_deps) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters