diff --git a/src/cq_cli/cqcodecs/codec_helpers.py b/src/cq_cli/cqcodecs/codec_helpers.py index 8cfc228..c7c50b5 100644 --- a/src/cq_cli/cqcodecs/codec_helpers.py +++ b/src/cq_cli/cqcodecs/codec_helpers.py @@ -1,10 +1,43 @@ from contextlib import contextmanager, redirect_stderr, redirect_stdout from os import devnull - +import tempfile +import shutil +import logging @contextmanager def suppress_stdout_stderr(): """A context manager that redirects stdout and stderr to devnull""" + logging.info("STDOUT and STDERR are send to devnull.") with open(devnull, "w") as fnull: with redirect_stderr(fnull) as err, redirect_stdout(fnull) as out: yield (err, out) + + +class temp_dir(): + def __init__(self): + """ + A function to create temp codec files inside temporary directories, and + enable multiple codec processing, preventing file writing racing conditions. + Returns + ------- + str, Temporary directory path. + """ + self.path = tempfile.mkdtemp() + logging.debug(f"temp_dir: {self.path}") + + def dlt(self): + """ + Safe temp_dir deletion and path invalidation. + Returns + ------- + None. + + """ + if self.path: + shutil.rmtree(self.path) + self.path = None + logging.debug("temp_dir.path: deleted") + return True + else: + logging.error("temp_dir.path: does not exist.") + return False \ No newline at end of file diff --git a/src/cq_cli/cqcodecs/cq_codec_dxf.py b/src/cq_cli/cqcodecs/cq_codec_dxf.py index 84e4f11..a190f4d 100644 --- a/src/cq_cli/cqcodecs/cq_codec_dxf.py +++ b/src/cq_cli/cqcodecs/cq_codec_dxf.py @@ -1,12 +1,12 @@ -import os, tempfile +import os from cadquery import exporters import cq_cli.cqcodecs.codec_helpers as helpers def convert(build_result, output_file=None, error_file=None, output_opts=None): # Create a temporary file to put the STL output into - temp_dir = tempfile.gettempdir() - temp_file = os.path.join(temp_dir, "temp_dxf.dxf") + temp_dir = helpers.temp_dir() + temp_file = os.path.join(temp_dir.path, "temp_dxf.dxf") # The exporters will add extra output that we do not want, so suppress it with helpers.suppress_stdout_stderr(): diff --git a/src/cq_cli/cqcodecs/cq_codec_glb.py b/src/cq_cli/cqcodecs/cq_codec_glb.py index f60b831..da84b5d 100644 --- a/src/cq_cli/cqcodecs/cq_codec_glb.py +++ b/src/cq_cli/cqcodecs/cq_codec_glb.py @@ -1,11 +1,11 @@ -import os, tempfile +import os import cq_cli.cqcodecs.codec_helpers as helpers def convert(build_result, output_file=None, error_file=None, output_opts=None): # Create a temporary file to put the STL output into - temp_dir = tempfile.gettempdir() - temp_file = os.path.join(temp_dir, "temp_glb.glb") + temp_dir = helpers.temp_dir() + temp_file = os.path.join(temp_dir.path, "temp_glb.glb") # The exporters will add extra output that we do not want, so suppress it with helpers.suppress_stdout_stderr(): diff --git a/src/cq_cli/cqcodecs/cq_codec_gltf.py b/src/cq_cli/cqcodecs/cq_codec_gltf.py index 3274be9..62c3bbf 100644 --- a/src/cq_cli/cqcodecs/cq_codec_gltf.py +++ b/src/cq_cli/cqcodecs/cq_codec_gltf.py @@ -1,11 +1,11 @@ -import os, tempfile +import os import cq_cli.cqcodecs.codec_helpers as helpers def convert(build_result, output_file=None, error_file=None, output_opts=None): # Create a temporary file to put the STL output into - temp_dir = tempfile.gettempdir() - temp_file = os.path.join(temp_dir, "temp_gltf.gltf") + temp_dir = helpers.temp_dir() + temp_file = os.path.join(temp_dir.path, "temp_gltf.gltf") # The exporters will add extra output that we do not want, so suppress it with helpers.suppress_stdout_stderr(): diff --git a/src/cq_cli/cqcodecs/cq_codec_step.py b/src/cq_cli/cqcodecs/cq_codec_step.py index 9f74a65..23afaa4 100644 --- a/src/cq_cli/cqcodecs/cq_codec_step.py +++ b/src/cq_cli/cqcodecs/cq_codec_step.py @@ -1,13 +1,12 @@ -import os, tempfile +import os from cadquery import exporters -import cadquery as cq import cq_cli.cqcodecs.codec_helpers as helpers def convert(build_result, output_file=None, error_file=None, output_opts=None): # Create a temporary file to put the STL output into - temp_dir = tempfile.gettempdir() - temp_file = os.path.join(temp_dir, "temp_step.step") + temp_dir = helpers.temp_dir() + temp_file = os.path.join(temp_dir.path, "temp_step.step") # The exporters will add extra output that we do not want, so suppress it with helpers.suppress_stdout_stderr(): diff --git a/src/cq_cli/cqcodecs/cq_codec_stl.py b/src/cq_cli/cqcodecs/cq_codec_stl.py index 3f67e64..404ccad 100644 --- a/src/cq_cli/cqcodecs/cq_codec_stl.py +++ b/src/cq_cli/cqcodecs/cq_codec_stl.py @@ -1,13 +1,11 @@ -import os, tempfile -from cadquery import exporters -import cadquery as cq +import os import cq_cli.cqcodecs.codec_helpers as helpers def convert(build_result, output_file=None, error_file=None, output_opts=None): # Create a temporary file to put the STL output into - temp_dir = tempfile.gettempdir() - temp_file = os.path.join(temp_dir, "temp_stl.stl") + temp_dir = helpers.temp_dir() + temp_file = os.path.join(temp_dir.path, "temp_stl.stl") linearDeflection = 0.1 angularDeflection = 0.1 diff --git a/src/cq_cli/cqcodecs/cq_codec_svg.py b/src/cq_cli/cqcodecs/cq_codec_svg.py index bc7d6be..690471a 100644 --- a/src/cq_cli/cqcodecs/cq_codec_svg.py +++ b/src/cq_cli/cqcodecs/cq_codec_svg.py @@ -1,12 +1,12 @@ -import os, tempfile +import os from cadquery import exporters import cq_cli.cqcodecs.codec_helpers as helpers def convert(build_result, output_file=None, error_file=None, output_opts=None): # Create a temporary file to put the STL output into - temp_dir = tempfile.gettempdir() - temp_file = os.path.join(temp_dir, "temp_svg.svg") + temp_dir = helpers.temp_dir() + temp_file = os.path.join(temp_dir.path, "temp_svg.svg") # The exporters will add extra output that we do not want, so suppress it with helpers.suppress_stdout_stderr(): diff --git a/src/cq_cli/cqcodecs/cq_codec_threejs.py b/src/cq_cli/cqcodecs/cq_codec_threejs.py index 15c61bd..4f879ac 100644 --- a/src/cq_cli/cqcodecs/cq_codec_threejs.py +++ b/src/cq_cli/cqcodecs/cq_codec_threejs.py @@ -1,13 +1,12 @@ -import os, tempfile +import os from cadquery import exporters -import cadquery as cq import cq_cli.cqcodecs.codec_helpers as helpers def convert(build_result, output_file=None, error_file=None, output_opts=None): # Create a temporary file to put the STL output into - temp_dir = tempfile.gettempdir() - temp_file = os.path.join(temp_dir, "temp_threejs.json") + temp_dir = helpers.temp_dir() + temp_file = os.path.join(temp_dir.path, "temp_threejs.json") # The exporters will add extra output that we do not want, so suppress it with helpers.suppress_stdout_stderr():