From 61d35d39725871cf3c3c7958624b8e07a003921a Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Thu, 4 Oct 2018 20:20:53 -0400 Subject: [PATCH] redo-whichdo: a command that explains the .do search path for a target. For example: $ redo-whichdo a/b/c/.x.y - a/b/c.x.y.do - a/b/default.x.y.do - a/b/default.y.do - a/b/default.do - a/default.x.y.do - a/default.y.do - a/default.do - default.x.y.do - default.y.do + default.do 1 a/b/c.x.y 2 a/b/c.x.y Lines starting with '-' mean a potential .do file that did not exist, so we moved onto the next choice (but consider using redo-ifcreate in case it gets created). '+' means the .do file we actually chose. '1' and '2' are the $1 and $2 to pass along to the given .do file if you want to call it for the given target. (The output format is a little weird to make sure it's parseable with sh 'read x y' calls, even when filenames contain spaces or special characters.) --- builder.py | 8 ++++---- redo-whichdo | 1 + redo-whichdo.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 120000 redo-whichdo create mode 100755 redo-whichdo.py diff --git a/builder.py b/builder.py index 3b33912..5c6aa34 100644 --- a/builder.py +++ b/builder.py @@ -13,7 +13,7 @@ def _default_do_files(filename): yield ("default%s.do" % ext), basename, ext -def _possible_do_files(t): +def possible_do_files(t): dirname,filename = os.path.split(t) yield (os.path.join(vars.BASE, dirname), "%s.do" % filename, '', filename, '') @@ -35,8 +35,8 @@ def _possible_do_files(t): subdir, os.path.join(subdir, basename), ext) -def _find_do_file(f): - for dodir,dofile,basedir,basename,ext in _possible_do_files(f.name): +def find_do_file(f): + for dodir,dofile,basedir,basename,ext in possible_do_files(f.name): dopath = os.path.join(dodir, dofile) debug2('%s: %s:%s ?\n' % (f.name, dodir, dofile)) if os.path.exists(dopath): @@ -126,7 +126,7 @@ class BuildJob: sf.save() return self._after2(0) sf.zap_deps1() - (dodir, dofile, basedir, basename, ext) = _find_do_file(sf) + (dodir, dofile, basedir, basename, ext) = find_do_file(sf) if not dofile: if os.path.exists(t): sf.set_static() diff --git a/redo-whichdo b/redo-whichdo new file mode 120000 index 0000000..0962250 --- /dev/null +++ b/redo-whichdo @@ -0,0 +1 @@ +redo-whichdo.py \ No newline at end of file diff --git a/redo-whichdo.py b/redo-whichdo.py new file mode 100755 index 0000000..03310b9 --- /dev/null +++ b/redo-whichdo.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +import sys, os + +import vars_init +vars_init.init([]) + +import builder +from log import err + +if len(sys.argv[1:]) != 1: + err('%s: exactly one argument expected.\n' % sys.argv[0]) + sys.exit(1) + +want = sys.argv[1] +for dodir,dofile,basedir,basename,ext in builder.possible_do_files(os.path.abspath(want)): + dopath = os.path.join('/', dodir, dofile) + relpath = os.path.relpath(dopath, '.') + exists = os.path.exists(dopath) + assert('\n' not in relpath) + if exists: + print '+', relpath + assert('\n' not in basename) + assert('\n' not in ext) + print '1', basename+ext + print '2', basename + sys.exit(0) + else: + print '-', relpath +sys.exit(1) # no appropriate dofile found