1
1
from pathlib import Path
2
2
from typing import Optional
3
-
3
+ import os
4
4
import click
5
5
from mlrun import code_to_function
6
6
from yaml import full_load
7
+ from cli .path_iterator import PathIterator
8
+ from cli .helpers import (
9
+ is_item_dir
10
+ )
7
11
8
12
9
13
@click .command ()
10
14
@click .option (
11
15
"-i" , "--item-path" , help = "Path to item.yaml file or a directory containing one"
12
16
)
13
17
@click .option ("-o" , "--output-path" , help = "Path to code_to_function output" )
14
- def item_to_function (item_path : str , output_path : Optional [str ] = None ):
15
- item_path = Path (item_path )
18
+ @click .option ("-r" , "--root-directory" , help = "Path to root directory containing multiple functions" )
19
+ def item_to_function (item_path : str , output_path : Optional [str ] = None , root_directory : Optional [str ] = None ):
20
+ if root_directory is None :
21
+ item_to_function_from_path (item_path , output_path )
22
+ else :
23
+ item_iterator = PathIterator (root = root_directory , rule = is_item_dir , as_path = True )
24
+
25
+ for inner_dir in item_iterator :
26
+ # Iterate individual files in each directory
27
+ for inner_file in inner_dir .iterdir ():
28
+ if inner_file .is_file () and inner_file .name == 'item.yaml' :
29
+ inner_file_dir = os .path .dirname (str (inner_file ))
30
+ output_file_path = inner_file_dir + "/" + "gen_function.yaml"
31
+ print (inner_file_dir )
32
+ try :
33
+ item_to_function_from_path (str (inner_file_dir ), output_file_path )
34
+ except Exception :
35
+ print ("failed to generate yaml for {}" .format (str (inner_dir )))
36
+
16
37
38
+ def item_to_function_from_path (item_path : str , output_path : Optional [str ] = None ):
39
+ item_path = Path (item_path )
40
+ base_path = ""
17
41
if item_path .is_dir ():
18
42
if (item_path / "item.yaml" ).exists ():
43
+ base_path = str (item_path )
19
44
item_path = item_path / "item.yaml"
45
+
20
46
else :
21
47
raise FileNotFoundError (f"{ item_path } does not contain a item.yaml file" )
22
48
elif not item_path .exists ():
@@ -30,10 +56,11 @@ def item_to_function(item_path: str, output_path: Optional[str] = None):
30
56
if filename .endswith (".ipynb" ):
31
57
code_output = Path (filename )
32
58
code_output = code_output .parent / f"{ code_output .stem } .py"
59
+ code_file_name = get_filename (base_path , item_yaml )
33
60
34
61
function_object = code_to_function (
35
62
name = item_yaml ["name" ],
36
- filename = item_yaml . get ( "spec" , {}). get ( "filename" ) ,
63
+ filename = code_file_name ,
37
64
handler = item_yaml .get ("spec" , {}).get ("handler" ),
38
65
kind = item_yaml .get ("spec" , {}).get ("kind" ),
39
66
code_output = code_output ,
@@ -58,5 +85,14 @@ def item_to_function(item_path: str, output_path: Optional[str] = None):
58
85
function_object .export (target = str (output_path .resolve ()))
59
86
60
87
88
+ def get_filename (base_path , item_yaml ):
89
+ filename = item_yaml .get ("spec" , {}).get ("filename" )
90
+ if filename is '' :
91
+ filename = base_path + "/" + item_yaml .get ("example" )
92
+ else :
93
+ filename = base_path + "/" + item_yaml .get ("spec" , {}).get ("filename" )
94
+ return filename
95
+
96
+
61
97
if __name__ == "__main__" :
62
98
item_to_function ()
0 commit comments