|
14 | 14 | class ServerKeywords(LibraryComponent): |
15 | 15 | _nbserver_handles = [] |
16 | 16 | _nbserver_tmpdirs = {} |
| 17 | + _nbserver_notebook_dirs = {} |
17 | 18 |
|
18 | 19 | @keyword |
19 | 20 | def start_new_jupyter_server(self, command="jupyter", *arguments, **configuration): |
20 | 21 | """ 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. |
21 | 28 | """ |
22 | 29 | plib = BuiltIn().get_library_instance("Process") |
23 | 30 | if not arguments: |
24 | 31 | arguments = self.build_jupyter_server_arguments() |
25 | 32 |
|
26 | 33 | tmpdir = tempfile.mkdtemp() |
27 | 34 |
|
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 | 35 | if "env:HOME" not in configuration: |
34 | 36 | home_dir = join(tmpdir, "home") |
35 | 37 | os.mkdir(home_dir) |
36 | 38 | configuration["env:HOME"] = home_dir |
37 | 39 |
|
| 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 | + |
38 | 46 | handle = plib.start_process("jupyter", *arguments, **configuration) |
39 | 47 |
|
40 | 48 | self._nbserver_handles += [handle] |
41 | 49 | self._nbserver_tmpdirs[handle] = tmpdir |
| 50 | + self._nbserver_notebook_dirs[handle] = notebook_dir |
42 | 51 |
|
43 | 52 | return handle |
44 | 53 |
|
45 | 54 | @keyword |
46 | 55 | def build_jupyter_server_arguments(self): |
| 56 | + """ Some default jupyter arguments |
| 57 | + """ |
47 | 58 | return ["notebook", "--no-browser"] |
48 | 59 |
|
| 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 | + |
49 | 89 | @keyword |
50 | 90 | def wait_for_jupyter_server_to_be_ready(self, *nbservers, **kwargs): |
51 | 91 | """ Wait for the most-recently started Jupyter server to be ready |
|
0 commit comments