[Diff][Fixed] Problems when missing file from repo

- When comparing to a repo point where the PCB/SCH didn't exist yet.
Closes #323
This commit is contained in:
Salvador E. Tropea 2022-11-05 14:37:19 -03:00
parent e7b35da4e1
commit c3f426a1d5
10 changed files with 105 additions and 1 deletions

View File

@ -44,7 +44,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
KiCad 5.
- SVG, PCB_Print, PcbDraw: Problems to display the outputs using Chrome and
Firefox.
- Diff: Problems when comparing to a repo point where the PCB/SCH didn't exist
yet. (#323)
## [1.4.0] - 2022-10-12
### Added

View File

@ -104,6 +104,10 @@ class DiffOptions(BaseOptions):
Note that when no differeces are found we get a page saying *No diff* """
self.only_first_sch_page = False
""" Compare only the main schematic page (root page) """
self.always_fail_if_missing = False
""" Always fail if the old/new file doesn't exist. Currently we don't fail if they are from a repo.
So if you refer to a repo point where the file wasn't created KiBot will use an empty file.
Enabling this option KiBot will report an error """
super().__init__()
self._expand_id = 'diff'
self._expand_ext = 'pdf'
@ -156,6 +160,13 @@ class DiffOptions(BaseOptions):
else:
GS.check_pcb()
name = GS.pcb_file
if not os.path.isfile(name):
if self.always_fail_if_missing:
raise KiPlotConfigurationError('Missing file to compare: `{}`'.format(name))
with NamedTemporaryFile(mode='w', suffix='.kicad_pcb', delete=False) as f:
f.write("(kicad_pcb (version 20171130) (host pcbnew 5.1.5))\n")
name = f.name
self._to_remove.append(name)
hash = self.get_digest(name)
self.add_to_cache(name, hash)
return hash
@ -167,6 +178,26 @@ class DiffOptions(BaseOptions):
else:
GS.check_sch()
name = GS.sch_file
ext = os.path.splitext(name)[1]
if not os.path.isfile(name):
if self.always_fail_if_missing:
raise KiPlotConfigurationError('Missing file to compare: `{}`'.format(name))
with NamedTemporaryFile(mode='w', suffix=ext, delete=False) as f:
logger.debug('Creating empty schematic: '+f.name)
if ext == '.kicad_sch':
f.write("(kicad_sch (version 20211123) (generator eeschema))\n")
else:
f.write("EESchema Schematic File Version 4\nEELAYER 30 0\nEELAYER END\n$Descr A4 11693 8268\n"
"$EndDescr\n$EndSCHEMATC\n")
name = f.name
self._to_remove.append(name)
if ext != '.kicad_sch':
lib_name = os.path.splitext(name)[0]+'-cache.lib'
if not os.path.isfile(lib_name):
logger.debug('Creating dummy cache lib: '+lib_name)
with open(lib_name, 'w') as f:
f.write("EESchema-LIBRARY Version 2.4\n#\n#End Library\n")
self._to_remove.append(lib_name)
# Schematics can have sub-sheets
sch = load_any_sch(name, os.path.splitext(os.path.basename(name))[0])
files = sch.get_files()
@ -441,6 +472,7 @@ class DiffOptions(BaseOptions):
def run(self, name):
self.command = self.ensure_tool('KiDiff')
self._to_remove = []
if self.old_type == 'git' or self.new_type == 'git':
self.git_command = self.ensure_tool('Git')
if not self.pcb:
@ -484,6 +516,8 @@ class DiffOptions(BaseOptions):
rmtree(d)
if self.incl_file:
os.remove(self.incl_file)
for f in self._to_remove:
os.remove(f)
@output_class

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1 @@
../5_1_6/light_control-diff_sch.pdf

View File

@ -0,0 +1 @@
../5_1_6/light_control-only_new.pdf

Binary file not shown.

Binary file not shown.

View File

@ -1405,6 +1405,48 @@ def test_diff_git_3(test_dir):
ctx.clean_up(keep_project=True)
def test_diff_git_4(test_dir):
""" Difference between the two repo points, wipe the current file, the first is missing """
prj = 'light_control'
yaml = 'diff_git_4'
ctx = context.TestContext(test_dir, prj, yaml)
# Create a git repo
repo_dir = os.path.join(ctx.output_dir, 'repo')
os.makedirs(repo_dir)
git_init(ctx, repo_dir)
# Copy the "old" file
pcb = prj+'.kicad_pcb'
sch = prj+context.KICAD_SCH_EXT
file = os.path.join(repo_dir, pcb)
# Create a dummy
dummy = os.path.join(repo_dir, 'dummy')
with open(dummy, 'wt') as f:
f.write('dummy\n')
# Add it to the repo
ctx.run_command(['git', 'add', 'dummy'], chdir_out=repo_dir)
ctx.run_command(['git', 'commit', '-m', 'Empty repo'], chdir_out=repo_dir)
# Tag it
ctx.run_command(['git', 'tag', '-a', 'v1', '-m', 'Tag description'], chdir_out=repo_dir)
# Add an extra commit, now with the files
shutil.copy2(ctx.board_file, file)
shutil.copy2(ctx.board_file.replace('.kicad_pcb', context.KICAD_SCH_EXT),
file.replace('.kicad_pcb', context.KICAD_SCH_EXT))
ctx.run_command(['git', 'add', sch, pcb], chdir_out=repo_dir)
ctx.run_command(['git', 'commit', '-m', 'Filled repo'], chdir_out=repo_dir)
# Copy the "new" file
shutil.copy2(ctx.board_file.replace(prj, prj+'_diff'), file)
# Add it to the repo
ctx.run_command(['git', 'add', pcb], chdir_out=repo_dir)
ctx.run_command(['git', 'commit', '-m', 'New version'], chdir_out=repo_dir)
# Now just wipe the current file
shutil.copy2(ctx.board_file.replace(prj, '3Rs'), file)
# Run the test
ctx.run(extra=['-b', file], no_board_file=True, extra_debug=True)
ctx.compare_pdf(prj+'-diff_pcb.pdf', prj+'-only_new.pdf', off_y=OFFSET_Y, tol=DIFF_TOL)
ctx.compare_pdf(prj+'-diff_sch.pdf', off_y=OFFSET_Y, tol=DIFF_TOL)
ctx.clean_up(keep_project=True)
@pytest.mark.slow
@pytest.mark.eeschema
def test_diff_file_sch_1(test_dir):

View File

@ -0,0 +1,25 @@
kibot:
version: 1
outputs:
- name: 'diff_pcb'
comment: "PCB difference with git HEAD"
type: diff
layers: ['F.Cu', 'In1.Cu']
options:
old: v1
old_type: git
new: HEAD
new_type: git
cache_dir: .cache
- name: 'diff_sch'
comment: "SCH difference with git HEAD"
type: diff
options:
pcb: false
old: v1
old_type: git
new: HEAD
new_type: git
# always_fail_if_missing: true