o
    ÙKÕiš*  ã                   @   sÂ  d dl Z d dlZd dlZd dlmZmZmZ d dlmZm	Z	m
Z
mZmZmZmZmZ d dlmZ d dlmZ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m Z  e !¡ Z"e #e $e%¡¡Z&dej'de(dee dee( de(de(fdd„Z)ddœdd„Z*ej+ej,e -ej.¡e /d¡dd„ ƒƒƒƒZ0edej1ej2e0ej3eedej4j5d 6e
j7e '¡ d�¡d�ddd �ed!ej8j5dd"�d#d$�ed%ej9j5d&d'�dd&d(gd&d)�ed*ej:d#d$�gd+�gd,�Z;dS )-é    N)ÚAnyÚDictÚList)ÚconfigÚcontractÚentitlementsÚevent_loggerÚ
exceptionsÚmessagesÚstatusÚutil)ÚProgressWrapper)ÚServiceWithDependenciesÚ_dependencies)Ú_enabled_services)Úcli_util)ÚProArgumentÚProArgumentGroupÚ
ProCommand)ÚHelpCategory)ÚCanDisableFailureÚcfgÚserviceÚall_dependenciesÚenabled_service_namesÚcalled_nameÚservice_titlec                 C   sŠ   g }|D ]}|j |ks|j |vrq|jD ]}|j |kr!| |j ¡ qq|D ]}	t | |	¡}
tjj||
d�}tj	|d�sBt
j||
d�‚q%d S )N)Úservice_being_disabledÚdependent_service)Úmsg)ÚnameÚ
depends_onÚappendr   Ú	get_titler
   ÚDEPENDENT_SERVICEÚformatr   Úprompt_for_confirmationr	   ÚDependentServiceStopsDisable)r   r   r   r   r   r   Údependent_servicesÚsÚrequirementr   Údependent_service_titleÚuser_msg© r-   ú6/usr/lib/python3/dist-packages/uaclient/cli/disable.pyÚprompt_for_dependency_handling   s0   

€þÿþþÿør/   T)Úupdate_statusc          	      C   s²   | j }|dur	|} |rtƒ }nttj|d�ƒ}|  |¡\}}|sIt | j¡ |durHt|t	ƒrH|j
durHt |j
j¡ tj|j
j|j
j| jd� nt | j¡ |rWtj|d� |S )a  Perform the disable action on a named entitlement.

    :param entitlement_name: the name of the entitlement to enable
    :param cfg: the UAConfig to pass to the entitlement
    :param json_output: output should be json only

    @return: True on success, False otherwise
    N©Ú
assume_yes)Ú	error_msgÚ
error_coder   ©r   )Úenabled_variantr   r   ÚCLIEnableDisableProgressÚdisableÚeventÚservice_failedr    Ú
isinstancer   ÚmessageÚinfor   ÚerrorÚservice_processedr   )	Úentitlementr   Újson_outputr2   r0   ÚvariantÚprogressÚretÚreasonr-   r-   r.   Úperform_disable>   s0   
ÿ
ý€rF   zpro disablec                K   sf  g }g }g }g }t jdddœ}| jdk}| j}	t |¡}
| jr(|	r(tjddd�‚t	| dg ƒ}t
 ||¡\}}d	d
„ t|ƒjD ƒ}t|ƒj}d}|D ]î}t
j||| jd�}|j}|dur]|}|	s—zt||j||||jd� W n) tjy– } zt |¡ |
|jƒ |
tjj|jd�ƒ d}W Y d}~qId}~ww |r�tƒ }nttj|	d�ƒ}| ¡ |_zT| |¡\}}t j |d� |såd}| !|¡ |durät"|t#ƒrä|j$durä|
|j$jƒ | !d|j|j$j|j$jdœ¡ n| !|¡ | %¡ }|rýd|d< |
tj&jdd�ƒ W qI tj�y7 } z+d}| !|¡ |
|jƒ |
tj'j|jd�ƒ | !d|j|j|j(|j)dœ¡ W Y d}~qId}~ww |�rtd}dd *t
j+|d�¡ d }d *t,j-|dddd�¡}tj.dd *|¡|d�}|
|jƒ | !dd|j|j(|j)dœ¡ t/ 0|¡}| 1¡  |�r¬| 2¡  | 2¡  |�r�dnd |d!< ||d"< ||d#< ||d$< ||d%< t3t4j5|t6j7dd&�ƒ |�r±d'S d(S ))zbPerform the disable action on a list of entitlements.

    @return: 0 on success, 1 otherwise
    ÚsuccessF)Ú_schema_versionÚresultÚneeds_rebootÚjsonú--purgeú--assume-yes)Úoption1Úoption2r   c                 S   s   g | ]}|j ‘qS r-   )r    )Ú.0r)   r-   r-   r.   Ú
<listcomp>”   s    ÿz"action_disable.<locals>.<listcomp>T)r   r    ÚpurgeN)r   r   )Útitler1   r5   )Útyper   r<   Úmessage_coderJ   zdisable operation)Ú	operation)rT   r   r<   rU   Úadditional_infozTry ú, Ú.Ú
éP   )ÚwidthÚbreak_long_wordsÚbreak_on_hyphensr8   )rV   Úinvalid_serviceÚservice_msgÚsystemÚfailurerI   Úprocessed_servicesÚfailed_servicesÚerrorsÚwarnings)ÚclsÚ	sort_keysr   é   )8r   ÚJSON_SCHEMA_VERSIONr%   r2   r   Ú&create_interactive_only_print_functionrR   r	   ÚInvalidOptionCombinationÚgetattrr   Úget_valid_entitlement_namesr   Úenabled_servicesr   ÚservicesÚentitlement_factoryr6   r/   r    rS   ÚUbuntuProErrorÚLOGÚ	exceptionr   r
   ÚENABLE_FAILEDr   r7   Úcalculate_total_disable_stepsÚtotal_stepsr8   r   r"   r;   r   r<   Ú_check_for_rebootÚENABLE_REBOOT_REQUIRED_TMPLÚDISABLE_FAILED_TMPLÚmsg_coderW   ÚjoinÚvalid_servicesÚtextwrapÚwrapÚInvalidServiceOpErrorr   ÚUAContractClientÚupdate_activity_tokenÚsortÚprintrK   Údumpsr   ÚDatetimeAwareJSONEncoder)Úargsr   Úkwargsrc   rd   re   rf   Újson_responserA   r2   Úinteractive_only_printÚnamesÚentitlements_foundÚentitlements_not_foundr   r   rD   Úent_nameÚentrB   ÚerC   Údisable_retrE   Úent_reboot_requiredÚvalid_namesr`   ÚerrÚcontract_clientr-   r-   r.   Úaction_disablel   s0  	ý
ÿ
ÿ
ýÿ
ý
ú

ÿ€ù	
ÿ


ÿ
üÿ€
	ÿÿ€

ÿûÿ€ùÿüÿý
ûÿ

ýÿr–   r8   rX   r5   )ÚoptionsÚstoreú+)ÚhelpÚactionÚnargsrM   )ÚcommandÚ
store_true)rš   r›   z--formatÚcli)ÚdefaultrK   )rš   r›   Úchoicesr    rL   )Ú	arguments)rš   Údescriptionr›   Úhelp_categoryÚargument_groups)<rK   Úloggingr~   Útypingr   r   r   Úuaclientr   r   r   r   r	   r
   r   r   Úuaclient.apir   Ú+uaclient.api.u.pro.services.dependencies.v1r   r   Ú-uaclient.api.u.pro.status.enabled_services.v1r   Úuaclient.clir   Úuaclient.cli.commandsr   r   r   Úuaclient.cli.parserr   Ú(uaclient.entitlements.entitlement_statusr   Úget_event_loggerr9   Ú	getLoggerÚreplace_top_level_logger_nameÚ__name__rs   ÚUAConfigÚstrr/   rF   Úverify_json_format_argsÚassert_rootÚassert_attachedÚ&_raise_enable_disable_unattached_errorÚassert_lock_filer–   ÚCLI_ROOT_DISABLEÚCLI_DISABLE_DESCÚOTHERÚCLI_DISABLE_SERVICEr%   r|   r}   ÚCLI_ASSUME_YESÚCLI_FORMAT_DESCÚ	CLI_PURGEÚdisable_commandr-   r-   r-   r.   Ú<module>   s’    (
ÿþýüû
ú ÿ.
 6ÿÿø
ýûýéÿÿ
ú