
                            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 ddlmZ ddlmZ ddlmZ d Z e
j6                  e
j8                  j:                  e
j8                  j<                         G d de
j>                               Z ddde _!        y)z=Command to SCP to/from a Cloud TPU Queued Resource's Node(s).    )absolute_import)division)unicode_literalsN)FilesCompleter)base)
exceptions)flags)	ssh_utils)ssh)util)log)
propertiesc                     | j                  ddd       | j                  dt        ddd	       | j                  d
dd       | j                  ddd       | j                  ddd       | j                  ddd       y)z>Additional flags and positional args to be passed to *scp(1)*.z
--scp-flagappendzR      Additional flags to be passed to *scp(1)*. This flag may be repeated.
      )actionhelpsourceszSpecifies the files to copy.z[[USER@]INSTANCE:]SRC+)	completerr   metavarnargsdestinationz-Specifies a destination for the source files.z[[USER@]INSTANCE:]DEST)r   r   z	--recurse
store_truezUpload directories recursively.z
--compresszEnable compression.--node0a-            TPU node(s) to connect to. The supported value is a single 0-based
          index of the node(s) in the case of a TPU Pod. It additionally
          supports a comma-separated list (e.g. '1,4,6'), range (e.g. '1-3'), or
          special keyword ``all" to run the command concurrently on each of the
          specified node(s).

          Note that when targeting multiple nodes, you should run 'ssh-add'
          with your private key prior to executing the gcloud command. Default:
          'ssh-add ~/.ssh/google_compute_engine'.
          )defaultr   N)add_argumentr   )parsers    0lib/surface/compute/tpus/queued_resources/scp.py
AddSCPArgsr    %   s    
   	)%   	:&   	,-N   	<.C   	
      c                   2    e Zd ZdZdZdZdZed        Zd Z	y)Scp;Copy files to and from a Cloud TPU Queued Resource via SCP.T@   c                     t         j                  j                  |       t        j                  || j
                  | j                  | j                         t        |       t        j                  |dd       y)z\Set up arguments for this command.

    Args:
      parser: An argparse.ArgumentParser.
    )enable_batching_defaulttpuscp)resource_typeoperation_typeN)r
   BaseSSHCLIHelperArgstpu_ssh_utilsAddTPUSSHArgs_ENABLE_IAP_ENABLE_BATCHINGDEFAULT_BATCH_SIZEr    r	   AddZoneFlag)clsr   s     r   r-   zScp.Args^   s^     ##F+ # 6 6	 v	fE%Hr!   c                 r   t        j                          }t        j                  j                  |j                        }|j
                  D cg c]!  }t        j                  j                  |      # }}t        j                  j                  ||d       |j                  xs |d   j                  }|j                  }|j                  s|D ]	  }||_         d}|j                  sd}t        j                  d      |_
        |j                  9t        j                  j                  j                  j!                  d      |_        t#        j$                  | j'                               }	|	j!                  ||j                        }
t)        j*                  |j,                  |
j.                  j0                        }t3        |      dkD  r%|d   j                  rt5        j6                  dd	      g }d}t3        |      }t9        j:                  |j<                  t3        |            }d g|z  }t?        |      D ]  \  }}|jA                  tC        jD                  t8        jF                  |jH                  || j'                         | jJ                  ||||||f

             |d   jM                          |dz  }||k(  st)        jN                  ||       d}g } |dkD  rt)        jN                  ||       |D cg c]  }|| }}t3        |      |k  r$tQ        jR                  djU                  |             t9        j:                  |j<                  | jV                        }t9        jX                  |||       tP        jZ                  j]                  dt        j                          |z
  z         y c c}w c c}w )NT)single_remoter   F)warn_on_account_user)required   r   z;cannot target multiple nodes while copying files to client.)targetargsz@Could not prepare all {} nodes, attempting to ssh into the rest.z!Completed execution in %s seconds)/timer   FileReferenceFromPathr   r   
SCPCommandVerifyremotehostuserGetDefaultSshUsernamezoner   VALUEScomputeGetqueued_resource_utilsTPUQueuedResourceReleaseTrackqr_ssh_utilsParseNodeFlagnoder(   nodeSpeclenr   InvalidArgumentExceptionr.   ParseBatchSize
batch_size	enumerater   	threadingThreadPrepareNodeForSCPnodeIdr1   startWaitForNodeBatchCompletionr   warningformatr2   SCPIntoPreppedNodesstatusPrint)selfr;   
start_timedstsrcsrcsrB   qr_nameusername_requestedqueued_resource_clientqueued_resource
node_specsprep_nodes_threadscurrent_batch_size	num_nodesprep_node_batch_sizeprepped_nodesindexrO   prepped_nodescp_batch_sizes                        r   RunzScp.Runo   s]   J



$
$T%5%5
6C7;||D|C&&s+|DDNN$48ZZ)47>>FkkG::#
  ;; --4Hfk yy##++0044d4Cdi2DD -00$))DO ++		?&&//J :tAw~~//

G 
 JI(77Z FY&M ,t


"44++##%''$" ""$A	3	3//	
 3 -6 A--.@-P
 *)L# 	)  
 =I%	kk6)$
 #1100N %% JJ+tyy{Z/GH} ETs   &N/7N4N)
__name__
__module____qualname____doc__r0   r1   r2   classmethodr-   rs    r!   r   r#   r#   V   s0    C+I I cr!   r#   r$   a  
        To copy a file (for example, a text file in the local home directory) to
        a Cloud Queued Resource, run:

            $ {command} ~/my-file my-qr:

        To copy a file into all nodes and workers in a Cloud TPU Queued Resource (with the default batch size), run:

            $ {command} ~/my-file my-qr: --worker=all --node=all

        To copy a file into all nodes and workers in a Cloud TPU Queued Resource with a batch size of 4, run:

            $ {command} ~/my-file my-qr: --worker=all --node=all --batch-size=4

        To copy a file into all workers in the first node of a Cloud TPU Queued Resource, run:

            $ {command} ~/my-file my-qr: --worker=all

        To copy a file from a Cloud TPU Queued Resource to the home directory of the local
        computer, run:

            $ {command} my-qr:~/my-file ~/

        To copy all files in a folder to a Cloud TPU Queued Resource, run:

            $ {command} ~/my-folder/ my-qr: --recurse
        )briefEXAMPLES)"rw   
__future__r   r   r   rV   r=   argcomplete.completersr   googlecloudsdk.callioper   r   "googlecloudsdk.command_lib.computer	   r
   8googlecloudsdk.command_lib.compute.tpus.queued_resourcesr   rM   r   rJ   .googlecloudsdk.command_lib.compute.tpus.tpu_vmr.   #googlecloudsdk.command_lib.util.sshgooglecloudsdk.corer   r   r    ReleaseTracksrL   ALPHAGACommandr#   detailed_helpry   r!   r   <module>r      s    D &  '   1 ( . 4 8 X b O 3 # *.b D%%++T->->-A-AB{$,, { C{~ K r!   