o
    UÀ7eÑ@  ã                   @   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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)ÚSoftwarePropertieszcom.ubuntu.SoftwarePropertiesú/T)Úset_as_defaultc                 C   s   t jdk r
|  d¡S | 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   .   s    r   c                       s‚  e Zd ZdNd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�dOdd„ƒZejje	ddd	d	d�dOdd„ƒZejje	dddd	d�dOdd„ƒZejje	dddd	d�dOdd „ƒZejje	dddd	d�dOd!d"„ƒZejje	dddd	d�dOd#d$„ƒZejje	ddd	d	d�dOd%d&„ƒZejje	ddd	d	d�dOd'd(„ƒZejje	dddd	d�dOd)d*„ƒZejje	ddd+d,d�dOd-d.„ƒZejje	ddd	d	d�dOd/d0„ƒZejje	ddd	d	d�dOd1d2„ƒZd3d4„ Zd5d6„ Zejje	ddd,d	d�dOd7d8„ƒZejje	ddd9d	d�dOd:d;„ƒZejje	ddd9d	d�dOd<d=„ƒZ ejje	ddd9d	d�dOd>d?„ƒZ!ejje	dddd	d�dOd@dA„ƒZ"ejje	dddd	d�dOdBdC„ƒZ#ejje	dddd,d�dOdDdE„ƒZ$ejje	dddd,d�dOdFdG„ƒZ%ejje	dddd,d�dOdHdI„ƒZ&ejje	ddd	d,d�dOdJdK„ƒZ'dLdM„ Z(‡  Z)S )PÚSoftwarePropertiesDBusNr   c                 C   sT   t j| |||d� d | _d | _tjjt|d�}tjj | |t	¡ d| _
t d¡ d S )N)ÚoptionsÚdatadirÚrootdir)ÚbusTzwaiting for connections)r   Ú__init__Ú	dbus_infoÚpolkitÚdbusÚserviceÚBusNameÚDBUS_INTERFACE_NAMEÚObjectÚ	DBUS_PATHÚenforce_polkitÚloggingÚdebug)Úselfr   r   r   r   Úbus_namer   r   r   r   3   s   zSoftwarePropertiesDBus.__init__c                    ó   t t| ƒ ¡  |  ¡  d S ©N)Úsuperr   Úsave_sourceslistÚSourcesListModified©r&   ©Ú	__class__r   r   r+   A   ó   z'SoftwarePropertiesDBus.save_sourceslistc                    r(   r)   )r*   r   Úwrite_configÚConfigModifiedr-   r.   r   r   r1   D   r0   z#SoftwarePropertiesDBus.write_configÚ )Údbus_interfaceÚ	signaturec                 C   ó   t  d¡ dS )ú0 emit signal when the sources.list got modified zSourcesListModified signalN©r$   r%   r-   r   r   r   r,   I   ó   z*SoftwarePropertiesDBus.SourcesListModifiedc                 C   r6   )r7   zConfigModified signalNr8   r-   r   r   r   r2   N   r9   z%SoftwarePropertiesDBus.ConfigModifiedc                 C   r6   )z, emit signal when the apt keys got modified zKeysModified signalNr8   r-   r   r   r   ÚKeysModifiedS   r9   z#SoftwarePropertiesDBus.KeysModifiedc                 C   r6   )z6 emit signal when the policykit authentication failed zAuth signalNr8   r-   r   r   r   Ú
AuthFailedX   r9   z!SoftwarePropertiesDBus.AuthFailedc                 C   r6   )z( emit signal when adding a cdrom failed zCdrom scan failed signalNr8   r-   r   r   r   ÚCdromScanFailed]   r9   z&SoftwarePropertiesDBus.CdromScanFailedÚsenderÚconn)Úsender_keywordÚconnection_keywordÚin_signatureÚout_signaturec                 C   ó   |   ||d¡ |  ¡  d S ©Nz*com.ubuntu.softwareproperties.applychanges)Ú_check_policykit_privilegeÚrevert©r&   r=   r>   r   r   r   ÚRevertd   ó   ÿzSoftwarePropertiesDBus.Revertc                 C   s   |   ¡  d S r)   )Úreload_sourceslistrG   r   r   r   ÚReloadl   s   zSoftwarePropertiesDBus.ReloadÚsc                 C   ó    |   ||d¡ |  t|ƒ¡ d S rD   )rE   Úenable_child_sourcer   ©r&   Útemplater=   r>   r   r   r   ÚEnableChildSources   ó   ÿz(SoftwarePropertiesDBus.EnableChildSourcec                 C   rM   rD   )rE   Údisable_child_sourcer   rO   r   r   r   ÚDisableChildSource{   rR   z)SoftwarePropertiesDBus.DisableChildSourcec                 C   rM   rD   )rE   Úenable_componentr   ©r&   Ú	componentr=   r>   r   r   r   ÚEnableComponentƒ   rR   z&SoftwarePropertiesDBus.EnableComponentc                 C   rM   rD   )rE   Údisable_componentr   rV   r   r   r   ÚDisableComponent‹   rR   z'SoftwarePropertiesDBus.DisableComponentc                 C   rC   rD   )rE   Úenable_source_code_sourcesrG   r   r   r   ÚEnableSourceCodeSources“   rI   z.SoftwarePropertiesDBus.EnableSourceCodeSourcesc                 C   s"   |   ||d¡ |  ¡  |  ¡  d S rD   )rE   Údisable_source_code_sourcesr+   rG   r   r   r   ÚDisableSourceCodeSources›   s
   ÿz/SoftwarePropertiesDBus.DisableSourceCodeSourcesc                 C   rM   rD   )rE   Útoggle_source_user   ©r&   Úsourcer=   r>   r   r   r   ÚToggleSourceUse¤   rR   z&SoftwarePropertiesDBus.ToggleSourceUseÚssÚbc                 C   s"   |   ||d¡ |  t|ƒt|ƒ¡S rD   )rE   Úreplace_source_entryr   )r&   ÚoldÚnewr=   r>   r   r   r   ÚReplaceSourceEntry¬   s   ÿÿz)SoftwarePropertiesDBus.ReplaceSourceEntryc                 C   rM   rD   )rE   Úchange_main_download_serverr   )r&   Úserverr=   r>   r   r   r   ÚChangeMainDownloadServerµ   rR   z/SoftwarePropertiesDBus.ChangeMainDownloadServerc                 C   rC   rD   )rE   Ú_add_cdrom_sourcerG   r   r   r   ÚAddCdromSource¾   rI   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)ÚtempfileÚNamedTemporaryFileÚnameÚ
subprocessÚPopenr   Útimeout_addÚ_wait_for_cdrom_scan_finish)r&   ÚtmpÚcmdÚpr   r   r   rl   Æ   s   
û
z(SoftwarePropertiesDBus._add_cdrom_sourcec                 C   sl   |  ¡ du rdS |  ¡ }|dkr|  ¡  dS d}t|jƒD ]}|}q|dkr4| jj dt|ƒ¡ |  ¡  dS )z= glib timeout helper to wait for the cdrom scanner to finish NTr   Fr3   )	Úpollr<   Úopenrq   ÚsourceslistÚlistÚinsertr   Úset_modified_sourceslist)r&   rx   rv   ÚresÚlineÚxr   r   r   ru   Ö   s   z2SoftwarePropertiesDBus._wait_for_cdrom_scan_finishc                 C   ó   |   ||d¡ |  |¡ d S rD   )rE   Úset_popcon_pariticipation)r&   Úparticipatesr=   r>   r   r   r   ÚSetPopconPariticipationë   ó   ÿz.SoftwarePropertiesDBus.SetPopconPariticipationÚic                 C   r‚   rD   )rE   Úset_update_automation_level©r&   Ústater=   r>   r   r   r   ÚSetUpdateAutomationLeveló   r†   z/SoftwarePropertiesDBus.SetUpdateAutomationLevelc                 C   r‚   rD   )rE   Úset_release_upgrades_policyr‰   r   r   r   ÚSetReleaseUpgradesPolicyû   r†   z/SoftwarePropertiesDBus.SetReleaseUpgradesPolicyc                 C   rM   rD   )rE   Úset_update_intervalÚint)r&   Údaysr=   r>   r   r   r   ÚSetUpdateInterval  rR   z(SoftwarePropertiesDBus.SetUpdateIntervalc                 C   s(   |   ||d¡ |  t|ƒ¡ |  ¡  d S rD   )rE   Úadd_source_from_liner   r:   )r&   Ú
sourceLiner=   r>   r   r   r   ÚAddSourceFromLine  s
   ÿz(SoftwarePropertiesDBus.AddSourceFromLinec                 C   rM   rD   )rE   Úremove_sourcer   r`   r   r   r   ÚRemoveSource  rR   z#SoftwarePropertiesDBus.RemoveSourcec                 C   ó(   |   ||d¡ |  |¡}|r|  ¡  |S rD   )rE   Úadd_keyr:   )r&   Úpathr=   r>   r   r   r   r   ÚAddKey   ó   ÿ
zSoftwarePropertiesDBus.AddKeyc                 C   r—   rD   )rE   Úadd_key_from_datar:   )r&   ÚkeyDatar=   r>   r   r   r   r   ÚAddKeyFromData+  r›   z%SoftwarePropertiesDBus.AddKeyFromDatac                 C   r—   rD   )rE   Ú
remove_keyr:   )r&   Úkeyidr=   r>   r   r   r   r   Ú	RemoveKey6  r›   z SoftwarePropertiesDBus.RemoveKeyc                 C   s&   |   ||d¡ |  ¡ }|r|  ¡  |S rD   )rE   Úupdate_keysr:   )r&   r=   r>   r   r   r   r   Ú
UpdateKeysA  s   ÿz!SoftwarePropertiesDBus.UpdateKeysc           	   
   C   s,  |du r
|du r
dS | j sdS | jdu r t | ddd¡d¡| _| j |¡}| jdu r9t 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 n# tjy{ } z|jdkrvd| _|  |||¡W  Y d}~S ‚ d}~ww |s”t 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-namerq   é   )Úvariant_levelr3   iX  )Ú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   Ú	InterfaceÚ
get_objectÚGetConnectionUnixProcessIDr   Ú	SystemBusÚCheckAuthorizationÚStringÚUInt32ÚDBusExceptionr   Ú_check_polkit_privileger$   r%   Ústrr;   r   )	r&   r=   r>   Ú	privilegeÚpidÚis_authÚ_ÚdetailsÚer   r   r   rE   M  sJ   

ÿÿ
þýþ
€úÿüz1SoftwarePropertiesDBus._check_policykit_privilege)NNr   )NN)*r   r   r   r   r+   r1   r   r   Úsignalr    r,   r2   r:   r;   r<   ÚmethodrH   rK   rQ   rT   rX   rZ   r\   r^   rb   rh   rk   rm   rl   ru   r…   r‹   r�   r‘   r”   r–   rš   rž   r¡   r£   rE   Ú__classcell__r   r   r.   r   r   1   sþ    





þþþþþþþþþþþþþþþþþþþþþþ	r   )Úgi.repositoryr   Údbus.servicer   r$   rr   ro   r	   Úaptsources.sourceslistr   Údbus.mainloop.glibr   Ú%softwareproperties.SoftwarePropertiesr   ÚDBUS_BUS_NAMEr"   r    r   r®   r   r   r!   r   r   r   r   r   Ú<module>   s    
