
    8                         S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSK	J
r
  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  S rS rS rS rS rS r SS jrS rS rS rS rS r S r!S r"S r#g)z:Validations of the arguments of custom-jobs command group.    )absolute_import)division)unicode_literalsN)util)
exceptions)	constants)
validation)
local_util)utils)filesc                 J    [         R                  " U [        R                  S9  g)zIValidate whether the given region is allowed for specifically custom job.)available_regionsN)r	   ValidateRegionr   SUPPORTED_TRAINING_REGIONS)regions    ;lib/googlecloudsdk/command_lib/ai/custom_jobs/validation.pyr   r       s    	 D DF    c                 j    U R                   (       a  [        U R                   U5        g[        U5        g)z;Validate the argument values specified in `create` command.N)worker_pool_spec_ValidateWorkerPoolSpecArgs"_ValidateWorkerPoolSpecsFromConfig)argsjob_spec_from_configversions      r   ValidateCreateArgsr   &   s&     
 5 5w?&';<r   c                 r    U S   (       d  [         R                  " SS5      e[        X5        [        U 5        g)a  Validates the argument values specified via `--worker-pool-spec` flags.

Args:
  worker_pool_specs: List[dict], a list of worker pool specs specified in
    command line.
  version: str, the API version this command will interact with, either GA or
    BETA.
r   --worker-pool-speczCEmpty value is not allowed for the first `--worker-pool-spec` flag.N)r   InvalidArgumentException%_ValidateHardwareInWorkerPoolSpecArgs%_ValidateSoftwareInWorkerPoolSpecArgs)worker_pool_specsr   s     r   r   r   /   s;     
1	

-
-MO O ((9C'(9:r   c                 Z   U  GH  nU(       d  M  SU;  a  [         R                  " SS5      eSU;   a  SU;  a  [         R                  " SS5      eUR                  SS5      nU(       d  Mh  [        R                  " SU5      R
                  nUR                  5        Vs/ s H3  nUR                  S	5      (       d  UR                  S
5      (       d  M1  UPM5     nnX6;  d  M  [         R                  " SSR                  USR                  S [        U5       5       5      S95      e   gs  snf )a  Validates the hardware related fields specified in `--worker-pool-spec` flags.

Args:
  worker_pool_specs: List[dict], a list of worker pool specs specified in
    command line.
  api_version: str, the API version this command will interact with, either GA
    or BETA.
zmachine-typer   z9Key [machine-type] required in dict arg but not provided.zaccelerator-countzaccelerator-typezDKey [accelerator-type] required as [accelerator-count] is specified.NMachineSpecNVIDIATPUzWFound invalid value of [accelerator-type]: {actual}. Available values are [{expected}]., c              3   $   #    U  H  ov   M     g 7f)N ).0vs     r   	<genexpr>8_ValidateHardwareInWorkerPoolSpecArgs.<locals>.<genexpr>e   s     %E1DAa1Ds   )actualexpected)r   r   getapi_util
GetMessageAcceleratorTypeValueValuesEnumnames
startswithformatjoinsorted)r!   api_versionspecaccelerator_type	type_enumtypevalid_typess          r   r   r   A   s0     dt	t	#11 GI 	I 
	$);4)G11 R
 	

 "4d;		'';((F(F 	 '__.
.Tx((DOOE,B . 	 
 .33"45;V* II%E1D%EE 6< 6GH H-  "
s   0D(D(c                 ^    [        U S   5      n[        U 5      S:  a  [        U SS U5        gg)zJValidates the software fields specified in all `--worker-pool-spec` flags.r      N)&_ValidateSoftwareInFirstWorkerPoolSpeclen&_ValidateSoftwareInRestWorkerPoolSpecs)r!   has_local_packages     r   r    r    h   s?    < 		a*+<QR+@+<>  r   c                 >    SU ;   a  [        U 5        g[        U 5        g)zValidates the software related fields specified in the first `--worker-pool-spec` flags.

Args:
  spec: dict, the specification of the first worker pool.

Returns:
  A boolean value whether a local package will be used.
local-package-pathTF)+_ValidateWorkerPoolSoftwareWithLocalPackage/_ValidateWorkerPoolSoftwareWithoutLocalPackages)r9   s    r   r@   r@   r   s"     T!/53D9r   c                     U  Hh  nU(       d  M  U(       a-  1 Skn[        UR                  UR                  5       5      SS9  M@  SU;   a  [        R                  " SS5      e[        U5        Mj     g)a&  Validates the argument values specified in all but the first `--worker-pool-spec` flags.

Args:
  specs: List[dict], the list all but the first worker pool specs specified in
    command line.
  is_local_package_specified: bool, whether local package is specified
    in the first worker pool.
>   scriptrequirements
extra-dirspython-moduleextra-packagesexecutor-image-uricontainer-image-urizA local package has been specified in the first `--worker-pool-spec` flag and to be used for all workers, do not specify these keys elsewhere.unexpected_keysreasonrE   r   zPKey [local-package-path] is only allowed in the first `--worker-pool-spec` flag.N)_RaiseErrorIfUnexpectedKeysintersectionkeysr   r   rG   )specsis_local_package_specifiedr9   software_fieldss       r   rB   rB      so     dt	#
 	$+88E;	=  4'33",. . 	8=1 r   c                    SU ;   d   e[        U S   SS9  SU ;  a  [        R                  " SS5      eSU ;   SU ;   -   S:w  a  [        R                  " SS	5      eS
U ;   ag  U S
   nUR                  S5      S   n/ SQnX#;  a-  UR	                  S5      (       d  [        R                  " SS5      e [
        R                  " U5        gg! [         a+  n[        R                  " SSR                  X5      5      eSnAff = f)z^Validate the software in a single `--worker-pool-spec` when `local-package-path` is specified.rE   r   )	flag_namerN   zLKey [executor-image-uri] is required when `local-package-path` is specified.rL   rI   r?   z_Exactly one of keys [python-module, script] is required when `local-package-path` is specified.output-image-uri/r   )zgcr.ioz	eu.gcr.iozasia.gcr.ioz	us.gcr.ioz-docker.pkg.devzSThe value of `output-image-uri` has to be a valid gcr.io or Artifact Registry image+'{}' is not a valid container image uri: {}N)	_RaiseErrorIfNotExistsr   r   splitendswithdocker_utilsValidateRepositoryAndTag
ValueErrorr5   )r9   output_imagehostnamecontainer_registrieses        r   rF   rF      s1   		%%	%
 ,@B %

-
-V 
 (d"23q8

-
-	23 3
 4*+L!!#&q)HN+H4E4E5 5//

_  ++L9     //

8
?
?    s   /C 
C<&C77C<c                 ^   SU ;  d   eSU ;   nSU ;   nSU ;   nX-   S:w  a  [         R                  " SS5      eU(       a  U(       a  [         R                  " SS5      eU(       a  U(       d  [         R                  " SS	5      e1 S
knUR                  U R                  5       5      n[	        USS9  g)zbValidate the software in a single `--worker-pool-spec` when `local-package-path` is not specified.rE   rN   rO   rL   r?   r   zJExactly one of keys [executor-image-uri, container-image-uri] is required.zKKey [python-module] is not allowed together with key [container-image-uri].z Key [python-module] is required.>   rI   rJ   rK   rM   z_Only allow to specify together with `local-package-path` in the first `--worker-pool-spec` flag)rR   N)r   r   rT   rU   rS   )r9   has_executor_imagehas_container_imagehas_python_modulelocal_package_only_keysrQ   s         r   rG   rG      s     
T	))	)+t3-5%-.14

-
-
 
 .

-
-
"$ $
  1

-
-@B B ,88E/nr   c           
          U (       a=  [         R                  " SSR                  SR                  [	        U 5      5      US95      eg )Nr   z(Keys [{keys}] are not allowed: {reason}.r&   )rU   rR   )r   r   r5   r6   r7   rP   s     r   rS   rS      sJ    

-
-HOO6/23F P DE E r   c                 r   U R                    H  nUR                  =(       a3    UR                  R                  =(       d    UR                  R                  nUR                  =(       a    UR                  R
                  nU(       a  U(       d  U(       a  M  U(       a  M  [        R                  " SS5      e   g)zHValidate WorkerPoolSpec message instances imported from the config file.z--configzuExactly one of fields [pythonPackageSpec, containerSpec] is required for a [workerPoolSpecs] in the YAML config file.N)workerPoolSpecspythonPackageSpecexecutorImageUripythonModulecontainerSpecimageUrir   r   )job_specr9   use_python_packageuse_containers       r   r   r      s     &&d// -// 	,++  &&F4+=+=+F+FM,mm6H6H//
JL L 'r   c                    / nU R                   (       a  UR                  S5        U R                  (       a  UR                  S5        U R                  (       a  UR                  S5        U R                  (       a  UR                  S5        U R
                  (       a  UR                  S5        U R                  (       a  UR                  S5        U$ )zAReturns names of all the flags specified only for image building.rI   rL   rJ   rM   rK   r[   )rI   appendpython_modulerJ   extra_packages
extra_dirsoutput_image_uri)r   image_build_argss     r   _ImageBuildArgSpecifiedr     s    	[[H%	O,	N+	,-	__L)	./	r   c           
         [        U 5      nU(       d  gU R                  (       dQ  U R                  (       d@  [        R                  " SS/SR                  SR                  [        U5      5      5      5      eU R                  (       a  SnO'[        R                  " U R                  5      U l        Sn[        R                  R                  [        R                  R                  U R                  U R                  5      5      n[        R                  R                  U5      (       a$  [        R                  R                  U5      (       d;  [        R                   " USR                  U R                  U R                  5      5      eU R"                  =(       d    /  H  n[        R                  R                  [        R                  R                  U R                  U5      5      n[        R                  R                  U5      (       a&  [        R                  R                  U5      (       a  M  [        R                   " SSR                  X@R                  5      5      e   U R$                  =(       d    /  H  n[        R                  R                  [        R                  R                  U R                  U5      5      n[        R                  R                  U5      (       a&  [        R                  R'                  U5      (       a  M  [        R                   " S	S
R                  X`R                  5      5      e   U R(                  (       a$  U R(                  n [*        R,                  " U5        g[*        R0                  " U R                  S9U l        g! [.         a+  n	[        R                   " SSR                  X5      5      eSn	A	ff = f)zEValidates the arguments related to image building and normalize them.Nz--scriptz--python-modulezThey are required to build a training container image. Otherwise, please remove flags [{}] to directly run the `executor-image-uri`.r&   z/File '{}' is not found under the package: '{}'.z--extra-packagesz7Package file '{}' is not found under the package: '{}'.z--extra-dirsz4Directory '{}' is not found under the package: '{}'.z--output-image-urir]   )	base_name)r   rI   rz   r   MinimumArgumentExceptionr5   r6   r7   r
   ModuleToPathospathnormpathlocal_package_pathexistsisfiler   r{   r|   isdirr}   ra   rb   rc   GenerateImageName)
r   build_args_specifiedarg_namescript_pathpackagepackage_path	directorydir_pathrd   rg   s
             r   _ValidBuildArgsOfLocalRunr     s   06	
	T//

-
-	&'	X			&!567	8	: : 
[[H))$*<*<=DK H  ggll4**DKK8:+		$	$BGGNN;,G,G

-
-DKKKK0023 3
 %%++g77##
T,,g68L77>>,''rww~~l/K/K//

D
K
K..01 1	 , OO)r)iww
T,,i8:H77>>(##277==+B+B//

A
H
H0023 3	 * 
((L ++L9 )::++D   //

8
?
?    s   N 
O	&OO	c                    U R                   nU(       a  [        R                  R                  [        R
                  " U5      5      n[        R                  R                  U5      (       a$  [        R                  R                  U5      (       d&  [        R                  " SSR                  U5      5      eO[        R                  " 5       nX l         [        U 5        U $ )zLValidates the arguments specified in `local-run` command and normalize them.z--local-package-pathDirectory '{}' is not found.)r   r   r   abspathr   ExpandHomeDirr   r   r   r   r5   GetCWDr   )r   args_local_package_pachwork_dirs      r   ValidateLocalRunArgsr   \  s     33wwu223JKLH77>>(##277==+B+B//
 
)
0
0
:< < ,C
 ||~H$D!	+r   c                 F   [         R                  R                  [        R                  " U 5      5      n[         R                  R                  U5      (       a$  [         R                  R                  U5      (       d&  [        R                  " USR                  U5      5      eg)zValidate the local package is valid.

Args:
  local_package_path: str, path of the local directory to check.
  flag_name: str, indicates in which flag the path is specified.
r   N)
r   r   r   r   r   r   r   r   r   r5   )r   rZ   r   s      r   r^   r^   n  ss     WW__U001CDE(		!	!x)@)@

-
-299(CE E *Ar   )F)$__doc__
__future__r   r   r   r   googlecloudsdk.api_lib.air   r0   googlecloudsdk.callioper   googlecloudsdk.command_lib.air   r	   )googlecloudsdk.command_lib.ai.custom_jobsr
   $googlecloudsdk.command_lib.ai.dockerr   ra   googlecloudsdk.core.utilr   r   r   r   r   r    r@   rB   rF   rG   rS   r   r   r   r   r^   r(   r   r   <module>r      s    A &  ' 	 6 . 3 4 @ F *F=;$$HN>$ GL">J" J#LEL"&;|$
Er   