o
    ïØ{fmH  ã                   @   sB  d dl m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
 d dlZd dlZe ¡  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zd dlmZmZmZmZmZ d dlmZ W n ey}   d dlmZmZmZmZmZ d dlmZ Y nw d d	l m Z  G d
d„ de!ƒZ"G dd„ dƒZ#dd„ Z$dd„ Z%dMdd„Z&dd„ Z'dNdd„Z(dd„ Z)dd„ Z*dd„ Z+d d!„ Z,d"d#„ Z-d$d%„ Z.G d&d'„ d'eƒZ/dMd(d)„Z0dMd*d+„Z1d,d-„ Z2d.d/„ Z3d0d1„ Z4d2d3„ Z5dMd4d5„Z6d6d7„ Z7dMd8d9„Z8dMd:d;„Z9d<d=„ Z:d>d?„ Z;d@dA„ Z<dBdC„ Z=dDdE„ Z>dFdG„ Z?dHdI„ Z@eAdJk�re:ddKdLƒ dS dS )Oé    )Úprint_function)Úgettext)Úngettext)ÚS_IMODEÚST_MODEÚS_IXUSR)ÚceilN)ÚProxyHandlerÚRequestÚbuild_openerÚinstall_openerÚurlopen)Úurlsplit)Úcopyc                   @   s*   e Zd ZdZd
dd„Zdd„ Zdd„ Zd	S )ÚExecutionTimezº
    Helper that can be used in with statements to have a simple
    measure of the timing of a particular block of code, e.g.
    with ExecutionTime("db flush"):
        db.flush()
    Ú c                 C   s
   || _ d S ©N)Úinfo)Úselfr   © r   ú:/usr/lib/python3/dist-packages/UpdateManager/Core/utils.pyÚ__init__G   s   
zExecutionTime.__init__c                 C   s   t   ¡ | _d S r   )ÚtimeÚnow©r   r   r   r   Ú	__enter__J   s   zExecutionTime.__enter__c                 C   s    t d| jt ¡ | j f ƒ d S )Nz%s: %s)Úprintr   r   r   )r   ÚtypeÚvalueÚstackr   r   r   Ú__exit__M   s    zExecutionTime.__exit__N)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r    r   r   r   r   r   @   s
    
r   c                   @   s(   e Zd ZdZdZdZdZdZdZdZ	dS )	ÚSoftwarePropertiesPager   é   é   é   é   é   é   N)
r!   r"   r#   Úubuntu_softwareÚother_softwareÚupdatesÚauthenticationÚadditional_driversÚdeveloper_optionsÚ
ubuntu_pror   r   r   r   r%   Q   s    r%   c                 C   sJ   t | ƒ}t|jƒ}|jr|j |jd¡|_|jr!|j |jd¡|_t|ƒS )Nzhidden-uzhidden-p)r   r   ÚuriÚusernameÚreplaceÚpasswordÚstr)ÚentryÚtmpÚ	url_partsr   r   r   Ú)get_string_with_no_auth_from_source_entry[   s   
r;   c                  C   sD   d} zddl }| ¡ }| d¡} W | S  ty!   t d¡ Y | S w )z+ return True if Unity is currently running Fr   Nzcom.canonical.Unityz&could not check for Unity dbus service)ÚdbusÚ
SessionBusÚname_has_ownerÚ	ExceptionÚloggingÚ	exception)Úunity_runningr<   Úbusr   r   r   Úis_unity_runninge   s   þþrD   c                 C   s–   |st  ¡ }|dkrId| }t|ƒ�}| ¡ }W d   ƒ n1 s!w   Y  | d¡d  d¡d }|| kr8dS t| d¡d  ¡ d ƒ}|dks
dS )	Nr   z/proc/%s/statú(r'   ú)Tr&   F)ÚosÚgetpidÚopenÚreadÚ	partitionÚ
rpartitionÚintÚsplit)ÚprocessnameÚpidÚ	stat_fileÚstat_fÚstatÚcommandr   r   r   Úis_child_of_process_nameq   s   

ÿ÷
rU   c                   C   s0   t j d¡rt j d¡sdS t  d¡t  d¡kS )z, returns True if we are inside a chroot
    z/procz/proc/1Tú/z/proc/1/root)rG   ÚpathÚexistsrS   r   r   r   r   Úinside_chroot�   s   rY   éF   r   c                 C   sL   d}|   ¡ D ]}t|ƒ| d¡ t|ƒ |kr|d| 7 }||d 7 }q|S )z¤ helpers inspired after textwrap - unfortunately
        we can not use textwrap directly because it break
        packagenames with "-" in them into new lines
    r   Ú
ú )rN   ÚlenÚrfind)ÚtÚwidthÚsubsequent_indentÚoutÚsr   r   r   Úwrap‹   s   rd   c                 K   s8   d}|   d¡}|D ]}t|fi |¤Ž} || d 7 }q	|S )Nr   r[   )rN   rd   )rc   ÚkwargsÚmsgÚparasÚparr   r   r   Útwrap˜   s   
ri   c                  C   sd   g } t j d¡s
g S tjdgtjdd�}| ¡ d  d¡}|d= |D ]}|r/|  | ¡ d ¡ q"| S )z= return list of loaded modules (or [] if lsmod is not found) z/sbin/lsmodT©ÚstdoutÚuniversal_newlinesr   r[   )	rG   rW   rX   Ú
subprocessÚPopenÚPIPEÚcommunicaterN   Úappend)ÚmodulesÚpÚlinesÚliner   r   r   Úlsmod¡   s   ÿ€rv   c                 C   sF   t j | ¡sdS tt  | ¡t ƒ}|t@ tks!t  | |tB ¡ dS dS )zB check if a given binary has the executable bit and if not, add itN)rG   rW   rX   r   rS   r   r   Úchmod)rW   Úmoder   r   r   Úcheck_and_fix_xbit³   s   ÿry   c                  C   s’   ddi} dt jvrdS t jd  ¡ }|dd… | v r"| |dd…  S d|v rA| d¡d  d¡d	 }d
|v r=| d
¡d }|d S |dd… d S )z: helper to get the country mirror from the current locale Úcr   ÚLANGNr*   Ú_Ú.r   r&   ú@r'   )rG   ÚenvironÚlowerrN   )Úlang_mirrorÚlangÚcountryr   r   r   Úcountry_mirror¼   s   
r„   c                  C   ó„   t j d¡} | rt d|  ¡ | S ddlm}m} |g d¢|dd�}| ¡ }|dkr4t	j
 d| ¡ d	S |j ¡  ¡ } |j ¡  | S )
z2 return the codename of the current runing distro ÚMETA_RELEASE_FAKE_CODENAMEzQusing fake release name '%s' (because of META_RELEASE_FAKE_CODENAME environment) r   ©rn   ro   )Úlsb_releasez-cú-sTrj   ú"lsb_release returned exitcode: %i
úunknown distribution©rG   r   Úgetr@   Úwarningrm   rn   ro   ÚwaitÚsysÚstderrÚwriterk   ÚreadlineÚstripÚclose)Údistrn   ro   rs   Úresr   r   r   Úget_distÒ   ó"   ÿ
ÿ
r˜   c                  C   r…   )
z2 return the version of the current running distro ÚMETA_RELEASE_FAKE_VERSIONzSusing fake release version '%s' (because of META_RELEASE_FAKE_VERSION environment) r   r‡   )rˆ   z-rr‰   Trj   rŠ   r‹   rŒ   )Údescrn   ro   rs   r—   r   r   r   Úget_dist_versionç   r™   rœ   c                   @   s   e Zd Zdd„ ZdS )ÚHeadRequestc                 C   ó   dS )NÚHEADr   r   r   r   r   Ú
get_methodý   s   zHeadRequest.get_methodN)r!   r"   r#   r    r   r   r   r   r�   ü   s    r�   c              
   C   sT  |sdd„  |d|  ƒ t | ƒ\}}}}}|d|||||f ƒ |dv rSztt| ƒƒ}| ¡  |jdkr7W dS W dS  tyR } z|d	| ƒ W Y d
}~dS d
}~ww |dkr¨dd
l}	z1|	 |¡}
|
 ¡  |
 	t
j |¡¡ |
 t
j |¡¡}|
 ¡  |rƒ|d| ƒ |dkrŠW dS W dS  ty§ } z|rœ|d| ƒ W Y d
}~dS d
}~ww dS )zÒ
    helper that checks if the given uri exists and is downloadable
    (supports optional debug_func function handler to support
     e.g. logging)

    Supports http (via HEAD) and ftp (via size request)
    c                 S   rž   )NTr   )Úxr   r   r   Ú<lambda>
  s    z"url_downloadable.<locals>.<lambda>zurl_downloadable: %sz"s='%s' n='%s' p='%s' q='%s' f='%s')ÚhttpÚhttpséÈ   TFzerror from httplib: '%s'NÚftpr   zftplib.size() returned: %szerror from ftplib: '%s')r   r   r�   r•   Úcoder?   ÚftplibÚFTPÚloginÚcwdrG   rW   ÚdirnameÚsizeÚbasenameÚquit)r3   Ú
debug_funcÚschemeÚnetlocrW   ÚquerryÚfragmentÚ	http_fileÚer¨   Úfr­   r   r   r   Úurl_downloadable  sP   
ÿ
€þ
ÿü€ýr¸   c           
      C   sB  d}i }t j d¡dkrt j d¡|d< n0tj |¡rEt  ¡ }t  ||¡ | dd¡}|rE| d¡}t	| 
d¡ƒ}|rE|rEd	||f |d< t j d
¡dkrVt j d
¡|d< n
d|v r`|d |d< |rŸ| ¡ D ]}t d|¡sztd| tjd�  dS qft|ƒ}t|ƒ}	t|	ƒ d|v r“t d|d ¡ d|v rŸt d|d ¡ |S )zË init proxy settings

    * use apt.conf http proxy if present,
    * otherwise look into synaptics config file,
    * otherwise the default behavior will use http_proxy environment
      if present
    z/root/.synaptic/synaptic.confzAcquire::http::Proxyr   r£   zSynaptic::useProxyFzSynaptic::httpProxyzSynaptic::httpProxyPortzhttp://%s:%s/zAcquire::https::Proxyr¤   zhttps?://\w+zproxy '%s' looks invalid)ÚfileNÚ
http_proxyÚhttps_proxy)Úapt_pkgÚconfigÚfindrG   rW   rX   ÚConfigurationÚread_config_fileÚfind_br7   Úfind_iÚvaluesÚreÚmatchr   r�   r‘   r	   r   r   Úputenv)
Ú	gsettingsÚSYNAPTIC_CONF_FILEÚproxiesÚcnfÚ	use_proxyÚ
proxy_hostÚ
proxy_portÚproxyÚproxy_supportÚopenerr   r   r   Ú
init_proxy,  s>   
þrÑ   c               
   C   s´   zOddl } |  | jj¡}z| dd¡}|  |d¡}| dd¡W W S  | jjyO } z!d}|j|kr2‚ | dd	¡}|  |d¡}| dd
¡W  Y d}~W S d}~ww  t	yY   Y dS w )z­
    Check via dbus if the system is running on battery.
    This function is using UPower per default, if UPower is not
    available it falls-back to DeviceKit.Power.
    r   Nzorg.freedesktop.UPowerz/org/freedesktop/UPowerzorg.freedesktop.DBus.PropertiesÚ	OnBatteryz)org.freedesktop.DBus.Error.ServiceUnknownzorg.freedesktop.DeviceKit.Powerz /org/freedesktop/DeviceKit/PowerÚ
on_batteryF)
r<   ÚBusÚTYPE_SYSTEMÚ
get_objectÚ	InterfaceÚGetÚ
exceptionsÚDBusExceptionÚ_dbus_error_namer?   )r<   rC   ÚdevobjÚdevr¶   Úerror_unknownr   r   r   rÓ   X  s.   ÿ
ÿ€ùýrÓ   c                  C   sz   z2ddl m} m} |  | jj¡}| dddd| dd¡d	dd
d	d	¡
\}}| j| 	¡ |d  d�}|W S  t
y<   Y dS w )z‚
    Send a dbus signal to logind to not suspend the system, it will be
    released when the return value drops out of scope
    r   )ÚGioÚGLibzorg.freedesktop.login1z/org/freedesktop/login1zorg.freedesktop.login1.ManagerÚInhibitz(ssss))zshutdown:sleepÚUpdateManagerzUpdating SystemÚblockNéÿÿÿÿ)ÚfdF)Úgi.repositoryrß   rà   Úbus_get_syncÚBusTypeÚSYSTEMÚcall_with_unix_fd_list_syncÚVariantÚUnixInputStreamÚ	steal_fdsr?   )rß   rà   Ú
connectionÚvarÚfdlistÚ	inhibitorr   r   r   Úinhibit_sleept  s    ÿ
ùþrò   c                 C   s   | dks
|   ¡ dkrdS dS )NÚ0ÚFALSEFT)Úupper)r7   r   r   r   Ústr_to_bool�  s   rö   c                  C   s:   dd l } z	t ¡ \}}|W S  ty   |  d¡ Y d S w )Nr   zgedefaultlocale() failed)r@   ÚlocaleÚgetdefaultlocaler?   rA   )r@   Úlocale_sÚencodingr   r   r   Úget_lang“  s   
þrû   c                 C   s   t | d�}| dd¡d S )z6 try to guess the flavor based on the running desktop ©Úcacheú-r&   r   )Úget_ubuntu_flavor_packagerN   )rý   Úpkgr   r   r   Úget_ubuntu_flavor�  s   
r  c                  C   s    ddl m}  | dƒ}| dd¡S )Nr   )ÚDistUpgradeConfigz"/usr/share/ubuntu-release-upgraderÚDistroÚMetaPkgs)Ú#DistUpgrade.DistUpgradeConfigParserr  Úgetlist)r  Úparserr   r   r   Ú_load_meta_pkg_list¦  s   r  c              
   C   sŽ   dg}z
|  ttƒ ƒ¡ W n ty$ } ztd|ƒ W Y d}~nd}~ww | du r-t ¡ } |D ]}|| v r9| | nd}|rD|jrD|  S q/dS )zB try to guess the flavor metapackage based on the running desktop zubuntu-desktopz%Could not load list of meta packages:N)ÚextendÚsortedr  r?   r   ÚaptÚCacheÚis_installed)rý   Ú	meta_pkgsr¶   Úmeta_pkgÚ	cache_pkgr   r   r   rÿ   ®  s   €ÿ
€rÿ   c                 C   sb   t | d�}ddi}||v r|| S | d¡r t| dd¡d ƒS | d¡r/t| dd¡d ƒS dS )	z; try to guess the flavor name based on the running desktop rü   zubuntustudio-desktopzUbuntu Studioz-desktopr&   r   z-netbookÚUbuntu)rÿ   ÚendswithÚcapitalize_first_wordÚrsplit)rý   r   Úlookupr   r   r   Úget_ubuntu_flavor_nameÃ  s   


r  c                 C   sŒ   dd l }| dd¡ ddlm}m} |j| |jj|jj	|j
jd�}| d||f ¡ | ¡  | ¡  |jj¡ | d¡ | ¡  | ¡  dS )	Nr   ÚGtkz3.0)r  ÚGdk)ÚparentÚflagsr   Úbuttonsz<big><b>%s</b></big>

%sr   F)ÚgiÚrequire_versionræ   r  r  ÚMessageDialogÚDialogFlagsÚMODALÚMessageTypeÚERRORÚButtonsTypeÚCLOSEÚ
set_markupÚrealizeÚ
get_windowÚset_functionsÚ
WMFunctionÚMOVEÚ	set_titleÚrunÚdestroy)r  ÚsummaryÚmessager  r  r  Údr   r   r   ÚerrorÒ  s   ý
r1  c                 C   sH   | dk rt t| tdƒ ƒƒ}tdd|ƒd|i S t tdƒ| d d ¡S )zG
    Convert a given size in bytes to a nicer better readable unit
    i@B iè  z%(size).0f kBr­   z%.1f MBg     @�@)rM   r   Úfloatr   r÷   Úformat_stringr|   )ÚbytesÚ
size_in_kbr   r   r   Úhumanize_sizeã  s   
ÿr6  c                   C   s   t j d¡S )NzAPT::Architecture)r¼   r½   r¾   r   r   r   r   Úget_archô  s   r7  c           
      C   s¦   d}d}d}t dƒ�?}| ¡ D ]1}| ¡ }|sq| ¡ }|| }||kr%q||  d¡d }t|dƒ}	|	| kr@ W d  ƒ dS qW d  ƒ d	S 1 sLw   Y  d	S )
z@ check if the current system is listening on the given tcp port r&   r(   Ú0Az/proc/net/tcpú:é   NTF)rI   Ú	readlinesr”   rN   rM   )
ÚportÚINDEX_LOCAL_ADDRÚINDEX_STATEÚSTATE_LISTENINGÚnet_tcpru   rÃ   ÚstateÚlocal_port_strÚ
local_portr   r   r   Úis_port_already_listeningø  s.   

ñó
ÿðrD  c                  C   sV   d} t  ¡ dkrtdƒ‚t j d¡sdS tjddgtjdd	� ¡ d }|| kr)dS dS )
z# Return True if iptables is active zøChain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
r   z%Need root to check the iptables statez/sbin/iptablesFÚiptablesz-nLTrj   )	rG   ÚgetuidÚOSErrorrW   rX   rm   rn   ro   rp   )Úiptables_emptyrb   r   r   r   Úiptables_active  s   	
þþrI  c                 C   s@   t | ƒdkr| d  ¡ r| d  ¡ s| d  ¡ | dd…  S | S )z3 this uppercases the first word's first letter
    r&   r   N)r]   ÚisalphaÚisupperÚ
capitalize)Ústringr   r   r   r  ,  s   $r  c                 C   s   t | jddƒ}t|ƒS )zX this takes a package synopsis and uppercases the first word's
        first letter
    r.  r   )ÚgetattrÚ	candidater  )r   Únamer   r   r   Úget_package_label4  s   rQ  Ú__main__ÚbarÚbazr   )rZ   r   )BÚ
__future__r   r   r|   r   rS   r   r   r   Úmathr   r  r¼   Úinit_configr÷   r@   rÄ   rG   rm   r�   r   Úurllib.requestr	   r
   r   r   r   Úurllib.parser   ÚImportErrorÚurllib2Úurlparser   Úobjectr   r%   r;   rD   rU   rY   rd   ri   rv   ry   r„   r˜   rœ   r�   r¸   rÑ   rÓ   rò   rö   rû   r  r  rÿ   r  r1  r6  r7  rD  rI  r  rQ  r!   r   r   r   r   Ú<module>   sr   ø





		

+,

	


û