3
i(]C                 @   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| j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dTd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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-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dad7d8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d9d	dddd>d?Z!ejje	dddd	dded@dAZ"ejje	dddd	ddfdBdCZ#ejje	dddd,ddgdDdEZ$ejje	dddd,ddhdFdGZ%ejje	dddd,ddidHdIZ&ejje	ddd	d,ddjdJdKZ'ejje	dddLdLdkdOdldPdQZ(dRdS Z)  Z*S )mSoftwarePropertiesDBusNr   c             C   s\   t j| |||d d | _d | _tjjt|d}tjjj| |t	 d| _
tj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| j  | j  d S )N)superr   save_sourceslistSourcesListModified)r&   )	__class__r   r   r(   E   s    z'SoftwarePropertiesDBus.save_sourceslistc                s   t t| j  | j  d S )N)r'   r   write_configConfigModified)r&   )r*   r   r   r+   H   s    z#SoftwarePropertiesDBus.write_config )Zdbus_interfaceZ	signaturec             C   s   t jd dS )z0 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 jd dS )z0 emit signal when the sources.list got modified zConfigModified signalN)r#   r$   )r&   r   r   r   r,   R   s    z%SoftwarePropertiesDBus.ConfigModifiedc             C   s   t jd dS )z, emit signal when the apt keys got modified zKeysModified signalN)r#   r$   )r&   r   r   r   KeysModifiedW   s    z#SoftwarePropertiesDBus.KeysModifiedc             C   s   t jd dS )z6 emit signal when the policykit authentication failed zAuth signalN)r#   r$   )r&   r   r   r   
AuthFailed\   s    z!SoftwarePropertiesDBus.AuthFailedc             C   s   t jd dS )z( emit signal when adding a cdrom failed zCdrom scan failed signalN)r#   r$   )r&   r   r   r   CdromScanFaileda   s    z&SoftwarePropertiesDBus.CdromScanFailedsenderconn)sender_keywordconnection_keywordin_signatureout_signaturec             C   s   | j ||d | j  d S )Nz*com.ubuntu.softwareproperties.applychanges)_check_policykit_privilegeZrevert)r&   r1   r2   r   r   r   Reverth   s    
zSoftwarePropertiesDBus.Revertc             C   s   | j   d S )N)Zreload_sourceslist)r&   r1   r2   r   r   r   Reloadp   s    zSoftwarePropertiesDBus.Reloadsc             C   s    | j ||d | jt| d S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zenable_child_sourcer   )r&   templater1   r2   r   r   r   EnableChildSourcew   s    
z(SoftwarePropertiesDBus.EnableChildSourcec             C   s    | j ||d | jt| d S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zdisable_child_sourcer   )r&   r;   r1   r2   r   r   r   DisableChildSource   s    
z)SoftwarePropertiesDBus.DisableChildSourcec             C   s    | j ||d | jt| d S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zenable_componentr   )r&   	componentr1   r2   r   r   r   EnableComponent   s    
z&SoftwarePropertiesDBus.EnableComponentc             C   s    | j ||d | jt| d S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zdisable_componentr   )r&   r>   r1   r2   r   r   r   DisableComponent   s    
z'SoftwarePropertiesDBus.DisableComponentc             C   s   | j ||d | j  d S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zenable_source_code_sources)r&   r1   r2   r   r   r   EnableSourceCodeSources   s    
z.SoftwarePropertiesDBus.EnableSourceCodeSourcesc             C   s"   | j ||d | j  | j  d S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zdisable_source_code_sourcesr(   )r&   r1   r2   r   r   r   DisableSourceCodeSources   s    
z/SoftwarePropertiesDBus.DisableSourceCodeSourcesc             C   s    | j ||d | jt| d S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Ztoggle_source_user   )r&   sourcer1   r2   r   r   r   ToggleSourceUse   s    
z&SoftwarePropertiesDBus.ToggleSourceUseZssbc             C   s"   | j ||d | jt|t|S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zreplace_source_entryr   )r&   oldnewr1   r2   r   r   r   ReplaceSourceEntry   s    
z)SoftwarePropertiesDBus.ReplaceSourceEntryc             C   s    | j ||d | jt| d S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zchange_main_download_serverr   )r&   Zserverr1   r2   r   r   r   ChangeMainDownloadServer   s    
z/SoftwarePropertiesDBus.ChangeMainDownloadServerc             C   s   | j ||d | j  d S )Nz*com.ubuntu.softwareproperties.applychanges)r7   _add_cdrom_source)r&   r1   r2   r   r   r   AddCdromSource   s    
z%SoftwarePropertiesDBus.AddCdromSourcec             C   sJ   t j }dddddddddddd|j g}tj|}tj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   rJ      s    
z(SoftwarePropertiesDBus._add_cdrom_sourcec             C   sp   |j  dkrdS |j  }|dkr,| j  dS d}xt|jD ]}|}q<W |dkrl| jjjdt| | j  dS )z= glib timeout helper to wait for the cdrom scanner to finish NTr   Fr-   )	Zpollr0   openrN   Zsourceslistlistinsertr   Zset_modified_sourceslist)r&   rT   rR   reslinexr   r   r   rQ      s    z2SoftwarePropertiesDBus._wait_for_cdrom_scan_finishc             C   s   | j ||d | j| d S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zset_popcon_pariticipation)r&   Zparticipatesr1   r2   r   r   r   SetPopconPariticipation   s    
z.SoftwarePropertiesDBus.SetPopconPariticipationic             C   s   | j ||d | j| d S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zset_update_automation_level)r&   stater1   r2   r   r   r   SetUpdateAutomationLevel   s    
z/SoftwarePropertiesDBus.SetUpdateAutomationLevelc             C   s   | j ||d | j| d S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zset_release_upgrades_policy)r&   r]   r1   r2   r   r   r   SetReleaseUpgradesPolicy   s    
z/SoftwarePropertiesDBus.SetReleaseUpgradesPolicyc             C   s   | j ||d | j| d S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zset_update_interval)r&   Zdaysr1   r2   r   r   r   SetUpdateInterval  s    
z(SoftwarePropertiesDBus.SetUpdateIntervalc             C   s(   | j ||d | jt| | j  d S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zadd_source_from_liner   r.   )r&   Z
sourceLiner1   r2   r   r   r   AddSourceFromLine  s    
z(SoftwarePropertiesDBus.AddSourceFromLinec             C   s    | j ||d | jt| d S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zremove_sourcer   )r&   rC   r1   r2   r   r   r   RemoveSource  s    
z#SoftwarePropertiesDBus.RemoveSourcec             C   s   | j ||d | j|S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zadd_key)r&   pathr1   r2   r   r   r   AddKey$  s    
zSoftwarePropertiesDBus.AddKeyc             C   s   | j ||d | j|S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zadd_key_from_data)r&   ZkeyDatar1   r2   r   r   r   AddKeyFromData,  s    
z%SoftwarePropertiesDBus.AddKeyFromDatac             C   s   | j ||d | j|S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Z
remove_key)r&   Zkeyidr1   r2   r   r   r   	RemoveKey4  s    
z SoftwarePropertiesDBus.RemoveKeyc             C   s   | j ||d | j S )Nz*com.ubuntu.softwareproperties.applychanges)r7   Zupdate_keys)r&   r1   r2   r   r   r   
UpdateKeys<  s    
z!SoftwarePropertiesDBus.UpdateKeysZbsreply_handlererror_handler)r3   r4   r5   r6   Zasync_callbacksc                s4    fdd}j ||d tj|dj  d S )Nc                  s&   j j tj fdd d S )Nc                  s     S )Nr   r   )rh   retr   r   <lambda>K  s    zXSoftwarePropertiesDBus.SetLivepatchEnabled.<locals>.enable_thread_func.<locals>.<lambda>)r%   Zset_enabledr   Zidle_addr   )enabledrh   r&   token)rj   r   enable_thread_funcI  s    zFSoftwarePropertiesDBus.SetLivepatchEnabled.<locals>.enable_thread_funcz*com.ubuntu.softwareproperties.applychanges)target)r7   	threadingZThreadstart)r&   rl   rm   rh   ri   r1   r2   rn   r   )rl   rh   r&   rm   r   SetLivepatchEnabledE  s    
z*SoftwarePropertiesDBus.SetLivepatchEnabledc       	      C   s,  |dkr|dkrdS | j sdS | jdkr@tj|jdddd| _| jj|}| jdkrrtjtj jdddd| _y>| jjdd	tj	|d
dif|dditj
d
ddd\}}}W nD tjk
r } z&|jdkrd| _| j|||S  W Y dd}~X nX |s(tjd||||t|f  | j  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-namerN      )Zvariant_levelr-   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$   strr/   r   )	r&   r1   r2   Z	privilegepidZis_auth_Zdetailser   r   r   r7   R  s6    


$
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)rh   ri   )NN)+r   r   r   r   r(   r+   r   r   signalr   r)   r,   r.   r/   r0   methodr8   r9   r<   r=   r?   r@   rA   rB   rD   rH   rI   rK   rJ   rQ   r[   r^   r_   r`   ra   rb   rd   re   rf   rg   rr   r7   __classcell__r   r   )r*   r   r   3   s   


r   )Zgi.repositoryr   Zdbus.servicer   r#   rO   rM   r	   rp   Zaptsources.sourceslistr   Zdbus.mainloop.glibr   Z#softwareproperties.LivepatchServicer   Z%softwareproperties.SoftwarePropertiesr   ZDBUS_BUS_NAMEr!   r   r   ru   r   r   r    r   r   r   r   r   <module>   s"   
