Bug 1170691 - part 1 - add the generating script's directory to sys.path in file_generate.py; r=glandium
authorNathan Froyd <froydnj@mozilla.com>
Tue, 02 Jun 2015 16:00:48 -0400
changeset 247176 b1aec902688456ab04ab8bd6e9ccce8e27bfd306
parent 247175 04383149db11d3579f9e73920b72cdb42e9fcf92
child 247177 282f50513dd461089a5ba9320e14ed4c51902eed
push id28855
push userkwierso@gmail.com
push dateFri, 05 Jun 2015 01:19:30 +0000
treeherdermozilla-central@227d356ac030 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglandium
bugs1170691
milestone41.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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.
python/mozbuild/mozbuild/action/file_generate.py
--- a/python/mozbuild/mozbuild/action/file_generate.py
+++ b/python/mozbuild/mozbuild/action/file_generate.py
@@ -25,16 +25,26 @@ def main(argv):
     parser.add_argument('output_file', metavar='output-file', type=str,
                         help='The file to generate')
     parser.add_argument('additional_arguments', metavar='arg', nargs='*',
                         help="Additional arguments to the script's main() method")
 
     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))
     method = args.method_name
     if not hasattr(module, method):
         print('Error: script "{0}" is missing a {1} method'.format(script, method),
               file=sys.stderr)
         return 1