[GRASS-dev] Popen and r.mapcalc, pygrass' implications

Dear devs,

Pygrass is not working properly with r.mapcalc, the problem is how the
Popen class is used internally, so playing with Popen class:

{{{
In [1]: import subprocess as sub

In [2]: cmd_list = ['r.mapcalc', 'expression="elev100=elevation*100"', '--o']

In [3]: cmd_shell = ' '.join(cmd_list)

In [4]: sub.Popen(cmd_list)
Out[4]: <subprocess.Popen at 0x7f1b46bf03d0>

syntax error, unexpected $end, expecting '='
Parse error
ERROR: parse error

In [5]: sub.Popen(cmd_shell, shell=True)
Out[5]: <subprocess.Popen at 0x7f1b46bf0c50>
100%

}}}

I saw that the function in grass.script.raster.mapcalc is using the
stdin to pass the expression to r.mapcalc, so instead of:

{{{
In [6]: from grass.pygrass.modules.shortcuts import raster as r

In [7]: r.mapcalc(expression="elev100=elevation*100", overwrite=True)
syntax error, unexpected $end, expecting '='
Parse error
ERROR: parse error

[...cut...]
CalledModuleError: Module run r.mapcalc r.mapcalc
expression='elev100=elevation*100' --o ended with error
Process ended with non-zero return code 1. See errors in the (error) output.
}}}

we can write:

{{{
In [8]: r.mapcalc(file='-', stdin_="elev100=elevation*100", overwrite=True)
100%
Out[8]: Module('r.mapcalc')

}}}

but I think is less intuitive, do you see any other solutions?

Kind regards

Pietro

Pietro wrote:

Pygrass is not working properly with r.mapcalc, the problem is how the
Popen class is used internally, so playing with Popen class:

{{{
In [1]: import subprocess as sub

In [2]: cmd_list = ['r.mapcalc', 'expression="elev100=elevation*100"', '--o']

This is wrong. The double-quote characters shouldn't be there.

In [5]: sub.Popen(cmd_shell, shell=True)

Passing a string instead of a list is rarely a good idea. Apart from
anything else, you have to construct the string differently depending
upon whether you're using Unix or Windows (/bin/sh and cmd.exe have
significant differences in their command syntax).

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

On Thu, Aug 14, 2014 at 7:08 AM, Glynn Clements <glynn@gclements.plus.com>
wrote:

> In [2]: cmd_list = ['r.mapcalc', 'expression="elev100=elevation*100"',
'--o']

This is wrong. The double-quote characters shouldn't be there.

It took me some time to realize that quotes in

r.mapcalc expression="a = b"

are processed by shell and that the above is equal to

r.mapcalc "expression=a = b"

On Thu, Aug 14, 2014 at 7:08 AM, Glynn Clements <glynn@gclements.plus.com>
wrote:

> In [5]: sub.Popen(cmd_shell, shell=True)

Passing a string instead of a list is rarely a good idea. Apart from
anything else, you have to construct the string differently depending
upon whether you're using Unix or Windows (/bin/sh and cmd.exe have
significant differences in their command syntax).

Note that after r61587 a lot of tests failed, in fact the failure was so
serious that testing framework ended with error when dividing by zero
(which was number of executed tests), this should be fixed now, so you can
see the errors online. See also the the documentation how to invoke tests
for the whole source code, it is:

python -m grass.gunittest.main --location nc_spm_grass7 --location-type nc

If this does not work for you, let me know, or better, create a ticket.

http://trac.osgeo.org/grass/changeset/61587

http://fatra.cnr.ncsu.edu/grassgistests/summary_report/nc/index.html
http://fatra.cnr.ncsu.edu/grassgistests/reports_for_date-2014-08-14-15-34/report_for_nc_spm_08_grass7_nc/testfiles.html
http://fatra.cnr.ncsu.edu/grassgistests/reports_for_date-2014-08-14-15-34/report_for_nc_spm_08_grass7_nc/lib/python/temporal/test_register_function/index.html
http://fatra.cnr.ncsu.edu/grassgistests/reports_for_date-2014-08-14-15-34/report_for_nc_spm_08_grass7_nc/raster/r.mapcalc/test_r_mapcalc/index.html

http://grass.osgeo.org/grass71/manuals/libpython/gunittest_testing.html#testing-with-gunittest-package-in-general