[Python-Dev] urllib.browse() issues

Greg Ward gward@mems-exchange.org
Thu, 6 Jul 2000 18:38:10 -0400

On 06 July 2000, Eric S. Raymond said:
> def iscommand(cmd):
>     # I'd still like to put a cross-platform version of this in the library
>     if os.name == 'posix':
>         return os.system('which 1>/dev/null 2>&1 ' + cmd) == 0
>     else:
> 	return 1

Speaking of heavyweight, this strikes me as excessive: N * 2 fork/execs,
each of which will scan PATH anew, just to determine if a command
exists?  (OK, only N if 'which' is a shell built-in.)  Why not scan PATH
once yourself if you really need to determine a priori which command
will fail?  Easy to do since this will only be done on Unix.


> def urlbrowse(url):
>     for browser in _browsers:
>         if _iscommand(string.split(browser)[0]):
>             if os.system((browser % url)) == 0:
>                 return 1
>     return 0

Rearrange this loop so it tries os.system() on each one in turn, and
completes (successfully) when it finds one that works.

(think think think)

Ooh, that may not work so well because of the need to background X
browsers.  Ick.  One could dream up a wild scheme that forks and forks
and does the backgrounding itself, but what the hell: we're launching a
big fat hairy *web browser* here, what does it matter if a shell is
involved to parse the "&"?  Maybe iscommand() is useful after all; I
still think it should do its own PATH-scanning, though.

Also, the wild variability of "which" across platforms and shells makes
me wonder if, somewhere out there, there isn't a "which" that fails to
return true/false on success.  (check check check).  Yes, there is: with
bash 2.0.3 on Solaris 2.6:

$ if which ls 1>/dev/null 2>&1 ; then echo yes ; fi
$ if which skdfhjkjahdfs 1>/dev/null 2>&1 ; then echo yes ; fi

... so much for trusting "which" (I never did, anyways).

Greg Ward - software developer                gward@mems-exchange.org
MEMS Exchange / CNRI                           voice: +1-703-262-5376
Reston, Virginia, USA                            fax: +1-703-262-5367