1

I'm writing a custom Joomla 4 component, and when I try to run python code within the Model php file to pass to the View, the exec command returns an resultValue of 2.

  • The python code is valid
  • The python code is executible for everyone 0755 permissions
  • The webpage returns the result value of two (as per my return of $resultValue instead of result)
  • The webpage of course returns nothing if I return $result instead
cat site/src/Model/WeatherModel.php 
<?php
namespace Vim\Component\com_vimsjoomlaweather\Site\Model;

#error checking
error_reporting(E_ALL);
ini_set('display_errors', 1);

use Joomla\CMS\MVC\Model\BaseDatabaseModel;

class WeatherModel extends BaseDatabaseModel {
  public function getWeatherData() {
        exec('python ./weathermodel.py', $result, $returnValue);
        return $returnValue;
  }
}
cat site/src/Model/weathermodel.py 
def get_weather_data():
    print(['weather data in print'])
    return ["weather data"]

if __name__ == '__main__':
    get_weather_data()

Also, this may need it's own ask, but the manifest xml document doesn't add the necessary permissions, and I am having to add them myself after component installation.

  • is <file mode="0755">src/Model/weathermodel.py</file> written wrong?
        <folder>src</folder>
        <folder>tmpl</folder>
    </files>
    <files folder="site/">
        <filename>src/Model/weathermodel.py</filename>
        <file mode="0755">src/Model/weathermodel.py</file>
    </files>```

1 Answer 1

1

Component not running python script, return value 2

Turns out PHP files inherit Joomla's working directory. The cwd ends up as the Joomla's working directory so /path/to/joomla just for clarity this does not end up being the components root directory. A simple way to get around this is to chdir in php.

chdir(__DIR__);
exec('python weathermodel.py', $result, $returnValue);

File permissions not being set at extension installation

Second issue about file permissions not being set well there doesn't seem to be a way to do that in the XML manifest file. But I deleted the extension and readded with the corrected version, as shown above and it works! It works because the python interpreter isn't held back by Linux permission bits.

A note on permissions

Even though the permissions are set as 644 / -rw-r--r--. it still runs. My guess is because it's being ran by the python interpreter and not BASH therefore file permissions don't come into play. I believe this to be the case since, I created another file called weathermodel.sh

  • when I set the permissions to 644 I don't get data back! and it comes back with a return value of 126.
  • But when I set the permissions to that file as 755 I get a return value of 0 and I get the data I want. If you want to add a BASH script with proper permissions on installation you'll probably have to add some sort of bootstrap file, I think..

In Summary

  • PHP files inherit Joomla's current working directory
  • Manifest files seem to not have the functionality set Linux permission bits
  • Result value 2: file not found
  • Result value 126: permissions issue
  • Result value 0: all well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.