U
    LwaD                     @   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	m
Z
 d dlmZ d dlmZ d dlmZ dZdZdZed	d
 dd ZG dd dejZG dd dejjeZdS )    )GLibN)SourceEntry)DBusGMainLoop)LivepatchService)SoftwarePropertieszcom.ubuntu.SoftwareProperties/T)Zset_as_defaultc                 C   s   t jdk r| dS | S d S )N3zutf-8)sysversionencode)string r   P/usr/lib/python3/dist-packages/softwareproperties/dbus/SoftwarePropertiesDBus.py_to_unicode*   s    

r   c                   @   s   e Zd ZdZdS )PermissionDeniedByPolicyz6com.ubuntu.SoftwareProperties.PermissionDeniedByPolicyN)__name__
__module____qualname___dbus_error_namer   r   r   r   r   0   s   r   c                       s  e Zd ZdSddZ fddZ fddZejje	d	d
dd Z
ejje	d	d
dd Zejje	d	d
dd Zejje	d	d
dd Zejje	d	d
dd Zejje	ddd	d	ddTddZejje	ddd	d	ddUddZejje	dddd	ddVddZejje	dddd	ddWdd Zejje	dddd	ddXd!d"Zejje	dddd	ddYd#d$Zejje	ddd	d	ddZd%d&Zejje	ddd	d	dd[d'd(Zejje	dddd	dd\d)d*Zejje	ddd+d,dd]d-d.Zejje	ddd	d	dd^d/d0Zejje	ddd	d	dd_d1d2Zd3d4 Zd5d6 Zejje	ddd,d	dd`d7d8Zejje	ddd9d	ddad:d;Zejje	ddd9d	ddbd<d=Z ejje	ddd9d	ddcd>d?Z!ejje	dddd	dddd@dAZ"ejje	dddd	ddedBdCZ#ejje	dddd,ddfdDdEZ$ejje	dddd,ddgdFdGZ%ejje	dddd,ddhdHdIZ&ejje	ddd	d,ddidJdKZ'ejje	dddLdLdMdNdjdOdPZ(dQdR Z)  Z*S )kSoftwarePropertiesDBusNr   c                 C   s\   t j| |||d d | _d | _tjjt|d}tjj| |t	 d| _
td t | _d S )N)optionsdatadirrootdir)busTzwaiting for connections)r   __init__	dbus_infopolkitdbusserviceZBusNameDBUS_INTERFACE_NAMEObject	DBUS_PATHenforce_polkitloggingdebugr   _livepatch_service)selfr   r   r   r   Zbus_namer   r   r   r   5   s    
zSoftwarePropertiesDBus.__init__c                    s   t t|   |   d S N)superr   save_sourceslistSourcesListModifiedr&   	__class__r   r   r)   E   s    z'SoftwarePropertiesDBus.save_sourceslistc                    s   t t|   |   d S r'   )r(   r   write_configConfigModifiedr+   r,   r   r   r.   H   s    z#SoftwarePropertiesDBus.write_config )Zdbus_interfaceZ	signaturec                 C   s   t d dS )0 emit signal when the sources.list got modified zSourcesListModified signalNr#   r$   r+   r   r   r   r*   M   s    z*SoftwarePropertiesDBus.SourcesListModifiedc                 C   s   t d dS )r1   zConfigModified signalNr2   r+   r   r   r   r/   R   s    z%SoftwarePropertiesDBus.ConfigModifiedc                 C   s   t d dS )z, emit signal when the apt keys got modified zKeysModified signalNr2   r+   r   r   r   KeysModifiedW   s    z#SoftwarePropertiesDBus.KeysModifiedc                 C   s   t d dS )z6 emit signal when the policykit authentication failed zAuth signalNr2   r+   r   r   r   
AuthFailed\   s    z!SoftwarePropertiesDBus.AuthFailedc                 C   s   t d dS )z( emit signal when adding a cdrom failed zCdrom scan failed signalNr2   r+   r   r   r   CdromScanFaileda   s    z&SoftwarePropertiesDBus.CdromScanFailedsenderconn)sender_keywordconnection_keywordin_signatureout_signaturec                 C   s   |  ||d |   d S N*com.ubuntu.softwareproperties.applychanges)_check_policykit_privilegeZrevertr&   r6   r7   r   r   r   Reverth   s      zSoftwarePropertiesDBus.Revertc                 C   s   |    d S r'   )Zreload_sourceslistr?   r   r   r   Reloadp   s    zSoftwarePropertiesDBus.Reloadsc                 C   s    |  ||d | t| d S r<   )r>   Zenable_child_sourcer   r&   templater6   r7   r   r   r   EnableChildSourcew   s      z(SoftwarePropertiesDBus.EnableChildSourcec                 C   s    |  ||d | t| d S r<   )r>   Zdisable_child_sourcer   rC   r   r   r   DisableChildSource   s      z)SoftwarePropertiesDBus.DisableChildSourcec                 C   s    |  ||d | t| d S r<   )r>   Zenable_componentr   r&   Z	componentr6   r7   r   r   r   EnableComponent   s      z&SoftwarePropertiesDBus.EnableComponentc                 C   s    |  ||d | t| d S r<   )r>   Zdisable_componentr   rG   r   r   r   DisableComponent   s      z'SoftwarePropertiesDBus.DisableComponentc                 C   s   |  ||d |   d S r<   )r>   Zenable_source_code_sourcesr?   r   r   r   EnableSourceCodeSources   s      z.SoftwarePropertiesDBus.EnableSourceCodeSourcesc                 C   s"   |  ||d |   |   d S r<   )r>   Zdisable_source_code_sourcesr)   r?   r   r   r   DisableSourceCodeSources   s      z/SoftwarePropertiesDBus.DisableSourceCodeSourcesc                 C   s    |  ||d | t| d S r<   )r>   Ztoggle_source_user   r&   sourcer6   r7   r   r   r   ToggleSourceUse   s      z&SoftwarePropertiesDBus.ToggleSourceUseZssbc                 C   s"   |  ||d | t|t|S r<   )r>   Zreplace_source_entryr   )r&   oldnewr6   r7   r   r   r   ReplaceSourceEntry   s       z)SoftwarePropertiesDBus.ReplaceSourceEntryc                 C   s    |  ||d | t| d S r<   )r>   Zchange_main_download_serverr   )r&   Zserverr6   r7   r   r   r   ChangeMainDownloadServer   s      z/SoftwarePropertiesDBus.ChangeMainDownloadServerc                 C   s   |  ||d |   d S r<   )r>   _add_cdrom_sourcer?   r   r   r   AddCdromSource   s      z%SoftwarePropertiesDBus.AddCdromSourcec                 C   sJ   t  }dddddddddddd|j g}t|}td	| j|| d
S )z  add a (already inserted) cdrom z	apt-cdromaddz-ozDebug::aptcdrom=1zDebug::identcdrom=1zacquire::cdrom::AutoDetect=1zacquire::cdrom::NoMount=1zDir::Etc::sourcelist=%si  N)tempfileZNamedTemporaryFilename
subprocessPopenr   Ztimeout_add_wait_for_cdrom_scan_finish)r&   tmpcmdpr   r   r   rT      s         
z(SoftwarePropertiesDBus._add_cdrom_sourcec                 C   sl   |  dkrdS |  }|dkr,|   dS d}t|jD ]}|}q:|dkrh| jjdt| |   dS )z= glib timeout helper to wait for the cdrom scanner to finish NTr   Fr0   )	Zpollr5   openrX   Zsourceslistlistinsertr   Zset_modified_sourceslist)r&   r^   r\   reslinexr   r   r   r[      s    z2SoftwarePropertiesDBus._wait_for_cdrom_scan_finishc                 C   s   |  ||d | | d S r<   )r>   Zset_popcon_pariticipation)r&   Zparticipatesr6   r7   r   r   r   SetPopconPariticipation   s      z.SoftwarePropertiesDBus.SetPopconPariticipationic                 C   s   |  ||d | | d S r<   )r>   Zset_update_automation_levelr&   stater6   r7   r   r   r   SetUpdateAutomationLevel   s      z/SoftwarePropertiesDBus.SetUpdateAutomationLevelc                 C   s   |  ||d | | d S r<   )r>   Zset_release_upgrades_policyrg   r   r   r   SetReleaseUpgradesPolicy   s      z/SoftwarePropertiesDBus.SetReleaseUpgradesPolicyc                 C   s    |  ||d | t| d S r<   )r>   Zset_update_intervalint)r&   Zdaysr6   r7   r   r   r   SetUpdateInterval  s      z(SoftwarePropertiesDBus.SetUpdateIntervalc                 C   s(   |  ||d | t| |   d S r<   )r>   Zadd_source_from_liner   r3   )r&   Z
sourceLiner6   r7   r   r   r   AddSourceFromLine  s      z(SoftwarePropertiesDBus.AddSourceFromLinec                 C   s    |  ||d | t| d S r<   )r>   Zremove_sourcer   rL   r   r   r   RemoveSource  s      z#SoftwarePropertiesDBus.RemoveSourcec                 C   s(   |  ||d | |}|r$|   |S r<   )r>   Zadd_keyr3   )r&   pathr6   r7   rb   r   r   r   AddKey$  s      
zSoftwarePropertiesDBus.AddKeyc                 C   s(   |  ||d | |}|r$|   |S r<   )r>   Zadd_key_from_datar3   )r&   ZkeyDatar6   r7   rb   r   r   r   AddKeyFromData/  s      
z%SoftwarePropertiesDBus.AddKeyFromDatac                 C   s(   |  ||d | |}|r$|   |S r<   )r>   Z
remove_keyr3   )r&   Zkeyidr6   r7   rb   r   r   r   	RemoveKey:  s      
z SoftwarePropertiesDBus.RemoveKeyc                 C   s&   |  ||d |  }|r"|   |S r<   )r>   Zupdate_keysr3   )r&   r6   r7   rb   r   r   r   
UpdateKeysE  s      z!SoftwarePropertiesDBus.UpdateKeysZbs)reply_handlererror_handler)r8   r9   r:   r;   Zasync_callbacksc                    s4    fdd} ||d tj|d  d S )Nc                      s&   j  t fdd d S )Nc                      s     S r'   r   r   )rt   retr   r   <lambda>W      zXSoftwarePropertiesDBus.SetLivepatchEnabled.<locals>.enable_thread_func.<locals>.<lambda>)r%   Zset_enabledr   Zidle_addr   enabledrt   r&   token)rv   r   enable_thread_funcU  s    zFSoftwarePropertiesDBus.SetLivepatchEnabled.<locals>.enable_thread_funcr=   )target)r>   	threadingZThreadstart)r&   rz   r{   rt   ru   r6   r7   r|   r   ry   r   SetLivepatchEnabledQ  s      z*SoftwarePropertiesDBus.SetLivepatchEnabledc           	   
   C   s4  |dkr|dkrdS | j sdS | jdkr@t|dddd| _| j|}| jdkrrtt dddd| _z>| jjdd	tj	|d
dif|ddit
d
ddd\}}}W nL tjk
r } z,|jdkrd| _| ||| W Y S  W 5 d}~X Y nX |s0td||||t|f  |   t|dS )a  Verify that sender has a given PolicyKit privilege.

        sender is the sender's (private) D-BUS name, such as ":1:42"
        (sender_keyword in @dbus.service.methods). conn is
        the dbus.Connection object (connection_keyword in
        @dbus.service.methods). privilege is the PolicyKit privilege string.

        This method returns if the caller is privileged, and otherwise throws a
        PermissionDeniedByPolicy exception.
        Nzorg.freedesktop.DBusz/org/freedesktop/DBus/BusFzorg.freedesktop.PolicyKit1z%/org/freedesktop/PolicyKit1/Authorityz$org.freedesktop.PolicyKit1.Authorityzsystem-bus-namerX      )Zvariant_levelr0   iX  )Ztimeoutz)org.freedesktop.DBus.Error.ServiceUnknownzW_check_polkit_privilege: sender %s on connection %s pid %i is not authorized for %s: %s)r"   r   r   Z	InterfaceZ
get_objectZGetConnectionUnixProcessIDr   Z	SystemBusZCheckAuthorizationStringZUInt32DBusExceptionr   Z_check_polkit_privileger#   r$   strr4   r   )	r&   r6   r7   Z	privilegepidZis_auth_Zdetailser   r   r   r>   ^  sP    

 
     
z1SoftwarePropertiesDBus._check_policykit_privilege)NNr   )NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)+r   r   r   r   r)   r.   r   r   signalr   r*   r/   r3   r4   r5   methodr@   rA   rE   rF   rH   rI   rJ   rK   rN   rR   rS   rU   rT   r[   re   ri   rj   rl   rm   rn   rp   rq   rr   rs   r   r>   __classcell__r   r   r,   r   r   3   sd  





                                            	   
r   )Zgi.repositoryr   Zdbus.servicer   r#   rY   rW   r	   r~   Zaptsources.sourceslistr   Zdbus.mainloop.glibr   Z#softwareproperties.LivepatchServicer   Z%softwareproperties.SoftwarePropertiesr   ZDBUS_BUS_NAMEr!   r   r   r   r   r   r    r   r   r   r   r   <module>   s"   
