1+ import os
2+ import shutil
13import subprocess
4+ import tempfile
25import time
6+ from os .path import join
37
48from robot .libraries .BuiltIn import BuiltIn
59from SeleniumLibrary .base import LibraryComponent , keyword
913
1014class ServerKeywords (LibraryComponent ):
1115 _nbserver_handles = []
16+ _nbserver_tmpdirs = {}
1217
1318 @keyword
1419 def start_new_jupyter_server (self , command = "jupyter" , * arguments , ** configuration ):
@@ -18,8 +23,23 @@ def start_new_jupyter_server(self, command="jupyter", *arguments, **configuratio
1823 if not arguments :
1924 arguments = self .build_jupyter_server_arguments ()
2025
26+ tmpdir = tempfile .mkdtemp ()
27+
28+ if "--notebook-dir" not in arguments :
29+ notebook_dir = join (tmpdir , "notebooks" )
30+ os .mkdir (notebook_dir )
31+ arguments += ["--notebook-dir" , notebook_dir ]
32+
33+ if "env:HOME" not in configuration :
34+ home_dir = join (tmpdir , "home" )
35+ os .mkdir (home_dir )
36+ configuration ["env:HOME" ] = home_dir
37+
2138 handle = plib .start_process ("jupyter" , * arguments , ** configuration )
39+
2240 self ._nbserver_handles += [handle ]
41+ self ._nbserver_tmpdirs [handle ] = tmpdir
42+
2343 return handle
2444
2545 @keyword
@@ -83,7 +103,11 @@ def terminate_all_jupyter_servers(self, kill=False):
83103 plib .terminate_process (handle , kill = kill )
84104 terminated += 1
85105
106+ for tmpdir in self ._nbserver_tmpdirs .values ():
107+ shutil .rmtree (tmpdir )
108+
86109 self ._nbserver_handles = []
110+ self ._nbserver_tmpdirs = {}
87111
88112 return terminated
89113
0 commit comments