Skip to content

Commit

Permalink
switch to using flags.PROFILES_DIR
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Sep 14, 2021
1 parent 962a155 commit e1db970
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 58 deletions.
2 changes: 1 addition & 1 deletion core/dbt/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# all these are just exports, they need "noqa" so flake8 will not complain.
from .profile import Profile, PROFILES_DIR, read_user_config # noqa
from .profile import Profile, read_user_config # noqa
from .project import Project, IsFQNResource # noqa
from .runtime import RuntimeConfig, UnsetProfileConfig # noqa
7 changes: 2 additions & 5 deletions core/dbt/config/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
from .renderer import ProfileRenderer

DEFAULT_THREADS = 1
# This is where PROFILES_DIR is initially set. Override in main.py.

DEFAULT_PROFILES_DIR = os.path.join(os.path.expanduser('~'), '.dbt')
PROFILES_DIR = os.path.expanduser(
os.getenv('DBT_PROFILES_DIR', DEFAULT_PROFILES_DIR)
)

INVALID_PROFILE_MESSAGE = """
dbt encountered an error while trying to read your profiles.yml file.
Expand All @@ -44,7 +41,7 @@
defined in your profiles.yml file. You can find profiles.yml here:
{profiles_file}/profiles.yml
""".format(profiles_file=PROFILES_DIR)
""".format(profiles_file=DEFAULT_PROFILES_DIR)


def read_profile(profiles_dir: str) -> Dict[str, Any]:
Expand Down
36 changes: 7 additions & 29 deletions core/dbt/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
from pathlib import Path
from typing import Optional

# PROFILES_DIR must be set before the other flags
DEFAULT_PROFILES_DIR = os.path.join(os.path.expanduser('~'), '.dbt')
PROFILES_DIR = os.path.expanduser(
os.getenv('DBT_PROFILES_DIR', DEFAULT_PROFILES_DIR)
)

STRICT_MODE = False # Only here for backwards compatibility
FULL_REFRESH = False # subcommand
Expand All @@ -17,7 +22,6 @@
WRITE_JSON = None
PARTIAL_PARSE = None
USE_COLORS = None
PROFILES_DIR = None
DEBUG = None
LOG_FORMAT = None
VERSION_CHECK = None
Expand All @@ -34,7 +38,7 @@
"WRITE_JSON": True,
"PARTIAL_PARSE": False,
"USE_COLORS": True,
"PROFILES_DIR": None,
"PROFILES_DIR": DEFAULT_PROFILES_DIR,
"DEBUG": False,
"LOG_FORMAT": None,
"VERSION_CHECK": True,
Expand Down Expand Up @@ -101,8 +105,6 @@ def set_from_args(args, user_config):
WRITE_JSON = get_flag_value('WRITE_JSON', args, user_config)
PARTIAL_PARSE = get_flag_value('PARTIAL_PARSE', args, user_config)
USE_COLORS = get_flag_value('USE_COLORS', args, user_config)
if hasattr(args, 'profiles_dir'): # Should always exist except for tests
PROFILES_DIR = args.profiles_dir # special case
DEBUG = get_flag_value('DEBUG', args, user_config)
LOG_FORMAT = get_flag_value('LOG_FORMAT', args, user_config)
VERSION_CHECK = get_flag_value('VERSION_CHECK', args, user_config)
Expand All @@ -121,7 +123,7 @@ def get_flag_value(flag, args, user_config):
if env_value is not None and env_value != '':
env_value = env_value.lower()
# non Boolean values
if flag in ['LOG_FORMAT', 'PROFILES_DIR', 'PRINTER_WIDTH']:
if flag in ['LOG_FORMAT', 'PRINTER_WIDTH']:
flag_value = env_value
else:
flag_value = env_set_bool(env_value)
Expand Down Expand Up @@ -150,27 +152,3 @@ def get_flag_dict():
"send_anonymous_usage_stats": SEND_ANONYMOUS_USAGE_STATS,
"printer_width": PRINTER_WIDTH,
}


# This method isn't currently used, but leaving here in case it's useful for tests
def reset():
global STRICT_MODE, FULL_REFRESH, WARN_ERROR, \
USE_EXPERIMENTAL_PARSER, WRITE_JSON, PARTIAL_PARSE, USE_COLORS, \
STORE_FAILURES, DEBUG, LOG_FORMAT,\
VERSION_CHECK, FAIL_FAST, SEND_ANONYMOUS_USAGE_STATS, PRINTER_WIDTH

STRICT_MODE = False # Only here for backwards compatibility
FULL_REFRESH = False # subcommand
STORE_FAILURES = False # subcommand

USE_EXPERIMENTAL_PARSER = flag_defaults['USE_EXPERIMENTAL_PARSER']
WARN_ERROR = flag_defaults['WARN_ERROR']
WRITE_JSON = flag_defaults['WRITE_JSON']
PARTIAL_PARSE = flag_defaults['PARTIAL_PARSE']
USE_COLORS = flag_defaults['USE_COLORS']
DEBUG = flag_defaults['DEBUG']
LOG_FORMAT = flag_defaults['LOG_FORMAT']
VERSION_CHECK = flag_defaults['VERSION_CHECK']
FAIL_FAST = flag_defaults['FAIL_FAST']
SEND_ANONYMOUS_USAGE_STATS = flag_defaults['SEND_ANONYMOUS_USAGE_STATS']
PRINTER_WIDTH = flag_defaults['PRINTER_WIDTH']
13 changes: 8 additions & 5 deletions core/dbt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import dbt.tracking

from dbt.utils import ExitCodes
from dbt.config import PROFILES_DIR, read_user_config
from dbt.config.profile import DEFAULT_PROFILES_DIR, read_user_config
from dbt.exceptions import RuntimeException, InternalException


Expand Down Expand Up @@ -276,7 +276,7 @@ def _build_base_subparser():
type=str,
help='''
Which directory to look in for the profiles.yml file. Default = {}
'''.format(PROFILES_DIR)
'''.format(DEFAULT_PROFILES_DIR)
)

base_subparser.add_argument(
Expand Down Expand Up @@ -1029,12 +1029,12 @@ def parse_args(args, cls=DBTArgumentParser):

p.add_argument(
'--profiles-dir',
default=PROFILES_DIR,
default=None,
dest='profiles_dir',
type=str,
help='''
Which directory to look in for the profiles.yml file. Default = {}
'''.format(PROFILES_DIR)
'''.format(DEFAULT_PROFILES_DIR)
)

p.add_argument(
Expand Down Expand Up @@ -1110,8 +1110,11 @@ def parse_args(args, cls=DBTArgumentParser):
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 hasattr(parsed, 'profiles_dir') and parsed.profiles_dir is not None:
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
4 changes: 2 additions & 2 deletions core/dbt/task/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from dbt.adapters.factory import register_adapter
from dbt.config import RuntimeConfig, Project
from dbt.config.profile import read_profile, PROFILES_DIR
from dbt.config.profile import read_profile
import dbt.exceptions


Expand All @@ -35,7 +35,7 @@ def from_args(cls, args):
def read_profiles(profiles_dir=None):
"""This is only used for some error handling"""
if profiles_dir is None:
profiles_dir = PROFILES_DIR
profiles_dir = flags.PROFILES_DIR

raw_profiles = read_profile(profiles_dir)

Expand Down
4 changes: 2 additions & 2 deletions core/dbt/task/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import dbt.clients.system
import dbt.exceptions
from dbt.adapters.factory import get_adapter, register_adapter
from dbt.config import Project, Profile, PROFILES_DIR
from dbt.config import Project, Profile
from dbt.config.renderer import DbtProjectYamlRenderer, ProfileRenderer
from dbt.config.utils import parse_cli_vars
from dbt.context.base import generate_base_context
Expand Down Expand Up @@ -70,7 +70,7 @@ def __init__(self, *args, **kwargs):
class DebugTask(BaseTask):
def __init__(self, args, config):
super().__init__(args, config)
self.profiles_dir = getattr(self.args, 'profiles_dir', PROFILES_DIR)
self.profiles_dir = flags.PROFILES_DIR
self.profile_path = os.path.join(self.profiles_dir, 'profiles.yml')
try:
self.project_dir = get_nearest_project_dir(self.args)
Expand Down
3 changes: 2 additions & 1 deletion core/dbt/task/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import dbt.config
import dbt.clients.system
from dbt import flags
from dbt.version import _get_adapter_plugin_names
from dbt.adapters.factory import load_plugin, get_include_paths

Expand Down Expand Up @@ -93,7 +94,7 @@ def run(self):
except StopIteration:
logger.debug("No adapters installed, skipping")

profiles_dir = dbt.config.PROFILES_DIR
profiles_dir = flags.PROFILES_DIR
profiles_file = os.path.join(profiles_dir, 'profiles.yml')

self.create_profiles_dir(profiles_dir)
Expand Down
14 changes: 1 addition & 13 deletions test/unit/test_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .utils import normalize
from dbt import flags
from dbt.contracts.project import UserConfig
from dbt.config.profile import PROFILES_DIR
from dbt.config.profile import DEFAULT_PROFILES_DIR


class TestFlags(TestCase):
Expand Down Expand Up @@ -91,18 +91,6 @@ def test__flags(self):
os.environ.pop('DBT_USE_COLORS')
delattr(self.args, 'use_colors')

# profiles_dir
setattr(self.args, 'profiles_dir', PROFILES_DIR)
os.environ['DBT_PROFILES_DIR'] = '/user/jane/.dbt'
flags.set_from_args(self.args, self.user_config)
self.assertRegex(flags.PROFILES_DIR, r'.dbt')
setattr(self.args, 'profiles_dir', normalize('/user/joe/dbt-profiles'))
flags.set_from_args(self.args, self.user_config)
self.assertEqual(flags.PROFILES_DIR, normalize('/user/joe/dbt-profiles'))
# cleanup
os.environ.pop('DBT_PROFILES_DIR')
delattr(self.args, 'profiles_dir')

# debug
self.user_config.debug = True
flags.set_from_args(self.args, self.user_config)
Expand Down

0 comments on commit e1db970

Please sign in to comment.