Added support for recording dependencies while compiling OpenSCAD files.
This commit is contained in:
parent
60b25ad13d
commit
f9fa53eef8
5 changed files with 72 additions and 16 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import contextlib, subprocess, tempfile, shutil
|
||||
import contextlib, subprocess, tempfile, shutil, re, os
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
|
|
@ -16,3 +16,21 @@ def command(args):
|
|||
process.wait()
|
||||
|
||||
assert not process.returncode
|
||||
|
||||
|
||||
def bash_escape_string(string):
|
||||
return "'{}'".format(re.sub("'", "'\"'\"'", string))
|
||||
|
||||
|
||||
def write_file(path, data):
|
||||
temp_path = path + '~'
|
||||
|
||||
with open(temp_path, 'wb') as file:
|
||||
file.write(data)
|
||||
|
||||
os.rename(temp_path, path)
|
||||
|
||||
|
||||
def read_file(path):
|
||||
with open(path, 'rb') as file:
|
||||
return file.read()
|
||||
|
|
|
|||
0
support/openscad/__init__.py
Normal file
0
support/openscad/__init__.py
Normal file
37
support/openscad/__main__.py
Normal file
37
support/openscad/__main__.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import os, sys
|
||||
from lib import util
|
||||
|
||||
|
||||
def _openscad(in_path, out_path, deps_path):
|
||||
util.command([os.environ['OPENSCAD'], '-o', out_path, '-d', deps_path, in_path])
|
||||
|
||||
|
||||
def _write_dependencies(path, target, dependencies):
|
||||
util.write_file(path, '{}: {}\n'.format(target, ' '.join(dependencies)).encode())
|
||||
|
||||
|
||||
def main(in_path, out_path, deps_path):
|
||||
cwd = os.getcwd()
|
||||
|
||||
def relpath(path):
|
||||
return os.path.relpath(path, cwd)
|
||||
|
||||
with util.TemporaryDirectory() as temp_dir:
|
||||
temp_deps_path = os.path.join(temp_dir, 'deps')
|
||||
temp_mk_path = os.path.join(temp_dir, 'mk')
|
||||
temp_files_path = os.path.join(temp_dir, 'files')
|
||||
|
||||
_openscad(in_path, out_path, temp_deps_path)
|
||||
|
||||
mk_content = '%:; echo "$@" >> {}'.format(util.bash_escape_string(temp_files_path))
|
||||
|
||||
util.write_file(temp_mk_path, mk_content.encode())
|
||||
util.command(['make', '-s', '-B', '-f', temp_mk_path, '-f', temp_deps_path])
|
||||
|
||||
deps = set(map(relpath, util.read_file(temp_files_path).decode().splitlines()))
|
||||
ignored_files = set(map(relpath, [temp_deps_path, temp_mk_path, in_path, out_path]))
|
||||
|
||||
_write_dependencies(deps_path, relpath(out_path), deps - ignored_files)
|
||||
|
||||
|
||||
main(*sys.argv[1:])
|
||||
Loading…
Add table
Add a link
Reference in a new issue