diff --git a/pyproject.toml b/pyproject.toml index c61905d..dcc1e41 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,7 +17,7 @@ classifiers = [ "Operating System :: OS Independent", ] dependencies = [ - "cadquery@git+https://github.com/CadQuery/cadquery.git", + "cadquery==2.4.0", "cadquery_freecad_import_plugin" ] diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..a82b139 --- /dev/null +++ b/setup.py @@ -0,0 +1,38 @@ +from setuptools import setup, find_packages + +setup( + name="cq_cli", + version="2.3.0", + license="LICENSE", + author="Jeremy Wright", + description="Command Line Interface for executing CadQuery scripts and converting their output to another format.", + long_description=open("README.md", encoding="utf-8").read(), + long_description_content_type="text/markdown", + python_requires=">=3.9", + classifiers=[ + "Programming Language :: Python :: 3", + "Operating System :: OS Independent", + ], + packages=find_packages(where="src"), + package_dir={"": "src"}, + install_requires=[ + "cadquery==2.4.0", + "cadquery_freecad_import_plugin", + ], + extras_require={ + "dev": [ + "pytest", + "black==19.10b0", + "click==8.0.4", + ], + }, + entry_points={ + "console_scripts": [ + "cq-cli=cq_cli.main:main", + ], + }, + url="https://github.com/CadQuery/cq-cli", + project_urls={ + "Bug Tracker": "https://github.com/CadQuery/cq-cli/issues", + }, +) 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():