
                             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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Zd Z G d dej,                        Zy)z#Move local source snapshots to GCP.    )absolute_import)division)unicode_literalsN)metric_names)storage_util)gcloudignore)log)metrics)fileszSome files were not included in the source upload.

Check the gcloud log [{log_file}] to see which files and the contents of the
default gcloudignore file used (see `$ gcloud topic gcloudignore` to learn
more).
c                 >    dx| _         | _        dx| _        | _        | S )Nr   root)uidgidunamegname)tarinfos    1lib/googlecloudsdk/api_lib/cloudbuild/snapshot.py_ResetOwnershipr   *   s#    '+"(('-'-	.    c                   &    e Zd ZdZd Zd Z	 ddZy)Snapshotz7Snapshot is a manifest of the source in a directory.
  c                    t        j                  |d      }| j                  D ]  }|j                  |      }t        j
                  j                  |      r0t         j                  |_        t	        j                  |      |_
        n`t        j
                  j                  |      rt         j                  |_        n+t        j                  d|       t         j                  |_        t	        j                  |      j                   |_        |j%                  t'        |             t        j                  d|        | j(                  D ]/  }|j+                  |t&               t        j                  d|       1 |S )zConstructs a tarball of snapshot contents.

    Args:
      archive_path: Path to place tar file.

    Returns:
      tarfile.TarFile, The constructed tar file.
    zw:gz)modez=Adding [%s] as dir; os.path says is neither a dir nor a link.zAdded dir [%s])filterz
Added [%s])tarfileopendirs
gettarinfoospathislinkSYMTYPEtypereadlinklinknameisdirDIRTYPEr	   debugstatst_moder   addfiler   r   add)selfarchive_pathtfdpathtr    s         r   _MakeTarballzSnapshot._MakeTarball4   s     
l	0B
--
a		[['
77==		K	 wwu~%%afjj#$	ii %(  

ffT/f*	iid#  Ir   c                 $   t        j                  |dt         j                        }	 | j                  D ]  }|j	                  |        | j
                  D ]  }|j	                  |        	 |j                          y # |j                          w xY w)Nw)zipfileZipFileZIP_DEFLATEDr   writer   close)r-   r.   zip_filer0   r    s        r   _MakeZipFilezSnapshot._MakeZipFileR   sj    |S'2F2FGH99%u **$t  nnhnns   AA= =BNc                 0   t        j                  t        j                        5  t	        j
                  | j                        5  t	        j                         5 }|j                         j                  d      r2t        j                  j                  |d      }| j                  |       nAt        j                  j                  |d      }| j                  |      }|j                          t        j                  j                  | j                  |xs t         j"                        }| j$                  rt        j                  j'                  |      r%t)        j*                  dj-                  |             nG|sEt(        j.                  j1                  t2        j-                  t)        j4                                      |ss|j                         j                  d      rdnd}	t(        j.                  j7                  dj-                  |	| j                  |j8                  |j:                  	             |j=                  ||      cd
d
d
       cd
d
d
       cd
d
d
       S # 1 sw Y   nxY w	 d
d
d
       n# 1 sw Y   nxY wd
d
d
       y
# 1 sw Y   y
xY w)a  Copy an archive of the snapshot to GCS.

    Args:
      storage_client: storage_api.StorageClient, The storage client to use for
        uploading.
      gcs_object: storage.objects Resource, The GCS object to write.
      ignore_file: Override .gcloudignore file to specify skip files.
      hide_logs: boolean, not print the status message if the flag is true.

    Returns:
      storage_v1_messages.Object, The written GCS object.
    z.zipzfile.zipzfile.tgzzUsing ignore file [{}])log_filer5   tarballzAUploading {file_type} of [{src_dir}] to [gs://{bucket}/{object}]
)	file_typesrc_dirbucketobjectN)r
   RecordDurationr   UPLOAD_SOURCEr   ChDirr@   TemporaryDirectoryNameendswithr   r    joinr;   r2   r9   r   IGNORE_FILE_NAMEany_files_ignoredexistsr	   infoformatstatusPrint_IGNORED_FILE_MESSAGEGetLogFilePathr8   rA   rB   CopyFileToGCS)
r-   storage_client
gcs_objectignore_file	hide_logstmpr.   r/   ignore_file_pathr?   s
             r   CopyArchiveToGCSzSnapshot.CopyArchiveToGCS\   s    
		 : :	;;;t||$%%'3__''/77<<Z8Ll+77<<Z8L""<0BHHJWW\\llKH<+H+HJ
##ww~~./hh/667GHIjj'..8J8J8L.MO'__.77?	Y  JJ--3V' LL%,,%,,	 .4 .  --lJG; (' %$ 
<	;''' %$$ 
<	;	;sB    JI7G,I!	I7	J!I*&I7.	J7J 	<JJ)NF)__name__
__module____qualname____doc__r2   r;   rZ    r   r   r   r   0   s    < EJ.Hr   r   )r^   
__future__r   r   r   r   os.pathr   r5   !googlecloudsdk.api_lib.cloudbuildr   googlecloudsdk.api_lib.storager   googlecloudsdk.command_lib.utilr   googlecloudsdk.corer	   r
   googlecloudsdk.core.utilr   rQ   r   r   r_   r   r   <module>rg      sT    * &  ' 	    : 7 8 # ' * ZH|$$ ZHr   