o
    S¯Kj‹
  ã                   @   sž   d Z ddlZddlmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZmZ ddlmZ ddlmZ d	Zd
Zdefdd„Zdee fdd„Zdee fdd„ZdS )z"
Client version related functions
é    N)Úinf)ÚOptional)Úget_apt_cache_timeÚget_pkg_candidate_versionÚversion_compare)ÚCANDIDATE_CACHE_PATHÚUAC_RUN_PATH)ÚProcessExecutionError)Úsubpz37.2z37.2ubuntu~22.04.1Úreturnc                  C   sp   t  d¡st S tj tj t¡¡} tj tj | d¡¡r6g d¢}zt|ƒ\}}| 	¡ W S  t
y5   Y tS w tS )aO  Return the packaged version as a string

    Prefer the binary PACKAGED_VESION set by debian/rules to DEB_VERSION.
    If unavailable, check for a .git development environments:
      a. If run in our upstream repo `git describe` will gives a leading
         XX.Y so return the --long version to allow daily build recipes
         to count commit offset from upstream's XX.Y signed tag.
      b. If run in a git-ubuntu pkg repo, upstream tags aren't visible,
         believe __VERSION__ is correct - there is and MUST always be a
         test to make sure it matches debian/changelog
    z@@PACKAGED_VERSIONz.git)ÚgitÚdescribez
--abbrev=8z--match=[0-9]*z--long)ÚPACKAGED_VERSIONÚ
startswithÚosÚpathÚdirnameÚ__file__ÚexistsÚjoinr
   Ústripr	   Ú__VERSION__)ÚtopdirÚcmdÚoutÚ_© r   ú2/usr/lib/python3/dist-packages/uaclient/version.pyÚget_version   s   

þr   c                  C   sþ   t ƒ pt} tj t¡rt t¡j| k rUd }z.tdƒ}|rBtj	t
dd� ttdƒ�}| |¡ W d   ƒ |W S 1 s:w   Y  |W S W n tyT   |d urR| Y S Y nw zttdƒ�}| ¡  ¡ W  d   ƒ W S 1 smw   Y  W d S  ty~   Y d S w )Nzubuntu-pro-clientT)Úexist_okÚwÚr)r   r   r   r   r   r   ÚstatÚst_mtimer   Úmakedirsr   ÚopenÚwriteÚ	ExceptionÚreadr   )Úlast_apt_cache_updateÚcandidate_versionÚfr   r   r   Úget_last_known_candidate/   s<   

ÿ
ÿþüÿÿ
$ÿýýr,   c                  C   s"   t ƒ } | rt| tƒ ƒdkr| S d S )Nr   )r,   r   r   )Ú	candidater   r   r   Úcheck_for_new_versionL   s   r.   )Ú__doc__Úos.pathr   Úmathr   Útypingr   Úuaclient.aptr   r   r   Úuaclient.defaultsr   r   Úuaclient.exceptionsr	   Úuaclient.systemr
   r   r   Ústrr   r,   r.   r   r   r   r   Ú<module>   s    