import shutil
import sys
import optparse
+import time
################################################################
# Users of python modules should include this snippet
# Urg, Python 2.4 does not define stderr/stdout encoding
# Maybe guess encoding from LANG/LC_ALL/LC_CTYPE?
+reload (sys)
+sys.setdefaultencoding ('utf-8')
+import codecs
+sys.stdout = codecs.getwriter ('utf8') (sys.stdout)
+sys.stderr = codecs.getwriter ('utf8') (sys.stderr)
+
def encoded_write(f, s):
- f.write (s.encode (f.encoding or 'utf_8'))
+ f.write (s.encode (f.encoding or 'utf-8', 'replace'))
# ugh, Python 2.5 optparse requires Unicode strings in some argument
# functions, and refuse them in some other places
def display_encode (s):
- return s.encode (sys.stderr.encoding or 'utf_8')
+ return s.encode (sys.stderr.encoding or 'utf-8', 'replace')
# Lilylib globals.
program_version = '@TOPLEVEL_VERSION@'
ignore_error=False,
progress_p=True,
be_verbose=False,
+ redirect_output=False,
log_file=None):
import subprocess
name = command_name (cmd)
error_log_file = ''
- if be_verbose:
- show_progress = 1
- progress (_ ("Invoking `%s\'") % cmd)
+ if redirect_output:
+ progress (_ ("Processing %s.ly") % log_file)
else:
- progress ( _("Running %s...") % name)
-
+ if be_verbose:
+ show_progress = 1
+ progress (_ ("Invoking `%s\'") % cmd)
+ else:
+ progress ( _("Running %s...") % name)
stdout_setting = None
+ stderr_setting = None
if not show_progress:
- stdout_setting = subprocess.PIPE
+ stdout_setting = subprocess.PIPE
+
+ if redirect_output:
+ stdout_filename = ''.join([log_file, '.log'])
+ stderr_filename = ''.join([log_file, '.err.log'])
+ stdout_setting = open(stdout_filename, 'w')
+ stderr_setting = open(stderr_filename, 'w')
proc = subprocess.Popen (cmd,
shell=True,
log = ''
- if show_progress:
- retval = proc.wait()
+ if redirect_output:
+ while proc.poll()==None:
+ time.sleep(1)
+ retval = proc.returncode
+ stdout_setting.close()
+ stderr_setting.close()
else:
- log = proc.communicate ()
- retval = proc.returncode
+ if show_progress:
+ retval = proc.wait()
+ else:
+ log = proc.communicate ()
+ retval = proc.returncode
if retval: