From 90330f02c26cfe44a9258c7cc93c198d4252503b Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Sun, 12 Mar 2006 17:48:57 +0000 Subject: [PATCH] (system): rewrite system() using subprocess. Remove >& redirection trickery. --- ChangeLog | 3 ++ python/lilylib.py | 83 +++++++++++++++++++--------------------- scripts/lilypond-book.py | 7 +--- 3 files changed, 45 insertions(+), 48 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0f7df83956..a89782355e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2006-03-12 Han-Wen Nienhuys + * python/lilylib.py (system): rewrite system() using + subprocess. Remove >& redirection trickery. + * scripts/convert-ly.py (get_option_parser): 2006-03-11 Graham Percival diff --git a/python/lilylib.py b/python/lilylib.py index b54c6b29d5..9f5d07bba1 100644 --- a/python/lilylib.py +++ b/python/lilylib.py @@ -19,7 +19,7 @@ import shutil import string import sys import optparse -import tempfile +import subprocess ################################################################ # Users of python modules should include this snippet @@ -65,61 +65,58 @@ def command_name (cmd): cmd = re.match ('([\(\)]*)([^\\\ ]*)', cmd).group (2) return os.path.basename (cmd) -def error_log (name): - name = re.sub('[^a-z]','x', name) - return tempfile.mktemp ('%s.errorlog' % name) - - -def system (cmd, ignore_error = 0, progress_p = 0, be_verbose=0): +def system (cmd, + ignore_error=False, + progress_p=True, + be_verbose=False, + log_file=None): - '''System CMD. If IGNORE_ERROR, do not complain when CMD -returns non zero. If PROGRESS_P, always show progress. - -RETURN VALUE - -Exit status of CMD ''' - + show_progress= progress_p name = command_name (cmd) error_log_file = '' if be_verbose: - progress_p = 1 + show_progress = 1 progress (_ ("Invoking `%s\'") % cmd) else: progress ( _("Running %s...") % name) - redirect = '' - if not progress_p: - error_log_file = error_log (name) - redirect = ' 1>/dev/null 2>' + error_log_file - status = os.system (cmd + redirect) - signal = 0x0f & status - exit_status = status >> 8 - - if status: - - exit_type = 'status %d' % exit_status - if signal: - exit_type = 'signal %d' % signal + stdout_setting = None + if not show_progress: + stdout_setting = subprocess.PIPE - msg = _ ("`%s\' failed (%s)") % (name, exit_type) + proc = subprocess.Popen (cmd, + shell=True, + universal_newlines=True, + stdout=stdout_setting, + stderr=stdout_setting) + + log = '' + + if show_progress: + retval = proc.wait() + else: + log = proc.communicate () + retval = proc.returncode + + + if retval: + print >>sys.stderr, 'command failed:', cmd + if retval < 0: + print >>sys.stderr, "Child was terminated by signal", -retval + elif retval > 0: + print >>sys.stderr, "Child returned", retval + if ignore_error: - if be_verbose: - warning (msg + ' ' + _ ("(ignored)")) + print >>sys.stderr, "Error ignored" else: - error (msg) - if not progress_p and error_log_file: - error (_ ("The error log is as follows:")) - sys.stderr.write (open (error_log_file).read ()) - if error_log_file: - os.unlink (error_log_file) - exit (1) - - if error_log_file: - os.unlink (error_log_file) - progress ('\n') - return status + if not show_progress: + print log[0] + print log[1] + sys.exit (1) + + return abs (retval) def strip_extension (f, ext): (p, e) = os.path.splitext (f) diff --git a/scripts/lilypond-book.py b/scripts/lilypond-book.py index 948ddbd74d..5cfe971b6d 100644 --- a/scripts/lilypond-book.py +++ b/scripts/lilypond-book.py @@ -1367,11 +1367,8 @@ def process_snippets (cmd, ly_snippets, texstr_snippets, png_snippets): status = 0 def my_system (cmd): status = ly.system (cmd, - ignore_error = 1, progress_p = 1) - - if status: - error ('Process %s exited unsuccessfully.' % cmd) - raise Compile_error + be_verbose=global_options.verbose, + progress_p= 1) # UGH # the --process=CMD switch is a bad idea -- 2.39.2