3
3
# ---------------------------------------------------------
4
4
5
5
from pathlib import Path
6
- from typing import Dict
6
+ from typing import Dict , Union
7
7
8
8
from azure .ai .ml ._utils .utils import load_yaml
9
9
from azure .ai .ml .constants ._common import FILE_PREFIX
@@ -40,7 +40,7 @@ def __init__(
40
40
def _parse_file_path (value : str ) -> str :
41
41
return value [len (FILE_PREFIX ) :] if value .startswith (FILE_PREFIX ) else value
42
42
43
- def _validate_conda_section (self , source_path : str ) -> MutableValidationResult :
43
+ def _validate_conda_section (self , base_path : str , skip_path_validation : bool ) -> MutableValidationResult :
44
44
validation_result = _ValidationResultBuilder .success ()
45
45
if not self .conda :
46
46
return validation_result
@@ -53,56 +53,56 @@ def _validate_conda_section(self, source_path: str) -> MutableValidationResult:
53
53
)
54
54
if self .conda .get (self .CONDA_DEPENDENCIES_FILE ):
55
55
conda_dependencies_file = self .conda [self .CONDA_DEPENDENCIES_FILE ]
56
- if not (Path (source_path ). parent / conda_dependencies_file ).is_file ():
56
+ if not skip_path_validation and not (Path (base_path ) / conda_dependencies_file ).is_file ():
57
57
validation_result .append_error (
58
58
yaml_path = f"conda.{ self .CONDA_DEPENDENCIES_FILE } " ,
59
59
message = f"Cannot find conda dependencies file: { conda_dependencies_file !r} " ,
60
60
)
61
61
if self .conda .get (self .PIP_REQUIREMENTS_FILE ):
62
62
pip_requirements_file = self .conda [self .PIP_REQUIREMENTS_FILE ]
63
- if not (Path (source_path ). parent / pip_requirements_file ).is_file ():
63
+ if not skip_path_validation and not (Path (base_path ) / pip_requirements_file ).is_file ():
64
64
validation_result .append_error (
65
65
yaml_path = f"conda.{ self .PIP_REQUIREMENTS_FILE } " ,
66
66
message = f"Cannot find pip requirements file: { pip_requirements_file !r} " ,
67
67
)
68
68
return validation_result
69
69
70
- def _validate_docker_section (self , source_path : str ) -> MutableValidationResult :
70
+ def _validate_docker_section (self , base_path : str , skip_path_validation : bool ) -> MutableValidationResult :
71
71
validation_result = _ValidationResultBuilder .success ()
72
72
if not self .docker :
73
73
return validation_result
74
74
if not self .docker .get (self .BUILD ) or not self .docker [self .BUILD ].get (self .DOCKERFILE ):
75
75
return validation_result
76
76
dockerfile_file = self .docker [self .BUILD ][self .DOCKERFILE ]
77
77
dockerfile_file = self ._parse_file_path (dockerfile_file )
78
- if not (Path (source_path ). parent / dockerfile_file ).is_file ():
78
+ if not skip_path_validation and not (Path (base_path ) / dockerfile_file ).is_file ():
79
79
validation_result .append_error (
80
80
yaml_path = f"docker.{ self .BUILD } .{ self .DOCKERFILE } " ,
81
81
message = f"Dockerfile not exists: { dockerfile_file } " ,
82
82
)
83
83
return validation_result
84
84
85
- def _validate (self , source_path : str ) -> MutableValidationResult :
85
+ def _validate (self , base_path : str , skip_path_validation : bool = False ) -> MutableValidationResult :
86
86
validation_result = _ValidationResultBuilder .success ()
87
87
if self .os is not None and self .os not in {"Linux" , "Windows" }:
88
88
validation_result .append_error (
89
89
yaml_path = "os" ,
90
90
message = f"Only support 'Linux' and 'Windows', but got { self .os !r} " ,
91
91
)
92
- validation_result .merge_with (self ._validate_conda_section (source_path ))
93
- validation_result .merge_with (self ._validate_docker_section (source_path ))
92
+ validation_result .merge_with (self ._validate_conda_section (base_path , skip_path_validation ))
93
+ validation_result .merge_with (self ._validate_docker_section (base_path , skip_path_validation ))
94
94
return validation_result
95
95
96
- def _resolve_conda_section (self , source_path : str ) -> None :
96
+ def _resolve_conda_section (self , base_path : Union [ Path , str ] ) -> None :
97
97
if not self .conda :
98
98
return
99
99
if self .conda .get (self .CONDA_DEPENDENCIES_FILE ):
100
100
conda_dependencies_file = self .conda .pop (self .CONDA_DEPENDENCIES_FILE )
101
- self .conda [self .CONDA_DEPENDENCIES ] = load_yaml (Path (source_path ). parent / conda_dependencies_file )
101
+ self .conda [self .CONDA_DEPENDENCIES ] = load_yaml (Path (base_path ) / conda_dependencies_file )
102
102
return
103
103
if self .conda .get (self .PIP_REQUIREMENTS_FILE ):
104
104
pip_requirements_file = self .conda .pop (self .PIP_REQUIREMENTS_FILE )
105
- with open (Path (source_path ). parent / pip_requirements_file ) as f :
105
+ with open (Path (base_path ) / pip_requirements_file ) as f :
106
106
pip_requirements = f .read ().splitlines ()
107
107
self .conda = {
108
108
self .CONDA_DEPENDENCIES : {
@@ -117,7 +117,7 @@ def _resolve_conda_section(self, source_path: str) -> None:
117
117
}
118
118
return
119
119
120
- def _resolve_docker_section (self , source_path : str ) -> None :
120
+ def _resolve_docker_section (self , base_path : Union [ Path , str ] ) -> None :
121
121
if not self .docker :
122
122
return
123
123
if not self .docker .get (self .BUILD ) or not self .docker [self .BUILD ].get (self .DOCKERFILE ):
@@ -126,10 +126,10 @@ def _resolve_docker_section(self, source_path: str) -> None:
126
126
if not dockerfile_file .startswith (FILE_PREFIX ):
127
127
return
128
128
dockerfile_file = self ._parse_file_path (dockerfile_file )
129
- with open (Path (source_path ). parent / dockerfile_file , "r" ) as f :
129
+ with open (Path (base_path ) / dockerfile_file , "r" ) as f :
130
130
self .docker [self .BUILD ][self .DOCKERFILE ] = f .read ()
131
131
return
132
132
133
- def resolve (self , source_path : str ) -> None :
134
- self ._resolve_conda_section (source_path )
135
- self ._resolve_docker_section (source_path )
133
+ def resolve (self , base_path : Union [ Path , str ] ) -> None :
134
+ self ._resolve_conda_section (base_path )
135
+ self ._resolve_docker_section (base_path )
0 commit comments