
    5'                        S r SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKJr  Sr\" \
R                   5      R#                  5       rS	\;   rS
\;   rS\;   rS\;   r\(       aH  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SKJr  \R@                  " 5       S   \:H  r!OSr!S r"S r#S r$S(S jr%S r&S)S jr'S r(S*S jr)S r*S  r+S! r,S" r-S# r.S$ r/S% r0 " S& S'\Rb                  5      r2g)+a(  Shared utility structures and methods for interacting with the host system.

The methods in this module should be limited to obtaining system information and
simple file operations (disk info, retrieving metadata about existing files,
creating directories, fetching environment variables, etc.).
    )absolute_import)print_function)division)unicode_literalsN)WINDOWS_1252   win32cygwinlinuxdarwin)c_int)c_uint64)c_char_p)	c_wchar_p)windll)POINTER)WINFUNCTYPE)WinError   Fc           
      &   [         (       a}   [        [        [        [	        [
        5      [	        [
        5      [	        [
        5      5      nU" S[        R                  4S5      nS nX!l
        U" [        R                  " S5      5      $ [        R                  " U 5      u
  p4  p5        nXE-  $ ! [         aZ    [        [        [        [	        [
        5      [	        [
        5      [	        [
        5      5      nU" S[        R                  4S5      n Nf = f)z(Return path/drive free space (in bytes).GetDiskFreeSpaceExW))r   lpszPathName)   lpFreeUserSpace)r   lpTotalSpace)r   lpFreeSpaceGetDiskFreeSpaceExAc                 B    U (       d
  [        5       eUS   R                  $ )Nr   )r   value)resultunused_funcargss      *platform/gsutil/gslib/utils/system_util.pyGetDiskFreeSpaceExErrCheck2CheckFreeSpace.<locals>.GetDiskFreeSpaceExErrCheckU   s    j!W]]    SystemDrive)
IS_WINDOWSr   r   r   r   r   r   kernel32AttributeErrorr   errcheckosgetenvstatvfs)pathget_disk_free_space_exr$   _f_frsizef_bavails         r#   CheckFreeSpacer4   <   s    Z*5)WX=N+28+<gh>O Q5 &//
2 5 $
 'A#!"))M":;;35::d3C0Q!Q!Q1a+  	*5(GH<M+28+<gh>O Q5 &//
2 5 	s   AB, ,A!DDc                  F    [         R                  R                  S5      S:H  $ )N(CLOUDSDK_CORE_PASS_CREDENTIALS_TO_GSUTIL1r,   environget r&   r#   CloudSdkCredPassingEnabledr<   b   s    	B	Cs	JJr&   c                  B    [         R                  R                  SS5      $ )NCLOUDSDK_VERSION r8   r;   r&   r#   CloudSdkVersionr@   f   s    	*B	//r&   c                 .   [         R                  R                  U 5      (       d   [         R                  " X5        gg! [         aL  nUR
                  [
        R                  :w  a$  UR
                  [
        R                  :w  a  e  SnAg SnAgSnAff = f)z7Creates a directory, suppressing already-exists errors.N)r,   r/   existsmakedirsOSErrorerrnoEEXISTEISDIR)dir_pathmodees      r#   CreateDirIfNeededrK   j   si    		!	!	 	kk(!	 
"  	
ELL	 QWW%< &=	 s   > 
B=BBc                  X   Sn / n[        SS5       nUR                  5       SS nU HB  nUR                  5       u      pVUS   R                  5       (       d  M1  UR	                  U5        MD     SSS5        0 n[        SS5       nU Hu  nUR                  5       SS nUu    pVppppXa;   d  M&  [        U
5      U -  n
[        U5      U -  n[        U	5      n	[        U5      n[        U5      n[        U5      nXXX4Xv'   Mw     SSS5        U$ ! , (       d  f       N= f! , (       d  f       U$ = f)	a  Retrieves disk I/O statistics for all disks.

Adapted from the psutil module's psutil._pslinux.disk_io_counters:
  http://code.google.com/p/psutil/source/browse/trunk/psutil/_pslinux.py

Originally distributed under under a BSD license.
Original Copyright (c) 2009, Jay Loden, Dave Daeschler, Giampaolo Rodola.

Returns:
  A dictionary containing disk names mapped to the disk counters from
  /disk/diskstats.
i   z/proc/partitionsrr   Nz/proc/diskstats   )open	readlinessplitisdigitappendint)sector_size
partitionsflinesliner1   nameretdictvaluesreadsrbytesrtimewriteswbyteswtimes                  r#   GetDiskCountersrd   y   s)     +*$KKM!"EjjlmaA	b				$  % 's#qzz|CR fFLCa%F6f		V{*V{*E
VE
E
E  $ 
.) %$ $# 
.s%   AD	D	%D,AD	
D
D)c                     U(       d  U R                  5       nU R                  S[        R                  5        U R                  5       nU(       d  U R                  W5        U$ )z>Returns size of file, optionally leaving fp positioned at EOF.r   )tellseekr,   SEEK_END)fpposition_to_eofcur_poscur_file_sizes       r#   GetFileSizerm      sB    	ggiG''!R[['')-	GGG	r&   c                  B    [        5       (       a  [        5       (       a  gg)a  Returns a tuple of the gsutil OAuth2 client ID and secret.

Google OAuth2 clients always have a secret, even if the client is an installed
application/utility such as gsutil.  Of course, in such cases the "secret" is
actually publicly known; security depends entirely on the secrecy of refresh
tokens, which effectively become bearer tokens.

Returns:
  (str, str) A 2-tuple of (client ID, secret).
)z&32555940559.apps.googleusercontent.comZmssLNjJy2998hD4CTg2ejr2)z'909320924072.apps.googleusercontent.comzp3RlpR10xMFh9ZXBS/ZNLYUu)InvokedViaCloudSdkr<   r;   r&   r#   GetGsutilClientIdAndSecretrq      s      8::$
"r&   c                     U R                  5       (       a?  [        R                  (       a  [        R                  $ [        R                  R
                  $ [        U R                  U5      $ N)IsStreamsixPY2sysstdinbufferrP   object_name)storage_urlrI   s     r#   GetStreamFromFileUrlr|      sC    3995SYY%5%55''..r&   c                    ^^  SSK mSSKmUU4S jn U " S5      =(       d    U " S5      =(       d    U " S5      nU(       dW   [        R
                  " [        R                  " 5       [        R                  5      nU " U5      n[        R                  " U5        U(       d$  [        R                  R                  S[        5      n[        U5      $ ! [         a	    [        s $ f = f!    NP= f)z!Returns number of terminal lines.r   Nc                 ~   >  [         R                  " STR                  U TR                  S5      5      S   $ !    g= f)Nhh1234r   )structunpackioctl
TIOCGWINSZ)fdfcntltermioss    r#   ioctl_GWINSZ"GetTermLines.<locals>.ioctl_GWINSZ   s;    ]]4R1C1CV!LMaPPs   48 <r   r   LINES)r   r   ImportError_DEFAULT_NUM_TERM_LINESr,   rP   ctermidO_RDONLYcloser9   r:   rU   )r   iocr   r   r   s      @@r#   GetTermLinesr      s    # 	Q=<?=l1o#	772::<-bchhrl 

**.."9
:C	S/+ 
 #""#"
s   C AC# C C #C'c                  F    [         R                  R                  S5      S:H  $ )NCLOUDSDK_WRAPPERr7   r8   r;   r&   r#   rp   rp      s    	*	+s	22r&   c                  d    S[         R                  ;   n S[         R                  ;   nU =(       d    U$ )z<Returns True if running in a CI environment, e.g. GitHub CI.CIKOKORO_ROOT)r,   r9   )on_github_ci	on_kokoros     r#   IsRunningInCiEnvironmentr      s+     #,rzz))		""r&   c                      [         R                  R                  5       =(       aC    [         R                  R                  5       =(       a    [         R                  R                  5       $ )z9Returns True if currently running interactively on a TTY.)rw   stdoutisattystderrrx   r;   r&   r#   IsRunningInteractivelyr      s9    					K!2!2!4	K9I9I9KKr&   c                      [         R                  n U R                  S:X  a]  U R                  S:  d@  U R                  S:X  a  U R                  S:  d   U R                  S:X  a  U R                  S:X  a  [        5         g g g g )N         r   )rw   version_infomajorminormicro_MonkeyPatchHttpForPython_3x)vers    r#   MonkeyPatchHttpr      s`    # 	YY!^		A#))q.SYY]	aCIIN"$ + r&   c                     ^ SS K n U R                  R                  R                  mU4S jnXR                  R                  l        g )Nr   c                    > T" U 5        U R                   S:  a4  U R                  R                  5        H  u  p[        SUS-   U5        M     g g )Nr   zheader::)
debuglevelheadersitemsprint)selfhdrval	old_begins      r#   PatchedBegin2_MonkeyPatchHttpForPython_3x.<locals>.PatchedBegin  sD    dOll((*(#isC( + r&   )httpclientHTTPResponsebegin)r   r   r   s     @r#   r   r     s4     kk&&,,)) $0++ r&   c               #   ^   #    [         R                   H  n U R                  5       v   M     g7f)z3A generator function that returns lines from stdin.N)rw   rx   rstrip)rZ   s    r#   StdinIteratorr     s     iid
++- s   +-c                   $    \ rS rSrSrS rS rSrg)StdinIteratorClsi$  zzAn iterator that returns lines from stdin.
This is needed because Python 3 balks at pickling the
generator version above.
c                     U $ rs   r;   )r   s    r#   __iter__StdinIteratorCls.__iter__*  s    Kr&   c                     [         R                  R                  5       nU(       d
  [        5       eUR	                  5       $ rs   )rw   rx   readlineStopIterationr   )r   rZ   s     r#   __next__StdinIteratorCls.__next__-  s+    99DO;;=r&   r;   N)__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes__r;   r&   r#   r   r   $  s    
r&   r   )i  )F)rb)3r   
__future__r   r   r   r   rE   localer,   r   rw   ru   gslib.utils.constantsr   r   strplatformlowerPLATFORMr(   	IS_CYGWINIS_LINUXIS_OSXctypesr   r   r   r   r   r   r   r   getdefaultlocale	IS_CP1252r4   r<   r@   rK   rd   rm   rq   r|   r   rp   r   r   r   r   r   Iteratorr   r;   r&   r#   <module>r      s   ' %  '   	  
 
 . s||""$  
 	h	X	 %%'*l:))#LK0'T",/<3#L
%02s|| r&   