diff --git a/kibot/gs.py b/kibot/gs.py index fb5942a0..9a094930 100644 --- a/kibot/gs.py +++ b/kibot/gs.py @@ -405,6 +405,11 @@ class GS(object): """ Will be repplaced by kiplot.py """ raise AssertionError() + @staticmethod + def exec_with_retry(): + """ Will be repplaced by kiplot.py """ + raise AssertionError() + @staticmethod def load_board_low_level(file): return pcbnew.LoadBoard(file) diff --git a/kibot/kiplot.py b/kibot/kiplot.py index 9074d964..7e0f759f 100644 --- a/kibot/kiplot.py +++ b/kibot/kiplot.py @@ -175,7 +175,7 @@ def exec_with_retry(cmd, exit_with=None): cmd_str = shlex.join(cmd) logger.debug('Executing: '+cmd_str) if GS.debug_level > 2: - logger.debug('Command line: '+cmd_str) + logger.debug('Command line: '+str(cmd)) retry = 2 while retry: result = run(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True) @@ -982,3 +982,4 @@ def generate_examples(start_dir, dry, types): # To avoid circular dependencies: Optionable needs it, but almost everything needs Optionable GS.load_board = load_board GS.load_sch = load_sch +GS.exec_with_retry = exec_with_retry diff --git a/kibot/out_any_pcb_print.py b/kibot/out_any_pcb_print.py index 2e1f8e4a..f4599d97 100644 --- a/kibot/out_any_pcb_print.py +++ b/kibot/out_any_pcb_print.py @@ -6,7 +6,6 @@ import os from .pre_base import BasePreFlight from .gs import GS -from .kiplot import exec_with_retry from .misc import PDF_PCB_PRINT from .out_base import VariantOptions from .macros import macros, document, output_class # noqa: F401 @@ -97,8 +96,7 @@ class Any_PCB_PrintOptions(VariantOptions): if GS.ki6 and self.force_edge_cuts and not self.separated: cmd.append('Edge.Cuts') # Execute it - exec_with_retry(cmd, PDF_PCB_PRINT) - self.remove_temporals() + self.exec_with_retry(cmd, PDF_PCB_PRINT) def set_layers(self, layers): layers = Layer.solve(layers) diff --git a/kibot/out_any_sch_print.py b/kibot/out_any_sch_print.py index 6998870e..b7f78869 100644 --- a/kibot/out_any_sch_print.py +++ b/kibot/out_any_sch_print.py @@ -7,7 +7,6 @@ import os from tempfile import mkdtemp from shutil import copy2 from .gs import GS -from .kiplot import exec_with_retry from .out_base import VariantOptions from .kicad.config import KiConf from .macros import macros, document, output_class # noqa: F401 @@ -68,5 +67,4 @@ class Any_SCH_PrintOptions(VariantOptions): if self.all_pages: cmd.append('--all_pages') cmd.extend([sch_file, os.path.dirname(name)]) - exec_with_retry(self.add_extra_options(cmd), self._exit_error) - self.remove_temporals() + self.exec_with_retry(self.add_extra_options(cmd), self._exit_error) diff --git a/kibot/out_base.py b/kibot/out_base.py index 8f633419..a81a4ede 100644 --- a/kibot/out_base.py +++ b/kibot/out_base.py @@ -949,6 +949,18 @@ class VariantOptions(BaseOptions): self._files_to_remove.append(os.path.join(dir or cmd[-1], GS.get_kiauto_video_name(cmd))) return cmd + def exec_with_retry(self, cmd, exit_with): + try: + GS.exec_with_retry(cmd, exit_with) + finally: + if GS.debug_enabled: + if self._files_to_remove: + logger.error('Keeping temporal files: '+str(self._files_to_remove)) + else: + self.remove_temporals() + if self._files_to_remove: + self.remove_temporals() + def run(self, output_dir): """ Makes the list of components available """ self._files_to_remove = [] diff --git a/kibot/out_gencad.py b/kibot/out_gencad.py index c00b7429..ae7af621 100644 --- a/kibot/out_gencad.py +++ b/kibot/out_gencad.py @@ -13,7 +13,6 @@ import os from .gs import GS from .out_base import VariantOptions from .misc import FAILED_EXECUTE -from .kiplot import exec_with_retry from .macros import macros, document, output_class # noqa: F401 from . import log @@ -62,8 +61,7 @@ class GenCADOptions(VariantOptions): cmd.extend([board_name, os.path.dirname(name)]) cmd = self.add_extra_options(cmd) # Execute it - exec_with_retry(cmd, FAILED_EXECUTE) - self.remove_temporals() + self.exec_with_retry(cmd, FAILED_EXECUTE) @output_class diff --git a/kibot/out_netlist.py b/kibot/out_netlist.py index 807ba815..7743eb6b 100644 --- a/kibot/out_netlist.py +++ b/kibot/out_netlist.py @@ -14,7 +14,6 @@ import os from .gs import GS from .out_base import VariantOptions from .misc import FAILED_EXECUTE -from .kiplot import exec_with_retry from .macros import macros, document, output_class # noqa: F401 from . import log @@ -60,8 +59,7 @@ class NetlistOptions(VariantOptions): # Create the command line cmd = self.add_extra_options([command, subcommand, '--output_name', name, file, os.path.dirname(name)]) # Execute it - exec_with_retry(cmd, FAILED_EXECUTE) - self.remove_temporals() + self.exec_with_retry(cmd, FAILED_EXECUTE) @output_class diff --git a/kibot/out_pcb_print.py b/kibot/out_pcb_print.py index 7336e223..fb04999f 100644 --- a/kibot/out_pcb_print.py +++ b/kibot/out_pcb_print.py @@ -46,7 +46,6 @@ from .kicad.config import KiConf from .kicad.v5_sch import SchError from .kicad.pcb import PCB from .misc import PDF_PCB_PRINT, W_PDMASKFAIL, KICAD5_SVG_SCALE, W_MISSTOOL, PCBDRAW_ERR, W_PCBDRAW -from .kiplot import exec_with_retry from .create_pdf import create_pdf_from_pages from .macros import macros, document, output_class # noqa: F401 from .drill_marks import DRILL_MARKS_MAP, add_drill_marks @@ -485,10 +484,9 @@ class PCB_PrintOptions(VariantOptions): cmd = [command, 'export', '--output_name', output, '--monochrome', '--svg', '--pads', '0', pcb_name, dir_name, layer] # Execute it - exec_with_retry(self.add_extra_options(cmd, dir_name), PDF_PCB_PRINT) + self.exec_with_retry(self.add_extra_options(cmd, dir_name), PDF_PCB_PRINT) # Rotate the paper size if needed and remove the background (or it will be over the drawings) patch_svg_file(output, remove_bkg=True, is_portrait=self.paper_portrait) - self.remove_temporals() self._files_to_remove = cur_files_to_remove def plot_pads(self, la, pc, p, filelist): diff --git a/kibot/out_render_3d.py b/kibot/out_render_3d.py index 65b58847..2c40bc5c 100644 --- a/kibot/out_render_3d.py +++ b/kibot/out_render_3d.py @@ -18,7 +18,7 @@ import subprocess from .misc import (RENDER_3D_ERR, PCB_MAT_COLORS, PCB_FINISH_COLORS, SOLDER_COLORS, SILK_COLORS, KICAD_VERSION_6_0_2, MISSING_TOOL) from .gs import GS -from .kiplot import exec_with_retry, load_sch, get_board_comps_data +from .kiplot import load_sch, get_board_comps_data from .optionable import Optionable from .out_base_3d import Base3DOptions, Base3D from .macros import macros, document, output_class # noqa: F401 @@ -301,8 +301,7 @@ class Render3DOptions(Base3DOptions): self.undo_show_components() cmd.extend([board_name, os.path.dirname(output)]) # Execute it - exec_with_retry(self.add_extra_options(cmd), RENDER_3D_ERR) - self.remove_temporals() + self.exec_with_retry(self.add_extra_options(cmd), RENDER_3D_ERR) if self.auto_crop: _run_command([convert_command, output, '-trim', '+repage', '-trim', '+repage', output]) if self.transparent_background: diff --git a/kibot/out_step.py b/kibot/out_step.py index a48a2c29..eeec0f81 100644 --- a/kibot/out_step.py +++ b/kibot/out_step.py @@ -17,7 +17,6 @@ from .error import KiPlotConfigurationError from .misc import KICAD2STEP_ERR from .gs import GS from .out_base_3d import Base3DOptions, Base3D -from .kiplot import exec_with_retry from .macros import macros, document, output_class # noqa: F401 from . import log @@ -85,8 +84,7 @@ class STEPOptions(Base3DOptions): board_name = self.filter_components() cmd.append(board_name) # Execute it - exec_with_retry(self.add_extra_options(cmd, os.path.dirname(output)), KICAD2STEP_ERR) - self.remove_temporals() + self.exec_with_retry(self.add_extra_options(cmd, os.path.dirname(output)), KICAD2STEP_ERR) @output_class diff --git a/kibot/out_vrml.py b/kibot/out_vrml.py index 1066e2ad..4147fec6 100644 --- a/kibot/out_vrml.py +++ b/kibot/out_vrml.py @@ -13,7 +13,6 @@ import os from .gs import GS from .out_base_3d import Base3DOptions, Base3D from .misc import FAILED_EXECUTE -from .kiplot import exec_with_retry from .macros import macros, document, output_class # noqa: F401 from . import log @@ -76,8 +75,7 @@ class VRMLOptions(Base3DOptions): cmd.extend(['-x', str(x), '-y', str(x), '-u', units]) cmd.extend([board_name, os.path.dirname(name)]) # Execute it - exec_with_retry(self.add_extra_options(cmd), FAILED_EXECUTE) - self.remove_temporals() + self.exec_with_retry(self.add_extra_options(cmd), FAILED_EXECUTE) @output_class diff --git a/kibot/pre_base.py b/kibot/pre_base.py index 115f71cc..3df39eee 100644 --- a/kibot/pre_base.py +++ b/kibot/pre_base.py @@ -173,6 +173,19 @@ class BasePreFlight(Registrable): self._files_to_remove.append(os.path.join(dir or cmd[-1], GS.get_kiauto_video_name(cmd))) return cmd + def exec_with_retry(self, cmd, exit_with=None): + try: + ret = GS.exec_with_retry(cmd, exit_with) + finally: + if GS.debug_enabled: + if self._files_to_remove: + logger.error('Keeping temporal files: '+str(self._files_to_remove)) + else: + self.remove_temporals() + if self._files_to_remove: + self.remove_temporals() + return ret + def remove_temporals(self): logger.debug('Removing temporal files') for f in self._files_to_remove: diff --git a/kibot/pre_run_drc.py b/kibot/pre_run_drc.py index 11a4d3a2..60f7637c 100644 --- a/kibot/pre_run_drc.py +++ b/kibot/pre_run_drc.py @@ -15,7 +15,7 @@ from .macros import macros, pre_class # noqa: F401 from .error import KiPlotConfigurationError from .gs import GS from .optionable import Optionable -from .kiplot import exec_with_retry, load_board +from .kiplot import load_board from .misc import DRC_ERROR from .log import get_logger @@ -63,8 +63,7 @@ class Run_DRC(BasePreFlight): # noqa: F821 # If we are in verbose mode enable debug in the child cmd = self.add_extra_options(cmd) logger.info('- Running the DRC') - ret = exec_with_retry(cmd) - self.remove_temporals() + ret = self.exec_with_retry(cmd) if ret: if ret > 127: ret = -(256-ret) diff --git a/kibot/pre_run_erc.py b/kibot/pre_run_erc.py index e2580a4c..6fdc33e6 100644 --- a/kibot/pre_run_erc.py +++ b/kibot/pre_run_erc.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2020-2022 Salvador E. Tropea -# Copyright (c) 2020-2022 Instituto Nacional de TecnologĂ­a Industrial +# Copyright (c) 2020-2023 Salvador E. Tropea +# Copyright (c) 2020-2023 Instituto Nacional de TecnologĂ­a Industrial # License: GPL-3.0 # Project: KiBot (formerly KiPlot) """ @@ -15,7 +15,7 @@ from sys import exit from .macros import macros, pre_class # noqa: F401 from .gs import GS from .optionable import Optionable -from .kiplot import exec_with_retry, load_sch +from .kiplot import load_sch from .error import KiPlotConfigurationError from .misc import ERC_ERROR from .log import get_logger @@ -62,8 +62,7 @@ class Run_ERC(BasePreFlight): # noqa: F821 # If we are in verbose mode enable debug in the child cmd = self.add_extra_options(cmd) logger.info('- Running the ERC') - ret = exec_with_retry(cmd) - self.remove_temporals() + ret = self.exec_with_retry(cmd) if ret: if ret > 127: ret = -(256-ret) diff --git a/kibot/pre_update_xml.py b/kibot/pre_update_xml.py index f78537b0..c089ec39 100644 --- a/kibot/pre_update_xml.py +++ b/kibot/pre_update_xml.py @@ -16,7 +16,7 @@ import xml.etree.ElementTree as ET from .macros import macros, document, pre_class # noqa: F401 from .error import KiPlotConfigurationError from .gs import GS -from .kiplot import exec_with_retry, load_board +from .kiplot import load_board from .misc import BOM_ERROR, NETLIST_DIFF, W_PARITY, MISSING_TOOL from .log import get_logger from .optionable import Optionable @@ -177,7 +177,6 @@ class Update_XML(BasePreFlight): # noqa: F821 if not os.path.isfile(side_effect_file): self._files_to_remove.append(side_effect_file) logger.info('- Updating BoM in XML format') - exec_with_retry(cmd, BOM_ERROR) - self.remove_temporals() + self.exec_with_retry(cmd, BOM_ERROR) if self._check_pcb_parity: self.check_pcb_parity()