Hello all,

I'm looking over #8486 and the patch there tests to see if the xelatex
program is available with

    os.system('which xelatex >/dev/null')

Now, the Python documentation says one should use the subprocess module
instead of os.system [1], and also I recall that apparently the "which"
command is not POSIX-specified, so we cannot rely on it being installed.
I came up with this snippet of code:

    import subprocess

    try:
        subprocess.check_call(['/usr/bin/env', 'bash', '-c', 'type xelatex'], 
stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT)
        found_it = True
    except subprocess.CalledProcessError:
        found_it = False

    if found_it:
        print 'yes, found it'
    else:
        print 'no, could not find it'

I know that we can rely on /usr/bin/env existing, and we assume that we
can use bash, so I figure it's okay to rely on bash's "type" builtin.
(Googling around, it seems like the "type" builtin is part of something
called XSI extensions, and it seemed like most shells will have a "type"
command, but it seems safe to stick with bash and not take our chances
with whatever /bin/sh might be.)

The check_call function [2] raises the exception if the return code of
the command is anything other than zero. This seems like a good,
cross-platform way to check if a program is available. I've tried this
on Ubuntu, OS X, and t2.math and it works there. I think this will work
on Cygwin, except for maybe the /dev/null redirection.

Comments? Should this be our standard way of checking to see if a
program is available?

Dan

  1. http://docs.python.org/library/os.html#os.system
  2. http://docs.python.org/library/subprocess.html#subprocess.check_call
-- 
---  Dan Drake
-----  http://mathsci.kaist.ac.kr/~drake
-------

Attachment: signature.asc
Description: Digital signature

Reply via email to