From ebabc0b37ba0f37db413611ade201e845a42235c Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Mon, 26 Dec 2022 08:02:14 -0300 Subject: [PATCH] [Windows] Fixed problems with os.rename - Now using os.replace - It removes the target if already there --- kibot/gs.py | 4 +--- kibot/kicad/v5_sch.py | 4 +--- kibot/kicad/v6_sch.py | 4 +--- kibot/out_any_drill.py | 2 +- kibot/out_any_layer.py | 2 +- kibot/out_ibom.py | 2 +- kibot/out_kibom.py | 2 +- tests/test_plot/test_pdf.py | 2 +- tests/test_plot/test_preflight.py | 12 ++++++------ 9 files changed, 14 insertions(+), 20 deletions(-) diff --git a/kibot/gs.py b/kibot/gs.py index 42a0e5dc..c2fb89c3 100644 --- a/kibot/gs.py +++ b/kibot/gs.py @@ -291,9 +291,7 @@ class GS(object): @staticmethod def make_bkp(fname): bkp = fname+'-bak' - if os.path.isfile(bkp): - os.remove(bkp) - os.rename(fname, bkp) + os.replace(fname, bkp) @staticmethod def zones(): diff --git a/kibot/kicad/v5_sch.py b/kibot/kicad/v5_sch.py index 22007c0a..5bb9c0aa 100644 --- a/kibot/kicad/v5_sch.py +++ b/kibot/kicad/v5_sch.py @@ -1797,9 +1797,7 @@ class Schematic(object): # Keep a back-up of existing files if os.path.isfile(fname): bkp = fname+'-bak' - if os.path.isfile(bkp): - os.remove(bkp) - os.rename(fname, bkp) + os.replace(fname, bkp) with open(fname, 'wt') as f: f.write('EESchema Schematic File Version {}\n'.format(self.version)) f.write('EELAYER {} {}\n'.format(self.eelayer_n, self.eelayer_m)) diff --git a/kibot/kicad/v6_sch.py b/kibot/kicad/v6_sch.py index 295c574b..8b739949 100644 --- a/kibot/kicad/v6_sch.py +++ b/kibot/kicad/v6_sch.py @@ -1751,9 +1751,7 @@ class SchematicV6(Schematic): # Keep a back-up of existing files if os.path.isfile(fname): bkp = fname+'-bak' - if os.path.isfile(bkp): - os.remove(bkp) - os.rename(fname, bkp) + os.replace(fname, bkp) with open(fname, 'wt') as f: f.write(dumps(sch)) f.write('\n') diff --git a/kibot/out_any_drill.py b/kibot/out_any_drill.py index fba787c4..65aab0e1 100644 --- a/kibot/out_any_drill.py +++ b/kibot/out_any_drill.py @@ -177,7 +177,7 @@ class AnyDrill(BaseOptions): for k_f, f in files.items(): if f: logger.debug("Renaming {} -> {}".format(k_f, f)) - os.rename(k_f, f) + os.replace(k_f, f) # Generate the report if self.report: drill_report_file = self.expand_filename(output_dir, self.report, 'drill_report', 'txt') diff --git a/kibot/out_any_layer.py b/kibot/out_any_layer.py index 17452133..3c2bf876 100644 --- a/kibot/out_any_layer.py +++ b/kibot/out_any_layer.py @@ -157,7 +157,7 @@ class AnyLayerOptions(VariantOptions): plot_ctrl.PlotLayer() plot_ctrl.ClosePlot() if self.output and k_filename != filename: - os.rename(k_filename, filename) + os.replace(k_filename, filename) if create_job: jobfile_writer.AddGbrFile(id, os.path.basename(filename)) generated[la.layer] = os.path.basename(filename) diff --git a/kibot/out_ibom.py b/kibot/out_ibom.py index ab1855ba..d8aae84c 100644 --- a/kibot/out_ibom.py +++ b/kibot/out_ibom.py @@ -239,7 +239,7 @@ class IBoMOptions(VariantOptions): logger.debug('Output from command:\n'+cmd_output_dec+'\n') if output: logger.debug('Renaming output file: {} -> {}'.format(cur, output)) - os.rename(cur, output) + os.replace(cur, output) @output_class diff --git a/kibot/out_kibom.py b/kibot/out_kibom.py index c85ea94e..b1a5345a 100644 --- a/kibot/out_kibom.py +++ b/kibot/out_kibom.py @@ -423,7 +423,7 @@ class KiBoMOptions(BaseOptions): if m and m.group(1) != output: cur = m.group(1) logger.debug('Renaming output file: {} -> {}'.format(cur, output)) - os.rename(cur, output) + os.replace(cur, output) logger.debug('Output from command:\n'+cmd_output.decode()) diff --git a/tests/test_plot/test_pdf.py b/tests/test_plot/test_pdf.py index 69069601..d83694cd 100644 --- a/tests/test_plot/test_pdf.py +++ b/tests/test_plot/test_pdf.py @@ -43,7 +43,7 @@ def test_pdf_refill_2(test_dir): assert os.path.isfile(bkp) finally: if os.path.isfile(bkp): - os.rename(bkp, ori) + os.replace(bkp, ori) ctx.clean_up() diff --git a/tests/test_plot/test_preflight.py b/tests/test_plot/test_preflight.py index dc490375..d6f22817 100644 --- a/tests/test_plot/test_preflight.py +++ b/tests/test_plot/test_preflight.py @@ -161,7 +161,7 @@ def test_update_xml_1(test_dir): ctx = context.TestContext(test_dir, prj, 'update_xml', '') # The XML should be created where the schematic is located xml = os.path.abspath(os.path.join(ctx.get_board_dir(), prj+'.xml')) - os.rename(xml, xml+'-bak') + os.replace(xml, xml+'-bak') try: ctx.run() # Check all outputs are there @@ -171,7 +171,7 @@ def test_update_xml_1(test_dir): logging.debug(os.path.basename(xml)+' OK') finally: os.remove(xml) - os.rename(xml+'-bak', xml) + os.replace(xml+'-bak', xml) ctx.clean_up() @@ -246,7 +246,7 @@ def test_sch_replace_1(test_dir): assert m.group(1) == text finally: for k, v in files.items(): - os.rename(v, k) + os.replace(v, k) ctx.clean_up() @@ -276,7 +276,7 @@ def test_pcb_replace_1(test_dir): assert m is not None assert m.group(1) == text finally: - os.rename(file_back, file) + os.replace(file_back, file) ctx.clean_up(keep_project=True) @@ -304,7 +304,7 @@ def test_set_text_variables_1(test_dir): assert 'Comment4' in data['text_variables'] assert data['text_variables']['Comment4'] == text finally: - os.rename(file_back, file) + os.replace(file_back, file) ctx.expect_out_file(prj+'-bom_'+hash+'.csv') ctx.clean_up(keep_project=True) @@ -329,6 +329,6 @@ def test_set_text_variables_2(test_dir): assert 'text_variables' in data assert 'Comment4' not in data['text_variables'] finally: - os.rename(file_back, file) + os.replace(file_back, file) ctx.expect_out_file(prj+'-bom_'+hash+'.csv') ctx.clean_up(keep_project=True)