Skip to content

Commit

Permalink
WIP, need to fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenyuLInx committed Feb 8, 2023
1 parent a6e1e27 commit ea716ec
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 25 deletions.
31 changes: 16 additions & 15 deletions dbt_rpc/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
FailedToConnectError
)
from dbt.config.utils import parse_cli_vars
import dbt.flags as flags

from dbt.flags import get_flags, set_from_args
from dbt_rpc.task.server import RPCServerTask


def initialize_tracking_from_flags():
# NOTE: this is copied from dbt-core
# Setting these used to be in UserConfig, but had to be moved here
flags = get_flags()
if flags.SEND_ANONYMOUS_USAGE_STATS:
dbt.tracking.initialize_tracking(flags.PROFILES_DIR)
else:
Expand Down Expand Up @@ -170,14 +170,10 @@ def adapter_management():
def handle_and_check(args):
with log_manager.applicationbound():
parsed = parse_args(args)

# Set flags from args, user config, and env vars
user_config = read_user_config(flags.PROFILES_DIR) # This is read again later
flags.set_from_args(parsed, user_config)
initialize_tracking_from_flags()
# Set log_format from flags
parsed.cls.set_log_format()

flags = get_flags()
# we've parsed the args and set the flags - we can now decide if we're debug or not
if flags.DEBUG:
log_manager.set_debug()
Expand Down Expand Up @@ -570,19 +566,24 @@ def parse_args(args, cls=DBTArgumentParser):
sys.exit(1)

parsed = p.parse_args(args)
from dbt.cli.resolvers import default_profiles_dir
parsed.profiles_dir = default_profiles_dir()
parsed.defer_mode = 'eager'
set_from_args(parsed, None)
flags = get_flags()
# profiles_dir is set before subcommands and after, so normalize
if hasattr(parsed, 'sub_profiles_dir'):
if parsed.sub_profiles_dir is not None:
parsed.profiles_dir = parsed.sub_profiles_dir
delattr(parsed, 'sub_profiles_dir')
if hasattr(parsed, 'profiles_dir'):
if parsed.profiles_dir is None:
parsed.profiles_dir = flags.PROFILES_DIR
else:
parsed.profiles_dir = os.path.abspath(parsed.profiles_dir)
# needs to be set before the other flags, because it's needed to
# read the profile that contains them
flags.PROFILES_DIR = parsed.profiles_dir
# if hasattr(parsed, 'profiles_dir'):
# if parsed.profiles_dir is None:
# parsed.profiles_dir = flags.PROFILES_DIR
# else:
# parsed.profiles_dir = os.path.abspath(parsed.profiles_dir)
# # needs to be set before the other flags, because it's needed to
# # read the profile that contains them
# flags.PROFILES_DIR = parsed.profiles_dir

# version_check is set before subcommands and after, so normalize
if hasattr(parsed, 'sub_version_check'):
Expand Down
7 changes: 4 additions & 3 deletions dbt_rpc/rpc/task_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from dbt.dataclass_schema import dbtClassMixin, ValidationError

import dbt.exceptions
from dbt.flags import env_set_truthy
from dbt.flags import env_set_truthy, get_flags, set_from_args
import dbt.tracking
from dbt.adapters.factory import (
cleanup_connections, load_plugin, register_adapter,
Expand Down Expand Up @@ -74,8 +74,9 @@ def _spawn_setup(self):
user_config = None
if self.task.config is not None:
user_config = self.task.config.user_config
dbt.flags.set_from_args(self.task.args, user_config)
dbt.tracking.initialize_from_flags(dbt.flags.ANONYMOUS_USAGE_STATS, dbt.flags.PROFILES_DIR)
set_from_args(self.task.args, user_config)
flags = get_flags()
dbt.tracking.initialize_from_flags(flags.ANONYMOUS_USAGE_STATS, flags.PROFILES_DIR)
# reload the active plugin
load_plugin(self.task.config.credentials.type)
# register it
Expand Down
1 change: 1 addition & 0 deletions dbt_rpc/rpc/task_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def set_parsing(self) -> bool:
return True

def parse_manifest(self) -> None:
register_adapter(self.config)
self.manifest = ManifestLoader.get_full_manifest(self.config, reset=True)

def set_compile_exception(self, exc, logs=List[LogMessage]) -> None:
Expand Down
2 changes: 1 addition & 1 deletion dbt_rpc/task/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RPCTask(
RemoteManifestMethod[Parameters, RemoteExecutionResult]
):
def __init__(self, args, config, manifest):
super().__init__(args, config)
super().__init__(args, config, manifest)
RemoteManifestMethod.__init__(
self, args, config, manifest # type: ignore
)
Expand Down
13 changes: 7 additions & 6 deletions dbt_rpc/task/project_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path
from typing import List, Optional, Union

from dbt import flags
from dbt.flags import get_flags
from dbt.contracts.graph.manifest import WritableManifest
from dbt_rpc.contracts.rpc import (
GetManifestParameters,
Expand Down Expand Up @@ -65,10 +65,11 @@ def handle_request(self) -> RemoteExecutionResult:


def state_path(state: Optional[str]) -> Optional[Path]:
flags = get_flags()
if state is not None:
return Path(state)
elif flags.ARTIFACT_STATE_PATH is not None:
return Path(flags.ARTIFACT_STATE_PATH)
elif flags.STATE is not None:
return Path(flags.STATE)
else:
return None

Expand Down Expand Up @@ -106,7 +107,7 @@ def set_args(self, params: RPCRunParameters) -> None:
if params.threads is not None:
self.args.threads = params.threads
if params.defer is None:
self.args.defer = flags.DEFER_MODE
self.args.defer = get_flags().DEFER_MODE
else:
self.args.defer = params.defer

Expand Down Expand Up @@ -146,7 +147,7 @@ def set_args(self, params: RPCTestParameters) -> None:
if params.threads is not None:
self.args.threads = params.threads
if params.defer is None:
self.args.defer = flags.DEFER_MODE
self.args.defer = get_flags().DEFER_MODE
else:
self.args.defer = params.defer

Expand Down Expand Up @@ -332,7 +333,7 @@ def set_args(self, params: RPCBuildParameters) -> None:
if params.threads is not None:
self.args.threads = params.threads
if params.defer is None:
self.args.defer = flags.DEFER_MODE
self.args.defer = get_flags().DEFER_MODE
else:
self.args.defer = params.defer

Expand Down
2 changes: 2 additions & 0 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@ def built_schema(project_dir, schema, profiles_dir, project_def):
os.chdir(project_dir)
start = os.getcwd()
try:
from dbt.flags import set_from_args
set_from_args(args, None)
cfg = RuntimeConfig.from_args(args)
finally:
os.chdir(start)
Expand Down

0 comments on commit ea716ec

Please sign in to comment.