changeset 150:553784dccf97

Factored out common program execution logic into <sh:exec> command, which is used by <python:exec>.
author cmlenz
date Sun, 21 Aug 2005 20:56:22 +0000
parents b2623edffb19
children ebf13ea88ff2
files bitten/build/ctools.py bitten/build/pythontools.py bitten/build/shtools.py
diffstat 3 files changed, 73 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/bitten/build/ctools.py
+++ b/bitten/build/ctools.py
@@ -20,7 +20,6 @@
 
 import logging
 
-from bitten.build import BuildError
 from bitten.util import xmlio
 from bitten.util.cmdline import Commandline
 
@@ -43,12 +42,11 @@
     for out, err in cmdline.execute():
         if out is not None:
             log.info(out)
-            if out:
-                xmlio.SubElement(log_elem, 'message', level='info')[out]
+            xmlio.SubElement(log_elem, 'message', level='info')[out]
         if err is not None:
             log.error(err)
-            if err:
-                xmlio.SubElement(log_elem, 'message', level='error')[err]
+            xmlio.SubElement(log_elem, 'message', level='error')[err]
     ctxt.log(log_elem)
+
     if cmdline.returncode != 0:
         ctxt.error('make failed (%s)' % cmdline.returncode)
--- a/bitten/build/pythontools.py
+++ b/bitten/build/pythontools.py
@@ -21,9 +21,7 @@
 import logging
 import os
 import re
-import shlex
 
-from bitten.recipe import Recipe
 from bitten.util import xmlio
 from bitten.util.cmdline import Commandline
 
@@ -35,8 +33,7 @@
     log_elem = xmlio.Fragment()
     for out, err in cmdline.execute():
         if out is not None:
-            if out:
-                log.info(out)
+            log.info(out)
             xmlio.SubElement(log_elem, 'message', level='info')[out]
         if err is not None:
             level = 'error'
@@ -46,8 +43,7 @@
                 log.warning(err)
             else:
                 log.error(err)
-            if err:
-                xmlio.SubElement(log_elem, 'message', level=level)[err]
+            xmlio.SubElement(log_elem, 'message', level=level)[err]
     ctxt.log(log_elem)
     if cmdline.returncode != 0:
         ctxt.error('distutils failed (%s)' % cmdline.returncode)
@@ -65,44 +61,12 @@
         file_ = mod.__file__
 
     if args:
-        args = shlex.split(args)
+        args = file_ + ' ' + args
     else:
-        args = []
-
-    output_file = None
-    if output:
-        output = ctxt.resolve(output)
-        output_file = file(output, 'w')
+        args = file_
 
-    try:
-        cmdline = Commandline('python', [file_] + args, cwd=ctxt.basedir)
-        log_elem = xmlio.Fragment()
-        for out, err in cmdline.execute():
-            if out is not None:
-                log.info(out)
-                if output_file:
-                    output_file.write(out + os.linesep)
-                if out:
-                    xmlio.SubElement(log_elem, 'message', level='info')[out]
-            if err is not None:
-                level = 'error'
-                if err.startswith('warning: '):
-                    err = err[9:]
-                    level = 'warning'
-                    log.warning(err)
-                else:
-                    log.error(err)
-                if output_file:
-                    output_file.write(err + os.linesep)
-                if err:
-                    xmlio.SubElement(log_elem, 'message', level=level)[err]
-        ctxt.log(log_elem)
-    finally:
-        if output_file:
-            output_file.close()
-
-    if cmdline.returncode != 0:
-        ctxt.error('distutils failed (%s)' % cmdline.returncode)
+    from bitten.build import shtools
+    shtools.exec_(ctxt, file_='python', output=output, args=args)
 
 def pylint(ctxt, file_=None):
     """Extract data from a `pylint` run written to a file."""
new file mode 100644
--- /dev/null
+++ b/bitten/build/shtools.py
@@ -0,0 +1,64 @@
+# -*- coding: iso8859-1 -*-
+#
+# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de>
+#
+# Bitten is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of the
+# License, or (at your option) any later version.
+#
+# Trac is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+# Author: Christopher Lenz <cmlenz@gmx.de>
+
+import logging
+import os
+import shlex
+
+from bitten.util import xmlio
+from bitten.util.cmdline import Commandline
+
+log = logging.getLogger('bitten.build.shtools')
+
+def exec_(ctxt, file_=None, output=None, args=None):
+    """Execute a shell script."""
+    assert file_, 'Missing required attribute "file"'
+
+    if args:
+        args = shlex.split(args)
+    else:
+        args = []
+
+    output_file = None
+    if output:
+        output = ctxt.resolve(output)
+        output_file = file(output, 'w')
+
+    try:
+        cmdline = Commandline(file_, args, cwd=ctxt.basedir)
+        log_elem = xmlio.Fragment()
+        for out, err in cmdline.execute():
+            if out is not None:
+                log.info(out)
+                xmlio.SubElement(log_elem, 'message', level='info')[out]
+                if output:
+                    output_file.write(out + os.linesep)
+            if err is not None:
+                log.error(err)
+                xmlio.SubElement(log_elem, 'message', level='error')[err]
+                if output:
+                    output_file.write(err + os.linesep)
+        ctxt.log(log_elem)
+    finally:
+        if output:
+            output_file.close()
+
+    if cmdline.returncode != 0:
+        ctxt.error('exec failed (%s)' % cmdline.returncode)
Copyright (C) 2012-2017 Edgewall Software