Skip to content

Commit

Permalink
Better error messages if hook script is not found
Browse files Browse the repository at this point in the history
Currently, edalize raises an Exception if a hook script cannot be
executed because it is not found:

```
Traceback (most recent call last):
  File "/home/philipp/.local/bin/fusesoc", line 11, in <module>
    load_entry_point('fusesoc', 'console_scripts', 'fusesoc')()
  File "/home/philipp/src/fusesoc/fusesoc/main.py", line 583, in main
    args.func(cm, args)
  File "/home/philipp/src/fusesoc/fusesoc/main.py", line 268, in run
    flags, args.system_name, args.system, args.backendargs, args.build_root)
  File "/home/philipp/src/fusesoc/fusesoc/main.py", line 357, in run_backend
    backend.build()
  File "/home/philipp/src/edalize/edalize/edatool.py", line 121, in build
    self.build_post()
  File "/home/philipp/src/edalize/edalize/edatool.py", line 133, in build_post
    self._run_scripts(self.hooks['post_build'])
  File "/home/philipp/src/edalize/edalize/edatool.py", line 283, in _run_scripts
    env = _env)
  File "/usr/lib64/python3.7/subprocess.py", line 342, in check_call
    retcode = call(*popenargs, **kwargs)
  File "/usr/lib64/python3.7/subprocess.py", line 323, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/usr/lib64/python3.7/subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "/usr/lib64/python3.7/subprocess.py", line 1522, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '../../../../../test/fpga_error_check.sh': '../../../../../test/fpga_error_check.sh'
```

After applying this patch, the user gets a more helpful error message:

```
INFO: Running post_build script fpga_error_check
DEBUG: Environment: {'SHELL': '/bin/bash', ... }
DEBUG: Working directory: /some/path/sim-verilator
ERROR: Failed to build xxx:xxx:xxx:xxx : Unable to run post_build script 'fpga_error_check': [Errno 2] No such file or directory: '../../../../../test/fpga_error_check.sh': '../../../../../test/fpga_error_check.sh'
```

Note that the INFO and DEBUG output relies on
olofk/fusesoc#307.
The Environment debug line can be rather long (depending on the shell
environment), but is very helpful for debugging script errors. Trading
off a "nice log" for a "useful log" here.
  • Loading branch information
imphil authored and olofk committed Oct 25, 2019
1 parent 0d98606 commit 0285070
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions edalize/edatool.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ def build(self):

def build_pre(self):
if 'pre_build' in self.hooks:
self._run_scripts(self.hooks['pre_build'])
self._run_scripts(self.hooks['pre_build'], 'pre_build')

def build_main(self):
logger.info("Building");
self._run_tool('make')

def build_post(self):
if 'post_build' in self.hooks:
self._run_scripts(self.hooks['post_build'])
self._run_scripts(self.hooks['post_build'], 'post_build')

def run(self, args):
logger.info("Running")
Expand All @@ -141,14 +141,14 @@ def run(self, args):
def run_pre(self, args):
self.parse_args(args, self.argtypes)
if 'pre_run' in self.hooks:
self._run_scripts(self.hooks['pre_run'])
self._run_scripts(self.hooks['pre_run'], 'pre_run')

def run_main(self):
pass

def run_post(self):
if 'post_run' in self.hooks:
self._run_scripts(self.hooks['post_run'])
self._run_scripts(self.hooks['post_run'], 'post_run')

def parse_args(self, args, paramtypes):
if self.parsed_args:
Expand Down Expand Up @@ -271,19 +271,24 @@ def __init__(self, name, file_type, logical_name):
def _param_value_str(self, param_value, str_quote_style="", bool_is_str=False):
return jinja_filter_param_value_str(param_value, str_quote_style, bool_is_str)

def _run_scripts(self, scripts):
def _run_scripts(self, scripts, hook_name):
for script in scripts:
_env = self.env.copy()
if 'env' in script:
_env.update(script['env'])
logger.info("Running " + script['name'])
logger.info("Running {} script {}".format(hook_name, script['name']))
logger.debug("Environment: " + str(_env))
logger.debug("Working directory: " + self.work_root)
try:
subprocess.check_call(script['cmd'],
cwd = self.work_root,
env = _env)
except FileNotFoundError as e:
msg = "Unable to run {} script '{}': {}"
raise RuntimeError(msg.format(hook_name, script['name'], str(e)))
except subprocess.CalledProcessError as e:
msg = "'{}' exited with error code {}"
raise RuntimeError(msg.format(script['name'], e.returncode))
msg = "{} script '{}' exited with error code {}"
raise RuntimeError(msg.format(hook_name, script['name'], e.returncode))

def _run_tool(self, cmd, args=[]):
logger.debug("Running " + cmd)
Expand Down

0 comments on commit 0285070

Please sign in to comment.