diff options
author | Tavian Barnes <tavianator@gmail.com> | 2011-09-20 11:06:51 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2011-09-20 11:06:51 -0400 |
commit | 2476507c0de2dee1aaf809d838dcd5406f09e34f (patch) | |
tree | 4b07872f8874bff2aa64a8380cb048a300aa75d9 | |
parent | a68821424f9826718e32606f9830c84b74737032 (diff) | |
download | dimension-2476507c0de2dee1aaf809d838dcd5406f09e34f.tar.xz |
chdir() into the same directory as the scene file.
-rw-r--r-- | dimension/dimension.in | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/dimension/dimension.in b/dimension/dimension.in index c637d26..d9cecb2 100644 --- a/dimension/dimension.in +++ b/dimension/dimension.in @@ -21,8 +21,9 @@ import argparse as _argparse import re as _re -import os.path as _path +import os as _os import sys as _sys +from contextlib import contextmanager as _contextmanager # Specialized parser to print --version output to stdout rather than stderr, # to pass distcheck @@ -33,6 +34,16 @@ class _DimensionArgumentParser(_argparse.ArgumentParser): file.write(message) _sys.exit(status) +# Change the working directory within a with statement +@_contextmanager +def _chdir(newwd): + oldwd = _os.getcwd() + try: + _os.chdir(newwd) + yield + finally: + _os.chdir(oldwd) + # Parse the command line _parser = _DimensionArgumentParser( epilog = "@PACKAGE_STRING@\n" @@ -102,7 +113,7 @@ else: # Default output is basename(input).png if _args.output is None: - _noext = _path.splitext(_path.basename(_args.input))[0] + _noext = _os.path.splitext(_os.path.basename(_args.input))[0] _args.output = _noext + ".png" # Imports available to scripts @@ -157,8 +168,11 @@ recursion_limit = None if not _args.quiet: print("Parsing scene ...") +# Run with the script's dirname as the working directory +_workdir = _os.path.dirname(_os.path.abspath(_args.input)) + parse_timer = Timer() -with open(_args.input) as _fh: +with open(_args.input) as _fh, _chdir(_workdir): exec(compile(_fh.read(), _args.input, "exec")) parse_timer.stop() |