-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
started refactoring of SSH__Python methods
- Loading branch information
Showing
4 changed files
with
97 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from osbot_utils.base_classes.Type_Safe import Type_Safe | ||
from osbot_utils.helpers.ssh.SSH__Execute import SSH__Execute | ||
|
||
|
||
class SSH__Python(Type_Safe): | ||
ssh_execute: SSH__Execute | ||
|
||
def execute_python__code(self, python_code, python_executable='python3'): | ||
python_command = f"{python_executable} -c \"{python_code}\"" | ||
return self.ssh_execute.execute_command(python_command) | ||
|
||
def execute_python__code__return_stdout(self, *args, **kwargs): | ||
return self.execute_python__code(*args, **kwargs).get('stdout').strip() | ||
|
||
def execute_python__function(self, function, python_executable='python3'): | ||
function_name = function.__name__ | ||
function_code = function_source_code(function) | ||
exec_code = f"{function_code}\nresult= {function_name}(); print(result)" | ||
python_command = f"{python_executable} -c \"{exec_code}\"" | ||
return self.execute_command(python_command) | ||
|
||
def execute_python__function__return_stderr(self, *args, **kwargs): | ||
return self.execute_python__function(*args, **kwargs).get('stderr').strip() | ||
|
||
def execute_python__function__return_stdout(self, *args, **kwargs): | ||
return self.execute_python__function(*args, **kwargs).get('stdout').strip() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from osbot_utils.helpers.ssh.SSH__Execute import SSH__Execute | ||
from osbot_utils.helpers.ssh.SSH__Python import SSH__Python | ||
from osbot_utils.helpers.ssh.TestCase__SSH import TestCase__SSH | ||
|
||
|
||
class test_SSH__Python(TestCase__SSH): | ||
ssh_python = SSH__Python | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
cls.ssh_python = cls.ssh.ssh_python() | ||
|
||
def test_setUpClass(self): | ||
assert type(self.ssh_python ) is SSH__Python | ||
assert type(self.ssh_python.ssh_execute) is SSH__Execute | ||
|
||
|
||
def test_execute_python__code(self): | ||
with self.ssh_python as _: | ||
assert _.execute_python__code('print(40+2)').get('stdout') == '42\n' | ||
|
||
def test__execute_python__code__return_stdout(self): | ||
multi_line_command = """ | ||
import sys | ||
print(sys.version_info.major, sys.version_info.minor, sys.version_info.micro) | ||
""" | ||
with self.ssh_python as _: | ||
assert '3 12 2' in _.execute_python__code__return_stdout(multi_line_command) | ||
|
||
# assert _.execute_python__code__return_stdout(multi_line_command) == '3 9 16'# | ||
|
||
# def an_function(): | ||
# return 'Hello from the EC2 instance!' | ||
# assert _.execute_python__function(an_function).get('stdout') == 'Hello from the EC2 instance!\n' | ||
# pprint(exec_code) | ||
|
||
# def test_osbot_utils(): | ||
# from osbot_utils.utils.Misc import str_to_base64 | ||
# an_value = 'this will be base64 encoded!' | ||
# return str_to_base64(an_value) | ||
# | ||
# function_return_value = _.execute_python__function__return_stdout(test_osbot_utils) | ||
# assert base64_to_str(function_return_value) == 'this will be base64 encoded!' |