MCAD-Library/openscad_utils.py

65 lines
2 KiB
Python
Raw Permalink Normal View History

2018-09-23 12:03:17 -05:00
import py, re, os, signal, time, subprocess, sys
from subprocess import Popen, PIPE
2010-07-26 09:35:32 -05:00
mod_re = (r"\bmodule\s+(", r")\s*\(\s*")
func_re = (r"\bfunction\s+(", r")\s*\(")
def extract_definitions(fpath, name_re=r"\w+", def_re=""):
regex = name_re.join(def_re)
2010-07-26 09:35:32 -05:00
matcher = re.compile(regex)
return (m.group(1) for m in matcher.finditer(fpath.read()))
def extract_mod_names(fpath, name_re=r"\w+"):
return extract_definitions(fpath, name_re=name_re, def_re=mod_re)
2010-07-26 09:35:32 -05:00
def extract_func_names(fpath, name_re=r"\w+"):
return extract_definitions(fpath, name_re=name_re, def_re=func_re)
2010-07-26 09:35:32 -05:00
def collect_test_modules(dirpath=None):
dirpath = dirpath or py.path.local("./")
2018-09-23 12:03:17 -05:00
print("Collecting openscad test module names")
2010-07-26 09:35:32 -05:00
test_files = {}
for fpath in dirpath.visit('*.scad'):
2018-09-23 12:03:17 -05:00
#print(fpath)
2010-07-26 09:35:32 -05:00
modules = extract_mod_names(fpath, r"test\w*")
#functions = extract_func_names(fpath, r"test\w*")
test_files[fpath] = modules
return test_files
class Timeout(Exception): pass
def call_openscad(path, stlpath, timeout=5):
2011-09-07 20:52:11 -05:00
if sys.platform == 'darwin': exe = 'OpenSCAD.app/Contents/MacOS/OpenSCAD'
else: exe = 'openscad'
2018-09-23 12:03:17 -05:00
command = [exe, '-o', str(stlpath), str(path)]
print(command)
if timeout:
try:
proc = Popen(command,
stdout=PIPE, stderr=PIPE, close_fds=True)
calltime = time.time()
time.sleep(0.05)
2018-09-23 12:03:17 -05:00
#print(calltime)
while True:
if proc.poll() is not None:
break
time.sleep(0.5)
2018-09-23 12:03:17 -05:00
#print(time.time())
if time.time() > calltime + timeout:
raise Timeout()
finally:
try:
proc.terminate()
proc.kill()
except OSError:
pass
return (proc.returncode,) + proc.communicate()
else:
2018-09-23 12:03:17 -05:00
output = subprocess.getstatusoutput(" ".join(command)).decode("utf-8")
return output + ('', '')
def parse_output(text):
pass