[Global options] Restore the project at exit

Related to #250
This commit is contained in:
Salvador E. Tropea 2022-08-19 09:39:25 -03:00
parent 0180136fd6
commit 85538b0b27
7 changed files with 73 additions and 2 deletions

View File

@ -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)

View File

@ -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.

View File

@ -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.

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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]