diff --git a/.gitignore b/.gitignore index 9afdab2..938ac35 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ tests/deps/ *.log *.html times.txt +*_diff.png +*.echo diff --git a/gallery/HydraBot.png b/gallery/HydraBot.png index cb745bd..b364ace 100644 Binary files a/gallery/HydraBot.png and b/gallery/HydraBot.png differ diff --git a/gallery/readme.md b/gallery/readme.md index 62ca8e6..0f008ff 100644 --- a/gallery/readme.md +++ b/gallery/readme.md @@ -7,7 +7,7 @@ Arduino thermostat to control a beer fridge to use it as an environmental chambe --- ## HydraBot -Current state of HydraRaptor after being modified for laser engarving. +Current state of HydraRaptor after being modified for laser engraving. ![](HydraBot.png) diff --git a/scripts/gallery.py b/scripts/gallery.py index 4d08bfe..96faadb 100644 --- a/scripts/gallery.py +++ b/scripts/gallery.py @@ -28,6 +28,7 @@ from colorama import Fore, init from tests import do_cmd import re from shutil import copyfile +from tests import update_image project_dir = '../..' target_dir = 'gallery' @@ -53,7 +54,10 @@ def gallery(): if image.startswith('![Main Assembly](assemblies/'): file = image[17 : -1] line = line.replace(image, '![](%s.png)' % project) - copyfile(path + '/' + file, '%s/%s.png' %(target_dir, project)) + tmp_name = 'tmp.png' + target_name = '%s/%s.png' %(target_dir, project) + copyfile(path + '/' + file, tmp_name) + update_image(tmp_name, target_name) else: line = line.replace(image, '') else: diff --git a/scripts/tests.py b/scripts/tests.py index 754c4d0..63d34bd 100644 --- a/scripts/tests.py +++ b/scripts/tests.py @@ -28,8 +28,10 @@ import bom import times import time import json +import shutil from deps import * from blurb import * +from colorama import Fore w = 4096 h = w @@ -38,7 +40,20 @@ def do_cmd(cmd, output = sys.stdout): for arg in cmd: print(arg, end = " ") print() - subprocess.call(cmd, stdout = output) + return subprocess.call(cmd, stdout = output, stderr = output) + +def update_image(tmp_name, png_name): + """Update an image only if different, otherwise just change the mod time""" + with open(os.devnull, 'w') as null: + diff_name = png_name.replace('.png', '_diff.png') + if not os.path.isfile(png_name) or do_cmd(("magick compare -metric AE %s %s %s" % (png_name, tmp_name, diff_name)).split(), output = null): + shutil.copyfile(tmp_name, png_name) + print(Fore.GREEN + png_name + " updated" + Fore.WHITE) + else: + os.utime(png_name, None) + os.remove(diff_name) + os.remove(tmp_name) + def depluralise(name): if name[-3:] == "ies" and name != "zipties": @@ -159,9 +174,11 @@ def tests(tests): if changed: print(changed) t = time.time() - openscad.run("-D", "$bom=2", "--projection=p", "--imgsize=%d,%d" % (w, h), "--camera=0,0,0,70,0,315,500", "--autocenter", "--viewall", "-d", dname, "-o", png_name, scad_name); + tmp_name = 'tmp.png' + openscad.run("-D", "$bom=2", "--projection=p", "--imgsize=%d,%d" % (w, h), "--camera=0,0,0,70,0,315,500", "--autocenter", "--viewall", "-d", dname, "-o", tmp_name, scad_name); times.add_time(scad_name, t) - do_cmd(["magick", png_name, "-trim", "-resize", "1000x600", "-bordercolor", "#ffffe5", "-border", "10", png_name]) + do_cmd(["magick", tmp_name, "-trim", "-resize", "1000x600", "-bordercolor", "#ffffe5", "-border", "10", tmp_name]) + update_image(tmp_name, png_name) BOM = bom.parse_bom() with open(bom_name, 'wt') as outfile: json.dump(BOM.flat_data(), outfile, indent = 4) diff --git a/scripts/views.py b/scripts/views.py index 8bdb095..60d3a7d 100644 --- a/scripts/views.py +++ b/scripts/views.py @@ -25,7 +25,7 @@ from __future__ import print_function from set_config import * import openscad -from tests import do_cmd +from tests import do_cmd, update_image import time import times from deps import * @@ -33,6 +33,7 @@ import os import json import blurb import bom +import shutil from colorama import Fore def is_assembly(s): @@ -151,12 +152,15 @@ def views(target, do_assemblies = None): if changed: print(changed) t = time.time() - openscad.run("-D$pose=1", "-D$explode=%d" % explode, "--projection=p", "--imgsize=4096,4096", "--autocenter", "--viewall", "-d", dname, "-o", png_name, png_maker_name); + tmp_name = 'tmp.png' + openscad.run("-D$pose=1", "-D$explode=%d" % explode, "--projection=p", "--imgsize=4096,4096", "--autocenter", "--viewall", "-d", dname, "-o", tmp_name, png_maker_name); times.add_time(png_name, t) - do_cmd(["magick", png_name, "-trim", "-resize", "1004x1004", "-bordercolor", "#ffffe5", "-border", "10", png_name]) + do_cmd(["magick", tmp_name, "-trim", "-resize", "1004x1004", "-bordercolor", "#ffffe5", "-border", "10", tmp_name]) + update_image(tmp_name, png_name) tn_name = png_name.replace('.png', '_tn.png') if mtime(png_name) > mtime(tn_name): - do_cmd(("magick "+ png_name + " -trim -resize 280x280 -background #ffffe5 -gravity Center -extent 280x280 -bordercolor #ffffe5 -border 10 " + tn_name).split()) + do_cmd(("magick "+ png_name + " -trim -resize 280x280 -background #ffffe5 -gravity Center -extent 280x280 -bordercolor #ffffe5 -border 10 " + tmp_name).split()) + update_image(tmp_name, tn_name) os.remove(png_maker_name) done_assemblies.append(module) else: