Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add environment variables for macro debugging flags (#1628) #1629

Merged
merged 1 commit into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions core/dbt/clients/jinja.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import codecs
import linecache
import os
import tempfile

import jinja2
import jinja2._compat
Expand All @@ -18,6 +19,34 @@
from dbt.logger import GLOBAL_LOGGER as logger # noqa


def _linecache_inject(source, write):
if write:
# this is the only reliable way to accomplish this. Obviously, it's
# really darn noisy and will fill your temporary directory
tmp_file = tempfile.NamedTemporaryFile(
prefix='dbt-macro-compiled-',
suffix='.py',
delete=False,
mode='w+',
encoding='utf-8',
)
tmp_file.write(source)
filename = tmp_file.name
else:
filename = codecs.encode(os.urandom(12), 'hex').decode('ascii')

# encode, though I don't think this matters
filename = jinja2._compat.encode_filename(filename)
# put ourselves in the cache
linecache.cache[filename] = (
len(source),
None,
[line + '\n' for line in source.splitlines()],
filename
)
return filename


class MacroFuzzParser(jinja2.parser.Parser):
def parse_macro(self):
node = jinja2.nodes.Macro(lineno=next(self.stream).lineno)
Expand All @@ -43,22 +72,16 @@ def _parse(self, source, name, filename):

def _compile(self, source, filename):
"""Override jinja's compilation to stash the rendered source inside
the python linecache for debugging.
the python linecache for debugging when the appropriate environment
variable is set.

If the value is 'write', also write the files to disk.
WARNING: This can write a ton of data if you aren't careful.
"""
if filename == '<template>':
# make a better filename
filename = 'dbt-{}'.format(
codecs.encode(os.urandom(12), 'hex').decode('ascii')
)
# encode, though I don't think this matters
filename = jinja2._compat.encode_filename(filename)
# put ourselves in the cache
linecache.cache[filename] = (
len(source),
None,
[line + '\n' for line in source.splitlines()],
filename
)
macro_compile = os.environ.get('DBT_MACRO_DEBUGGING')
if filename == '<template>' and macro_compile:
write = macro_compile == 'write'
filename = _linecache_inject(source, write)

return super(MacroFuzzEnvironment, self)._compile(source, filename)

Expand Down
9 changes: 9 additions & 0 deletions core/dbt/context/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ def call(name):
return call


def _debug_here():
import sys
import ipdb
frame = sys._getframe(3)
ipdb.set_trace(frame)


def _add_sql_handlers(context):
sql_results = {}
return dbt.utils.merge(context, {
Expand Down Expand Up @@ -415,6 +422,8 @@ def generate_base(model, model_dict, config, manifest, source_config,
"target": target,
"try_or_compiler_error": try_or_compiler_error(model)
})
if os.environ.get('DBT_MACRO_DEBUGGING'):
context['debug'] = _debug_here

return context

Expand Down