Skip to content

Commit

Permalink
squelch conda output, make silent mode (-a) work better
Browse files Browse the repository at this point in the history
  • Loading branch information
msarahan committed Nov 15, 2018
1 parent 9601554 commit ef1d3f9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 31 deletions.
28 changes: 16 additions & 12 deletions conda_build/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,22 +394,24 @@ def update_index(dir_paths, config=None, force=False, check_md5=False, remove=Fa
subdirs=ensure_list(subdir))


def debug(recipe_or_package_path_or_metadata_tuples, path=None, test=False, output_id=None, config=None, **kwargs):
def debug(recipe_or_package_path_or_metadata_tuples, path=None, test=False, output_id=None, config=None,
verbose=True, **kwargs):
"""Set up either build/host or test environments, leaving you with a quick tool to debug
your package's build or test phase.
"""
from fnmatch import fnmatch
import logging
import os
import time
from conda_build.conda_interface import string_types
from conda_build.build import test as run_test, build as run_build
from conda_build.utils import CONDA_TARBALL_EXTENSIONS, on_win
from conda_build.utils import CONDA_TARBALL_EXTENSIONS, on_win, LoggingContext
is_package = False
default_config = get_or_merge_config(config, **kwargs)
args = {"set_build_id": False}
if not path:
path = os.path.join(default_config.croot, "debug_{}".format(int(time.time() * 1000)))
config = get_or_merge_config(config=default_config, croot=path, **args)
config = get_or_merge_config(config=default_config, croot=path, verbose=verbose, **args)

metadata_tuples = []

Expand All @@ -428,7 +430,7 @@ def debug(recipe_or_package_path_or_metadata_tuples, path=None, test=False, outp
outputs = get_output_file_paths(metadata_tuples)
matched_outputs = outputs
if output_id:
matched_outputs = [_ for _ in outputs if fnmatch(os.path.basename(_).rsplit("-", 2)[0], output_id)]
matched_outputs = [_ for _ in outputs if fnmatch(os.path.basename(_), output_id)]
if len(matched_outputs) > 1:
raise ValueError("Specified --output-id matches more than one output ({}). Please refine your output id so that only "
"a single output is found.".format(matched_outputs))
Expand All @@ -443,16 +445,20 @@ def debug(recipe_or_package_path_or_metadata_tuples, path=None, test=False, outp
target_metadata = metadata_tuples[outputs.index(matched_outputs[0])][0]

ext = ".bat" if on_win else ".sh"

if verbose:
log_context = LoggingContext()
else:
log_context = LoggingContext(logging.CRITICAL + 1)

if not test:
run_build(target_metadata, stats={}, provision_only=True)
with log_context:
run_build(target_metadata, stats={}, provision_only=True)
activation_file = "build_env_setup" + ext
print("#" * 80)
print("Build and/or host environments created for debugging. To enter a debugging environment:\n")
activation_string = "cd {work_dir} && {source} {activation_file}\n".format(
work_dir=target_metadata.config.work_dir,
source="call" if on_win else "source",
activation_file=os.path.join(target_metadata.config.work_dir, activation_file))
print(activation_string)
else:
if not is_package:
raise ValueError("Debugging for test mode is only supported for package files that already exist. "
Expand All @@ -461,13 +467,11 @@ def debug(recipe_or_package_path_or_metadata_tuples, path=None, test=False, outp
test_input = recipe_or_package_path_or_metadata_tuples
# use the package to create an env and extract the test files. Stop short of running the tests.
# tell people what steps to take next
run_test(test_input, config=config, stats={}, provision_only=True)
with log_context:
run_test(test_input, config=config, stats={}, provision_only=True)
activation_file = os.path.join(config.test_dir, "conda_test_env_vars" + ext)
print("#" * 80)
print("Test environment created for debugging. To enter a debugging environment:\n")
activation_string = "cd {work_dir} && {source} {activation_file}\n".format(
work_dir=config.test_dir,
source="call" if on_win else "source",
activation_file=os.path.join(config.test_dir, activation_file))
print(activation_string)
return activation_string
6 changes: 4 additions & 2 deletions conda_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1432,9 +1432,11 @@ def build(m, stats, post=None, need_source_download=True, need_reparse_in_env=Fa
# dependening on the source.
src_dir = m.config.work_dir
if isdir(src_dir):
print("source tree in:", src_dir)
if m.config.verbose:
print("source tree in:", src_dir)
else:
print("no source - creating empty work folder")
if m.config.verbose:
print("no source - creating empty work folder")
os.makedirs(src_dir)

utils.rm_rf(m.config.info_dir)
Expand Down
25 changes: 16 additions & 9 deletions conda_build/cli/main_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,15 @@ def parse_args(args):
'recipe_or_package_file_path',
help="Path to recipe directory or package file to use for dependency and source information",
)
p.add_argument("-t", "--test", action="store_true",
help=("Generate debugging env for test environment, rather than build-time. Requires a"
" package (not a recipe) as input."))
p.add_argument("-p", "--path",
help=("root path in which to place envs, source and activation script. Defaults to a "
"standard conda-build work folder (packagename_timestamp) in your conda-bld folder."))
p.add_argument("-o", "--output-id",
help=("fnmatch pattern that is associated with the output that you want to create an env for. "
"Must match only one file, as we don't support creating envs for more than one output at a time. "
"The top-level recipe can be specified by passing 'TOPLEVEL' here"))
p.add_argument("-a", "--activate-path-only", action="store_true",
help="Output only the path to the generated activation script. Use this for creating envs in scripted "
p.add_argument("-a", "--activate-string-only", action="store_true",
help="Output only the string to the used generated activation script. Use this for creating envs in scripted "
"environments.")

# cut out some args from render that don't make sense here
Expand All @@ -59,15 +56,25 @@ def parse_args(args):

def execute(args):
p, _args = parse_args(args)
test = False

try:
if not any(os.path.splitext(_args.recipe_or_package_file_path)[1] in ext for ext in CONDA_TARBALL_EXTENSIONS):
if _args.test:
raise ValueError("Error: debugging for test mode is only supported for package files that already exist. "
"Please build your package first, then use it to create the debugging environment.")
# --output silences console output here
thing_to_debug = render_execute(args, print_results=False)
test = True
else:
thing_to_debug = _args.recipe_or_package_file_path
api.debug(thing_to_debug, **_args.__dict__)
activation_string = api.debug(thing_to_debug, verbose=(not _args.activate_string_only), **_args.__dict__)

if not _args.activate_string_only:
print("#" * 80)
if test:
print("Test environment created for debugging. To enter a debugging environment:\n")
else:
print("Build and/or host environments created for debugging. To enter a debugging environment:\n")
print(activation_string)

except ValueError as e:
print(str(e))
sys.exit(1)
Expand Down
2 changes: 1 addition & 1 deletion conda_build/cli/main_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def parse_args(args):
action='store_true',
help='Enable verbose output from download tools and progress updates',
)
args, unknown_args = p.parse_known_args(args)
args, _ = p.parse_known_args(args)
return p, args


Expand Down
16 changes: 10 additions & 6 deletions conda_build/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
from .conda_interface import memoized
from .conda_interface import package_cache, TemporaryDirectory
from .conda_interface import pkgs_dirs, root_dir, symlink_conda, create_default_packages
from .conda_interface import reset_context

from conda_build import utils
from conda_build.exceptions import BuildLockError, DependencyNeedsBuildingError
from conda_build.features import feature_list
from conda_build.index import get_build_index
from conda_build.os_utils import external
from conda_build.utils import ensure_list, prepend_bin_path
from conda_build.utils import ensure_list, prepend_bin_path, env_var
from conda_build.variants import get_default_variant


Expand Down Expand Up @@ -544,9 +545,9 @@ def windows_vars(m, get_default, prefix):
get_default('PROCESSOR_ARCHITECTURE')
get_default('PROCESSOR_IDENTIFIER')
get_default('BUILD', win_arch + '-pc-windows-' + win_msvc)
for env_var in os.environ.keys():
if re.match('VS[0-9]{2,3}COMNTOOLS', env_var):
get_default(env_var)
for k in os.environ.keys():
if re.match('VS[0-9]{2,3}COMNTOOLS', k):
get_default(k)


def unix_vars(m, get_default, prefix):
Expand Down Expand Up @@ -848,11 +849,14 @@ def create_env(prefix, specs_or_actions, env, config, subdir, clear_cache=True,
locking=config.locking,
timeout=config.timeout)
utils.trim_empty_keys(actions)
display_actions(actions, index)
if config.verbose:
display_actions(actions, index)
if utils.on_win:
for k, v in os.environ.items():
os.environ[k] = str(v)
execute_actions(actions, index, verbose=config.debug)
with env_var('CONDA_QUIET', not config.verbose, reset_context):
with env_var('CONDA_JSON', not config.verbose, reset_context):
execute_actions(actions, index)
except (SystemExit, PaddingError, LinkError, DependencyNeedsBuildingError,
CondaError, BuildLockError) as exc:
if (("too short in" in str(exc) or
Expand Down
2 changes: 1 addition & 1 deletion conda_build/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def write_build_scripts(m, env, bld_bat):
with open(bld_bat) as fi:
data = fi.read()
with open(work_script, 'w') as fo:
fo.write("call {}".format())
fo.write("call {}".format(env_script))
fo.write("REM ===== end generated header =====\n")
fo.write(data)
return work_script, env_script
Expand Down

0 comments on commit ef1d3f9

Please sign in to comment.