1- import os
21import shutil
3- import subprocess
42from http import HTTPStatus
3+ from subprocess import (DEVNULL ,
4+ check_call ,
5+ TimeoutExpired ,
6+ CalledProcessError )
7+ from os .path import basename , splitext
58from tempfile import NamedTemporaryFile
69
710from mfr .core import exceptions
811from mfr .core .extension import BaseExporter
9- from mfr .extensions .jsc3d .settings import (FREECAD_BIN ,
10- FREECAD_CONVERT_SCRIPT )
12+ from mfr .extensions .jsc3d .settings import (TIMEOUT ,
13+ FREECAD_BIN ,
14+ CONVERSION_SCRIPT )
1115
1216
1317class JSC3DExporter (BaseExporter ):
@@ -22,16 +26,14 @@ def export(self):
2226 temp_source_file .name = self .source_file_path + '.step'
2327 shutil .copy2 (self .source_file_path , temp_source_file .name )
2428
25- subprocess .check_call ([
26- FREECAD_BIN ,
27- FREECAD_CONVERT_SCRIPT ,
28- temp_source_file .name ,
29- self .output_file_path ,
30- # silence output from freecadcmd
31- ], stdout = subprocess .DEVNULL )
29+ check_call (
30+ [FREECAD_BIN , CONVERSION_SCRIPT , temp_source_file .name , self .output_file_path ],
31+ stdout = DEVNULL , # silence output from freecadcmd
32+ timeout = TIMEOUT ,
33+ )
3234
33- except subprocess . CalledProcessError as err :
34- name , extension = os . path . splitext (os . path . split (self .source_file_path )[ - 1 ] )
35+ except CalledProcessError as err :
36+ name , extension = splitext (basename (self .source_file_path ))
3537 raise exceptions .SubprocessError (
3638 'Unable to export the file in the requested format, please try again later.' ,
3739 process = 'freecad' ,
@@ -42,3 +44,19 @@ def export(self):
4244 extension = extension or '' ,
4345 exporter_class = 'jsc3d' ,
4446 )
47+
48+ except TimeoutExpired as err :
49+ name , extension = splitext (basename (self .source_file_path ))
50+ # The return code 52 is not the error code returned by the
51+ # subprocess, but the error given to it by this waterbutler
52+ # processs, for timing out.
53+ raise exceptions .SubprocessError (
54+ 'JSC3D Conversion timed out.' ,
55+ code = HTTPStatus .GATEWAY_TIMEOUT ,
56+ process = 'freecad' ,
57+ cmd = str (err .cmd ),
58+ returncode = 52 ,
59+ path = str (self .source_file_path ),
60+ extension = extension or '' ,
61+ exporter_class = 'jsc3d'
62+ )
0 commit comments