o
    &P                     @   s   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	Z	ddl
mZ dZdd	 ZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZdS )z@Utilities for determining the current platform and architecture.    )absolute_import)division)unicode_literalsN)encoding)/:*?"<>|c              	   C   s   t d| r| dd g}d}ng }d}d}t|t| D ]}| | tv r5d}|dt| |  q|| |  qd|}|rRt	j
d	dt| | |S )
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_string r"   ?/tmp/google-cloud-sdk/lib/googlecloudsdk/core/util/platforms.pyMakePathWindowsCompatible"   s,   

r$   c                   @   s   e Zd ZdZdS )Errorz2Base class for exceptions in the platforms moudle.N)__name__
__module____qualname____doc__r"   r"   r"   r#   r%   =   s    r%   c                       s    e Zd ZdZ fddZ  ZS )InvalidEnumValuezFException for when a string could not be parsed to a valid enum value.c              	      s$   t t| d||d| dS )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)superr*   __init__r   r   )selfgiven	enum_typeoptions	__class__r"   r#   r,   E   s   
	zInvalidEnumValue.__init__)r&   r'   r(   r)   r,   __classcell__r"   r"   r1   r#   r*   B   s    r*   c                   @   s   e Zd ZdZG dd deZedddZeddd	Zed
ddZedddZ	edddZ
eeee	e
gZedd ZedddZedd Zedd ZdS )OperatingSystemz=An enum representing the operating system you are running on.c                   @   s|   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	dd Z
dd Zdd Zdd Zdd Zedd Zedd ZdS )zOperatingSystem._OSzA single operating system.c                 C      || _ || _|| _d S Nidname	file_namer-   r8   r9   r:   r"   r"   r#   r,   Z      
zOperatingSystem._OS.__init__c                 C      | j S r6   r8   r-   r"   r"   r#   __str___      zOperatingSystem._OS.__str__c                 C   2   t |t| o| j|jko| j|jko| j|jkS r6   
isinstancetyper8   r9   r:   r-   otherr"   r"   r#   __eq__b      


zOperatingSystem._OS.__eq__c                 C      t | jt | j t | j S r6   hashr8   r9   r:   r?   r"   r"   r#   __hash__h      zOperatingSystem._OS.__hash__c                 C   
   | |k S r6   r"   rF   r"   r"   r#   __ne__k      
zOperatingSystem._OS.__ne__c                 C      ||k||k  S z;Just a helper equivalent to the cmp() function in Python 2.r"   clsxyr"   r"   r#   
_CmpHelpern      zOperatingSystem._OS._CmpHelperc                 C   (   |  | j| j| jf|j|j|jfdk S Nr   rX   r8   r9   r:   rF   r"   r"   r#   __lt__s      zOperatingSystem._OS.__lt__c                 C   (   |  | j| j| jf|j|j|jfdkS r[   r\   rF   r"   r"   r#   __gt__x   r^   zOperatingSystem._OS.__gt__c                 C      |  | S r6   r`   rF   r"   r"   r#   __le__}      zOperatingSystem._OS.__le__c                 C   ra   r6   r]   rF   r"   r"   r#   __ge__   rd   zOperatingSystem._OS.__ge__c                 C   s   | t jkr	t S t S )z%Returns the operating system version.)r4   WINDOWSplatformversionreleaser?   r"   r"   r#   ri      s   
zOperatingSystem._OS.versionc                 C   sn   | j }| tjkr!| }|dv r|S |dr!|dd ddS td|}|s+dS dd	d
 |	 D S )z:Returns a cleaned version of the operating system version.)XPVISTASERVERN    _z(\d+)(\.\d+)?(\.\d+)?.*r   c                 s   s    | ]}|r|V  qd S r6   r"   ).0groupr"   r"   r#   	<genexpr>   s    z4OperatingSystem._OS.clean_version.<locals>.<genexpr>)
ri   r4   rg   upper
startswithreplacer   matchr   groups)r-   ri   capitalizedmatchesr"   r"   r#   clean_version   s   

z!OperatingSystem._OS.clean_versionN)r&   r'   r(   r)   r,   r@   rH   rM   rP   classmethodrX   r]   r`   rc   rf   propertyri   r{   r"   r"   r"   r#   _OSV   s"    

r~   rg   WindowswindowsMACOSXzMac OS XdarwinLINUXLinuxlinuxCYGWINCygwincygwinMSYSMsysmsysc                   C   
   t tjS zRGets all possible enum values.

    Returns:
      list, All the enum values.
    )listr4   _ALLr"   r"   r"   r#   	AllValues      
zOperatingSystem.AllValuesTc                 C   F   | sdS t jD ]}|j| kr|  S q|r!t| ddd t jD dS )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 Systemc                 S      g | ]}|j qS r"   r>   rq   valuer"   r"   r#   
<listcomp>       z*OperatingSystem.FromId.<locals>.<listcomp>)r4   r   r8   r*   )os_iderror_on_unknownoperating_systemr"   r"   r#   FromId      

zOperatingSystem.FromIdc                   C   sD   t jdkrtjS dtjv rtjS dtjv rtjS dtjv r tjS dS )zDetermines the current operating system.

    Returns:
      OperatingSystemTuple, One of the OperatingSystem constants or None if it
      cannot be determined.
    ntr   r   r   N)	osr9   r4   rg   r   rh   r   r   r   r"   r"   r"   r#   Current   s   



zOperatingSystem.Currentc                   C   s   t  t ju S )z8Returns True if the current operating system is Windows.)r4   r   rg   r"   r"   r"   r#   	IsWindows   s   zOperatingSystem.IsWindowsNT)r&   r'   r(   r)   objectr~   rg   r   r   r   r   r   staticmethodr   r   r   r   r"   r"   r"   r#   r4   S   s"    E

r4   c                   @   s   e Zd ZdZG dd deZedddZedddZedddZedddZ	eeee	gZ
i d	eded
edededededededede	de	de	de	de	de	Zedd ZedddZedd ZdS ) Architecturez@An enum representing the system architecture you are running on.c                   @   sd   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	dd Z
dd Zdd Zdd Zdd ZdS )zArchitecture._ARCHzA single architecture.c                 C   r5   r6   r7   r;   r"   r"   r#   r,      r<   zArchitecture._ARCH.__init__c                 C   r=   r6   r>   r?   r"   r"   r#   r@      rA   zArchitecture._ARCH.__str__c                 C   rB   r6   rC   rF   r"   r"   r#   rH      rI   zArchitecture._ARCH.__eq__c                 C   rJ   r6   rK   r?   r"   r"   r#   rM      rN   zArchitecture._ARCH.__hash__c                 C   rO   r6   r"   rF   r"   r"   r#   rP      rQ   zArchitecture._ARCH.__ne__c                 C   rR   rS   r"   rT   r"   r"   r#   rX      rY   zArchitecture._ARCH._CmpHelperc                 C   rZ   r[   r\   rF   r"   r"   r#   r]      r^   zArchitecture._ARCH.__lt__c                 C   r_   r[   r\   rF   r"   r"   r#   r`     r^   zArchitecture._ARCH.__gt__c                 C   ra   r6   rb   rF   r"   r"   r#   rc     rd   zArchitecture._ARCH.__le__c                 C   ra   r6   re   rF   r"   r"   r#   rf   
  rd   zArchitecture._ARCH.__ge__N)r&   r'   r(   r)   r,   r@   rH   rM   rP   r|   rX   r]   r`   rc   rf   r"   r"   r"   r#   _ARCH   s    
r   x86x86_64PPCppcarmamd64zi686-64i386i686ia64powerpczpower macintoshppc64armv6armv6larm64armv7armv7laarch64c                   C   r   r   )r   r   r   r"   r"   r"   r#   r     r   zArchitecture.AllValuesTc                 C   r   )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   c                 S   r   r"   r>   r   r"   r"   r#   r   =  r   z'Architecture.FromId.<locals>.<listcomp>)r   r   r8   r*   )architecture_idr   archr"   r"   r#   r   &  r   zArchitecture.FromIdc                   C   s   t jt  S )zDetermines the current system architecture.

    Returns:
      ArchitectureTuple, One of the Architecture constants or None if it cannot
      be determined.
    )r   _MACHINE_TO_ARCHITECTUREgetrh   machinelowerr"   r"   r"   r#   r   @  s   zArchitecture.CurrentNr   )r&   r'   r(   r)   r   r   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   r      s^    -
r   c                   @   sJ   e Zd ZdZdd Zdd ZedddZd	d
 Zdd Z	edd Z
dS )Platformz+Holds an operating system and architecture.c                 C   s   || _ || _dS )zConstructs a new platform.

    Args:
      operating_system: OperatingSystem, The OS
      architecture: Architecture, The machine architecture.
    N)r   architecture)r-   r   r   r"   r"   r#   r,   N  s   
zPlatform.__init__c                 C   s   d | j| jS )Nz{}-{})r   r   r   r?   r"   r"   r#   r@   X  s   zPlatform.__str__Nc                 C   s$   t | r| nt |r|S t S )ah  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   r4   r   r   )os_overridearch_overrider"   r"   r#   r   [  s   zPlatform.Currentc                 C   s   | j tjkrdj| j j| j jdS | j tjkr"dj| j j| j jdS | j tjkr?d}| jt	j
kr4| jjnd}|j|| j jdS dS )a  Generates 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}))r9   ri   z({name} NT {version})z&(Macintosh; {name} Mac OS X {version})Intelz())r   r4   r   r   r9   ri   rg   r   r   r   r   )r-   format_stringarch_stringr"   r"   r#   UserAgentFragmentk  s*   zPlatform.UserAgentFragmentc                 C   s   i }| j tjkrd|d< d}d}||B |d< |S tjd dkr+tjd dkr+d|d	< ntj|d
< d|d< tj|d< tj|d< tj|d< |S )a{  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   r4   rg   r   version_infor   setsid
subprocessPIPE)r-   argsdetached_processcreate_new_process_groupr"   r"   r#   AsyncPopenArgs  s   




zPlatform.AsyncPopenArgsc                  C   sP   g d} zt j| t jt jd}| \}}dt|v rW dS W dS    Y dS )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
stdoutdatarp   r"   r"   r#   IsActuallyM1ArmArchitecture  s   
z$Platform.IsActuallyM1ArmArchitecture)NN)r&   r'   r(   r)   r,   r@   r   r   r   r   r   r"   r"   r"   r#   r   K  s    
''r   c                   @   sj   e Zd ZdZdZdZeej	drdndZ
dZdZdZd	ZdddZdd Zdd Zdd ZdddZd
S )PythonVersiona
  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                 C   s6   |r|| _ d S ttdrtjd d | _ d S d | _ d S )Nr   r   )ri   hasattrr   r   )r-   ri   r"   r"   r#   r,     s
   


zPythonVersion.__init__c                 C   s   t  t jkr	dS dS )Nr   z~
To reinstall gcloud, run:
    $ gcloud components reinstall

This will also prompt to install a compatible version of Python.)r4   r   r   r?   r"   r"   r#   InstallMacPythonMessage  s   z%PythonVersion.InstallMacPythonMessagec                 C      d tjd tjd S Nz)Please use Python version {0}.{1} and up.r   r   )r   r   MIN_SUPPORTED_PY3_VERSIONr?   r"   r"   r#   SupportedVersionMessage      z%PythonVersion.SupportedVersionMessagec                 C   r   r   )r   r   "UPCOMING_PY3_MIN_SUPPORTED_VERSIONr?   r"   r"   r#   UpcomingSupportedVersionMessage  r   z-PythonVersion.UpcomingSupportedVersionMessageFc              
   C   s  d}t tjdd dk}d}| jsd|  |  }n,| jd dk r2d	|  |  }d
}n| jt	j
k rJd| jd | jd |  |  }|r_|r_|r_tj| tjt	j d
S |rv|rgt|tj| tjt	j dS | j| jk rtjd| jd |  |   tjdt	j  d
S t	jr| jt	jk rtjdt	jd t	jd t	j|  |   tjdt	j  d
S )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   ri   r   r   r   r   MIN_REQUIRED_PY3_VERSIONr   r   r   ENV_VAR_MESSAGEr%   r   r   UPCOMING_SUNSET_PY3_VERSIONUPCOMING_PY3_DEPRECATION_DATEr   )r-   raise_exceptionerror	allow_py2	py2_errorr"   r"   r#   IsCompatible  s~   
zPythonVersion.IsCompatibler6   )F)r&   r'   r(   r)   r   r   r   r   r   r   MAX_SUPPORTED_PY3_VERSIONr   r   r   r   r,   r   r   r   r   r"   r"   r"   r#   r     s&    

r   )r)   
__future__r   r   r   r   rh   r   r   r   googlecloudsdk.core.utilr   r   r$   	Exceptionr%   r*   r   r4   r   r   r   r"   r"   r"   r#   <module>   s(    n 	