
    &P                         S 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
Jr  SrS r " S	 S
\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)z@Utilities for determining the current platform and architecture.    )absolute_import)division)unicode_literalsN)encoding)/:*?"<>|c           	         [         R                  " SU 5      (       a	  U SS /nSnO/ nSnSn[        U[        U 5      5       H[  nX   [        ;   a9  SnUR                  SR                  [        R                  X   5      5      5        MH  UR                  X   5        M]     SR                  U5      nU(       aC  [        R                  R                  S	R                  SR                  [        5      U U5      5        U$ )
zGConverts invalid Windows characters to Unicode 'unsupported' character.z
^[A-Za-z]:N   r   FTz${} zgWARNING: The following characters are invalid in Windows file and directory names: {}
Renaming {} to {})researchrangelenINVALID_WINDOWS_PATH_CHARACTERSappendformatindexjoinsysstderrwrite)pathnew_pathstart_indexperformed_conversioninew_path_strings         )lib/googlecloudsdk/core/util/platforms.pyMakePathWindowsCompatibler%   "   s    YY}d##RazHKHKc$i(aw11!ooell
)
/
/
8: ; oodg ) GGH%/JJ HHN!@A4(I*+ 
    c                       \ rS rSrSrSrg)Error=   z2Base class for exceptions in the platforms moudle. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r*   r&   r$   r(   r(   =   s    :r&   r(   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )InvalidEnumValueB   zFException for when a string could not be parsed to a valid enum value.c           	      j   > [         [        U ]  SR                  XSR	                  U5      5      5        g)zConstructs a new exception.

Args:
  given: str, The given string that could not be parsed.
  enum_type: str, The human readable name of the enum you were trying to
    parse.
  options: list(str), The valid values for this enum.
z?Could not parse [{0}] into a valid {1}.  Valid values are [{2}]z, N)superr2   __init__r   r   )selfgiven	enum_typeoptions	__class__s       r$   r6   InvalidEnumValue.__init__E   s-     

D*I	$))G"4	57r&   r*   )r+   r,   r-   r.   r/   r6   r0   __classcell__)r;   s   @r$   r2   r2   B   s    N7 7r&   r2   c                       \ rS rSrSr " S S\5      r\" SSS5      r\" SS	S
5      r\" SSS5      r	\" SSS5      r
\" SSS5      r\\\	\
\/r\S 5       r\SS j5       r\S 5       r\S 5       rSrg)OperatingSystemS   z=An enum representing the operating system you are running on.c                   ~    \ rS rSrSrS rS rS rS rS r	\
S 5       rS	 rS
 rS rS r\S 5       r\S 5       rSrg)OperatingSystem._OSV   zA single operating system.c                 (    Xl         X l        X0l        g Nidname	file_namer7   rG   rH   rI   s       r$   r6   OperatingSystem._OS.__init__Z       gi nr&   c                     U R                   $ rE   rG   r7   s    r$   __str__OperatingSystem._OS.__str___       WWnr&   c                     [        U[        U 5      5      =(       aY    U R                  UR                  :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ rE   
isinstancetyperG   rH   rI   r7   others     r$   __eq__OperatingSystem._OS.__eq__b   V    T
+ 0gg!0ii5::%0 nn/1r&   c                     [        U R                  5      [        U R                  5      -   [        U R                  5      -   $ rE   hashrG   rH   rI   rO   s    r$   __hash__OperatingSystem._OS.__hash__h   +    $'']T$))_,tDNN/CCCr&   c                     X:X  + $ rE   r*   rW   s     r$   __ne__OperatingSystem._OS.__ne__k       r&   c                     X:  X:  -
  $ z;Just a helper equivalent to the cmp() function in Python 2.r*   clsxys      r$   
_CmpHelperOperatingSystem._OS._CmpHelpern        er&   c                     U R                  U R                  U R                  U R                  4UR                  UR                  UR                  45      S:  $ Nr   rl   rG   rH   rI   rW   s     r$   __lt__OperatingSystem._OS.__lt__s   G    __77DIIt~~
.88UZZ
13567 7r&   c                     U R                  U R                  U R                  U R                  4UR                  UR                  UR                  45      S:  $ rp   rq   rW   s     r$   __gt__OperatingSystem._OS.__gt__x   rt   r&   c                 .    U R                  U5      (       + $ rE   rv   rW   s     r$   __le__OperatingSystem._OS.__le__}       U###r&   c                 .    U R                  U5      (       + $ rE   rr   rW   s     r$   __ge__OperatingSystem._OS.__ge__   r|   r&   c                 ~    U [         R                  :X  a  [        R                  " 5       $ [        R                  " 5       $ )z%Returns the operating system version.)r?   WINDOWSplatformversionreleaserO   s    r$   r   OperatingSystem._OS.version   s0     
((	(!!r&   c                 R   U R                   nU [        R                  :X  aC  UR                  5       nUS;   a  U$ UR	                  S5      (       a  USS R                  SS5      $ [        R                  " SU5      nU(       d  gSR                  S	 UR                  5        5       5      $ )
z:Returns a cleaned version of the operating system version.)XPVISTASERVERN    _z(\d+)(\.\d+)?(\.\d+)?.*r   c              3   6   #    U  H  o(       d  M  Uv   M     g 7frE   r*   ).0groups     r$   	<genexpr>4OperatingSystem._OS.clean_version.<locals>.<genexpr>   s     B(8uEUU(8s   
	)
r   r?   r   upper
startswithreplacer   matchr   groups)r7   r   capitalizedmatchess       r$   clean_version!OperatingSystem._OS.clean_version   s     g	((	(mmo/).!!(++"%%c3/
/3W=gWWB(8BBBr&   rI   rG   rH   N)r+   r,   r-   r.   r/   r6   rP   rY   r_   rc   classmethodrl   rr   rv   rz   r   propertyr   r   r0   r*   r&   r$   _OSrB   V   sq    $!
1D  7
7
$$     C Cr&   r   r   WindowswindowsMACOSXzMac OS XdarwinLINUXLinuxlinuxCYGWINCygwincygwinMSYSMsysmsysc                  4    [        [        R                  5      $ zFGets all possible enum values.

Returns:
  list, All the enum values.
)listr?   _ALLr*   r&   r$   	AllValuesOperatingSystem.AllValues   s     $$%%r&   c                     U (       d  g[         R                   H  nUR                  U :X  d  M  Us  $    U(       a5  [        U S[         R                   Vs/ s H  o3R                  PM     sn5      egs  snf )a  Gets the enum corresponding to the given operating system id.

Args:
  os_id: str, The operating system id to parse
  error_on_unknown: bool, True to raise an exception if the id is unknown,
    False to just return None.

Raises:
  InvalidEnumValue: If the given value cannot be parsed.

Returns:
  OperatingSystemTuple, One of the OperatingSystem constants or None if the
  input is None.
NzOperating System)r?   r   rG   r2   )os_iderror_on_unknownoperating_systemvalues       r$   FromIdOperatingSystem.FromId   sm      +00				% 1 U$64C4H4HI4H5hh4HIK K J   A1c                  $   [         R                  S:X  a  [        R                  $ S[        R
                  ;   a  [        R                  $ S[        R
                  ;   a  [        R                  $ S[        R
                  ;   a  [        R                  $ g)zDetermines the current operating system.

Returns:
  OperatingSystemTuple, One of the OperatingSystem constants or None if it
  cannot be determined.
ntr   r   r   N)	osrH   r?   r   r   r   r   r   r   r*   r&   r$   CurrentOperatingSystem.Current   sf     
ww$$$$	CLL	 """	S\\	!###	S\\	!###r&   c                  J    [         R                  5       [         R                  L $ )z8Returns True if the current operating system is Windows.)r?   r   r   r*   r&   r$   	IsWindowsOperatingSystem.IsWindows   s     ""$(?(???r&   r*   NT)r+   r,   r-   r.   r/   objectr   r   r   r   r   r   r   staticmethodr   r   r   r   r0   r*   r&   r$   r?   r?   S   s    ECCF CCJ 	9i0'xX.&
gw
(%x8,&	VVV	$$
65&$	/$& &  2  " @ @r&   r?   c                   "   \ rS rSrSr " S S\5      r\" SSS5      r\" SSS5      r\" SSS5      r	\" S	S	S	5      r
\\\	\
/r0 S
\_S\_S\_S\_S\_S\_S\_S\	_S\	_S\	_S\
_S\
_S\
_S\
_S\
_S\
_r\S 5       r\SS j5       r\S 5       rSrg)Architecture   z@An enum representing the system architecture you are running on.c                   ^    \ rS rSrSrS rS rS rS rS r	\
S 5       rS	 rS
 rS rS rSrg)Architecture._ARCH   zA single architecture.c                 (    Xl         X l        X0l        g rE   rF   rJ   s       r$   r6   Architecture._ARCH.__init__   rL   r&   c                     U R                   $ rE   rN   rO   s    r$   rP   Architecture._ARCH.__str__   rR   r&   c                     [        U[        U 5      5      =(       aY    U R                  UR                  :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ rE   rT   rW   s     r$   rY   Architecture._ARCH.__eq__   r[   r&   c                     [        U R                  5      [        U R                  5      -   [        U R                  5      -   $ rE   r]   rO   s    r$   r_   Architecture._ARCH.__hash__   ra   r&   c                     X:X  + $ rE   r*   rW   s     r$   rc   Architecture._ARCH.__ne__   re   r&   c                     X:  X:  -
  $ rg   r*   rh   s      r$   rl   Architecture._ARCH._CmpHelper   rn   r&   c                     U R                  U R                  U R                  U R                  4UR                  UR                  UR                  45      S:  $ rp   rq   rW   s     r$   rr   Architecture._ARCH.__lt__   rt   r&   c                     U R                  U R                  U R                  U R                  4UR                  UR                  UR                  45      S:  $ rp   rq   rW   s     r$   rv   Architecture._ARCH.__gt__  rt   r&   c                 .    U R                  U5      (       + $ rE   ry   rW   s     r$   rz   Architecture._ARCH.__le__  r|   r&   c                 .    U R                  U5      (       + $ rE   r~   rW   s     r$   r   Architecture._ARCH.__ge__
  r|   r&   r   N)r+   r,   r-   r.   r/   r6   rP   rY   r_   rc   r   rl   rr   rv   rz   r   r0   r*   r&   r$   _ARCHr      sG     !
1D  7
7
$$r&   r   x86x86_64PPCppcarmamd64zi686-64i386i686ia64powerpczpower macintoshppc64armv6armv6larm64armv7armv7laarch64c                  4    [        [        R                  5      $ r   )r   r   r   r*   r&   r$   r   Architecture.AllValues  s     !!""r&   c                     U (       d  g[         R                   H  nUR                  U :X  d  M  Us  $    U(       a5  [        U S[         R                   Vs/ s H  o3R                  PM     sn5      egs  snf )a  Gets the enum corresponding to the given architecture id.

Args:
  architecture_id: str, The architecture id to parse
  error_on_unknown: bool, True to raise an exception if the id is unknown,
    False to just return None.

Raises:
  InvalidEnumValue: If the given value cannot be parsed.

Returns:
  ArchitectureTuple, One of the Architecture constants or None if the input
  is None.
Nr   )r   r   rG   r2   )architecture_idr   archr   s       r$   r   Architecture.FromId&  sh      !!	O	# " _n4@4E4EF4E5hh4EFH H Gr   c                      [         R                  R                  [        R                  " 5       R                  5       5      $ )zDetermines the current system architecture.

Returns:
  ArchitectureTuple, One of the Architecture constants or None if it cannot
  be determined.
)r   _MACHINE_TO_ARCHITECTUREgetr   machinelowerr*   r&   r$   r   Architecture.Current@  s-     0044X5E5E5G5M5M5OPPr&   r*   Nr   )r+   r,   r-   r.   r/   r   r   r   r   r   r   r   r   r   r   r   r   r0   r*   r&   r$   r   r      sV   H+$f +$Z 	eUE"#8X.&eUE"#eUE"#
vsC	 $v)2Fc3 %s c 	 (	 /6s	
 s
 c
 $+C
 29#
 @H  # #  2 Q Qr&   r   c                   T    \ rS rSrSrS rS r\SS j5       rS r	S r
\S	 5       rS
rg)PlatformiK  z+Holds an operating system and architecture.c                     Xl         X l        g)zConstructs a new platform.

Args:
  operating_system: OperatingSystem, The OS
  architecture: Architecture, The machine architecture.
N)r   architecture)r7   r   r  s      r$   r6   Platform.__init__N  s     -$r&   c                 N    SR                  U R                  U R                  5      $ )Nz{}-{})r   r   r  rO   s    r$   rP   Platform.__str__X  s    >>$//1B1BCCr&   Nc                     [        U (       a  U O[        R                  5       U(       a  U5      $ [        R                  5       5      $ )aL  Determines the current platform you are running on.

Args:
  os_override: OperatingSystem, A value to use instead of the current.
  arch_override: Architecture, A value to use instead of the current.

Returns:
  Platform, The platform tuple of operating system and architecture.  Either
  can be None if it could not be determined.
)r   r?   r   r   )os_overridearch_overrides     r$   r   Platform.Current[  s@     "(?(?(A&D D,8,@,@,BD Dr&   c                 P   U R                   [        R                  :X  a8  SR                  U R                   R                  U R                   R
                  S9$ U R                   [        R                  :X  a8  SR                  U R                   R                  U R                   R
                  S9$ U R                   [        R                  :X  a\  SnU R                  [        R                  :X  a  U R                  R                  OSnUR                  UU R                   R
                  S9$ g)zGenerates the fragment of the User-Agent that represents the OS.

Examples:
  (Linux 3.2.5-gg1236)
  (Windows NT 6.1.7601)
  (Macintosh; PPC Mac OS X 12.4.0)
  (Macintosh; Intel Mac OS X 12.4.0)

Returns:
  str, The fragment of the User-Agent string.
z({name} {version}))rH   r   z({name} NT {version})z&(Macintosh; {name} Mac OS X {version})Intelz())r   r?   r   r   rH   r   r   r   r  r   r   )r7   format_stringarch_strings      r$   UserAgentFragmentPlatform.UserAgentFragmentk  s     5 55 "(($$))''// ) 1 1 
		/"9"9	9 %++$$))''// , 1 1 
		/"8"8	8 ?m))\-=-== &&++CJ !!''// " 1 1 r&   c                 t   0 nU R                   [        R                  :X  a  SUS'   SnSnX#-  US'   U$ [        R                  S   S:X  a  [        R                  S   S:  a  SUS	'   O[
        R                  US
'   SUS'   [        R                  US'   [        R                  US'   [        R                  US'   U$ )aG  Returns the args for spawning an async process using Popen on this OS.

Make sure the main process does not wait for the new process. On windows
this means setting the 0x8 creation flag to detach the process.

Killing a group leader kills the whole group. Setting creation flag 0x200 on
Windows or running setsid on *nix makes sure the new process is in a new
session with the new process the group leader. This means it can't be killed
if the parent is killed.

Finally, all file descriptors (FD) need to be closed so that waiting for the
output of the main process does not inadvertently wait for the output of the
new process, which means waiting for the termination of the new process.
If the new process wants to write to a file, it can open new FDs.

Returns:
  {str:}, The args for spawning an async process using Popen on this OS.
T	close_fds   i   creationflagsr         start_new_session
preexec_fnstdinstdoutr   )	r   r?   r   r   version_infor   setsid
subprocessPIPE)r7   argsdetached_processcreate_new_process_groups       r$   AsyncPopenArgsPlatform.AsyncPopenArgs  s    & D 7 77d;#!+.Id? K 
		!		!c&6&6q&9A&=$( !YY\d; ood7m!d8n!d8nKr&   c                      / SQn  [         R                  " U [         R                  [         R                  S9nUR	                  5       u  p#S[
        R                  " U5      ;   a  g g!    g= f)a  Method that detects if platform is actually M1 Arm.

This will return True even in the case where x86 Python is running under
Rosetta 2. This will ONLY return true when running on a Macos M1 machine.
Normal methods, for example "uname -a" will see x86_64 in the M1 case when
Rosetta 2 is running, this method exists for when we want to know what the
actual hardware is.

Returns:
  True if M1 Arm detected, False otherwise.
)sysctlz-nzmachdep.cpu.brand_string)r  r   zApple M1TF)r  Popenr  STDOUTcommunicater   Decode)cmd_argsproc
stdoutdatar   s       r$   IsActuallyM1ArmArchitecture$Platform.IsActuallyM1ArmArchitecture  sm     <H
:??:3D3DFd &&(mj	xz2	2 
3 
s   AA' 'A+)r  r   )NN)r+   r,   r-   r.   r/   r6   rP   r   r   r  r   r+  r0   r*   r&   r$   r   r   K  sF    3%D D D%N%N  r&   r   c                       \ rS rSrSrSrSr\R                  " \	R                  S5      (       a  SOSrSrSrS	rS
rSS jrS rS rS rSS jrSrg)PythonVersioni  a   Class to validate the Python version we are using.

The Cloud CLI officially supports Python 3.9.

However, many commands do work with Python 3.6, so we don't error out when
users are using this (we consider it sometimes "compatible" but not
"supported").
)r     )r  	   )ENABLE_MAX_GCLOUD_PYTHON_VERSION_OVERRIDE)r     )r     )r  
   zJanuary 27th, 2026z
If you have a compatible Python interpreter installed, you can use it by setting
the CLOUDSDK_PYTHON environment variable to point to it.

Nc                     U(       a  Xl         g [        [        S5      (       a  [        R                  S S U l         g S U l         g )Nr  r   )r   hasattrr   r  )r7   r   s     r$   r6   PythonVersion.__init__  s2    l	n	%	%%%bq)dldlr&   c                 R    [         R                  5       [         R                  :w  a  gg)Nr   z~
To reinstall gcloud, run:
    $ gcloud components reinstall

This will also prompt to install a compatible version of Python.)r?   r   r   rO   s    r$   InstallMacPythonMessage%PythonVersion.InstallMacPythonMessage  s"     O$:$::Or&   c                 j    SR                  [        R                  S   [        R                  S   5      $ Nz)Please use Python version {0}.{1} and up.r   r  )r   r.  MIN_SUPPORTED_PY3_VERSIONrO   s    r$   SupportedVersionMessage%PythonVersion.SupportedVersionMessage   s1    6==//2//24 4r&   c                 j    SR                  [        R                  S   [        R                  S   5      $ r<  )r   r.  "UPCOMING_PY3_MIN_SUPPORTED_VERSIONrO   s    r$   UpcomingSupportedVersionMessage-PythonVersion.UpcomingSupportedVersionMessage  s1    6==88;88; r&   c           
         Sn[         R                  " [        R                  SS5      R	                  5       S:H  nSnU R
                  (       d/  SR                  U R                  5       U R                  5       5      nOU R
                  S   S:  a1  S	R                  U R                  5       U R                  5       5      nS
nOhU R
                  [        R                  :  aJ  SR                  U R
                  S   U R
                  S   U R                  5       U R                  5       5      nU(       a[  U(       aT  U(       aM  [        R                  R                  U5        [        R                  R                  [        R                  5        g
U(       a_  U(       a  [        U5      e[        R                  R                  U5        [        R                  R                  [        R                  5        gU R
                  U R                   :  a  [        R                  R                  SR                  U R
                  S   U R                  5       U R                  5       5      5        [        R                  R                  S[        R                  -   5        g
[        R"                  (       a  U R
                  [        R"                  :  a  [        R                  R                  SR                  [        R$                  S   [        R$                  S   [        R&                  U R)                  5       U R                  5       5      5        [        R                  R                  S[        R                  -   5        g
)a  Ensure that the Python version we are using is compatible.

This will print an error message if not compatible.

Compatible versions are 3.6+.
We don't guarantee support for 3.6 so we want to warn about it.

Args:
  raise_exception: bool, True to raise an exception rather than printing
    the error and exiting.

Raises:
  Error: If not compatible and raise_exception is True.

Returns:
  bool, True if the version is valid, False otherwise.
NCLOUDSDK_ALLOW_PY2FalsetrueFzZERROR: Your current version of Python is not compatible with the Google Cloud CLI. {0}{1}
r   r  zDERROR: Python 2 is not compatible with the Google Cloud CLI. {0}{1}
TzJERROR: Python {0}.{1} is not compatible with the Google Cloud CLI. {2}{3}
r  zyWARNING:  Python 3.{0}.x is no longer officially supported by the Google Cloud CLI
and may not function correctly. {1}{2}
z:WARNING:  Python {0}.{1} will be deprecated on {2}. {3}{4})r   GetEncodedValuer   environr   r   r   r>  r9  r.  MIN_REQUIRED_PY3_VERSIONr   r   r   ENV_VAR_MESSAGEr(   r=  rA  UPCOMING_SUNSET_PY3_VERSIONUPCOMING_PY3_DEPRECATION_DATErB  )r7   raise_exceptionerror	allow_py2	py2_errors        r$   IsCompatiblePythonVersion.IsCompatible  s   $ E  JJ,g	

%'	  I<<0t3353357  
a1	  &t'C'C'E'+'C'C'E!G 
 i	>>	>%t||AQ3353357  y	jju	jj}445		El	jju	jj}445 ||d444	jjEFLfll1o**,d.J.J.LGNO
 
jjtM999:"  
:
:
,,II
I	jj>>Df77:77:99224**,?	 
jjtM999:r&   )r   rE   )F)r+   r,   r-   r.   r/   rK  r=  r   rI  r   rJ  MAX_SUPPORTED_PY3_VERSIONrM  rA  rN  rL  r6   r9  r>  rB  rS  r0   r*   r&   r$   r.  r.    sy     $$ 
	!	!
**A
 
    !''.$"6/P4
Sr&   r.  )r/   
__future__r   r   r   r   r   r   r  r   googlecloudsdk.core.utilr   r   r%   	Exceptionr(   r2   r   r?   r   r   r.  r*   r&   r$   <module>rY     s     G &  ' 	  	  
 - #K 6I 
7u 7"G@f G@TkQ6 kQ\Ev EPKF Kr&   