diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f1bd37c..3850067b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Excellon drill: added `route_mode_for_oval_holes` option. - Default global `dir` option. - Global option to specify `out_dir` (like -d command line option) -- Pattern to expand the variant name: %V - 3D view render - SCH PDF Print: monochrome and no frame options. - New expansion patterns: @@ -40,12 +39,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 **%bF**, **%bp** and **%br** board data - **%sc**, **%sC1**, **%sC2**, **%sC3**, **%sC4**, **%sd**, **%sf**, **%sF**, **%sp** and **%sr** schematic data + - **%V** the variant name - Now patterns are also expanded in the out_dir name. - Global options to control the date format. - Outputs can use the options of other outputs as base (extend them). (#112) - Another experimental mechanism to change 3D models according to the variant. (#103) - A mechanism to avoid running some outputs by default. (#112) +- New pre-flight command to replace tags in the schematic. (#93) ### Changed - Internal BoM: now components with different Tolerance, Voltage, Current diff --git a/README.md b/README.md index cfe19460..083848e0 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,24 @@ This section is used to specify tasks that will be executed before generating an The report file name is controlled by the global output pattern (%i=drc %x=txt). - `run_erc`: [boolean=false] Runs the ERC (Electrical Rules Check). To ensure the schematic is electrically correct. The report file name is controlled by the global output pattern (%i=erc %x=txt). +- `sch_replace`: [dict] Replaces tags in the schematic. I.e. to insert the git hash or last revision date. + * Valid keys: + - `date_command`: [string=''] Command to get the date to use in the schematic. + git log -1 --format='%as' -- $KIBOT_SCH_NAME + Will return the date in YYYY-MM-DD format. + date -d @`git log -1 --format='%at' -- $KIBOT_SCH_NAME` +%Y-%m-%d_%H-%M-%S + Will return the date in YYYY-MM-DD_HH-MM-SS format. + - `replace_tags`: [dict|list(dict)] Tag or tags to replace. + * Valid keys: + - `after`: [string=''] Text to add after the output of `command`. + - `before`: [string=''] Text to add before the output of `command`. + - `command`: [string=''] Command to execute to get the text, will be used only if `text` is empty. + KIBOT_SCH_NAME variable is the name of the current sheet. + KIBOT_TOP_SCH_NAME variable is the name of the top sheet. + - `tag`: [string=''] Name of the tag to replace. Use `version` for a tag named `@version@`. + - `tag_delimiter`: [string='@'] Character used to indicate the beginning and the end of a tag. + Don't change it unless you really know about KiCad's file formats. + - `text`: [string=''] Text to insert instead of the tag. - `update_xml`: [boolean=false] Update the XML version of the BoM (Bill of Materials). To ensure our generated BoM is up to date. Note that this isn't needed when using the internal BoM generator (`bom`). diff --git a/docs/samples/generic_plot.kibot.yaml b/docs/samples/generic_plot.kibot.yaml index 721867ac..0f5359ab 100644 --- a/docs/samples/generic_plot.kibot.yaml +++ b/docs/samples/generic_plot.kibot.yaml @@ -23,6 +23,15 @@ preflight: # [boolean=false] Runs the ERC (Electrical Rules Check). To ensure the schematic is electrically correct. # The report file name is controlled by the global output pattern (%i=erc %x=txt). run_erc: true + # [dict] Replaces tags in the schematic. I.e. to insert the git hash or last revision date. + sch_replace: + date_command: "git log -1 --format='%as' -- $KIBOT_SCH_NAME" + replace_tags: + - tag: '@git_hash@' + command: 'git log -1 --format="%h" $KIBOT_SCH_NAME' + before: 'Git hash: <' + after: '>' + # [boolean=false] Update the XML version of the BoM (Bill of Materials). # To ensure our generated BoM is up to date. # Note that this isn't needed when using the internal BoM generator (`bom`). diff --git a/kibot/kicad/v5_sch.py b/kibot/kicad/v5_sch.py index 3f07ff13..6438e978 100644 --- a/kibot/kicad/v5_sch.py +++ b/kibot/kicad/v5_sch.py @@ -1397,6 +1397,8 @@ class SchematicSheet(object): raise SchFileError('Malformed sheet file name', line, f) sch.file = m.group(1) sch.file_size = int(m.group(2)) + if not os.path.isfile(sch.file): + raise SchFileError('Missing sub-sheet `{}`'.format(sch.file), line, f) else: sch.labels.append(SchematicPort.parse(line[1:], f)) line = f.get_line() diff --git a/kibot/misc.py b/kibot/misc.py index bac9c2de..9f891e2b 100644 --- a/kibot/misc.py +++ b/kibot/misc.py @@ -35,6 +35,7 @@ SVG_SCH_PRINT = 21 CORRUPTED_SCH = 22 WRONG_INSTALL = 23 RENDER_3D_ERR = 24 +FAILED_EXECUTE = 25 error_level_to_name = ['NONE', 'INTERNAL_ERROR', 'WRONG_ARGUMENTS', @@ -60,6 +61,7 @@ error_level_to_name = ['NONE', 'CORRUPTED_SCH', 'WRONG_INSTALL', 'RENDER_3D_ERR', + 'FAILED_EXECUTE', ] CMD_EESCHEMA_DO = 'eeschema_do' URL_EESCHEMA_DO = 'https://github.com/INTI-CMNB/KiAuto' @@ -208,6 +210,7 @@ W_NOVARIANTS = '(W069) ' W_NOENDLIB = '(W070) ' W_NEEDSPCB = '(W071) ' W_NOGLOBALS = '(W072) ' +W_EMPTREP = '(W073) ' class Rect(object): diff --git a/tests/test_plot/test_preflight.py b/tests/test_plot/test_preflight.py index 7a2f989c..32dcd430 100644 --- a/tests/test_plot/test_preflight.py +++ b/tests/test_plot/test_preflight.py @@ -16,7 +16,10 @@ pytest-3 --log-cli-level debug import os import sys +import shutil import logging +import re +from subprocess import run, PIPE # Look for the 'utils' module from where the script is running prev_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) if prev_dir not in sys.path: @@ -156,3 +159,41 @@ def test_update_xml_fail(test_dir): ctx = context.TestContext(test_dir, 'Update_XMLFail', prj, 'update_xml', '') ctx.run(BOM_ERROR) ctx.clean_up() + + +def test_sch_replace_1(test_dir): + """ Tags replacements in an schematic """ + prj = 'test_v5' + ctx = context.TestContext(test_dir, 'test_sch_replace_1', prj, 'sch_replace_1', '') + ctx.run(extra=[]) + files = {} + file = ctx.sch_file + files[file] = file + '-bak' + file = ctx.sch_file.replace('test_v5', 'deeper') + files[file] = file + '-bak' + file = ctx.sch_file.replace('test_v5', 'sub-sheet') + files[file] = file + '-bak' + for k, v in files.items(): + assert os.path.isfile(v), v + assert os.path.getsize(v) > 0 + try: + for k, v in files.items(): + logging.debug(k) + cmd = ['/bin/bash', '-c', "date -d @`git log -1 --format='%at' -- " + k + "` +%Y-%m-%d_%H-%M-%S"] + text = run(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True).stdout.strip() + with open(k, 'rt') as f: + c = f.read() + m = re.search('^Date \"(.*)\"$', c, re.MULTILINE) + logging.debug('Date: ' + text) + assert m is not None + assert m.group(1) == text + if 'test_v5' in k: + cmd = ['/bin/bash', '-c', "git log -1 --format='%h' " + k] + text = run(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True).stdout.strip() + m = re.search("Git hash: '(.*)'", c) + logging.debug('Hash: ' + text) + assert m is not None + assert m.group(1) == text + finally: + for k, v in files.items(): + os.rename(v, k) diff --git a/tests/yaml_samples/sch_replace_1.kibot.yaml b/tests/yaml_samples/sch_replace_1.kibot.yaml new file mode 100644 index 00000000..f11a73c6 --- /dev/null +++ b/tests/yaml_samples/sch_replace_1.kibot.yaml @@ -0,0 +1,12 @@ +# Example KiBot config file +kibot: + version: 1 + +preflight: + sch_replace: + date_command: date -d @`git log -1 --format='%at' -- $KIBOT_SCH_NAME` +%Y-%m-%d_%H-%M-%S + replace_tags: + - tag: "Comment4" + command: git log -1 --format="%h" $KIBOT_SCH_NAME + before: "Git hash: '" + after: "'"