o nnhF$@sddlmZddlZddlZddlZddlZddlZddlZddlm Z m Z ddl m Z ddl mZddlmZmZddlmZedZGd d d eZdS) ) annotationsN)MappingSequence) AnsibleError) to_native) text_type string_types) AnsiblePluginz^~[_.A-Za-z0-9][-_.A-Za-z0-9]*$cseZdZfddZddZd/fdd Zedd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZd0ddZddZd1d d!Zd2d#d$Zd%d&Zd3d'd(Zd)d*Zd+d,Zd-d.ZZS)4 ShellBasecs$tt|i|_d|_d|_dSN)superr __init__envtmpdir executableself __class__I/usr/local/lib/python3.10/dist-packages/ansible/plugins/shell/__init__.pyr %s zShellBase.__init__cCsHdd|dD}tdd|Dstdt||d|dS)NcSsg|]}|dqS)/)rstrip.0drrr 1sz7ShellBase._normalize_system_tmpdirs..system_tmpdirscss|] }tj|VqdSr )ospathisabsrrrr 5sz6ShellBase._normalize_system_tmpdirs..z`The configured system_tmpdirs contains a relative path: {0}. All system_tmpdirs must be absolute) get_optionallrformatr set_option)rnormalized_pathsrrr_normalize_system_tmpdirs-s  z#ShellBase._normalize_system_tmpdirsNcstt|j|||d|d}t|trtdt|ts!|g}|D]}t|ts2tdt ||j |q#z| WdSt yJYdSw)N) task_keys var_optionsdirect environmentzTThe "environment" keyword takes a list of dictionaries or a dictionary, not a stringzeThe "environment" keyword takes a list of dictionaries (or single dictionary), but got a "%s" instead)r r set_optionsr" isinstancerrrrtyperupdater'KeyError)rr(r)r*renv_dictrrrr,;s     zShellBase.set_optionscCsdtttddfS)Nzansible-tmp-%s-%s-%srl)timergetpidrandomrandintrrrr_generate_temp_dir_nameSsz!ShellBase._generate_temp_dir_namec sdfdd|DS)N cs&g|]\}}d|t|fqS)z%s=%s)quoter)rkvrrrrXs&z(ShellBase.env_prefix..)joinitems)rkwargsrrr env_prefixWszShellBase.env_prefixcGs tjj|Sr )rrr;)rargsrrr join_pathZs zShellBase.join_pathcCstj|}|Sr )rrbasenamestrip)rpathname base_namerrrget_remote_filename^szShellBase.get_remote_filenamecCs |dS)Nr)endswith)rrrrrpath_has_trailing_slashbs z!ShellBase.path_has_trailing_slashcCd|g}||t|S)Nchmodextendshlexr;)rpathsmodecmdrrrrIe  zShellBase.chmodcCrH)NchownrJ)rrMuserrOrrrrQjrPzShellBase.chowncCrH)NchgrprJ)rrMgrouprOrrrrSorPzShellBase.chgrpcCs&ddd||fg}||t|S)z5Only sets acls for users as that's really all we needsetfaclz-mzu:%s:%srJ)rrMrRrNrOrrr set_user_faclts  zShellBase.set_user_faclFcCs,||}d}|r |d7}|d||jfS)Nzrm -f z-r z%s %s)r8_SHELL_REDIRECT_ALLNULL)rrrecurserOrrrremovezs zShellBase.removecCsdd||g}d|S)Ntestz-er7)r8r;)rrrOrrrexistss zShellBase.existsc Cs|s|j}|r"|r|d}||dvr|}n|dd}n |dur,|d}n|}|||}d|j||jf}|d|j|j||jf7}|d|j||j||jf7}|rkd|@}d |j||j||j f}|S) Nrrr remote_tmpzmkdir -p %s echo %s %sz%s mkdir %s echo %s %sz %s echo %s=%s echo %s %siz%s umask %o %s %s %s) rr6rr"r@_SHELL_SUB_LEFT_SHELL_SUB_RIGHT _SHELL_AND_SHELL_GROUP_LEFT_SHELL_GROUP_RIGHT) rbasefilesystemrNr basetmpdirbasetmprO tmp_umaskrrrmkdtemps&      zShellBase.mkdtempcCs8|dkrt|s||}d|S|r||7}d|S)a Return a command to expand tildes in a path It can be either "~" or "~username". We just ignore $HOME We use the POSIX definition of a username: http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap03.html#tag_03_426 http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap03.html#tag_03_276 Falls back to 'current working directory' as we assume 'home is where the remote user ends up' ~zecho %s)_USER_HOME_PATH_REmatchr8)ruser_home_pathusernamerrr expand_users  zShellBase.expand_usercCsd|j|jfS)z-Return the working directory after connectingz echo %spwd%s)r^r_rrrrpwdsz ShellBase.pwdcs^|}|r |d7}|durd}|d||g}tfdd|D}d||fS)Nr7riz#!c3s$|] }|r|rVqdSr )rB)r raw_cmd_partstripped_cmd_partrrr!s  z1ShellBase.build_module_command..)rB removeprefixrLr;)r env_stringshebangrOarg_path cmd_partscleaned_up_cmdrrrrbuild_module_commands zShellBase.build_module_commandcCs|jr |d|j|f7}|S)z6Append an additional command if supported by the shellz %s %s)r`)rrO cmd_to_appendrrrappend_commandszShellBase.append_commandcCs|S)z_wrap script execution with any necessary decoration (eg '&' for quoted powershell script paths)rrrOrrr wrap_for_execszShellBase.wrap_for_execcCs t|S)z[Returns a shell-escaped string that can be safely used as one token in a shell command line)rLr8r}rrrr8s zShellBase.quote)NNN)F)NFr\N)rir )__name__ __module__ __qualname__r r'r, staticmethodr6r>r@rErGrIrQrSrVrYr[rhrorprzr|r~r8 __classcell__rrrrr $s.     * r ) __future__rros.pathr4rerLr2collections.abcrransible.errorsr+ansible.module_utils.common.text.convertersransible.module_utils.sixrransible.pluginsr compilerkr rrrrs