a )gu%@sddlmZmZmZeZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z ddlmZddlmZmZddlmZmZddlmZe dZGd d d eZdS) )absolute_importdivisionprint_functionN) AnsibleError) to_native) text_type string_types)MappingSequence) 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__B/usr/lib/python3.9/site-packages/ansible/plugins/shell/__init__.pyr%szShellBase.__init__cCsHdd|dD}tdd|Ds8tdt||d|dS)NcSsg|]}|dqS)/)rstrip.0drrr 1z7ShellBase._normalize_system_tmpdirs..system_tmpdirscss|]}tj|VqdSr )ospathisabsrrrr 5rz6ShellBase._normalize_system_tmpdirs..z`The configured system_tmpdirs contains a relative path: {0}. All system_tmpdirs must be absolute) get_optionallrformatrZ set_option)rZnormalized_pathsrrr_normalize_system_tmpdirs-s z#ShellBase._normalize_system_tmpdirsNcstt|j|||d|d}t|tr2tdt|tsB|g}|D]*}t|tsdtdt ||j |qFz | Wnt yYn0dS)N) task_keys var_optionsdirectZ environmentzUThe "envirionment" keyword takes a list of dictionaries or a dictionary, not a stringzfThe "envirionment" keyword takes a list of dictionaries (or single dictionary), but got a "%s" instead)rr set_optionsr% isinstancerrr r typerupdater(KeyError)rr)r*r+rZenv_dictrrrr,;s      zShellBase.set_optionscCsdtttddfS)Nzansible-tmp-%s-%s-%srl)timer!getpidrandomZrandintrrrr_generate_temp_dir_nameSsz!ShellBase._generate_temp_dir_namecKsddd|DS)N cSs&g|]\}}d|tt|fqS)z%s=%s)shlexquoter)rkvrrrrXrz(ShellBase.env_prefix..)joinitems)rkwargsrrr env_prefixWszShellBase.env_prefixcGs tjj|Sr )r!r"r:)rargsrrr join_pathZszShellBase.join_pathcCstj|}|Sr )r!r"basenamestrip)rpathname base_namerrrget_remote_filename^szShellBase.get_remote_filenamecCs |dS)Nr)endswith)rr"rrrpath_has_trailing_slashbsz!ShellBase.path_has_trailing_slashcCs*d|g}||dd|D}d|S)NchmodcSsg|]}t|qSrr6r7rcrrrrhrz#ShellBase.chmod..r5extendr:)rpathsmodecmdrrrrGes zShellBase.chmodcCs*d|g}||dd|D}d|S)NchowncSsg|]}t|qSrrHrIrrrrorz#ShellBase.chown..r5rK)rrMuserrOrrrrPls zShellBase.chowncCs*d|g}||dd|D}d|S)NchgrpcSsg|]}t|qSrrHrIrrrrvrz#ShellBase.chgrp..r5rK)rrMgrouprOrrrrRss zShellBase.chgrpcCs4ddd||fg}||dd|D}d|S)z5Only sets acls for users as that's really all we needZsetfaclz-mzu:%s:%scSsg|]}t|qSrrHrIrrrr~rz+ShellBase.set_user_facl..r5rK)rrMrQrNrOrrr set_user_faclzs zShellBase.set_user_faclFcCs,t|}d}|r|d7}|d||jfS)Nzrm -f z-r z%s %s)r6r7Z_SHELL_REDIRECT_ALLNULL)rr"ZrecurserOrrrremoves  zShellBase.removecCsddt|g}d|S)Ntestz-er5)r6r7r:)rr"rOrrrexistsszShellBase.existsc Cs|s|j}|rD|r |d}||dvr4|}q\|dd}n|durX|d}n|}|||}d|j||jf}|d|j|j||jf7}|d|j||j||jf7}|rd|@}d |j||j||j f}|S) Nrr rZ 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) rr4rr%r?_SHELL_SUB_LEFT_SHELL_SUB_RIGHT _SHELL_ANDZ_SHELL_GROUP_LEFTZ_SHELL_GROUP_RIGHT) rZbasefilesystemrNrZ basetmpdirZbasetmprOZ tmp_umaskrrrmkdtemps&     zShellBase.mkdtempcCs2|dkrt|s*t|}n |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_REmatchr6r7)rZuser_home_pathusernamerrr expand_users   zShellBase.expand_usercCsd|j|jfS)z-Return the working directory after connectingz echo %spwd%s)rYrZrrrrpwdsz ShellBase.pwdcCsh|dkrt|}g}|r0|dd}nd}||||g|durZ||d|}|S)Nr^z#!r5)rAr6r7replacerLappendr:)rZ env_stringZshebangrOZarg_pathZ cmd_partsZnew_cmdrrrbuild_module_commands    zShellBase.build_module_commandcCs|jr|d|j|f7}|S)z6Append an additional command if supported by the shellz %s %s)r[)rrOZ 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 linerHrirrrr7szShellBase.quote)NNN)F)NFrXN)r^)N)__name__ __module__ __qualname__rr(r, staticmethodr4r=r?rDrFrGrPrRrTrUrWr]rcrdrgrhrjr7 __classcell__rrrrr $s,    *  r )Z __future__rrrr.Z __metaclass__r!Zos.pathr3rer6r1Zansible.errorsrZansible.module_utils._textrZansible.module_utils.sixrrZ/ansible.module_utils.common._collections_compatr r Zansible.pluginsr compiler`r rrrrs