-# Python < 1.5.2 compatibility
-#
-# On most platforms, this is equivalent to
-#`normpath(join(os.getcwd()), PATH)'. *Added in Python version 1.5.2*
-
-if os.path.__dict__.has_key ('abspath'):
- abspath = os.path.abspath
-else:
- def abspath (path):
- return os.path.normpath (os.path.join (os.getcwd (), path))
-
-if os.__dict__.has_key ('makedirs'):
- makedirs = os.makedirs
-else:
- def makedirs (dir, mode=0777):
- system ('mkdir -p %s' % dir)
-
-
-def mkdir_p (dir, mode=0777):
- if not os.path.isdir (dir):
- makedirs (dir, mode)
-
-
-environment = {}
-
-# tex needs lots of memory, more than it gets by default on Debian
-non_path_environment = {
- 'extra_mem_top' : '1000000',
- 'extra_mem_bottom' : '1000000',
- 'pool_size' : '250000',
-}
-
-def setup_environment ():
- global environment
-
- kpse = read_pipe ('kpsexpand \$TEXMF')
- texmf = re.sub ('[ \t\n]+$','', kpse)
- type1_paths = read_pipe ('kpsewhich -expand-path=\$T1FONTS')
-
- environment = {
- # TODO: * prevent multiple addition.
- # * clean TEXINPUTS, MFINPUTS, TFMFONTS,
- # as these take prevalence over $TEXMF
- # and thus may break tex run?
-
- 'TEXMF' : "{%s,%s}" % (datadir, texmf) ,
-
- # 'GS_FONTPATH' : type1_paths,
- # 'GS_LIB' : datadir + '/ps',
- 'GS_FONTPATH' : "",
- 'GS_LIB' : "",
- }
-
- # $TEXMF is special, previous value is already taken care of
- if os.environ.has_key ('TEXMF'):
- del os.environ['TEXMF']
-
- for key in environment.keys ():
- val = environment[key]
- if os.environ.has_key (key):
- val = os.environ[key] + os.pathsep + val
- os.environ[key] = val
-
- for key in non_path_environment.keys ():
- val = non_path_environment[key]
- os.environ[key] = val
-
-def print_environment ():
- for (k,v) in os.environ.items ():
- sys.stderr.write ("%s=\"%s\"\n" % (k, v))
-
-BOUNDING_BOX_RE = '^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)'
-def get_bbox (filename):
- bbox = filename + '.bbox'
- ## -sOutputFile does not work with bbox?
- cmd = 'gs -sDEVICE=bbox -q -dNOPAUSE %s -c showpage -c quit 2>%s' % \
- (filename, bbox)
- system (cmd, progress_p = 1)
- box = open (bbox).read ()
- m = re.match (BOUNDING_BOX_RE, box)
- gr = []
- if m:
- gr = map (string.atoi, m.groups ())
-
- return gr
-
-
-def make_ps_images (ps_name, resolution = 90):
- ## todo:
- ## have better algorithm for deciding when to crop page,
- ## and when to show full page
- base = re.sub (r'\.e?ps', '', ps_name)
-
- header = open (ps_name).read (1024)
-
- match = re.search (BOUNDING_BOX_RE, header, re.MULTILINE)
- bbox = []
- if match:
- bbox = map (string.atoi, match.groups ())
-
- multi_page = re.search ('\n%%Pages: ', header)
- cmd = ''
-
- if multi_page == None:
-
- if bbox == []:
- bbox = get_bbox (ps_name)
-
- trans_ps = ps_name + '.trans.ps'
- output_file = re.sub (r'\.e?ps', '.png', ps_name)
-
- # need to have margin, otherwise edges of letters will
- # be cropped off.
-
- margin = 3
- fo = open (trans_ps, 'w')
- fo.write ('%d %d translate\n' % (-bbox[0] + margin,
- -bbox[1] + margin))
- fo.close ()
-
- x = (2* margin + bbox[2] - bbox[0]) \
- * resolution / 72.0
- y = (2* margin + bbox[3] - bbox[1]) \
- * resolution / 72.0
- if x == 0:
- x = 1
- if y == 0:
- y = 1
-
- cmd = r'''gs -g%dx%d -sDEVICE=pnggray -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -q -sOutputFile=%s -r%d -dNOPAUSE %s %s -c showpage -c quit ''' % \
- (x, y, output_file, resolution, trans_ps, ps_name)
-
- rms = glob.glob (base + '-page*.png')
- map (os.unlink, rms)
+def command_name (cmd):
+ # Strip all stuf after command,
+ # deal with "((latex ) >& 1 ) .." too
+ cmd = re.match ('([\(\)]*)([^\\\ ]*)', cmd).group (2)
+ return os.path.basename (cmd)
+
+def subprocess_system (cmd,
+ ignore_error=False,
+ progress_p=True,
+ be_verbose=False,
+ log_file=None):
+ import subprocess
+
+ show_progress= progress_p
+ name = command_name (cmd)
+ error_log_file = ''
+
+ if be_verbose:
+ show_progress = 1
+ progress (_ ("Invoking `%s\'") % cmd)
+ else:
+ progress ( _("Running %s...") % name)
+
+
+ stdout_setting = None
+ if not show_progress:
+ stdout_setting = subprocess.PIPE
+
+ 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:
+ print >>sys.stderr, "Error ignored"