Skip to content

Commit

Permalink
tty: make tty.* print exception types
Browse files Browse the repository at this point in the history
- make tty.msg, tty.info, etc. print the exception type and stringified
  message if the message argument is an exception.

- simplify parts of the code that call tty.debug(str(e))

- add extra tty.debug statements in places where exceptions were
  previously ignored
  • Loading branch information
tldahlgren authored and tgamblin committed Jun 6, 2019
1 parent 8c173da commit 8e3fd3f
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 26 deletions.
6 changes: 6 additions & 0 deletions lib/spack/llnl/util/tty/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ def msg(message, *args, **kwargs):
if not msg_enabled():
return

if isinstance(message, Exception):
message = "%s: %s" % (message.__class__.__name__, str(message))

newline = kwargs.get('newline', True)
st_text = ""
if _stacktrace:
Expand All @@ -156,6 +159,9 @@ def msg(message, *args, **kwargs):


def info(message, *args, **kwargs):
if isinstance(message, Exception):
message = "%s: %s" % (message.__class__.__name__, str(message))

format = kwargs.get('format', '*b')
stream = kwargs.get('stream', sys.stdout)
wrap = kwargs.get('wrap', False)
Expand Down
13 changes: 7 additions & 6 deletions lib/spack/spack/binary_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,22 +321,22 @@ def build_tarball(spec, outdir, force=False, rel=False, unsigned=False,
# create info for later relocation and create tar
write_buildinfo_file(spec.prefix, workdir, rel=rel)

# optinally make the paths in the binaries relative to each other
# optionally make the paths in the binaries relative to each other
# in the spack install tree before creating tarball
if rel:
try:
make_package_relative(workdir, spec.prefix, allow_root)
except Exception as e:
shutil.rmtree(workdir)
shutil.rmtree(tarfile_dir)
tty.die(str(e))
tty.die(e)
else:
try:
make_package_placeholder(workdir, spec.prefix, allow_root)
except Exception as e:
shutil.rmtree(workdir)
shutil.rmtree(tarfile_dir)
tty.die(str(e))
tty.die(e)
# create compressed tarball of the install prefix
with closing(tarfile.open(tarfile_path, 'w:gz')) as tar:
tar.add(name='%s' % workdir,
Expand Down Expand Up @@ -521,7 +521,7 @@ def extract_tarball(spec, filename, allow_root=False, unsigned=False,
Gpg.verify('%s.asc' % specfile_path, specfile_path)
except Exception as e:
shutil.rmtree(tmpdir)
tty.die(str(e))
tty.die(e)
else:
shutil.rmtree(tmpdir)
raise NoVerifyException(
Expand Down Expand Up @@ -575,7 +575,7 @@ def extract_tarball(spec, filename, allow_root=False, unsigned=False,
relocate_package(workdir, allow_root)
except Exception as e:
shutil.rmtree(workdir)
tty.die(str(e))
tty.die(e)
# Delay creating spec.prefix until verification is complete
# and any relocation has been done.
else:
Expand Down Expand Up @@ -809,7 +809,8 @@ def _download_buildcache_entry(mirror_root, descriptions):

try:
stage.fetch()
except fs.FetchError:
except fs.FetchError as e:
tty.debug(e)
if fail_if_missing:
tty.error('Failed to download required url {0}'.format(url))
return False
Expand Down
8 changes: 4 additions & 4 deletions lib/spack/spack/build_systems/autotools.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ def _do_patch_config_guess(self):
check_call([my_config_guess], stdout=PIPE, stderr=PIPE)
# The package's config.guess already runs OK, so just use it
return
except Exception:
pass
except Exception as e:
tty.debug(e)
else:
return

Expand Down Expand Up @@ -142,8 +142,8 @@ def _do_patch_config_guess(self):
os.chmod(my_config_guess, mod)
shutil.copyfile(config_guess, my_config_guess)
return
except Exception:
pass
except Exception as e:
tty.debug(e)

raise RuntimeError('Failed to find suitable config.guess')

Expand Down
1 change: 1 addition & 0 deletions lib/spack/spack/cmd/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ def install(parser, args, **kwargs):
specs = spack.cmd.parse_specs(
args.package, concretize=True, tests=tests)
except SpackError as e:
tty.debug(e)
reporter.concretization_report(e.message)
raise

Expand Down
1 change: 1 addition & 0 deletions lib/spack/spack/cmd/mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def _read_specs_from_file(filename):
s.package
specs.append(s)
except SpackError as e:
tty.debug(e)
tty.die("Parse error in %s, line %d:" % (filename, i + 1),
">>> " + string, str(e))
return specs
Expand Down
1 change: 1 addition & 0 deletions lib/spack/spack/cmd/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ def refresh(module_type, specs, args):
try:
x.write(overwrite=True)
except Exception as e:
tty.debug(e)
msg = 'Could not write module file [{0}]'
tty.warn(msg.format(x.layout.filename))
tty.warn('\t--> {0} <--'.format(str(e)))
Expand Down
6 changes: 4 additions & 2 deletions lib/spack/spack/cmd/upload_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,17 @@ def upload_spec(args):
try:
spec = Spec(args.spec)
spec.concretize()
except Exception:
except Exception as e:
tty.debug(e)
tty.error('Unable to concrectize spec from string {0}'.format(
args.spec))
sys.exit(1)
else:
try:
with open(args.spec_yaml, 'r') as fd:
spec = Spec.from_yaml(fd.read())
except Exception:
except Exception as e:
tty.debug(e)
tty.error('Unable to concrectize spec from yaml {0}'.format(
args.spec_yaml))
sys.exit(1)
Expand Down
6 changes: 3 additions & 3 deletions lib/spack/spack/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,7 @@ def _construct_from_directory_layout(self, directory_layout, old_data):
except Exception as e:
# Something went wrong, so the spec was not restored
# from old data
tty.debug(e.message)
pass
tty.debug(e)

self._check_ref_counts()

Expand Down Expand Up @@ -659,7 +658,8 @@ def _write(self, type, value, traceback):
with open(temp_file, 'w') as f:
self._write_to_file(f)
os.rename(temp_file, self._index_path)
except BaseException:
except BaseException as e:
tty.debug(e)
# Clean up temp file if something goes wrong.
if os.path.exists(temp_file):
os.remove(temp_file)
Expand Down
6 changes: 4 additions & 2 deletions lib/spack/spack/fetch_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,11 +1082,13 @@ def from_list_url(pkg):

# construct a fetcher
return URLFetchStrategy(url_from_list, checksum)
except KeyError:
except KeyError as e:
tty.debug(e)
tty.msg("Cannot find version %s in url_list" % pkg.version)

except BaseException:
except BaseException as e:
# TODO: Don't catch BaseException here! Be more specific.
tty.debug(e)
tty.msg("Could not determine url from list_url.")


Expand Down
4 changes: 3 additions & 1 deletion lib/spack/spack/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ def __call__(self, *argv, **kwargs):
self.returncode = e.code

except BaseException as e:
tty.debug(e)
self.error = e
if fail_on_error:
raise
Expand Down Expand Up @@ -695,12 +696,13 @@ def main(argv=None):
return _invoke_command(command, parser, args, unknown)

except SpackError as e:
tty.debug(e)
e.die() # gracefully die on any SpackErrors

except Exception as e:
if spack.config.get('config:debug'):
raise
tty.die(str(e))
tty.die(e)

except KeyboardInterrupt:
sys.stderr.write('\n')
Expand Down
1 change: 1 addition & 0 deletions lib/spack/spack/mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def add_single_spec(spec, mirror_root, categories, **kwargs):
spec.package.do_clean()

except Exception as e:
tty.debug(e)
if spack.config.get('config:debug'):
sys.excepthook(*sys.exc_info())
else:
Expand Down
17 changes: 12 additions & 5 deletions lib/spack/spack/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,9 @@ def do_patch(self):
patch.apply(self.stage)
tty.msg('Applied patch %s' % patch.path_or_url)
patched = True
except spack.error.SpackError:
except spack.error.SpackError as e:
tty.debug(e)

# Touch bad file if anything goes wrong.
tty.msg('Patch %s failed.' % patch.path_or_url)
touch(bad_file)
Expand All @@ -1088,7 +1090,10 @@ def do_patch(self):
# no patches are needed. Otherwise, we already
# printed a message for each patch.
tty.msg("No patches needed for %s" % self.name)
except spack.error.SpackError:
except spack.error.SpackError as e:
tty.debug(e)

# Touch bad file if anything goes wrong.
tty.msg("patch() function failed for %s" % self.name)
touch(bad_file)
raise
Expand Down Expand Up @@ -1724,9 +1729,9 @@ def log(self):
try:
# log_install_path and env_install_path are inside this
shutil.rmtree(packages_dir)
except Exception:
except Exception as e:
# FIXME : this potentially catches too many things...
pass
tty.debug(e)

# Archive the whole stdout + stderr for the package
install(self.log_path, log_install_path)
Expand Down Expand Up @@ -1762,7 +1767,9 @@ def log(self):
# copying a file in
mkdirp(os.path.dirname(target))
install(f, target)
except Exception:
except Exception as e:
tty.debug(e)

# Here try to be conservative, and avoid discarding
# the whole install procedure because of copying a
# single file failed
Expand Down
6 changes: 4 additions & 2 deletions lib/spack/spack/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,10 @@ def get(self, spec):
except spack.error.SpackError:
# pass these through as their error messages will be fine.
raise
except Exception:
# make sure other errors in constructors hit the error
except Exception as e:
tty.debug(e)

# Make sure other errors in constructors hit the error
# handler by wrapping them
if spack.config.get('config:debug'):
sys.excepthook(*sys.exc_info())
Expand Down
1 change: 1 addition & 0 deletions lib/spack/spack/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,7 @@ def from_json(stream):
data = sjson.load(stream)
return Spec.from_dict(data)
except Exception as e:
tty.debug(e)
raise sjson.SpackJSONError("error parsing JSON spec:", str(e))

def _concretize_helper(self, presets=None, visited=None):
Expand Down
4 changes: 3 additions & 1 deletion lib/spack/spack/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,8 @@ def destroy(self):
# Make sure we don't end up in a removed directory
try:
os.getcwd()
except OSError:
except OSError as e:
tty.debug(e)
os.chdir(os.path.dirname(self.path))

# mark as destroyed
Expand Down Expand Up @@ -530,6 +531,7 @@ def _add_to_root_stage(self):
try:
os.makedirs(target_path)
except OSError as err:
tty.debug(err)
if err.errno == errno.EEXIST and os.path.isdir(target_path):
pass
else:
Expand Down

0 comments on commit 8e3fd3f

Please sign in to comment.