U
    M[^H                     @   s~  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Zd dlZd dl	m
Z
mZ d dlmZ d dlZd dlmZ d dlmZ d dlmZ z,d dlmZmZmZmZmZmZmZmZ W n ek
r   Y nX d	Zd
Z e! Z"dZ#G dd dej$Z%G dd de&Z'G dd de&Z(G dd dej)Z*G dd dej+Z,dddddZ-e.ddddZ/edd Z0dd  Z1dWdde.dd"d#d$Z2e.e.d%d&d'Z3d(d)d*d+Z4dXe.e5d-d.d/Z6e.e5dd0d1Z7e.e5d2d3d4Z8dYe.e5e.d6d7d8Z9dZd9d(d:d;d<Z:e5d)d=d>Z;di dfe.d?d(d9d@dAdBdCZ<d[dEdFe5dGdHdIdJdKZ=d\dEdFe5dGdLdHdMdNdOZ>e.d9dPdQdRZ?d]e.e.e@ddTdUdVZAdS )^    )ENOENTN)errorrequest)urlparse)contextmanagerwraps)HTTPMessage)AnyDictListMappingOptionalSequenceTupleUnionz/etc/machine-idz/var/lib/dbus/machine-idz0(?P<release>\d+\.\d+) (LTS )?\((?P<series>\w+).*c                   @   s.   e Zd ZejdejdiZejedddZ	dS )LogFormatterzERROR: %(message)szDEBUG: %(message)s)recordreturnc                 C   s    | j |jd}t||S )Nz%(message)s)FORMATSgetZlevelnologging	Formatterformat)selfr   Zlog_fmt r   //usr/lib/python3/dist-packages/uaclient/util.pyr   /   s    zLogFormatter.formatN)
__name__
__module____qualname__r   ZERRORDEBUGr   Z	LogRecordstrr   r   r   r   r   r   (   s     r   c                       s,   e Zd Zdejdddd fddZ  ZS )	UrlErrorNOptional[int]zOptional[Dict[str, str]]Optional[str])causecodeheadersurlc                    sR   t |dd rt|j}nt|}t | || _|| _| jd krHi | _|| _d S )Nreason)getattrr!   r)   super__init__r&   r'   r(   )r   r%   r&   r'   r(   Zcause_error	__class__r   r   r,   5   s    
zUrlError.__init__)NNN)r   r   r   r   ZURLErrorr,   __classcell__r   r   r-   r   r"   4   s      r"   c                       s,   e Zd Zdedeedd fddZ  ZS )ProcessExecutionErrorN r#   )cmd	exit_codestdoutstderrr   c                    s<   || _ || _|| _|sd}nd}t |j|||d d S )Nz"Invalid command specified '{cmd}'.zEFailed running command '{cmd}' [exit({exit_code})]. Message: {stderr})r2   r5   r3   )r4   r5   r3   r+   r,   r   )r   r2   r3   r4   r5   Zmessage_tmplr-   r   r   r,   I   s    zProcessExecutionError.__init__)Nr1   r1   )r   r   r   r!   r,   r/   r   r   r-   r   r0   H   s      r0   c                       s    e Zd ZdZ fddZ  ZS )DatetimeAwareJSONEncoderzBA json.JSONEncoder subclass that writes out isoformat'd datetimes.c                    s    t |tjr| S t |S N)
isinstancedatetimeZ	isoformatr+   default)r   or-   r   r   r:   b   s    z DatetimeAwareJSONEncoder.default)r   r   r   __doc__r:   r/   r   r   r-   r   r6   _   s   r6   c                       s,   e Zd ZdZ fddZedd Z  ZS )DatetimeAwareJSONDecodera,  
    A JSONDecoder that parses some ISO datetime strings to datetime objects.

    Important note: the "some" is because we seem to only be able extend
    Python's json library in a way that lets us convert string values within
    JSON objects (e.g. '{"lastModified": "2019-07-25T14:35:51"}'). Strings
    outside of JSON objects (e.g. '"2019-07-25T14:35:51"') will not be passed
    through our decoder.

    (N.B. This will override any object_hook specified using arguments to it,
    or used in load or loads calls that specify this as the cls.)
    c                    s.   d|kr| d t j|d| ji| d S )Nobject_hook)popr+   r,   r>   )r   argskwargsr-   r   r   r,   v   s    
z!DatetimeAwareJSONDecoder.__init__c              	   C   sT   |   D ]F\}}t|trztj|d}W n tk
rD   |}Y nX || |< q| S )Nz%Y-%m-%dT%H:%M:%S)itemsr8   r!   r9   strptime
ValueError)r;   keyvalue	new_valuer   r   r   r>   {   s    
 

z$DatetimeAwareJSONDecoder.object_hook)r   r   r   r<   r,   staticmethodr>   r/   r   r   r-   r   r=   h   s   r=   zDict[str, Any])orig_accessr   c                 C   s   t t| td| kgs$td| t d }| di }|di |i }| D ]8\}}| d |}t|tr|	| qV|| d |< qVdS )a  Apply series-specific overrides to an entitlement dict.

    This function mutates orig_access dict by applying any series-overrides to
    the top-level keys under 'entitlement'. The series-overrides are sparse
    and intended to supplement existing top-level dict values. So, sub-keys
    under the top-level directives, obligations and affordance sub-key values
    will be preserved if unspecified in series-overrides.

    To more clearly indicate that orig_access in memory has already had
    the overrides applied, the 'series' key is also removed from the
    orig_access dict.

    :param orig_access: Dict with original entitlement access details
    Zentitlementz?Expected entitlement access dict. Missing "entitlement" key: {}seriesN)
allr8   dictRuntimeErrorr   get_platform_infor   r?   rB   update)rI   Zseries_nameZorig_entitlementZ	overridesrE   rF   Zcurrentr   r   r   apply_series_overrides   s    

rP   )pathr   c              
   C   sD   zt |  W n0 tk
r> } z|jtkr.|W 5 d }~X Y nX d S r7   )osunlinkOSErrorerrnor   )rQ   er   r   r   del_file   s
    
rW   c               	   c   st   dd t  jD } | s,t d dV  dS | d }|j}|t jkrNdV  dS |d z
dV  W 5 || X dS )a  
    A context manager that disables logging to console in its body

    N.B. This _will not_ disable console logging if it finds the console
    handler is configured at DEBUG level; the assumption is that this means we
    want as much output as possible, even if it risks duplication.

    This context manager will allow us to gradually move away from using the
    logging framework for user-facing output, by applying it to parts of the
    codebase piece-wise. (Once the conversion is complete, we should have no
    further use for it and it can be removed.)

    (Note that the @contextmanager decorator also allows this function to be
    used as a decorator.)
    c                 S   s   g | ]}|j d kr|qS )Zconsole)name).0Zhandlerr   r   r   
<listcomp>   s   
z*disable_log_to_console.<locals>.<listcomp>z0disable_log_to_console: no console handler foundNr   i  )r   Z	getLoggerZhandlersdebuglevelr    ZsetLevel)Zpotential_handlersZconsole_handlerZ	old_levelr   r   r   disable_log_to_console   s     



r]   c                    s    fdd}|S )a  Decorator to retry on exception for retry_sleeps.

     @param retry_sleeps: List of sleep lengths to apply between
        retries. Specifying a list of [0.5, 1] tells subp to retry twice
        on failure; sleeping half a second before the first retry and 1 second
        before the second retry.
     @param exception: The exception class to catch and retry for the provided
        retry_sleeps. Any other exception types will not be caught by the
        decorator.
    c                    s   t   fdd}|S )Nc               
      sn     }z| |W S   k
rf } z4|s.|tt|d t| t|d W 5 d }~X Y qX qd S )N Retrying %d more times.r   )copyr   r[   r!   lentimesleepr?   )r@   rA   ZsleepsrV   )	exceptionfretry_sleepsr   r   	decorator   s    
 z)retry.<locals>.wrapper.<locals>.decoratorr   )rd   rf   rc   re   )rd   r   wrapper   s    zretry.<locals>.wrapperr   )rc   re   rh   r   rg   r   retry   s    ri   r1   )	orig_dictnew_dictrQ   r   c           	      C   s   i }|   D ]\}}||t}|s(|n
|d | }t|trp||krft||| |d}|rn|||< qt||< q||krtd|| |||< q|  D ]\}}|| kr|||< q|S )z<Return a dictionary of delta between orig_dict and new_dict..rQ   z'Contract value for '%s' changed to '%s')rB   r   DROPPED_KEYr8   rL   get_dict_deltasr   r[   )	rj   rk   rQ   ZdeltasrE   rF   rG   Zkey_pathZ	sub_deltar   r   r   ro      s2    
  

  

ro   )data_dirr   c                 C   s^   t j| d}tt|fD ]*}t j|rt|d}|r|  S qtt	
 }t|| |S )z=Get system's unique machine-id or create our own in data_dir.z
machine-id
)rR   rQ   joinETC_MACHINE_IDDBUS_MACHINE_IDexists	load_filerstripr!   uuidZuuid4
write_file)rp   Zfallback_machine_id_filerQ   contentZ
machine_idr   r   r   get_machine_id  s    

r{   zDict[str, str])r   c                  C   s   t  } | dddd}| d }d|kr8dj|d }tdd	|}||d
< tt|}|srtd| d ||	 }|
|d |d  d t }|j|d< tddg\}}| |d< |S )z
    Returns a dict of platform information.

    N.B. This dict is sent to the contract server, which requires the
    distribution, type and release keys.
    NAMEZUNKNOWNZLinux)ZdistributiontypeZVERSIONz, z{} ({})z\.\d LTSz LTSversionz<Could not parse /etc/os-release VERSION: {} (modified to {})releaserJ   )r   rJ   ZkernelZdpkgz--print-architectureZarch)parse_os_releaser   r   splitresubmatchREGEX_OS_RELEASE_VERSIONrM   	groupdictrO   lowerrR   unamer   subpstrip)Z
os_releaseZplatform_infor~   r   Z
match_dictr   outZ_errr   r   r   rN   +  s8    
 

rN   /run)run_pathr   c              	   C   s\   zt dddg W dS  ttfk
r,   Y nX dD ]$}tj| |}tj|r2 dS q2dS )z>Checks to see if this code running in a container of some sortzsystemd-detect-virtz--quietz--containerT)Zcontainer_typezsystemd/containerF)r   IOErrorrT   rR   rQ   rr   ru   )r   filenamerQ   r   r   r   is_containerV  s    r   c                 C   s   t j| ot | t jS r7   )rR   rQ   isfileaccessX_OKrm   r   r   r   is_exed  s    r   )r(   r   c                 C   s6   zt | }W n tk
r"   Y dS X |jdkr2dS dS )NF)ZhttpsZhttpT)r   rD   Zscheme)r(   Z
parsed_urlr   r   r   is_service_urli  s    
r   T)r   decoder   c              	   C   s4   t d|  t| d}| }W 5 Q R X |dS )z!Read filename and decode content.zReading file: %srbutf-8)r   r[   openreadr   )r   r   streamrz   r   r   r   rv   s  s    rv   r$   )release_filer   c                 C   sH   | sd} i }t |  D ]*}|dd\}}|r| d||< q|S )Nz/etc/os-release=   ")rv   
splitlinesr   r   )r   datalinerE   rF   r   r   r   r   {  s    r   c                  C   s    t d} |   dkrdS dS )z
    Display a confirmation prompt, returning a bool indicating the response

    This function will only prompt a single time, and defaults to "no" (i.e. it
    returns False).
    zAre you sure? (y/N) )yZyesTF)inputr   r   )rF   r   r   r   prompt_for_confirmation  s    r   zOptional[bytes]z1Tuple[Any, Union[HTTPMessage, Mapping[str, str]]])r(   r   r'   methodr   c                 C   s   |r|sd}t j| |||d}td|p*d| || t |}| d}dt|j	ddkrlt
|}td	|pxd| |j| ||jfS )
NZPOST)r   r'   r   z#URL [%s]: %s, headers: %s, data: %sZGETr   zapplication/jsonzContent-typer1   z,URL [%s] response: %s, headers: %s, data: %s)r   ZRequestr   r[   Zurlopenr   r   r!   r'   r   jsonloads)r(   r   r'   r   ZreqZresprz   r   r   r   readurl  s,    

r   FzSequence[str]zOptional[List[int]]zOptional[float]zTuple[str, str])r@   rcscapturetimeoutr   c                 C   s  dd | D }|dkrdg}z(t j|t jt jd}|j|d\}}W nb tk
r   z(td| |j|d|dd	W n$ t	k
r   td| d
Y nX Y nX |j|krtd| |j|d|dd	|rt
dd| |j| |d|dfS )a  Run a command and return a tuple of decoded stdout, stderr.

    @param args: A list of arguments to feed to subprocess.Popen
    @param rcs: A list of allowed return_codes. If returncode not in rcs
        raise a ProcessExecutionError.
    @param capture: Boolean set True to log the command and response.
    @param timeout: Optional float indicating number of seconds to wait for
        subp to return.

    @return: Tuple of utf-8 decoded stdout, stderr
    @raises ProcessExecutionError on invalid command or returncode not in rcs.
    @raises subprocess.TimeoutError when timeout specified and the command
        exceeds that number of seconds.
    c                 S   s$   g | ]}t |tr|n|d qS )r   )r8   bytesencode)rY   xr   r   r   rZ     s    z_subp.<locals>.<listcomp>Nr   )r4   r5   )r    r   )r2   r3   r4   r5   )r2   zRan cmd: %s, rc: %s stderr: %s)
subprocessPopenPIPEZcommunicaterT   r0   rr   
returncoder   UnboundLocalErrorr   r[   )r@   r   r   r   Z
bytes_argsprocr   errr   r   r   _subp  sJ      

r   zOptional[List[float]])r@   r   r   r   re   r   c              
   C   s   |dk	r|  nd}zt| |||\}}W qW q tk
r } zD|rRtt| |sX tt|d t| t|	d W 5 d}~X Y qX q||fS )a  Run a command and return a tuple of decoded stdout, stderr.

     @param subp: A list of arguments to feed to subprocess.Popen
     @param rcs: A list of allowed return_codes. If returncode not in rcs
         raise a ProcessExecutionError.
     @param capture: Boolean set True to log the command and response.
     @param timeout: Optional float indicating number of seconds to wait for a
         subp call to return.
     @param retry_sleeps: Optional list of sleep lengths to apply between
        retries. Specifying a list of [0.5, 1] instructs subp to retry twice
        on failure; sleeping half a second before the first retry and 1 second
        before the next retry.

    @return: Tuple of utf-8 decoded stdout, stderr
    @raises ProcessExecutionError on invalid command or returncode not in rcs.
    @raises subprocess.TimeoutError when timeout specified and the command
        exceeds that number of seconds.
    Nr^   r   )
r_   r   r0   r   r[   r!   r`   ra   rb   r?   )r@   r   r   r   re   r   r   rV   r   r   r   r     s    
 $r   )programr   c                 C   sr   t jj| krt| r| S dd t jddt jD }dd |D }|D ]"}t j|| }t|rJ|  S qJdS )z;Find whether the provided program is executable in our PATHc                 S   s   g | ]}| d qS )r   )r   rY   pr   r   r   rZ     s    zwhich.<locals>.<listcomp>PATHr1   c                 S   s   g | ]}t j|qS r   )rR   rQ   abspathr   r   r   r   rZ     s     N)	rR   rQ   sepr   environr   r   pathseprr   )r   pathsZnormalized_pathsrQ   Zprogram_pathr   r   r   which  s    
r     )r   rz   moder   c              	   C   sJ   t d|  t| d}||d |  W 5 Q R X t| | dS )a:  Write content to the provided filename encoding it if necessary.

    @param filename: The full path of the file to write.
    @param content: The content to write to the file.
    @param mode: The filesystem mode to set on the file.
    @param omode: The open mode used when opening the file (w, wb, a, etc.)
    zWriting file: %swbr   N)r   r[   r   writer   flushrR   chmod)r   rz   r   Zfhr   r   r   ry   %  s
    ry   )r1   )r   )T)N)NFN)NFNN)r   )BrU   r   r9   r   r   rR   r   r   ra   Zurllibr   r   Zurllib.parser   rx   
contextlibr   	functoolsr   Zhttp.clientr	   typingr
   r   r   r   r   r   r   r   ImportErrorrs   rt   objectrn   r   r   r   r   r"   r0   ZJSONEncoderr6   ZJSONDecoderr=   rP   r!   rW   r]   ri   ro   r{   rN   boolr   r   r   rv   r   r   r   r   r   r   intry   r   r   r   r   <module>   s   ,
	""
)!   +
    ;    *