Added pre-flight to replace in the PCB
- Also added some constrains to what can be substituted to avoid corrupting the files.
This commit is contained in:
parent
f9239349af
commit
6a691cf362
|
|
@ -46,7 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
- Another experimental mechanism to change 3D models according to the variant.
|
- Another experimental mechanism to change 3D models according to the variant.
|
||||||
(#103)
|
(#103)
|
||||||
- A mechanism to avoid running some outputs by default. (#112)
|
- A mechanism to avoid running some outputs by default. (#112)
|
||||||
- New pre-flight command to replace tags in the schematic. (#93)
|
- New pre-flight commands to replace tags in the schematic and PCB. (#93)
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- Internal BoM: now components with different Tolerance, Voltage, Current
|
- Internal BoM: now components with different Tolerance, Voltage, Current
|
||||||
|
|
|
||||||
17
README.md
17
README.md
|
|
@ -117,6 +117,23 @@ This section is used to specify tasks that will be executed before generating an
|
||||||
- `regex`: [string='None'] Regular expression to match the text for the error we want to exclude.
|
- `regex`: [string='None'] Regular expression to match the text for the error we want to exclude.
|
||||||
- *regexp*: Alias for regex.
|
- *regexp*: Alias for regex.
|
||||||
- `ignore_unconnected`: [boolean=false] Option for `run_drc`. Ignores the unconnected nets. Useful if you didn't finish the routing.
|
- `ignore_unconnected`: [boolean=false] Option for `run_drc`. Ignores the unconnected nets. Useful if you didn't finish the routing.
|
||||||
|
- `pcb_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_PCB_NAME
|
||||||
|
Will return the date in YYYY-MM-DD format.
|
||||||
|
date -d @`git log -1 --format='%at' -- $KIBOT_PCB_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_PCB_NAME variable is the name of the current PCB.
|
||||||
|
- `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.
|
||||||
- `run_drc`: [boolean=false] Runs the DRC (Distance Rules Check). To ensure we have a valid PCB.
|
- `run_drc`: [boolean=false] Runs the DRC (Distance Rules Check). To ensure we have a valid PCB.
|
||||||
The report file name is controlled by the global output pattern (%i=drc %x=txt).
|
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.
|
- `run_erc`: [boolean=false] Runs the ERC (Electrical Rules Check). To ensure the schematic is electrically correct.
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,15 @@ preflight:
|
||||||
regex: 'Regular expression to match'
|
regex: 'Regular expression to match'
|
||||||
# [boolean=false] Option for `run_drc`. Ignores the unconnected nets. Useful if you didn't finish the routing.
|
# [boolean=false] Option for `run_drc`. Ignores the unconnected nets. Useful if you didn't finish the routing.
|
||||||
ignore_unconnected: false
|
ignore_unconnected: false
|
||||||
|
# [dict] Replaces tags in the schematic. I.e. to insert the git hash or last revision date.
|
||||||
|
pcb_replace:
|
||||||
|
date_command: "git log -1 --format='%as' -- $KIBOT_PCB_NAME"
|
||||||
|
replace_tags:
|
||||||
|
- tag: '@git_hash@'
|
||||||
|
command: 'git log -1 --format="%h" $KIBOT_PCB_NAME'
|
||||||
|
before: 'Git hash: <'
|
||||||
|
after: '>'
|
||||||
|
|
||||||
# [boolean=false] Runs the DRC (Distance Rules Check). To ensure we have a valid PCB.
|
# [boolean=false] Runs the DRC (Distance Rules Check). To ensure we have a valid PCB.
|
||||||
# The report file name is controlled by the global output pattern (%i=drc %x=txt).
|
# The report file name is controlled by the global output pattern (%i=drc %x=txt).
|
||||||
run_drc: true
|
run_drc: true
|
||||||
|
|
|
||||||
|
|
@ -211,6 +211,7 @@ W_NOENDLIB = '(W070) '
|
||||||
W_NEEDSPCB = '(W071) '
|
W_NEEDSPCB = '(W071) '
|
||||||
W_NOGLOBALS = '(W072) '
|
W_NOGLOBALS = '(W072) '
|
||||||
W_EMPTREP = '(W073) '
|
W_EMPTREP = '(W073) '
|
||||||
|
W_BADCHARS = '(W074) '
|
||||||
|
|
||||||
|
|
||||||
class Rect(object):
|
class Rect(object):
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ from .gs import GS
|
||||||
from .error import KiPlotConfigurationError
|
from .error import KiPlotConfigurationError
|
||||||
from .optionable import Optionable
|
from .optionable import Optionable
|
||||||
from .kiplot import load_sch
|
from .kiplot import load_sch
|
||||||
from .misc import W_EMPTREP, FAILED_EXECUTE
|
from .misc import W_EMPTREP, FAILED_EXECUTE, W_BADCHARS
|
||||||
from .macros import macros, document, pre_class # noqa: F401
|
from .macros import macros, document, pre_class # noqa: F401
|
||||||
from . import log
|
from . import log
|
||||||
|
|
||||||
|
|
@ -39,6 +39,7 @@ class TagReplace(Optionable):
|
||||||
""" Text to add before the output of `command` """
|
""" Text to add before the output of `command` """
|
||||||
self.after = ''
|
self.after = ''
|
||||||
""" Text to add after the output of `command` """
|
""" Text to add after the output of `command` """
|
||||||
|
self._relax_check = False
|
||||||
|
|
||||||
def config(self, parent):
|
def config(self, parent):
|
||||||
super().config(parent)
|
super().config(parent)
|
||||||
|
|
@ -110,6 +111,12 @@ class SCH_Replace(BasePreFlight): # noqa: F821
|
||||||
continue
|
continue
|
||||||
text = result.stdout.strip()
|
text = result.stdout.strip()
|
||||||
text = r.before + text + r.after
|
text = r.before + text + r.after
|
||||||
|
if not r._relax_check:
|
||||||
|
new_text = re.sub(r'["\\\\\s]', '_', text)
|
||||||
|
if new_text != text:
|
||||||
|
logger.warning(W_BADCHARS+"Replace text can't contain double quotes, backslashes or white spaces ({})".
|
||||||
|
format(text))
|
||||||
|
text = new_text
|
||||||
logger.debug('- ' + r.tag + ' -> ' + text)
|
logger.debug('- ' + r.tag + ' -> ' + text)
|
||||||
content = re.sub(r.tag, text, content, flags=re.MULTILINE)
|
content = re.sub(r.tag, text, content, flags=re.MULTILINE)
|
||||||
os.rename(file, file + '-bak')
|
os.rename(file, file + '-bak')
|
||||||
|
|
@ -125,6 +132,7 @@ class SCH_Replace(BasePreFlight): # noqa: F821
|
||||||
t.command = o.date_command
|
t.command = o.date_command
|
||||||
t.before = 'Date "'
|
t.before = 'Date "'
|
||||||
t.after = '"'
|
t.after = '"'
|
||||||
|
t._relax_check = True
|
||||||
o.replace_tags.append(t)
|
o.replace_tags.append(t)
|
||||||
load_sch()
|
load_sch()
|
||||||
os.environ['KIBOT_TOP_SCH_NAME'] = GS.sch_file
|
os.environ['KIBOT_TOP_SCH_NAME'] = GS.sch_file
|
||||||
|
|
|
||||||
|
|
@ -189,10 +189,40 @@ def test_sch_replace_1(test_dir):
|
||||||
if 'test_v5' in k:
|
if 'test_v5' in k:
|
||||||
cmd = ['/bin/bash', '-c', "git log -1 --format='%h' " + k]
|
cmd = ['/bin/bash', '-c', "git log -1 --format='%h' " + k]
|
||||||
text = run(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True).stdout.strip()
|
text = run(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True).stdout.strip()
|
||||||
m = re.search("Git hash: '(.*)'", c)
|
m = re.search("Git_hash:'(.*)'", c)
|
||||||
logging.debug('Hash: ' + text)
|
logging.debug('Hash: ' + text)
|
||||||
assert m is not None
|
assert m is not None
|
||||||
assert m.group(1) == text
|
assert m.group(1) == text
|
||||||
finally:
|
finally:
|
||||||
for k, v in files.items():
|
for k, v in files.items():
|
||||||
os.rename(v, k)
|
os.rename(v, k)
|
||||||
|
|
||||||
|
|
||||||
|
def test_pcb_replace_1(test_dir):
|
||||||
|
""" Tags replacements in a PCB """
|
||||||
|
prj = 'light_control'
|
||||||
|
ctx = context.TestContext(test_dir, 'test_pcb_replace_1', prj, 'pcb_replace_1', '')
|
||||||
|
ctx.run(extra=[])
|
||||||
|
files = {}
|
||||||
|
file = ctx.board_file
|
||||||
|
file_back = file + '-bak'
|
||||||
|
assert os.path.isfile(file_back), v
|
||||||
|
assert os.path.getsize(file_back) > 0
|
||||||
|
try:
|
||||||
|
logging.debug(file)
|
||||||
|
cmd = ['/bin/bash', '-c', "date -d @`git log -1 --format='%at' -- " + file + "` +%Y-%m-%d_%H-%M-%S"]
|
||||||
|
text = run(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True).stdout.strip()
|
||||||
|
with open(file, 'rt') as f:
|
||||||
|
c = f.read()
|
||||||
|
m = re.search(r'^ \(date (\S+|"(?:[^"]|\\")+")\)$', c, re.MULTILINE)
|
||||||
|
logging.debug('Date: ' + text)
|
||||||
|
assert m is not None
|
||||||
|
assert m.group(1) == '"' + text + '"'
|
||||||
|
cmd = ['/bin/bash', '-c', "git log -1 --format='%h' " + file]
|
||||||
|
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:
|
||||||
|
os.rename(file_back, file)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
# Example KiBot config file
|
||||||
|
kibot:
|
||||||
|
version: 1
|
||||||
|
|
||||||
|
preflight:
|
||||||
|
pcb_replace:
|
||||||
|
date_command: date -d @`git log -1 --format='%at' -- $KIBOT_PCB_NAME` +%Y-%m-%d_%H-%M-%S
|
||||||
|
replace_tags:
|
||||||
|
- tag: "Comment4"
|
||||||
|
command: git log -1 --format="%h" $KIBOT_PCB_NAME
|
||||||
|
before: "Git_hash:'"
|
||||||
|
after: "'"
|
||||||
|
|
@ -8,5 +8,5 @@ preflight:
|
||||||
replace_tags:
|
replace_tags:
|
||||||
- tag: "Comment4"
|
- tag: "Comment4"
|
||||||
command: git log -1 --format="%h" $KIBOT_SCH_NAME
|
command: git log -1 --format="%h" $KIBOT_SCH_NAME
|
||||||
before: "Git hash: '"
|
before: "Git_hash:'"
|
||||||
after: "'"
|
after: "'"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue