Skip to content

Commit 3332cb7

Browse files
committed
add keywords for copying files to notebook dir
1 parent 010fcde commit 3332cb7

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

atest/acceptance/01_server/00_basic.robot

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Test Teardown Terminate All Processes
33
Library JupyterLibrary
44
Library Process
5+
Library OperatingSystem
56

67
*** Test Cases ***
78
Start one server
@@ -29,3 +30,17 @@ Start three servers
2930
Should Not Contain ${log3} The Jupyter Notebook is running msg=Unawaited server log should not contain expected status message
3031
${terminated} = Terminate All Jupyter Servers
3132
Should be equal as integers ${terminated} 0 msg=No servers should have been terminated
33+
34+
Notebook Files
35+
[Setup] Create File ${OUTPUT_DIR}${/}foo.txt bar
36+
${nb1} = Start New Jupyter Server
37+
Copy Files to Jupyter Directory ${OUTPUT_DIR}${/}*.txt
38+
${nbdir} = Get Jupyter Directory ${nb1}
39+
${out} = Get File ${nbdir}${/}foo.txt
40+
Should Be Equal ${out} bar
41+
Copy Files from Jupyter Directory foo.txt ${OUTPUT_DIR}
42+
Terminate All Jupyter Servers
43+
${out} = Get File ${OUTPUT_DIR}${/}foo.txt
44+
Should Be Equal ${out} bar
45+
File Should Not Exist ${nbdir}${/}foo.txt
46+
[Teardown] Remove File ${OUTPUT_DIR}${/}foo.txt

src/JupyterLibrary/keywords/server.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,78 @@
1414
class ServerKeywords(LibraryComponent):
1515
_nbserver_handles = []
1616
_nbserver_tmpdirs = {}
17+
_nbserver_notebook_dirs = {}
1718

1819
@keyword
1920
def start_new_jupyter_server(self, command="jupyter", *arguments, **configuration):
2021
""" Start a Jupyter server
22+
23+
If not configured, the HOME environment variable and current
24+
working directory will be set to avoid leaking configuration
25+
between between runs (or the test instance) itself. These
26+
directories will be cleaned up after the server process is
27+
terminated.
2128
"""
2229
plib = BuiltIn().get_library_instance("Process")
2330
if not arguments:
2431
arguments = self.build_jupyter_server_arguments()
2532

2633
tmpdir = tempfile.mkdtemp()
2734

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-
3335
if "env:HOME" not in configuration:
3436
home_dir = join(tmpdir, "home")
3537
os.mkdir(home_dir)
3638
configuration["env:HOME"] = home_dir
3739

40+
notebook_dir = configuration.get("cwd")
41+
if notebook_dir is None:
42+
notebook_dir = join(tmpdir, "notebooks")
43+
os.mkdir(notebook_dir)
44+
configuration["cwd"] = notebook_dir
45+
3846
handle = plib.start_process("jupyter", *arguments, **configuration)
3947

4048
self._nbserver_handles += [handle]
4149
self._nbserver_tmpdirs[handle] = tmpdir
50+
self._nbserver_notebook_dirs[handle] = notebook_dir
4251

4352
return handle
4453

4554
@keyword
4655
def build_jupyter_server_arguments(self):
56+
""" Some default jupyter arguments
57+
"""
4758
return ["notebook", "--no-browser"]
4859

60+
@keyword
61+
def copy_files_to_jupyter_directory(self, *sources, **kwargs):
62+
""" Copy some files into the (temporary) jupyter server root
63+
"""
64+
nbserver = kwargs.get("nbserver", self._nbserver_handles[-1])
65+
notebook_dir = self._nbserver_notebook_dirs[nbserver]
66+
BuiltIn().import_library("OperatingSystem")
67+
osli = BuiltIn().get_library_instance("OperatingSystem")
68+
osli.copy_files(*(list(sources) + [notebook_dir]))
69+
70+
@keyword
71+
def copy_files_from_jupyter_directory(self, *sources_and_destinations, **kwargs):
72+
""" Copy some files from the (temporary) jupyter server root
73+
74+
Patterns will have the notebook directory prepended
75+
"""
76+
nbserver = kwargs.get("nbserver", self._nbserver_handles[-1])
77+
notebook_dir = self._nbserver_notebook_dirs[nbserver]
78+
BuiltIn().import_library("OperatingSystem")
79+
osli = BuiltIn().get_library_instance("OperatingSystem")
80+
sources = [join(notebook_dir, src) for src in sources_and_destinations[:-1]]
81+
dest = sources_and_destinations[-1]
82+
osli.copy_files(*sources + [dest])
83+
84+
@keyword
85+
def get_jupyter_directory(self, nbserver=None):
86+
nbserver = nbserver if nbserver is not None else self._nbserver_handles[-1]
87+
return self._nbserver_notebook_dirs[nbserver]
88+
4989
@keyword
5090
def wait_for_jupyter_server_to_be_ready(self, *nbservers, **kwargs):
5191
""" Wait for the most-recently started Jupyter server to be ready

0 commit comments

Comments
 (0)