Help to understand how this function works.

I have a bit of code here that seems to me to be searching the wrong place for a specific program called ghostscript:

1
2
3
4
5
6
7
8
9
10
11
  def _findGhostscript():
    """Find path to ghostscript (the gs execuble)

    TODO- This function needs some more work.  It should
          search through your path to find a 9.x or newer
          version of ghostscript.  
    """
    result = "/usr/local/bin/gs"
    if not os.path.isfile(result): result = "gs"
    if os.name=='nt':              result = r'c:\cygwin64\bin\gs.exe'
    return result


I downloaded ghostscript the the executable is located @ /usr/bin. So it would seem the program is looking in the wrong place because its looking at /usr/local/bin but I am not sure because there is a bit of the code that I dont understand:

if not os.path.isfile(result): result = "gs"

Does that line account for when gs is not in /usr/local/bin ? Will that line force this program to look elsewhere for ghostscript, say /usr/bin?
Last edited on
what is the class/struct os (eg os.path..) ?
I mean I can guess that the offending line is saying 'if the provided path is no good, try gs as the path (perhaps this becomes c:\gs\ or something later on?)

this just seems like a mess to me.
there is a good chance there is a way to just ask the OS what the path is. (Its probably in some list you can read of installed programs or the registry).
if not, run this when you install the program (run from c:\)
dir /s /b gs.exe > gspath.txt
and read that file when you run the program.

I mean even if you have the cygwin version, you have to have pathed out cygwin or the program won't run (it will be missing the cyg**.dlls). Not everyone with cyg does that, some use the shell instead.
Last edited on
jonnin wrote:
(perhaps this becomes c:\gs\ or something later on?)

Probably not, looks like it tries to look for the gs file inside the current directory (as a relative path) if it can't find it in /usr/local/bin/gs.

vysero wrote:
Does that line account for when gs is not in /usr/local/bin ?

Yes.

vysero wrote:
Will that line force this program to look elsewhere for ghostscript, say /usr/bin?

Not in /usr/bin. If you wanted that, you'd do something like:

1
2
3
4
5
    result = "/usr/local/bin/gs"
    if not os.path.isfile(result):
        result = "/usr/bin" # first fallback
        if not os.path.isfile(result):
            result = "gs" # second fallback 
makes sense. I still recommend just finding the thing once and storing its location. But it depends on the setup. if its a public multi-user unix machine where people install junk in their home folders or such, it will be a free for all and you will have to think about it more. You shuold still be able to ask unix where it is, somehow, some way.
Last edited on
Oh definitely. Looks like the code acts as some heuristic glue for handling both the case of being on a proper *nix system vs being on cygwin, but it's not bulletproof by any means.
Last edited on
Yes and I believe as a double, triple fallback it has 2 other programs it will use to try and convert images if it can't find this one, thanks guys!
Last edited on
Topic archived. No new replies allowed.