[GRASS-dev] porting r.in.wms to python

Glynn Clements glynn at gclements.plus.com
Mon Oct 6 22:17:09 EDT 2008


Paul Kelly wrote:

> > Oops. It turns out that Python doesn't understand %PATHEXT% (but it
> > least it's consistent; it doesn't work with .bat or .cmd files
> > either).
> 
> It looks to me like it doesn't work with .exe either, i.e. it doesn't 
> assume the extension - e.g. trying to run a script on Windows I had to 
> change line 139 of grass.py from
>      os.execvp("g.parser", [name] + argv)
> to
>      os.execvp("g.parser.exe", [name] + argv)
> to get it to do anything at all.

Full marks for consistency, I suppose.

> > If we want to handle this ourselves from within GRASS' Python scripts,
> > we can use the _winreg module (note: leading underscore), e.g.:

> This seems more complicated than a solution analogous to 
> windows_launch.bat with a GRASS_PYTHON environment variable. I'm not sure 
> what's best.

Yeah, but we would need to do something similar for .bat files (except
that we don't need a separate variable; %COMSPEC% exists for that).

Essentially, there are two choices:

1. Mimic cmd.exe's handling of PATH, PATHEXT, assoc and ftype, i.e.
identify the full pathname corresponding to the command, then look up
the program used to run such files.

2. Invoke all commands via cmd.exe (well %COMSPEC%). For Popen(), it
should suffice to set shell=True, which prepends "%COMSPEC% /c" to the
argument string; it converts the argument list to a string regardless
of the value of "shell".

Either way, the issue is where to do this. The subprocess.Popen()
class defines a helper method, _execute_child(), which varies between
platforms. The POSIX version uses os.fork and os.execvp[e], while the
Windows version uses CreateProcess().

We can either create and use a subclass that overrides that method, or
we can simply replace the method with a wrapper. It depends on whether
we only need to affect direct use of Popen() from GRASS scripts, or
also affect "internal" use.

If we also need to affect the os.exec* and os.spawn* functions, we
need to override os._execvpe.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list