script.py 1.0 KB

1234567891011121314151617181920212223242526272829
  1. import subprocess
  2. import os
  3. from core.NeuronModule import NeuronModule, MissingParameterException, InvalidParameterException
  4. class Script(NeuronModule):
  5. def __init__(self, **kwargs):
  6. super(Script, self).__init__(**kwargs)
  7. self.path = kwargs.get("path", None)
  8. # check parameters
  9. if self._is_parameters_ok():
  10. p = subprocess.Popen(self.path, stdout=subprocess.PIPE, shell=True)
  11. (output, err) = p.communicate()
  12. def _is_parameters_ok(self):
  13. """
  14. Check if received parameters are ok to perform operations in the neuron
  15. :return: true if parameters are ok, raise an exception otherwise
  16. """
  17. if self.path is None:
  18. raise MissingParameterException("You must provide a script path.")
  19. if not os.path.isfile(self.path):
  20. raise InvalidParameterException("Script not found or is not a file.")
  21. if not os.access(self.path, os.X_OK):
  22. raise InvalidParameterException("Script not Executable.")
  23. return True