[GRASS-user] R: exwcuting grass python script from php web page

Moritz Lennert mlennert at club.worldonline.be
Tue Sep 29 08:31:25 PDT 2020


Hi Roberta,

I'm really no expert in this domain, so just doing some educated guessing.

On 28/09/20 18:37, roberta fagandini wrote:
> 
> On 28/09/20 17:51, roberta fagandini wrote:
>> Sorry, maybe I didn't explain myself well because actually, I don't get
>> any error from the php web page, simply the code related to grass is not 
>> executed.For instance gsetup.init(gisbase, gisdb, location, mapset) does 
>> not create any file in my /tmp/ folder and the print(gscript.gisenv()) 
>> is not printed.
>> I checked the import of the grass libraries (print(sys.modules)) and 
>> they seem to be correctly imported.
> 
> Have you checked your webservers error logs ?
> 
> I have already checked and this is the error in the webserver log file
> 
> File "importgrass.py", line 82, in <module>
>      main()
>    File "importgrass.py", line 71, in main
>      rcfile = gsetup.init(gisbase, gisdb, location, mapset)
>    File "/usr/lib/grass74/etc/python/grass/script/setup.py", line 170, 
> in init
>      config_dir = os.path.join(os.getenv('HOME'), config_dirname)
>    File "/usr/lib/python3.6/posixpath.py", line 80, in join
>      a = os.fspath(a)
> TypeError: expected str, bytes or os.PathLike object, not NoneType

First of all, the importgrass.py you sent only has 72 lines, so I don't 
know what the line 82 reference in your above error points to.

But most importantly, I would really recommend that you upgrade to a 
newer version of GRASS GIS. Current stable is 7.8. There's been a lot of 
improvement in terms of python3 compatibility and maybe this is an issue 
that has already been resolved.

It sounds like an issue with a variable containing a path. Apparently it 
is of type NoneType...


> 
> How do you call the python script from PhP?
> 
> I tried  both with:
> <?php
>         $command = escapeshellcmd('/usr/bin/python3 importgrass.py ');
>          echo exec($command, $output, $return);
> ?>
> 
> and
> 
> <?php
>         $command = escapeshellcmd('/usr/bin/python3 importgrass.py ');
>          echo system($command, $return);
> ?>
> 
> and
> 
> <?php
>         $intestazione='#!/bin/bash'.PHP_EOL;
>         $command = escapeshellcmd('/usr/bin/python3 importgrass.py ');
>         $myfile = fopen("./tmp/script.sh", "w") or die("Unable to open 
> file!");
>         fwrite($myfile, $intestazione);
>         fwrite($myfile, $command);
>         fclose($myfile);
>         echo exec("/bin/sh ./tmp/script.sh", $output, $return);
>         print_r($output);
> ?>
> 

What happens when you replace '/usr/bin/python3 importgrass.py ' by, for 
example '/usr/bin/grass --version', just to check if other executables 
work ?

Moritz


More information about the grass-user mailing list