9
9
10
10
from importlib import import_module , reload
11
11
from io import StringIO
12
-
13
-
12
+ from pathlib import Path
13
+
14
14
def log (message ):
15
15
js .postMessage (json .dumps ({"std_output" : message }))
16
16
17
17
18
18
requirements_list = json .loads (requirements )
19
-
20
19
try :
21
- import robot
22
- from robot .libdocpkg import LibraryDocumentation
23
- except ImportError :
24
- log (f"Install Robot Framework" )
25
- rf_version = f"=={ version } " if version else ""
26
20
try :
27
- await micropip .install (requirements_list or f"robotframework{ rf_version } " )
28
- time .sleep (1 )
29
21
import robot
30
22
from robot .libdocpkg import LibraryDocumentation
31
- except Exception as e :
32
- js .console .log (f"Robot Run Exception: { e } " )
33
- js .console .log (traceback .format_exc ())
34
- log (f" = version { robot .__version__ } \n " )
35
- if requirements_list :
36
- log (f"Installed Requirements: { requirements_list } \n " )
23
+ except ImportError :
24
+ robot = None
37
25
38
- os .chdir ("/" )
39
- dirname = "robot_files"
40
- if os .path .exists (dirname ):
41
- js .console .log ("Clean up working dir." )
42
- shutil .rmtree (dirname )
43
- os .makedirs (dirname )
44
- os .chdir (dirname )
45
- sys .path .append (os .getcwd ())
46
- js .console .log (f"Python working dir: { os .getcwd ()} " )
26
+ if robot is None :
27
+ log (f"Install Robot Framework" )
28
+ rf_version = f"=={ version } " if version else ""
29
+ requirements_list .insert (0 , f"robotframework{ rf_version } " )
30
+ try :
31
+ await micropip .install (requirements_list , keep_going = True )
32
+ time .sleep (1 )
33
+ import robot
34
+ from robot .libdocpkg import LibraryDocumentation
35
+ log (f" = version { robot .__version__ } \n " )
36
+ except Exception as e :
37
+ js .console .log (f"Installation Exception: { e } " )
38
+ js .console .log (traceback .format_exc ())
39
+ log (f"\n { traceback .format_exc ()} " )
40
+ raise e
41
+ if requirements_list :
42
+ log (f"Installed Requirements: { requirements_list } \n " )
47
43
44
+ os .chdir ("/" )
45
+ dirname = "/robot_files"
46
+ if os .path .exists (dirname ):
47
+ js .console .log ("Clean up working dir." )
48
+ shutil .rmtree (dirname )
49
+ os .makedirs (dirname )
50
+ os .chdir (dirname )
51
+ sys .path .append (os .getcwd ())
52
+ js .console .log (f"Python working dir: { os .getcwd ()} " )
48
53
49
- class Listener :
50
54
51
- ROBOT_LISTENER_API_VERSION = 2
55
+ class Listener :
52
56
53
- def _post_message (self ):
54
- log (sys .stdout .getvalue ())
55
- sys .__stdout__ .truncate (0 )
57
+ ROBOT_LISTENER_API_VERSION = 2
56
58
57
- def library_import (self , name , attrs ):
58
- self ._gen_libdoc (attrs ["source" ])
59
+ def _post_message (self ):
60
+ log (sys .stdout .getvalue ())
61
+ sys .__stdout__ .truncate (0 )
59
62
60
- def resource_import (self , name , attrs ):
61
- self ._gen_libdoc (attrs ["source" ])
63
+ def library_import (self , name , attrs ):
64
+ self ._gen_libdoc (attrs ["source" ])
62
65
63
- def _gen_libdoc (self , source ):
64
- try :
65
- if not robot .__version__ .startswith ("3" ):
66
- libdoc = LibraryDocumentation (source )
67
- js .postMessage (json .dumps ({"libdocJson" : libdoc .to_json ()}))
68
- except :
69
- pass
66
+ def resource_import (self , name , attrs ):
67
+ self ._gen_libdoc (attrs ["source" ])
70
68
71
- def start_suite (self , name , args ):
72
- self ._post_message ()
69
+ def _gen_libdoc (self , source ):
70
+ try :
71
+ if not robot .__version__ .startswith ("3" ):
72
+ libdoc = LibraryDocumentation (source )
73
+ js .postMessage (json .dumps ({"libdocJson" : libdoc .to_json ()}))
74
+ except :
75
+ pass
73
76
74
- def start_test (self , name , args ):
75
- self ._post_message ()
77
+ def start_suite (self , name , args ):
78
+ self ._post_message ()
76
79
77
- def start_keyword (self , name , args ):
78
- self ._post_message ()
80
+ def start_test (self , name , args ):
81
+ self ._post_message ()
79
82
80
- def end_keyword (self , name , args ):
81
- self ._post_message ()
83
+ def start_keyword (self , name , args ):
84
+ self ._post_message ()
82
85
83
- def end_test (self , name , args ):
84
- self ._post_message ()
86
+ def end_keyword (self , name , args ):
87
+ self ._post_message ()
85
88
86
- def end_suite (self , name , args ):
87
- self ._post_message ()
89
+ def end_test (self , name , args ):
90
+ self ._post_message ()
88
91
89
- def close (self ):
90
- self ._post_message ()
92
+ def end_suite (self , name , args ):
93
+ self ._post_message ()
91
94
95
+ def close (self ):
96
+ self ._post_message ()
92
97
93
- try :
94
98
95
- def write_file (file ):
96
- with open (file ["fileName" ], "w" ) as f :
97
- js .console .log (f'Writing file { file ["fileName" ]} to folder { dirname } .' )
98
- f .writelines (file ["content" ])
99
+ try :
99
100
100
- file_list = json .loads (file_catalog )
101
- robot_arguments = json .loads (robot_args )
101
+ def write_file (file ):
102
+ with open (file ["fileName" ], "w" ) as f :
103
+ js .console .log (f'Writing file { file ["fileName" ]} to folder { dirname } .' )
104
+ f .writelines (file ["content" ])
102
105
103
- for file in file_list :
104
- write_file (file )
105
- js .console .log (f"Files in working dir: { os .listdir ('.' )} " )
106
- result = - 2
106
+ file_list = json .loads (file_catalog )
107
+ robot_arguments = json .loads (robot_args )
107
108
108
- try :
109
- if test_case_name :
110
- kwargs = {"test" : test_case_name }
111
- testcli = f' --test "{ test_case_name } "'
112
- else :
113
- kwargs = {}
114
- testcli = ""
115
-
116
- if robot_arguments :
117
- log (f"Robot Run Arguments: { robot_args } \n " )
118
- log (f"\n Running Robot Framework:\n " )
119
- else :
120
- log (f"> robot --loglevel TRACE:INFO --exclude EXCL --skip SKIP\n "
121
- f" --removekeywords tag:REMOVE --flattenkeywords tag:FLAT{ testcli } .\n " )
122
-
123
- org_stdout = sys .__stdout__
124
- org_stderr = sys .__stderr__
125
- sys .stdout = sys .__stdout__ = StringIO ()
126
- sys .stderr = sys .__stderr__ = sys .__stdout__
127
109
for file in file_list :
128
- file_name , file_ext = os .path .splitext (
129
- file ["fileName" ]
130
- ) # TODO: does not work correctly
131
- if file_ext == ".py" :
132
- js .console .log (f'reimporting: { file ["fileName" ]} ' )
133
- m = import_module (file_name )
134
- m = reload (m )
135
- if robot_arguments :
136
- robot_arguments ["listener" ] = [Listener ()] + robot_arguments .get ("listener" , [])
137
- robot_arguments ["consolecolors" ] = "ansi"
138
- result = robot .run (
139
- "." ,
140
- ** robot_arguments
141
- )
142
- else :
143
- result = robot .run (
144
- "." ,
145
- consolecolors = "ansi" ,
146
- listener = [Listener ()],
147
- loglevel = "TRACE:INFO" ,
148
- exclude = "EXCL" ,
149
- skip = "SKIP" ,
150
- removekeywords = "tag:REMOVE" ,
151
- flattenkeywords = "tag:FLAT" ,
152
- ** kwargs ,
110
+ write_file (file )
111
+ js .console .log (f"Files in working dir: { os .listdir ('.' )} " )
112
+ result = - 2
113
+
114
+ try :
115
+ if test_case_name :
116
+ kwargs = {"test" : test_case_name }
117
+ testcli = f' --test "{ test_case_name } "'
118
+ else :
119
+ kwargs = {}
120
+ testcli = ""
121
+
122
+ if robot_arguments :
123
+ log (f"Robot Run Arguments: { robot_args } \n " )
124
+ log (f"\n Running Robot Framework:\n " )
125
+ else :
126
+ log (f"> robot --loglevel TRACE:INFO --exclude EXCL --skip SKIP\n "
127
+ f" --removekeywords tag:REMOVE --flattenkeywords tag:FLAT{ testcli } .\n " )
128
+
129
+ org_stdout = sys .__stdout__
130
+ org_stderr = sys .__stderr__
131
+ sys .stdout = sys .__stdout__ = StringIO ()
132
+ sys .stderr = sys .__stderr__ = sys .__stdout__
133
+ for file in file_list :
134
+ file_name , file_ext = os .path .splitext (
135
+ file ["fileName" ]
136
+ ) # TODO: does not work correctly
137
+ if file_ext == ".py" :
138
+ js .console .log (f'reimporting: { file ["fileName" ]} ' )
139
+ m = import_module (file_name )
140
+ m = reload (m )
141
+ if robot_arguments :
142
+ robot_arguments ["listener" ] = [Listener ()] + robot_arguments .get ("listener" , [])
143
+ robot_arguments ["consolecolors" ] = "ansi"
144
+ result = robot .run (
145
+ "." ,
146
+ outputdir = "/results" ,
147
+ ** robot_arguments
148
+ )
149
+ else :
150
+ result = robot .run (
151
+ "." ,
152
+ consolecolors = "ansi" ,
153
+ listener = [Listener ()],
154
+ loglevel = "TRACE:INFO" ,
155
+ exclude = "EXCL" ,
156
+ skip = "SKIP" ,
157
+ removekeywords = "tag:REMOVE" ,
158
+ flattenkeywords = "tag:FLAT" ,
159
+ outputdir = "/results" ,
160
+ ** kwargs ,
161
+ )
162
+ js .console .log (f"result: { result } " )
163
+ except Exception as e :
164
+ js .console .log (f"Robot Run Exception: { e } " )
165
+ js .console .log (traceback .format_exc ())
166
+ traceback .print_exc (file = sys .__stdout__ )
167
+ finally :
168
+ std_output = sys .__stdout__ .getvalue ()
169
+ sys .__stdout__ = org_stdout
170
+ sys .stdout = sys .__stdout__
171
+ log (std_output )
172
+
173
+ with open ("/results/log.html" , "r" , encoding = "UTF-8" ) as f :
174
+ log_html = str (f .read ())
175
+
176
+ with open ("/results/report.html" , "r" ) as f :
177
+ report_html = str (f .read ())
178
+
179
+ js .postMessage (
180
+ json .dumps (
181
+ {"log_html" : log_html , "report_html" : report_html , "std_output" : std_output }
153
182
)
154
- js .console .log (f"result: { result } " )
155
- except Exception as e :
156
- js .console .log (f"Robot Run Exception: { e } " )
157
- js .console .log (traceback .format_exc ())
158
- traceback .print_exc (file = sys .__stdout__ )
159
- finally :
160
- std_output = sys .__stdout__ .getvalue ()
161
- sys .__stdout__ = org_stdout
162
- sys .stdout = sys .__stdout__
163
- log (std_output )
164
-
165
- with open ("/log.html" , "r" , encoding = "UTF-8" ) as f :
166
- log_html = str (f .read ())
167
-
168
- with open ("/report.html" , "r" ) as f :
169
- report_html = str (f .read ())
170
-
171
- js .postMessage (
172
- json .dumps (
173
- {"log_html" : log_html , "report_html" : report_html , "std_output" : std_output }
174
183
)
175
- )
176
184
185
+ except Exception as e :
186
+ print ("Exception:" )
187
+ js .console .log (traceback .format_exc ())
188
+ js .postMessage (json .dumps ({}))
177
189
except Exception as e :
178
- print ("Exception:" )
179
- js .console .log (traceback .format_exc ())
190
+ pass
0 commit comments