Bug 1170691 - part 1 - add the generating script's directory to sys.path in file_generate.py; r=glandium

The old way of writing scripts for generated files would invoke the script thusly:

    python script.py arg1...

Invoking the script this way means that the script's directory is
automatically added to sys.path, and importing modules from that
directory is easy.  Let's make it equally easy in the new world for
GENERATED_FILES, too.
This commit is contained in:
Nathan Froyd 2015-06-02 16:00:48 -04:00
parent dda4d33feb
commit eff065ece5

View File

@ -30,6 +30,16 @@ def main(argv):
args = parser.parse_args(argv)
script = args.python_script
# Permit the script to import modules from the same directory in which it
# resides. The justification for doing this is that if we were invoking
# the script as:
#
# python script arg1...
#
# then importing modules from the script's directory would come for free.
# Since we're invoking the script in a roundabout way, we provide this
# bit of convenience.
sys.path.append(os.path.dirname(script))
with open(script, 'r') as fh:
module = imp.load_module('script', fh, script,
('.py', 'r', imp.PY_SOURCE))