From 85538b0b2745b588776c264f6c7fff141e3301f4 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Fri, 19 Aug 2022 09:39:25 -0300 Subject: [PATCH] [Global options] Restore the project at exit Related to #250 --- CHANGELOG.md | 4 ++- README.md | 2 ++ kibot/globals.py | 3 +++ kibot/gs.py | 1 + kibot/kiplot.py | 14 +++++++++- tests/test_plot/test_preflight.py | 25 ++++++++++++++++++ .../set_text_variables_2.kibot.yaml | 26 +++++++++++++++++++ 7 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 tests/yaml_samples/set_text_variables_2.kibot.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md index 4896ba71..e45d1ca3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Try to download missing tools and Python modules. The user also gets more information when something is missing. It can be disabled from the command line. -- A global option to cross components without a body (#219) +- Global options: + - Cross components without a body (#219) + - Restore the project at exit (#250) - Imports: - Now you can nest imports (import from an imported file) (#218) - Preflights can be imported (#181) diff --git a/README.md b/README.md index d8f94ce0..41a9c701 100644 --- a/README.md +++ b/README.md @@ -692,6 +692,8 @@ global: Immersion Ag, ImAu, Immersion Gold, Immersion Au, Immersion Tin, Immersion Nickel, OSP and HT_OSP. - `pcb_material`: [string='FR4'] PCB core material. Currently used for documentation and to choose default colors. Currently known are FR1 to FR5. + - `restore_project`: [boolean=false] Restore the KiCad project after execution. + Note that this option will undo operations like `set_text_variables`. - `silk_screen_color`: [string='white'] Color for the markings. Currently used for documentation and to choose default colors. KiCad 6: you should set this in the Board Setup -> Physical Stackup. Currently known are black and white. diff --git a/kibot/globals.py b/kibot/globals.py index 982d4d59..3397c9b0 100644 --- a/kibot/globals.py +++ b/kibot/globals.py @@ -147,6 +147,9 @@ class Globals(FiltersOptions): self.pcb_material = 'FR4' """ PCB core material. Currently used for documentation and to choose default colors. Currently known are FR1 to FR5 """ + self.restore_project = False + """ Restore the KiCad project after execution. + Note that this option will undo operations like `set_text_variables` """ self.silk_screen_color = 'white' """ Color for the markings. Currently used for documentation and to choose default colors. KiCad 6: you should set this in the Board Setup -> Physical Stackup. diff --git a/kibot/gs.py b/kibot/gs.py index fe92f9a6..60c9976c 100644 --- a/kibot/gs.py +++ b/kibot/gs.py @@ -118,6 +118,7 @@ class GS(object): global_output = None global_pcb_finish = None global_pcb_material = None + global_restore_project = None global_silk_screen_color = None global_silk_screen_color_bottom = None global_silk_screen_color_top = None diff --git a/kibot/kiplot.py b/kibot/kiplot.py index 0a5573f0..9a3e0676 100644 --- a/kibot/kiplot.py +++ b/kibot/kiplot.py @@ -373,7 +373,7 @@ def run_output(out, dont_stop=False): raise -def generate_outputs(outputs, targets, invert, skip_pre, cli_order, no_priority, dont_stop=False): +def _generate_outputs(outputs, targets, invert, skip_pre, cli_order, no_priority, dont_stop): logger.debug("Starting outputs for board {}".format(GS.pcb_file)) # Make a list of target outputs n = len(targets) @@ -432,6 +432,18 @@ def generate_outputs(outputs, targets, invert, skip_pre, cli_order, no_priority, run_output(out, dont_stop) +def generate_outputs(outputs, targets, invert, skip_pre, cli_order, no_priority, dont_stop=False): + prj = None + if GS.global_restore_project: + # Memorize the project content to restore it at exit + prj = GS.read_pro() + try: + _generate_outputs(outputs, targets, invert, skip_pre, cli_order, no_priority, dont_stop) + finally: + # Restore the project file + GS.write_pro(prj) + + def adapt_file_name(name): if not name.startswith('/usr'): name = os.path.relpath(name) diff --git a/tests/test_plot/test_preflight.py b/tests/test_plot/test_preflight.py index dda755da..2a45e46f 100644 --- a/tests/test_plot/test_preflight.py +++ b/tests/test_plot/test_preflight.py @@ -282,3 +282,28 @@ def test_set_text_variables_1(test_dir): os.rename(file_back, file) ctx.expect_out_file(prj+'-bom_'+hash+'.csv') ctx.clean_up(keep_project=True) + + +@pytest.mark.skipif(context.ki5(), reason="KiCad 6 text vars (fail already tested)") +def test_set_text_variables_2(test_dir): + """ KiCad 6 variables, test volatile changes (project restored) """ + prj = 'test_vars' + ctx = context.TestContextSCH(test_dir, prj, 'set_text_variables_2', '') + ctx.run() + file = os.path.join(ctx.get_board_dir(), ctx.board_name+context.PRO_EXT) + file_back = file + '-bak' + assert os.path.isfile(file_back), file_back + assert os.path.getsize(file_back) > 0 + try: + logging.debug(file) + cmd = ['/bin/bash', '-c', "git log -1 --format='%h' " + ctx.sch_file] + hash = run(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True).stdout.strip() + with open(file, 'rt') as f: + c = f.read() + data = json.loads(c) + assert 'text_variables' in data + assert 'Comment4' not in data['text_variables'] + finally: + os.rename(file_back, file) + ctx.expect_out_file(prj+'-bom_'+hash+'.csv') + ctx.clean_up(keep_project=True) diff --git a/tests/yaml_samples/set_text_variables_2.kibot.yaml b/tests/yaml_samples/set_text_variables_2.kibot.yaml new file mode 100644 index 00000000..f0768c19 --- /dev/null +++ b/tests/yaml_samples/set_text_variables_2.kibot.yaml @@ -0,0 +1,26 @@ +# Example KiBot config file +kibot: + version: 1 + +globals: + restore_project: true + +preflight: + set_text_variables: + - variable: "Comment4" + command: git log -1 --format="%h" $KIBOT_SCH_NAME + before: "Git_hash:'" + after: "' (%f)" + - variable: "git_hash" + command: git log -1 --format="%h" $KIBOT_SCH_NAME + +outputs: + - name: 'bom_internal' + comment: "Bill of Materials in CSV format" + type: bom + options: + csv: + hide_pcb_info: true + hide_stats_info: true + output: '%f-%i_%r.%x' + columns: [References, Value]