
                         l   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddl	m
Z
 ddl	mZ dd	l	mZ dd
lmZ ddlmZ ddlmZ ddlmZ  ej,                  ej.                  j0                  ej.                  j2                  ej.                  j4                         G d dej6                               Zy)z.Command to run a training application locally.    )absolute_import)division)unicode_literalsN)base)flags)
local_util)
validation)build)run)log)filesc                   P    e Zd ZdZ ej
                  d      ddZed        Zd Z	y)CreatezmRun a custom training locally.

  Packages your training code into a Docker image and executes it locally.
  aI            {description}

          You should execute this command in the top folder which includes all
          the code and resources you want to pack and run, or specify the
          'work-dir' flag to point to it. Any other path you specified via flags
          should be a relative path to the work-dir and under it; otherwise it
          will be unaccessible.

          Supposing your directories are like the following structures:

            /root
              - my_project
                  - my_training
                      - task.py
                      - util.py
                      - setup.py
                  - other_modules
                      - some_module.py
                  - dataset
                      - small.dat
                      - large.dat
                  - config
                  - dep
                      - foo.tar.gz
                  - bar.whl
                  - requirements.txt
              - another_project
                  - something

          If you set 'my_project' as the package, then you should
          execute the task.py by specifying "--script=my_training/task.py" or
          "--python-module=my_training.task", the 'requirements.txt' will be
          processed. And you will also be able to install extra packages by,
          e.g. specifying "--extra-packages=dep/foo.tar.gz,bar.whl" or include
          extra directories, e.g. specifying "--extra-dirs=dataset,config".

          If you set 'my_training' as the package, then you should
          execute the task.py by specifying "--script=task.py" or
          "--python-module=task", the 'setup.py' will be processed. However, you
          won't be able to access any other files or directories that are not in
          'my_training' folder.

          See more details in the HELP info of the corresponding flags.
          a            To execute an python module with required dependencies, run:

            $ {command} --python-module=my_training.task --executor-image-uri=gcr.io/my/image --requirements=pandas,scipy>=1.3.0

          To execute a python script using local GPU, run:

            $ {command} --script=my_training/task.py --executor-image-uri=gcr.io/my/image --gpu

          To execute an arbitrary script with custom arguments, run:

            $ {command} --script=my_run.sh --executor-image-uri=gcr.io/my/image -- --my-arg bar --enable_foo

          To run an existing container training without building new image, run:

            $ {command} --executor-image-uri=gcr.io/my/custom-training-image
          )DESCRIPTIONEXAMPLESc                 .    t        j                  |        y )N)r   AddLocalRunCustomJobFlags)parsers    'lib/surface/ai/custom_jobs/local_run.pyArgszCreate.Argsl   s    	##F+    c                    t        j                  |      }t        j                  |j                        5  t
        j                  j                  dj                  |j                               |j                  xs |j                  }|j                  rt        j                  ||j                  |j                  |j                  |j                  |j                   |j"                  |j$                        }|j&                  }t
        j                  j                  d       t
        j                  j                  d       t)        j*                  ||j,                  |j.                  |j0                         t
        j2                  j                  dj                  |             t4        j6                  j9                  t4        j6                  j;                  |j                  |j                  xs d            \  }}t=        j>                  |      r.t
        j                  j                  dj                  |             d d d        y # 1 sw Y   y xY w)	NzPackage is set to {}.)
base_imagehost_workdirmain_scriptpython_modulerequirementsextra_packages
extra_dirsoutput_image_namezA training image is built.zStarting to run ...)
image_name
enable_gpuservice_account_key	user_argsz<A local run is finished successfully using custom image: {}. z'Cleaned Python cache from directory: {}) r	   ValidateLocalRunArgsr   ChDirlocal_package_pathr   statusPrintformatexecutor_image_urir   scriptdocker_builder
BuildImager   r   r   r   output_image_urinamedocker_runnerRunContainergpuservice_account_key_fileargsoutospathsplitjoinr   ClearPyCache)selfr6   executable_imagebuilt_image	cache_dir_s         r   Runz
Create.Runp   s   **40D	T,,	-	jj.55d6M6MNO00CDOO	$//'00,,**.."335 '++

56	jj,-  %XX";;II	 
ggmm
H
O
O !
 WW]]
'',,t..0Ar
BDli		 	 	+

5<<YG	IC 
.	-	-s   HI

IN)
__name__
__module____qualname____doc__textwrapdedentdetailed_helpstaticmethodr   rB    r   r   r   r   "   sF     (// , ,\aA-F , ,%Ir   r   )rF   
__future__r   r   r   r8   rG   googlecloudsdk.callioper   )googlecloudsdk.command_lib.ai.custom_jobsr   r   r	   $googlecloudsdk.command_lib.ai.dockerr
   r.   r   r2   googlecloudsdk.corer   googlecloudsdk.core.utilr   ReleaseTracksReleaseTrackGABETAALPHACreateCommandr   rK   r   r   <module>rX      s    5 &  ' 	  ( ; @ @ H E # * D%%(($*;*;*@*@%%++-qIT qI-qIr   