[Scons-users] use of abspath
Fred Wright
fw at fwright.net
Fri Aug 11 23:08:10 EDT 2017
On Fri, 11 Aug 2017, Bill Deegan wrote:
> re abspath in scripts (Jack)
> In such cases where you know the relative position of the script and the
> resources it needs in your build tree, relative location can be used.
> If you used abs path and you move your build anywhere, it stops working.
> Take a little more time to setup, but saves the guy who moved the tree
> somewhere (or unpacked an archive) from banging his head against the wall
> trying to figure out why it doesn't work.
Often a convenient way to solve that problem is to have the script start
by cd'ing to its own directory. That's rarely as convenient as it ought
to be, but usually not too hard. Then paths relative to the script
location work.
In bash:
SCRIPT_DIR=$(dirname "$0")
cd "$SCRIPT_DIR"
In Python:
os.chdir(os.path.dirname(__file__))
This approach even works recursively as long as any nested scripts are run
in subprocesses. If they're run directly, then saving and restoring the
current directory around the call may be needed.
Fred Wright
More information about the Scons-users
mailing list