apenwarr-redo/redo.py

134 lines
3.4 KiB
Python
Raw Normal View History

#!/usr/bin/python
import sys, os, subprocess
import options
optspec = """
redo [targets...]
--
2010-11-12 07:03:06 -08:00
d,debug print dependency checks as they happen
2010-11-12 07:10:55 -08:00
v,verbose print commands as they are run
"""
o = options.Options('redo', optspec)
(opt, flags, extra) = o.parse(sys.argv[1:])
targets = extra or ['all']
if opt.debug:
os.environ['REDO_DEBUG'] = '1'
if opt.verbose:
os.environ['REDO_VERBOSE'] = '1'
if not os.environ.get('REDO_BASE', ''):
base = os.path.commonprefix([os.path.abspath(os.path.dirname(t))
for t in targets] + [os.getcwd()])
bsplit = base.split('/')
for i in range(len(bsplit)-1, 0, -1):
newbase = '%s/.redo' % '/'.join(bsplit[:i])
if os.path.exists(newbase):
base = newbase
break
os.environ['REDO_BASE'] = base
os.environ['REDO_STARTDIR'] = os.getcwd()
import vars
from helpers import *
2010-11-13 01:21:59 -08:00
class BuildError(Exception):
pass
def find_do_file(t):
2010-11-12 07:03:06 -08:00
dofile = '%s.do' % t
if os.path.exists(dofile):
add_dep(t, 'm', dofile)
return dofile
else:
2010-11-12 07:03:06 -08:00
add_dep(t, 'c', dofile)
return None
2010-11-12 07:03:06 -08:00
def stamp(t):
stampfile = sname('stamp', t)
depfile = sname('dep', t)
2010-11-13 00:11:34 -08:00
if not os.path.exists(vars.BASE + '/.redo'):
# .redo might not exist in a 'make clean' target
return
2010-11-12 07:03:06 -08:00
open(stampfile, 'w').close()
open(depfile, 'a').close()
2010-11-12 07:03:06 -08:00
try:
mtime = os.stat(t).st_mtime
except OSError:
mtime = 0
os.utime(stampfile, (mtime, mtime))
def _preexec(t):
os.environ['REDO_TARGET'] = t
2010-11-13 00:11:34 -08:00
os.environ['REDO_DEPTH'] = vars.DEPTH + ' '
dn = os.path.dirname(t)
if dn:
os.chdir(dn)
def build(t):
unlink(sname('dep', t))
open(sname('dep', t), 'w').close()
dofile = find_do_file(t)
if not dofile:
if os.path.exists(t): # an existing source file
2010-11-12 07:03:06 -08:00
stamp(t)
return # success
else:
2010-11-13 01:21:59 -08:00
raise BuildError('no rule to make %r' % t)
stamp(dofile)
unlink(t)
tmpname = '%s.redo.tmp' % t
unlink(tmpname)
f = open(tmpname, 'w+')
argv = [os.environ.get('SHELL', 'sh'), '-e',
os.path.basename(dofile),
os.path.basename(t), 'FIXME', os.path.basename(tmpname)]
2010-11-13 00:11:34 -08:00
if vars.VERBOSE:
2010-11-12 07:10:55 -08:00
argv[1] += 'v'
log('%s\n' % relpath(t, vars.STARTDIR))
rv = subprocess.call(argv, preexec_fn=lambda: _preexec(t),
stdout=f.fileno())
st = os.stat(tmpname)
stampfile = sname('stamp', t)
2010-11-12 07:03:06 -08:00
if rv==0:
if st.st_size:
os.rename(tmpname, t)
else:
unlink(tmpname)
stamp(t)
else:
unlink(tmpname)
2010-11-12 07:03:06 -08:00
unlink(stampfile)
f.close()
if rv != 0:
2010-11-13 01:21:59 -08:00
raise BuildError('%s: exit code %d' % (t,rv))
2010-11-12 07:03:06 -08:00
2010-11-13 00:11:34 -08:00
if not vars.DEPTH:
# toplevel call to redo
exenames = [os.path.abspath(sys.argv[0]), os.path.realpath(sys.argv[0])]
if exenames[0] == exenames[1]:
exenames = [exenames[0]]
dirnames = [os.path.dirname(p) for p in exenames]
os.environ['PATH'] = ':'.join(dirnames) + ':' + os.environ['PATH']
2010-11-13 01:21:59 -08:00
retcode = 0
startdir = os.getcwd()
for t in targets:
if os.path.exists('%s/all.do' % t):
# t is a directory, but it has a default target
t = '%s/all' % t
2010-11-13 00:11:34 -08:00
mkdirp('%s/.redo' % vars.BASE)
os.chdir(startdir)
2010-11-13 01:21:59 -08:00
try:
build(t)
except BuildError, e:
err('%s\n' % e)
retcode = 1
sys.exit(retcode)