o
    (fw*                     @   s  d dl Z d dlZd dlZd dl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 Z/edej0ej1e/ej2eedej3j4d5e	j6e& ddddd ed!ej7j4dd"d#d$ed%ej8j4d&d'dd&d(gd&d)ed*ej9d#d$gd+gd,Z:dS )-    N)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_outputr1   r/   variantprogressretreasonr,   r,   r-   perform_disable>   s0   

rE   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]|}|	szt||j||||jd W n) tjy } zt| |
|j |
tjj|jd d}W Y d}~qId}~ww |rt }nttj|	d}| |_zT||\}}t j |d |sd}|!| |durt"|t#r|j$dur|
|j$j |!d|j|j$j|j$jd n|!| |% }|rd|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  |rdnd |d!< ||d"< ||d#< ||d$< ||d%< t3t4j5|t6j7dd& |rd'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   )titler0   r4   )typer   r;   message_coderI   zdisable operation)	operation)rS   r   r;   rT   additional_infozTry , .
P   )widthbreak_long_wordsbreak_on_hyphensr7   )rU   invalid_serviceservice_msgsystemfailurerH   processed_servicesfailed_serviceserrorswarnings)cls	sort_keysr      )8r   JSON_SCHEMA_VERSIONr$   r1   r   &create_interactive_only_print_functionrQ   r   InvalidOptionCombinationgetattrr   get_valid_entitlement_namesr   enabled_servicesr   servicesentitlement_factoryr5   r.   r   rR   UbuntuProErrorLOG	exceptionr   r	   ENABLE_FAILEDr   r6   calculate_total_disable_stepstotal_stepsr7   r
   r!   r:   r   r;   _check_for_rebootENABLE_REBOOT_REQUIRED_TMPLDISABLE_FAILED_TMPLmsg_coderV   joinvalid_servicestextwrapwrapInvalidServiceOpErrorr   UAContractClientupdate_activity_tokensortprintrJ   dumpsr   DatetimeAwareJSONEncoder)argsr   kwargsrb   rc   rd   re   json_responser@   r1   interactive_only_printnamesentitlements_foundentitlements_not_foundr   r   rC   ent_nameentrA   erB   disable_retrD   ent_reboot_requiredvalid_namesr_   errcontract_clientr,   r,   r-   action_disablel   s0  	






	





	




r   r7   rW   r4   )optionsstore+)helpactionnargsrL   )command
store_true)r   r   z--formatcli)defaultrJ   )r   r   choicesr   rK   )	arguments)r   descriptionr   help_categoryargument_groups);rJ   loggingr}   typingr   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_loggerr8   	getLoggerreplace_top_level_logger_name__name__rr   UAConfigstrr.   rE   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

