[gobject-introspection] scanner: Don't leave temporary directory around if we fail



commit b89a110fdd9f6a0c4c0291a6016cb154bb715b0a
Author: Colin Walters <walters verbum org>
Date:   Thu Oct 7 15:56:05 2010 -0400

    scanner: Don't leave temporary directory around if we fail
    
    ...by default.  However, the GI_SCANNER_DEBUG=save-temps option
    helps for debugging.

 giscanner/dumper.py      |   43 +++++++++++++++++++++++++++----------------
 giscanner/gdumpparser.py |    4 +++-
 2 files changed, 30 insertions(+), 17 deletions(-)
---
diff --git a/giscanner/dumper.py b/giscanner/dumper.py
index a552397..c3a7be7 100644
--- a/giscanner/dumper.py
+++ b/giscanner/dumper.py
@@ -21,10 +21,11 @@
 
 import os
 import subprocess
+import shutil
 import tempfile
 
 from .gdumpparser import IntrospectionBinary
-from .utils import get_libtool_command
+from . import utils
 from .config import DATADIR
 
 # bugzilla.gnome.org/558436
@@ -78,9 +79,6 @@ class DumpCompiler(object):
     def __init__(self, options, get_type_functions):
         self._options = options
         self._get_type_functions = get_type_functions
-        # We have to use the current directory to work around Unix
-        # sysadmins who mount /tmp noexec
-        self._tmpdir = tempfile.mkdtemp('', 'tmp-introspect', dir=os.getcwd())
 
         self._compiler_cmd = os.environ.get('CC', 'gcc')
         self._linker_cmd = os.environ.get('CC', self._compiler_cmd)
@@ -93,6 +91,10 @@ class DumpCompiler(object):
     # Public API
 
     def run(self):
+        # We have to use the current directory to work around Unix
+        # sysadmins who mount /tmp noexec
+        tmpdir = tempfile.mkdtemp('', 'tmp-introspect', dir=os.getcwd())
+
         tpl_args = {}
         if self._uninst_srcdir is not None:
             gdump_path = os.path.join(self._uninst_srcdir, 'girepository', 'gdump.c')
@@ -107,7 +109,7 @@ class DumpCompiler(object):
         tpl_args['gdump_include'] = gdump_contents
         tpl_args['init_sections'] = "\n".join(self._options.init_sections)
 
-        c_path = self._generate_tempfile('.c')
+        c_path = self._generate_tempfile(tmpdir, '.c')
         f = open(c_path, 'w')
         f.write(_PROGRAM_TEMPLATE % tpl_args)
 
@@ -128,27 +130,31 @@ class DumpCompiler(object):
             f.write("\n};\n")
         f.close()
 
-        o_path = self._generate_tempfile('.o')
-        bin_path = self._generate_tempfile()
+        o_path = self._generate_tempfile(tmpdir, '.o')
+        bin_path = self._generate_tempfile(tmpdir)
 
         try:
             self._compile(o_path, c_path)
         except CompilerError, e:
-            raise SystemExit('ERROR: ' + str(e))
+            if not utils.have_debug_flag('save-temps'):
+                shutil.rmtree(tmpdir)
+            raise SystemExit('compilation of temporary binary failed:' + str(e))
 
         try:
             self._link(bin_path, o_path)
         except LinkerError, e:
-            raise SystemExit('ERROR: ' + str(e))
+            if not utils.have_debug_flag('save-temps'):
+                shutil.rmtree(tmpdir)
+            raise SystemExit('linking of temporary binary failed: ' + str(e))
 
-        return IntrospectionBinary([bin_path], self._tmpdir)
+        return IntrospectionBinary([bin_path], tmpdir)
 
     # Private API
 
-    def _generate_tempfile(self, suffix=''):
+    def _generate_tempfile(self, tmpdir, suffix=''):
         tmpl = '%s-%s%s' % (self._options.namespace_name,
                             self._options.namespace_version, suffix)
-        return os.path.join(self._tmpdir, tmpl)
+        return os.path.join(tmpdir, tmpl)
 
     def _run_pkgconfig(self, flag):
         proc = subprocess.Popen(
@@ -180,11 +186,14 @@ class DumpCompiler(object):
         if not self._options.quiet:
             print "g-ir-scanner: compile: %s" % (
                 subprocess.list2cmdline(args), )
-        subprocess.check_call(args)
+        try:
+            subprocess.check_call(args)
+        except subprocess.CalledProcessError, e:
+            raise CompilerError(e)
 
     def _link(self, output, *sources):
         args = []
-        libtool = get_libtool_command(self._options)
+        libtool = utils.get_libtool_command(self._options)
         if libtool:
             args.extend(libtool)
             args.append('--mode=link')
@@ -230,8 +239,10 @@ class DumpCompiler(object):
         if not self._options.quiet:
             print "g-ir-scanner: link: %s" % (
                 subprocess.list2cmdline(args), )
-        subprocess.check_call(args)
-
+        try:
+            subprocess.check_call(args)
+        except subprocess.CalledProcessError, e:
+            raise LinkerError(e)
 
 def compile_introspection_binary(options, get_type_functions):
     dc = DumpCompiler(options, get_type_functions)
diff --git a/giscanner/gdumpparser.py b/giscanner/gdumpparser.py
index 012e5b9..1c6adfe 100644
--- a/giscanner/gdumpparser.py
+++ b/giscanner/gdumpparser.py
@@ -27,6 +27,7 @@ from xml.etree.cElementTree import parse
 
 from . import ast
 from . import message
+from . import utils
 from .transformer import TransformerException
 
 # GParamFlags
@@ -167,7 +168,8 @@ blob containing data gleaned from GObject's primitive introspection."""
                 raise SystemExit(e)
             return parse(out_path)
         finally:
-            shutil.rmtree(self._binary.tmpdir)
+            if not utils.have_debug_flag('save-temps'):
+                shutil.rmtree(self._binary.tmpdir)
 
     def _create_gobject(self, node):
         type_name = 'G' + node.name



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]