Skip to content

Commit

Permalink
Unified the exit point for errors running external commands
Browse files Browse the repository at this point in the history
  • Loading branch information
set-soft committed Jan 5, 2024
1 parent e9733d9 commit 190dc0a
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 77 deletions.
21 changes: 0 additions & 21 deletions .coveragerc

This file was deleted.

14 changes: 12 additions & 2 deletions kibot/gs.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,13 +779,23 @@ def trace_dump():
print_tb(traceback)

@staticmethod
def exit_with_error(msg, level):
def exit_with_error(msg, level, run_error=None, hints=None):
GS.trace_dump()
if isinstance(msg, (tuple, list)):
for m in msg:
logger.error(m)
else:
elif msg:
logger.error(msg)
if run_error:
if not msg:
logger.error('Running {} returned {}'.format(run_error.cmd, run_error.returncode))
if run_error.output:
out = run_error.output.decode()
logger.debug('- Output from command: '+out)
if hints:
for h in hints:
if h[0] in out:
logger.error(h[1])
exit(level)

@staticmethod
Expand Down
5 changes: 1 addition & 4 deletions kibot/kiplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from collections import OrderedDict
import os
import re
from sys import exit
from sys import path as sys_path
from shutil import which, copy2
from subprocess import run, PIPE, STDOUT, Popen, CalledProcessError
Expand Down Expand Up @@ -164,9 +163,7 @@ def run_command(command, change_to=None, just_raise=False, use_x11=False):
except CalledProcessError as e:
if just_raise:
raise
logger.error('Running {} returned {}'.format(e.cmd, e.returncode))
debug_output(e)
exit(FAILED_EXECUTE)
GS.exit_with_error(None, FAILED_EXECUTE, e)
debug_output(res)
return res.stdout.decode().rstrip()

Expand Down
3 changes: 1 addition & 2 deletions kibot/out_base_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,7 @@ def filter_components(self, highlight=None, force_wrl=False):
if GS.sch_file:
GS.load_sch()
all_comps = GS.sch.get_components()
if (GS.global_kicad_dnp_applies_to_3D and
any((c.kicad_dnp is not None and c.kicad_dnp for c in all_comps))):
if (GS.global_kicad_dnp_applies_to_3D and any((c.kicad_dnp is not None and c.kicad_dnp for c in all_comps))):
# One or more components are DNP, remove them
reset_filters(all_comps)
all_comps_hash = {c.ref: c for c in all_comps}
Expand Down
6 changes: 1 addition & 5 deletions kibot/out_compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import os
import glob
import sys
from sys import exit
from subprocess import check_output, STDOUT, CalledProcessError
from zipfile import ZipFile, ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2, ZIP_LZMA
from tarfile import open as tar_open
Expand Down Expand Up @@ -126,10 +125,7 @@ def create_rar(self, output, files):
try:
check_output(cmd, stderr=STDOUT)
except CalledProcessError as e:
logger.error('Failed to invoke rar command, error {}'.format(e.returncode))
if e.output:
logger.debug('Output from command: '+e.output.decode())
exit(WRONG_INSTALL)
GS.exit_with_error(f'Failed to invoke rar command, error {e.returncode}', WRONG_INSTALL, e)

def solve_extension(self):
if self.format == 'ZIP':
Expand Down
5 changes: 1 addition & 4 deletions kibot/out_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,10 +543,7 @@ def do_compare(self, old, old_type, new, new_type, name, name_ori):
except CalledProcessError as e:
if e.returncode == 10:
GS.exit_with_error('Diff above the threshold', DIFF_TOO_BIG)
logger.error('Running {} returned {}'.format(e.cmd, e.returncode))
if e.stdout:
logger.debug('- Output from command: '+e.stdout.decode())
exit(FAILED_EXECUTE)
GS.exit_with_error(None, FAILED_EXECUTE, e)
if self.add_link_id:
name_comps = os.path.splitext(name_ori)
target = name_comps[0]+'_'+gh1+'-'+gh2+name_comps[1]
Expand Down
9 changes: 3 additions & 6 deletions kibot/out_ibom.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,9 @@ def run(self, name):
if 'ERROR Parsing failed' in cmd_output_dec:
raise CalledProcessError(1, cmd, cmd_output)
except CalledProcessError as e:
logger.error('Failed to create BoM, error %d', e.returncode)
if e.output:
logger.debug('Output from command: '+e.output.decode())
if "'PCB_SHAPE' object has no attribute 'GetAngle'" in e.output.decode():
logger.error("Update Interactive HTML BoM your version doesn't support KiCad 6 files")
exit(BOM_ERROR)
GS.exit_with_error(f'Failed to create BoM, error {e.returncode}', BOM_ERROR, e,
("'PCB_SHAPE' object has no attribute 'GetAngle'",
"Update Interactive HTML BoM your version doesn't support KiCad 6 files"))
finally:
if net_dir:
logger.debug('Removing temporal variant dir `{}`'.format(net_dir))
Expand Down
10 changes: 2 additions & 8 deletions kibot/out_kibom.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,7 @@ def _get_columns():
with open(csv, 'rt') as f:
columns = f.readline().rstrip().split(',')
except CalledProcessError as e:
logger.error('Failed to get the column names for `{}`, error {}'.format(xml, e.returncode))
if e.output:
logger.debug('Output from command: '+e.output.decode())
exit(BOM_ERROR)
GS.exit_with_error(f'Failed to get the column names for `{xml}`, error {e.returncode}', BOM_ERROR, e)
finally:
if config:
os.remove(config)
Expand Down Expand Up @@ -433,10 +430,7 @@ def run(self, name):
try:
cmd_output = check_output(cmd, stderr=STDOUT)
except CalledProcessError as e:
logger.error('Failed to create BoM, error %d', e.returncode)
if e.output:
logger.debug('Output from command: '+e.output.decode())
exit(BOM_ERROR)
GS.exit_with_error(f'Failed to create BoM, error {e.returncode}', BOM_ERROR, e)
if force_output:
# When we create the .ini we can control the name.
# But when the user does it we can trust the settings.
Expand Down
5 changes: 1 addition & 4 deletions kibot/out_kicost.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,7 @@ def run(self, name):
cmd_output = check_output(cmd, stderr=STDOUT)
cmd_output_dec = cmd_output.decode()
except CalledProcessError as e:
logger.error('Failed to create costs spreadsheet, error %d', e.returncode)
if e.output:
logger.debug('Output from command: '+e.output.decode())
exit(BOM_ERROR)
GS.exit_with_error(f'Failed to create costs spreadsheet, error {e.returncode}', BOM_ERROR, e)
finally:
if net_dir:
logger.debug('Removing temporal variant dir `{}`'.format(net_dir))
Expand Down
5 changes: 1 addition & 4 deletions kibot/out_pcb_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ def _run_command(cmd):
try:
cmd_output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
logger.error('Failed to run %s, error %d', cmd[0], e.returncode)
if e.output:
logger.debug('Output from command: '+e.output.decode())
exit(PDF_PCB_PRINT)
GS.exit_with_error(None, PDF_PCB_PRINT, e)
if cmd_output.strip():
logger.debug('- Output from command:\n'+cmd_output.decode())

Expand Down
5 changes: 1 addition & 4 deletions kibot/out_pcbdraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ def _run_command(cmd):
try:
cmd_output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
logger.error('Failed to run %s, error %d', cmd[0], e.returncode)
if e.output:
logger.debug('Output from command: '+e.output.decode())
exit(PCBDRAW_ERR)
GS.exit_with_error(None, PCBDRAW_ERR, e)
out = cmd_output.decode()
if out.strip():
logger.debug('Output from command:\n'+out)
Expand Down
6 changes: 1 addition & 5 deletions kibot/out_pdfunite.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import re
import os
import glob
from sys import exit
from subprocess import check_output, STDOUT, CalledProcessError
from .gs import GS
from .error import KiPlotConfigurationError
Expand Down Expand Up @@ -113,10 +112,7 @@ def run_external(self, files, output):
except FileNotFoundError:
GS.exit_with_error('Missing `pdfunite` command, install it (poppler-utils)', MISSING_TOOL)
except CalledProcessError as e:
logger.error('Failed to invoke pdfunite command, error {}'.format(e.returncode))
if e.output:
logger.error('Output from command: '+e.output.decode())
exit(WRONG_INSTALL)
GS.exit_with_error(f'Failed to invoke pdfunite command, error {e.returncode}', WRONG_INSTALL, e)

def run(self, output):
# Output file name
Expand Down
5 changes: 1 addition & 4 deletions kibot/out_render_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ def _run_command(cmd):
try:
cmd_output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
logger.error('Failed to run %s, error %d', cmd[0], e.returncode)
if e.output:
logger.debug('Output from command: '+e.output.decode())
exit(RENDER_3D_ERR)
GS.exit_with_error(None, RENDER_3D_ERR, e)
if cmd_output.strip():
logger.debug('- Output from command:\n'+cmd_output.decode())

Expand Down
5 changes: 1 addition & 4 deletions kibot/out_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,10 +854,7 @@ def convert(self, fname):
try:
check_output(cmd, stderr=STDOUT)
except CalledProcessError as e:
logger.error('{} error: {}'.format(command, e.returncode))
if e.output:
logger.debug('Output from command: '+e.output.decode())
exit(FAILED_EXECUTE)
GS.exit_with_error(None, FAILED_EXECUTE, e)

def run(self, fname):
self.pcb_material = GS.global_pcb_material
Expand Down

0 comments on commit 190dc0a

Please sign in to comment.