|
| 1 | +from pathlib import Path |
| 2 | +from subprocess import CalledProcessError, PIPE, run, STDOUT |
| 3 | +from tempfile import TemporaryDirectory |
| 4 | +import venv |
| 5 | + |
| 6 | + |
| 7 | +def _process_output(output): |
| 8 | + """Converts bytes string to list of String lines. |
| 9 | +
|
| 10 | + Args: |
| 11 | + output: Bytes string. |
| 12 | +
|
| 13 | + Returns: |
| 14 | + List of strings. |
| 15 | + """ |
| 16 | + return [x for x in output.decode("utf-8").split("\n") if x] |
| 17 | + |
| 18 | + |
| 19 | +class VirtualEnvManager(object): |
| 20 | + """Helper class for creating/running simple command in a virtual environment.""" |
| 21 | + def __init__(self, path): |
| 22 | + self.path = Path(path) |
| 23 | + self.activate = f"source {self.path / 'bin' / 'activate'}" |
| 24 | + |
| 25 | + @staticmethod |
| 26 | + def _execute(commands): |
| 27 | + """Runs input commands through bash in a child process. |
| 28 | +
|
| 29 | + Args: |
| 30 | + commands: List of string commands. |
| 31 | +
|
| 32 | + Returns: |
| 33 | + List of string output. |
| 34 | + """ |
| 35 | + with TemporaryDirectory() as tmp: |
| 36 | + script_path = Path(tmp) / "script" |
| 37 | + with open(script_path, "w") as script: |
| 38 | + script.write("\n".join(commands)) |
| 39 | + try: |
| 40 | + process = run(["bash", str(script_path)], stdout=PIPE, stderr=STDOUT, |
| 41 | + check=True) |
| 42 | + except CalledProcessError as err: |
| 43 | + for line in _process_output(err.output): |
| 44 | + print(line) |
| 45 | + raise |
| 46 | + return _process_output(process.stdout) |
| 47 | + |
| 48 | + def _execute_python_script(self, commands): |
| 49 | + """Runs input python code in bash in a child process. |
| 50 | +
|
| 51 | + Args: |
| 52 | + commands: List of string python code lines. |
| 53 | +
|
| 54 | + Returns: |
| 55 | + List of string output. |
| 56 | + """ |
| 57 | + with TemporaryDirectory() as tmp: |
| 58 | + script_path = Path(tmp) / "python_script" |
| 59 | + with open(script_path, "w") as script: |
| 60 | + script.write("\n".join(commands)) |
| 61 | + commands = [self.activate, f"python3 {str(script_path)}"] |
| 62 | + return self._execute(commands) |
| 63 | + |
| 64 | + def create_env(self): |
| 65 | + """Creates the virtual environment.""" |
| 66 | + venv.create(self.path, with_pip=True) |
| 67 | + |
| 68 | + def destroy_env(self): |
| 69 | + """Destroys the virtual environment.""" |
| 70 | + raise NotImplementedError("this feature is not implemented yet.") |
| 71 | + |
| 72 | + def install_package(self, name): |
| 73 | + """Installs a package in the virtual environment. |
| 74 | +
|
| 75 | + Args: |
| 76 | + name: String name of the package. |
| 77 | +
|
| 78 | + Returns: |
| 79 | + List of string output. |
| 80 | + """ |
| 81 | + commands = [self.activate, "python3 -m pip --upgrade pip", |
| 82 | + f"python3 -m pip install {name}"] |
| 83 | + return self._execute(commands) |
| 84 | + |
| 85 | + def list_plugins(self): |
| 86 | + """Returns a list of plugins that are available in the virtual environment. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + List of plugins. |
| 90 | + """ |
| 91 | + python_script = [ |
| 92 | + "from analysis_scripts import available_plugins", |
| 93 | + "for plugin in available_plugins():", |
| 94 | + " print(plugin)" |
| 95 | + ] |
| 96 | + return self._execute_python_script(python_script) |
| 97 | + |
| 98 | + def run_analysis_plugin(self, name, catalog, output_directory, config=None): |
| 99 | + """Returns a list of paths to figures created by the plugin from the virtual |
| 100 | + environment. |
| 101 | +
|
| 102 | + Args: |
| 103 | + name: String name of the analysis package. |
| 104 | + catalog: Path to the data catalog. |
| 105 | + output_directory: Path to the output directory. |
| 106 | +
|
| 107 | + Returns: |
| 108 | + List of figure paths. |
| 109 | + """ |
| 110 | + if config: |
| 111 | + python_script = [f"config = {str(config)}",] |
| 112 | + else: |
| 113 | + python_script = ["config = None",] |
| 114 | + python_script += [ |
| 115 | + "from analysis_scripts import run_plugin", |
| 116 | + f"paths = run_plugin('{name}', '{catalog}', '{output_directory}', config=config)", |
| 117 | + "for path in paths:", |
| 118 | + " print(path)" |
| 119 | + ] |
| 120 | + return self._execute_python_script(python_script) |
| 121 | + |
| 122 | + def uninstall_package(self, name): |
| 123 | + """Uninstalls a package from the virtual environment. |
| 124 | +
|
| 125 | + Args: |
| 126 | + name: String name of the package. |
| 127 | +
|
| 128 | + Returns: |
| 129 | + List of string output. |
| 130 | + """ |
| 131 | + commands = [self.activate, f"pip uninstall {name}"] |
| 132 | + return self._execute(commands) |
| 133 | + |
| 134 | + |
| 135 | +def analysis_script_test(): |
| 136 | + env = VirtualEnvManager("foo") |
| 137 | + name = "freanalysis_clouds" |
| 138 | + url = "https://github.com/noaa-gfdl/analysis-scripts.git" |
| 139 | + with TemporaryDirectory() as tmp: |
| 140 | + tmp_path = Path(tmp) |
| 141 | + run(["git", "clone", url, str(tmp_path / "scripts")]) |
| 142 | + output = env.install_package(str(tmp_path / "scripts" / "core" / "figure_tools")) |
| 143 | + output = env.install_package(str(tmp_path / "scripts" / "core" / "analysis_scripts")) |
| 144 | + output = env.install_package(str(tmp_path / "scripts" / "user-analysis-scripts" / name)) |
| 145 | + env.run_analysis_plugin(name, "fake-catalog", ".", config={"a": "b"}) |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + analysis_script_test() |
0 commit comments