|
| 1 | +from datetime import datetime |
| 2 | +from pathlib import Path |
| 3 | +from typing import Union |
| 4 | + |
| 5 | +import click |
| 6 | +import yaml |
| 7 | + |
| 8 | +from cli.helpers import is_function_dir |
| 9 | +from cli.path_iterator import PathIterator |
| 10 | + |
| 11 | + |
| 12 | +@click.command() |
| 13 | +@click.option( |
| 14 | + "-p", |
| 15 | + "-path", |
| 16 | + help="Path to one of: specific function.yaml, directory containing function.yaml or a root directory to search function.yamls in", |
| 17 | +) |
| 18 | +def function_to_item_cli(path: str): |
| 19 | + function_to_item(path) |
| 20 | + |
| 21 | + |
| 22 | +def function_to_item(path: str): |
| 23 | + path = Path(path) |
| 24 | + |
| 25 | + if not path.exists(): |
| 26 | + click.echo(f"{path} not found") |
| 27 | + exit(1) |
| 28 | + |
| 29 | + # If this is a directory, its either project root or a specific directory in the project |
| 30 | + if path.is_dir(): |
| 31 | + function_path = path / "function.yaml" |
| 32 | + # If its a function directory |
| 33 | + if function_path.exists(): |
| 34 | + item_path = path / "item.yaml" |
| 35 | + item = function_yaml_to_item(function_path) |
| 36 | + with open(item_path, "w") as f: |
| 37 | + yaml.dump(item, f) |
| 38 | + # Otherwise its a root directory to be iterated |
| 39 | + else: |
| 40 | + function_iterator = PathIterator( |
| 41 | + root=path, rule=is_function_dir, as_path=True |
| 42 | + ) |
| 43 | + for function in function_iterator: |
| 44 | + function_path = function / "function.yaml" |
| 45 | + item_path = function / "item.yaml" |
| 46 | + item = function_yaml_to_item(function_path) |
| 47 | + with open(item_path, "w") as f: |
| 48 | + yaml.dump(item, f) |
| 49 | + # Otherwise its a path to function.yaml |
| 50 | + else: |
| 51 | + path_dir = path.parent |
| 52 | + item_path = path_dir / "item.yaml" |
| 53 | + item = function_yaml_to_item(path) |
| 54 | + with open(item_path, "w") as f: |
| 55 | + yaml.dump(item, f) |
| 56 | + exit(0) |
| 57 | + |
| 58 | + |
| 59 | +def function_yaml_to_item(function_path: Union[str, Path]) -> dict: |
| 60 | + |
| 61 | + function_path = Path(function_path) |
| 62 | + function_yaml = yaml.full_load(open(function_path)) |
| 63 | + |
| 64 | + metadata = function_yaml.get("metadata", {}) |
| 65 | + spec = function_yaml.get("spec", {}) |
| 66 | + |
| 67 | + item = { |
| 68 | + "apiVersion": "v1", |
| 69 | + "categories": metadata.get("categories") or [], |
| 70 | + "description": spec.get("description") or "", |
| 71 | + "doc": "", |
| 72 | + "example": get_ipynb_file(function_path.parent), |
| 73 | + "generationDate": datetime.utcnow().strftime("%Y-%m-%d:%H-%M"), |
| 74 | + "icon": "", |
| 75 | + "labels": metadata.get("labels") or {}, |
| 76 | + "maintainers": [], |
| 77 | + "mlrunVersion": "", |
| 78 | + "name": metadata.get("name") or "", |
| 79 | + "platformVersion": "", |
| 80 | + "spec": { |
| 81 | + "filename": get_py_file(function_path.parent), |
| 82 | + "handler": get_handler(function_yaml), |
| 83 | + "image": get_image(function_yaml), |
| 84 | + "kind": function_yaml.get("kind") or "", |
| 85 | + "requirements": get_requirements(function_yaml), |
| 86 | + }, |
| 87 | + "url": "", |
| 88 | + "version": metadata.get("tag") or "0.0.1", |
| 89 | + "marketplaceType": "", |
| 90 | + } |
| 91 | + |
| 92 | + return item |
| 93 | + |
| 94 | + |
| 95 | +def get_ipynb_file(path: Path) -> str: |
| 96 | + default = path / f"{path.name}.ipynb" |
| 97 | + |
| 98 | + if default.exists(): |
| 99 | + return f"{path.name}.ipynb" |
| 100 | + |
| 101 | + ipynbs = list(filter(lambda d: d.suffix == ".ipynb", path.iterdir())) |
| 102 | + ipynbs = list(filter(lambda d: not d.name.startswith("test"), ipynbs)) |
| 103 | + |
| 104 | + if len(ipynbs) > 1: |
| 105 | + click.echo(f"{path.name}: Notebook not found") |
| 106 | + return "" |
| 107 | + elif len(ipynbs) == 1: |
| 108 | + return ipynbs[0].name |
| 109 | + |
| 110 | + click.echo(f"{path.name}: Notebook not found") |
| 111 | + return "" |
| 112 | + |
| 113 | + |
| 114 | +def get_py_file(path: Path) -> str: |
| 115 | + default_py_file = path / "function.py" |
| 116 | + |
| 117 | + if default_py_file.exists(): |
| 118 | + return "function.py" |
| 119 | + |
| 120 | + py_file = filter(lambda d: d.suffix == ".py", path.iterdir()) |
| 121 | + py_file = list(filter(lambda d: not d.name.startswith("test"), py_file)) |
| 122 | + |
| 123 | + if len(py_file) > 1: |
| 124 | + click.echo(f"{path.name}: Python file not found") |
| 125 | + return "" |
| 126 | + elif len(py_file) == 1: |
| 127 | + return py_file[0].name |
| 128 | + |
| 129 | + click.echo(f"{path.name}: Python file not found") |
| 130 | + return "" |
| 131 | + |
| 132 | + |
| 133 | +def get_handler(function_yaml: dict) -> str: |
| 134 | + spec = function_yaml["spec"] |
| 135 | + handler = spec.get("default_handler", "handler") |
| 136 | + return handler |
| 137 | + |
| 138 | + |
| 139 | +def get_image(function_yaml: dict): |
| 140 | + spec = function_yaml.get("spec", {}) |
| 141 | + spec_image = spec.get("image") |
| 142 | + |
| 143 | + build = spec.get("build", {}) |
| 144 | + build_image = build.get("base_image") |
| 145 | + |
| 146 | + base_spec = spec.get("base_spec", {}).get("spec", {}) |
| 147 | + base_spec_image = base_spec.get("build", {}).get("baseImage") |
| 148 | + |
| 149 | + return spec_image or build_image or base_spec_image or "" |
| 150 | + |
| 151 | + |
| 152 | +def get_requirements(function_yaml: dict): |
| 153 | + spec = function_yaml.get("spec", {}) |
| 154 | + base_spec = spec.get("base_spec", {}).get("spec", {}) |
| 155 | + |
| 156 | + spec_commands = spec.get("build", {}).get("commands", []) |
| 157 | + base_spec_commands = base_spec.get("build", {}).get("commands", []) |
| 158 | + |
| 159 | + commands = set() |
| 160 | + |
| 161 | + for command in spec_commands: |
| 162 | + commands.add(command) |
| 163 | + |
| 164 | + for command in base_spec_commands: |
| 165 | + commands.add(command) |
| 166 | + |
| 167 | + requirements = [] |
| 168 | + for command in commands: |
| 169 | + if "uninstall" in command: |
| 170 | + click.echo(f"{function_yaml['metadata']['name']}: Unsupported requirements") |
| 171 | + return [] |
| 172 | + if "python -m pip install " in command: |
| 173 | + command = command.split("python -m pip install ")[-1] |
| 174 | + elif "pip install " in command: |
| 175 | + command = command.split("pip install ")[-1] |
| 176 | + else: |
| 177 | + click.echo(f"{function_yaml['metadata']['name']}: Unsupported requirements") |
| 178 | + return [] |
| 179 | + |
| 180 | + allowed_chars = {".", "_", "-", "="} |
| 181 | + |
| 182 | + if " " in command: |
| 183 | + sub_commands = command.split(" ") |
| 184 | + for sub_command in sub_commands: |
| 185 | + for char in sub_command: |
| 186 | + if not char.isalnum() and char not in allowed_chars: |
| 187 | + click.echo( |
| 188 | + f"{function_yaml['metadata']['name']}: Unsupported requirements" |
| 189 | + ) |
| 190 | + return [] |
| 191 | + requirements.append(sub_command) |
| 192 | + else: |
| 193 | + requirements.append(command) |
| 194 | + return requirements |
| 195 | + |
| 196 | + |
| 197 | +if __name__ == "__main__": |
| 198 | + function_to_item("/home/michaell/projects/functions") |
0 commit comments