diff --git a/scripts/exports.py b/scripts/exports.py index bd34a5e..5d0f114 100644 --- a/scripts/exports.py +++ b/scripts/exports.py @@ -28,6 +28,7 @@ from set_config import * import time import times from deps import * +from tmpdir import * import json import shutil @@ -65,8 +66,11 @@ def make_parts(target, part_type, parts = None): target_dir = top_dir + part_type + 's' deps_dir = target_dir + "/deps" bom_dir = top_dir + "bom" + tmp_dir = mktmpdir(top_dir) + if not os.path.isdir(target_dir): os.makedirs(target_dir) + if not os.path.isdir(deps_dir): os.makedirs(deps_dir) @@ -131,9 +135,9 @@ def make_parts(target, part_type, parts = None): # # make a file to use the module # - part_maker_name = part_type + ".scad" + part_maker_name = tmp_dir + '/' + part_type + ".scad" with open(part_maker_name, "w") as f: - f.write("use <%s/%s>\n" % (dir, filename)) + f.write("use <%s/%s>\n" % (reltmp(dir, target), filename)) f.write("%s();\n" % module); t = time.time() openscad.run("-D$bom=1", "-d", dname, "-o", part_file, part_maker_name) @@ -150,6 +154,10 @@ def make_parts(target, part_type, parts = None): with open(bounds_fname, 'w') as outfile: json.dump(bounds_map, outfile, indent = 4) # + # Remove tmp dir + # + rmtmpdir(tmp_dir) + # # List the ones we didn't find # if targets: diff --git a/scripts/render.py b/scripts/render.py index 9e35938..4f2926e 100755 --- a/scripts/render.py +++ b/scripts/render.py @@ -30,6 +30,7 @@ from tests import do_cmd, update_image, colour_scheme, background from deps import mtime from colorama import init import json +from tmpdir import * def usage(): print("\nusage:\n\trender [target_config] - Render images of the stl and dxf files."); @@ -40,6 +41,7 @@ def render(target, type): # Make the target directory # top_dir = set_config(target, usage) + tmp_dir = mktmpdir(top_dir) target_dir = top_dir + type + 's' bom_dir = top_dir + 'bom' if not os.path.isdir(target_dir): @@ -80,7 +82,7 @@ def render(target, type): # make a file to import the stl # if mtime(part_file) > mtime(png_name): - png_maker_name = "png.scad" + png_maker_name = tmp_dir + "/png.scad" pp1 = [0, 146/255, 0] colour = pp1 if part in colours: @@ -88,15 +90,19 @@ def render(target, type): if not '[' in colour: colour = '"' + colour + '"' with open(png_maker_name, "w") as f: - f.write('color(%s) import("%s");\n' % (colour, part_file)) + f.write('color(%s) import("%s");\n' % (colour, reltmp(part_file, target))) cam = "--camera=0,0,0,70,0,315,500" if type == 'stl' else "--camera=0,0,0,0,0,0,500" render = "--preview" if type == 'stl' or colour != pp1 else "--render" - tmp_name = 'tmp.png' + tmp_name = tmp_dir + '/' + part[:-4] + '.png' openscad.run(colour_scheme, "--projection=p", "--imgsize=4096,4096", cam, render, "--autocenter", "--viewall", "-o", tmp_name, png_maker_name); do_cmd(("magick "+ tmp_name + " -trim -resize 280x280 -background %s -gravity Center -extent 280x280 -bordercolor %s -border 10 %s" % (background, background, tmp_name)).split()) update_image(tmp_name, png_name) os.remove(png_maker_name) + # + # Remove tmp dir + # + rmtmpdir(tmp_dir) if __name__ == '__main__': init() diff --git a/scripts/tests.py b/scripts/tests.py index 1f6ea08..677661a 100755 --- a/scripts/tests.py +++ b/scripts/tests.py @@ -34,6 +34,7 @@ import shutil from deps import * from blurb import * from colorama import Fore +from tmpdir import * w = 4096 h = w @@ -94,6 +95,7 @@ def usage(): def tests(tests): scad_dir = "tests" + tmp_dir = mktmpdir(scad_dir + '/') deps_dir = scad_dir + "/deps" png_dir = scad_dir + "/png" bom_dir = scad_dir + "/bom" @@ -234,7 +236,7 @@ def tests(tests): if changed: print(changed) t = time.time() - tmp_name = 'tmp.png' + tmp_name = tmp_dir + '/tmp.png' openscad.run_list(options.list() + ["-D$bom=2", colour_scheme, "--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", tmp_name, "-trim", "-resize", "1000x600", "-bordercolor", background, "-border", "10", tmp_name]) @@ -303,6 +305,11 @@ def tests(tests): with open(doc_base_name + ".html", "wt") as html_file: do_cmd(("python -m markdown -x tables " + doc_name).split(), html_file) times.print_times() + # + # Remove tmp dir + # + rmtmpdir(tmp_dir) + do_cmd(('codespell -L od ' + doc_name).split()) if __name__ == '__main__': diff --git a/scripts/tmpdir.py b/scripts/tmpdir.py new file mode 100644 index 0000000..f19681d --- /dev/null +++ b/scripts/tmpdir.py @@ -0,0 +1,40 @@ +# +# NopSCADlib Copyright Chris Palmer 2021 +# nop.head@gmail.com +# hydraraptor.blogspot.com +# +# This file is part of NopSCADlib. +# +# NopSCADlib is free software: you can redistribute it and/or modify it under the terms of the +# GNU General Public License as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# NopSCADlib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; +# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with NopSCADlib. +# If not, see . +# +""" +Make a directory for tmp files. +""" +import os +import time + +def mktmpdir(top_dir): + tmp_dir = top_dir + 'tmp' + if not os.path.isdir(tmp_dir): + os.makedirs(tmp_dir) + else: + for file in os.listdir(tmp_dir): + os.remove(tmp_dir + '/' + file) + return tmp_dir + +def reltmp(dir, target): + return dir if os.path.isabs(dir) else '../../' + dir if target else '../' + dir + +def rmtmpdir(tmp_dir): + os.rmdir(tmp_dir) + while os.path.isdir(tmp_dir): + time.sleep(0.1) diff --git a/scripts/views.py b/scripts/views.py index 91080a9..c5dbaf7 100755 --- a/scripts/views.py +++ b/scripts/views.py @@ -38,6 +38,7 @@ import shutil import re import copy from colorama import Fore +from tmpdir import * def is_assembly(s): return s[-9:] == '_assembly' or s[-11:] == '_assemblies' @@ -129,6 +130,7 @@ def views(target, do_assemblies = None): # Make the target directory # top_dir = set_config(target, usage) + tmp_dir = mktmpdir(top_dir) target_dir = top_dir + 'assemblies' deps_dir = target_dir + "/deps" bom_dir = top_dir + "bom" @@ -204,15 +206,15 @@ def views(target, do_assemblies = None): changed = check_deps(png_name, dname) changed = times.check_have_time(changed, png_name) changed = options.have_changed(changed, png_name) - tmp_name = 'tmp.png' + tmp_name = tmp_dir + '/' + real_name + '.png' if changed: print(changed) # # make a file to use the module # - png_maker_name = 'png.scad' + png_maker_name = tmp_dir + '/png.scad' with open(png_maker_name, "w") as f: - f.write("use <%s/%s>\n" % (dir, filename)) + f.write("use <%s/%s>\n" % (reltmp(dir, target), filename)) f.write("%s();\n" % module); t = time.time() target_def = ['-D$target="%s"' % target] if target else [] @@ -439,6 +441,10 @@ def views(target, do_assemblies = None): dst.write(line) i += 1 # + # Remove tmp dir + # + rmtmpdir(tmp_dir) + # # Spell check # do_cmd(('codespell -L od ' + top_dir + 'readme.md').split())