3
N9V1;                 @   sx   d dl Z d dlZd dlZd dlZejjjdd d dlZd dlZd dl	Z	d dl
Z
d dlZd dlT G dd dejjZdS )    NT)Zset_as_default)*c               @   s  e Zd ZdZdZdDZdZdZd	Zd
Z	dEddZ
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zejjed d d!d"d#dFd$d%Zd&d' Zd(d) Zd*d+ Zd,d- Zejjed.d/d!d"d#dGd0d1Zd2d3 Zd4d5 Zejjed d/d!d"d#dHd6d7Zejjed8d9d!d"d#dId:d;Z ejjed9d/d!d"d#dJd<d=Z!ejjed8d/d!d"d#dKd>d?Z"d@dA Z#ejjed8d/d!d"d#dLdBdCZ$dS )MServiceBackendzc 
    the main backend class that supports various system settings like
    proxy and keyboard
    zcom.ubuntu.SystemServicehttpftphttpssocksz/var/lib/dpkg/lockz/var/cache/apt/archives/lockz/var/lib/apt/lists/lockz!/var/run/unattended-upgrades.lockNc             C   s@   |d krt j }t jj| j|d}t jjj| |d tj  d S )N)bus/)	dbusZ	SystemBusserviceZBusNameDBUS_INTERFACE_NAMEObject__init__apt_pkginit_config)selfr   Zbus_name r   =/usr/lib/python3/dist-packages/UbuntuSystemService/backend.pyr      s    
zServiceBackend.__init__c             C   sT   t jjdsdS x>tdD ]2}|jd| r|j jd\}}|jd}|S qW dS )z2 internal that returns the /etc/environment proxy z/etc/environment z	%s_proxy==")ospathexistsopen
startswithstripsplit)r   
proxy_typelinekeyvaluer   r   r   _etc_environment_proxy'   s    
z%ServiceBackend._etc_environment_proxyc             C   s   | j d}| jd}|S )z5 internal helper that returns the current http proxy r   )
_apt_proxyr"   )r   	apt_proxy	env_proxyr   r   r   _http_proxy2   s    

zServiceBackend._http_proxyc             C   s   t j  t jjd| }|S )z6 internal helper that returns the configured apt proxyzAcquire::%s::proxy)r   r   configfind)r   r   proxyr   r   r   r#   9   s    zServiceBackend._apt_proxyc             C   s   | j d}| jd}|S )Nr   )r#   r"   )r   r$   r%   r   r   r   
_ftp_proxy?   s    

zServiceBackend._ftp_proxyc             C   s   | j d}|S )Nr   )r"   )r   r%   r   r   r   _socks_proxyE   s    
zServiceBackend._socks_proxyc             C   s   t j  t jjd}|S )z6 internal helper that returns the configured apt proxyzAcquire::ftp::proxy)r   r   r'   r(   )r   Z
http_proxyr   r   r   _ftp_apt_proxyI   s    zServiceBackend._ftp_apt_proxyc             C   s   | j d}|S )z6 internal helper that returns the current https proxy r   )r"   )r   r%   r   r   r   _https_proxyO   s    
zServiceBackend._https_proxyc             C   s
   t ||S )z8 internal helper, verify that the proxy string is valid )Zverify_proxy)r   r   r)   r   r   r   _verify_proxyT   s    zServiceBackend._verify_proxyc             C   s   t |S )z; internal helper, verify that the no_proxy string is valid )Zverify_no_proxy)r   r)   r   r   r   _verify_no_proxyX   s    zServiceBackend._verify_no_proxyssenderconn)Zin_signatureZout_signatureZsender_keywordZconnection_keywordc             C   sP   |dkr| j  S |dkr | j S |dkr0| j S |dkr@| j S td| dS )z 
        Get the current system-wide proxy  for type "proxy_type"

        This function will look in the apt configuration to 
        find the current http proxy.
        r   r   r   r   z'proxy_type '%s' is unknown in get_proxyN)r&   r-   r*   r+   UnknownProxyTypeError)r   r   r1   r2   r   r   r   	get_proxy\   s    zServiceBackend.get_proxyc       
         s  t jjd | j||sdS  fddtjtjj dD }|jdtjj d d}x|D ]}g }d}yPt	|}xB|D ]:}	|	j
 jd| r|rqzd	}d
||f }	d	}|j|	 qzW W n tk
r   Y nX | r| r|jd
||f  d	}t	|djdj| q^W d	S )z& helper that writes the new apt proxy zDir::EtcFc                s   g | ]}t jj d |qS )z
apt.conf.d)r   r   join).0n)confdirr   r   
<listcomp>z   s    z3ServiceBackend._write_apt_proxy.<locals>.<listcomp>z
apt.conf.dr   zapt.confzacquire::%s::proxy TzAcquire::%s::proxy "%s";
wr   )r   r'   find_dirr.   r   listdirr   r5   insertr   lowerr   append	Exceptionwrite)
r   r   	new_proxyapt_conffilesZalready_savedfnew_contentfoundfiler   r   )r8   r   _write_apt_proxys   s6    


zServiceBackend._write_apt_proxyc             C   s   | j ||sdS d}g }d||f }x0tdD ]$}|jd| rH|}d}|j| q.W |rrtddjdj| ntddj| dS )	NFz%s_proxy="%s"
z/etc/environmentz	%s_proxy=Tr:   r   a)r.   r   r   r?   rA   r5   )r   r   rB   rF   rE   new_proxy_liner   r   r   r   _write_etc_environment_proxy   s    z+ServiceBackend._write_etc_environment_proxyc             C   sV   d}g }x.t dD ]"}|jd| r*d}q|j| qW |rRt ddjdj| dS )NFz/etc/environmentz	%s_proxy=Tr:   r   )r   r   r?   rA   r5   )r   r   rF   rE   r   r   r   r   _clear_etc_environment_proxy   s    z+ServiceBackend._clear_etc_environment_proxyc                s   t jjd  fddtjtjj dD }|jdtjj d xb|D ]Z}g }d}x2t|D ]&}|j	 j
d| r|d	}q`|j| q`W |rJt|d
jdj| qJW d	S )z" helper that clears the apt proxy zDir::Etcc                s   g | ]}t jj d |qS )z
apt.conf.d)r   r   r5   )r6   r7   )r8   r   r   r9      s    z3ServiceBackend._clear_apt_proxy.<locals>.<listcomp>z
apt.conf.dr   zapt.confFzacquire::%s::proxy Tr:   r   )r   r'   r;   r   r<   r   r5   r=   r   r>   r   r?   rA   )r   r   rC   rD   rE   rF   r   r   )r8   r   _clear_apt_proxy   s    

zServiceBackend._clear_apt_proxyZssbc             C   s   t ||ds t ||ds td|| jkr6td| |dksF|dkr`| j|}|| j|M }n| j||}|| j||M }|S )z
        Set a new system-wide proxy that looks like e.g.:
        http://proxy.host.net:port/

        This function will set a new apt configuration and
        modify /etc/environment
        
        z!com.ubuntu.systemservice.setproxyz#org.gnome.gconf.defaults.set-systemzPermission denied by policyz'proxy_type '%s' is unknown in set_proxyr   N)authWithPolicyKitPermissionDeniedErrorSUPPORTED_PROXIESr3   rM   rL   rH   rK   )r   r   rB   r1   r2   resr   r   r   	set_proxy   s    

zServiceBackend.set_proxyc             C   sR   d}g }x*t dD ]}|jdr&d}q|j| qW |rNt ddjdj| dS )NFz/etc/environmentz	no_proxy=Tr:   r   )r   r   r?   rA   r5   )r   rF   rE   r   r   r   r   _clear_etc_environment_no_proxy   s    
z.ServiceBackend._clear_etc_environment_no_proxyc             C   s|   | j |sdS d}g }d| }x,tdD ] }|jdr>|}d}|j| q(W |rhtddjdj| ntddj| dS )	NFzno_proxy="%s"
z/etc/environmentz	no_proxy=Tr:   r   rI   )r/   r   r   r?   rA   r5   )r   rB   rF   rE   rJ   r   r   r   r   _write_etc_environment_no_proxy   s    

z.ServiceBackend._write_etc_environment_no_proxyc             C   sH   t ||ds t ||ds td|dks0|dkr:| j }n
| j|}|S )z
        Set a new system-wide no_proxy list that looks like e.g.:
        localhost,foo.com

        This function will modify /etc/environment
        
        z#com.ubuntu.systemservice.setnoproxyz#org.gnome.gconf.defaults.set-systemzPermission denied by policyr   N)rO   rP   Z_clear_no_proxyrU   )r   Znew_no_proxyr1   r2   rR   r   r   r   set_no_proxy
  s    

zServiceBackend.set_no_proxyr   Zssssc             C   s   t  \}}}}||||fS )aT  
        Set the system default keyboard configuration. 

        It expects four input arguments (strings):
        model -- the model (evdev, pc105, ...)
        layout -- the layout (de, us, ...)
        variant -- the variant (nodeadkeys, ..)
        options -- keyboard options (nocaps, ...)

        It returns True on sucess
        )Zget_keyboard_from_etc)r   r1   r2   modellayoutvariantoptionsr   r   r   get_keyboard%  s    zServiceBackend.get_keyboardc             C   s>   t ||ds t ||ds tdt||||s:td dS dS )z~
        Get the current keyboard configuration. This returns four
        strings: (model, layout, variant, options)
        z$com.ubuntu.systemservice.setkeyboardz#org.gnome.gconf.defaults.set-systemzPermission denied by policyz could not write keyboard to /etcFT)rO   rP   Zset_keyboard_to_etcprint)r   rW   rX   rY   rZ   r1   r2   r   r   r   set_keyboard9  s    zServiceBackend.set_keyboardc             C   s   d}| j   otjj|S )zT
        Check if a reboot is required to finish installing
        updates
        z/var/run/reboot-required)_is_package_system_lockedr   r   r   )r   r1   r2   ZREBOOT_REQUIREDr   r   r   is_reboot_requiredQ  s    

z!ServiceBackend.is_reboot_requiredc          	   C   s   x| j | j| j| jgD ]h}tjj|s(qtjdt	j
ddddd}t|}t	j	|t	j|}tjd|d }|j  |t	j
krdS qW dS )NZhhllhlr   TF)	DPKG_LOCKAPT_LISTS_LOCKAPT_ARCHIVES_LOCKUNATTENDED_UPGRADES_LOCKr   r   r   structZpackfcntlZF_WRLCKr   ZF_GETLKZunpackclose)r   lockZflkrD   rvZlockvr   r   r   r^   c  s    
z(ServiceBackend._is_package_system_lockedc             C   s   t ||dstd| j S )z7
        Check if the package system is locked
        z*com.ubuntu.systemservice.ispkgsystemlockedzPermission denied by policy)rO   rP   r^   )r   r1   r2   r   r   r   is_package_system_lockedv  s    
z'ServiceBackend.is_package_system_locked)r   r   r   r   )N)NN)NN)NN)NN)NN)NN)NN)%__name__
__module____qualname____doc__r   rQ   r`   rb   ra   rc   r   r"   r&   r#   r*   r+   r,   r-   r.   r/   r
   r   methodr4   rH   rK   rL   rM   rS   rT   rU   rV   r[   r]   r_   r^   ri   r   r   r   r   r      s   
	&r   )sysr
   Zdbus.serviceZdbus.mainloop.glibZmainloopZglibZDBusGMainLoopr   
subprocessr   rd   re   ZUbuntuSystemService.utilsr   r   r   r   r   r   r   <module>   s   