U (f@sddlmZddlmZddlmZmZmZmZm Z m Z m Z m Z m Z mZddlmZddlmZmZddlmZddlmZe ZGdd d ejZd d Zed d dZd"ddZddZ ddZ!d#ddZ"ddZ#ddZ$eddddZ%eee&ee&ddd d!Z'dS)$wraps)Optional) actionsapidaemon entitlements event_logger exceptionslockmessagesstatusutil) _is_attached) AptProxyScopesetup_apt_proxy)UAConfig) machine_tokenc@sDeZdZedddZeeeeeedddZeddd Z d S) CLIEnableDisableProgress) assume_yescCs| |_||_dSN)Zis_interactiver)selfrr7/usr/lib/python3/dist-packages/uaclient/cli/cli_util.py__init__sz!CLIEnableDisableProgress.__init__) total_steps done_stepsprevious_step_messagecurrent_step_messagecCs|dk rt|dSr)print)rrrrrrrrprogresssz!CLIEnableDisableProgress.progress)eventcCs4|dkrt|n|dkr0t||js0tdS)NinfoZmessage_operation)rrZhandle_message_operationsrr ZPromptDeniedError)rr!Zpayloadrrr _on_event(s  z"CLIEnableDisableProgress._on_eventN) __name__ __module__ __qualname__boolrintrstrr r#rrrrrs rcOsdSrrargskwargsrrr _null_print0sr-Z json_outputcCs|rtStSdSr)r-rr.rrr&create_interactive_only_print_function4sr/Ncsfdd}|S)z1Decorator asserting exclusive access to lock filecstfdd}|S)Nc s0tjdd|d|i|}W5QRX|S)N) lock_holderZ sleep_timecfg)r Z RetryLock)r2r+r,Zretval)fr1rrnew_f?sz0assert_lock_file..wrapper..new_frr3r4r1r3rwrapper>sz!assert_lock_file..wrapperr)r1r8rr6rassert_lock_file;s r9cstfdd}|S)zDecorator asserting root usercs tstn ||SdSr)rZwe_are_currently_rootr ZNonRootUserErrorr*r7rrr4Ms zassert_root..new_frr5rr7r assert_rootJsr:cstfdd}|S)z>Decorator to verify if correct params are used for json formatcsB|s|f||S|jdkr.|js.tn|f||SdS)NZjson)formatrr ZCLIJSONFormatRequireAssumeYes)Zcmd_argsr+r,r7rrr4Zs  z&verify_json_format_args..new_frr5rr7rverify_json_format_argsWs r<csfdd}|S)zDecorator asserting attached config. :param msg_function: Optional function to generate a custom message if raising an UnattachedError cstfdd}|S)NcsRt|js>r6t|dd}t|dd}|||dnt|fd|i|S)NcommandZservice)r= service_namesr2r2)r is_attachedgetattrr ZUnattachedError)r+r2r,r=r?)r3raise_custom_error_functionrrr4ns   z/assert_attached..wrapper..new_frr5rBr7rr8ms z assert_attached..wrapperr)rBr8rrCrassert_attachedgs rDcstfdd}|S)z&Decorator asserting unattached config.cs<t|jr(t}tj|jddd|fd|i|S)Nnamer>)Z account_namer2)rr@rget_machine_token_filer ZAlreadyAttachedErrorZaccountget)r+r2r,machine_token_filer7rrr4s   z"assert_not_attached..new_frr5rr7rassert_not_attachedsrIcCsntj||d\}}|r:|r:tjd||d|ddn0|rTtjd||dntj|d|dddS)zRaises a custom error for enable/disable commands when unattached. Takes into consideration if the services exist or not, and notify the user accordingly.)namesr2z, r>) valid_service operationinvalid_service service_msg)rKrL)rLrMrNN)rZget_valid_entitlement_namesr ZUnattachedMixedServicesErrorjoinZUnattachedValidServicesErrorZUnattachedInvalidServicesError)r=r?r2Zentitlements_foundZentitlements_not_foundrrr&_raise_enable_disable_unattached_errors* rP)r2returncCs|t|}|j}|r*ttjj|dn ttjt t |t |\}}t |}tt|tdS)N) contract_name)rrFrRr!r"r ZATTACH_SUCCESS_TMPLr;ZATTACH_SUCCESS_NO_CONTRACT_NAMErstopZcleanuprr Zformat_tabularrZhandle_unicode_charactersZprocess_events)r2rHrRZ status_dictZ_retoutputrrrpost_cli_attachs     rU)r2scopeset_key set_valuerQcCsR|tjkr|j}|j}n|tjkr.|j}|j}d|kr<|}n|}t|||ddS)zS Handles setting part the apt proxies - global and uaclient scoped proxies Zhttps) http_proxy https_proxyZ proxy_scopeN)rZGLOBALZglobal_apt_http_proxyZglobal_apt_https_proxyZUACLIENTZua_apt_http_proxyZua_apt_https_proxyr)r2rVrWrXrYrZrrrconfigure_apt_proxys  r[)N)N)( functoolsrtypingrZuaclientrrrrr r r r r rZ(uaclient.api.u.pro.status.is_attached.v1rZ uaclient.aptrrZuaclient.configrZuaclient.filesrZget_event_loggerr!ZAbstractProgressrr-r'r/r9r:r<rDrIrPrUr)r[rrrrs.  0