a 'Dgp1 @sdZddlZddlZddlZddlZddlmZddlmZddl m Z m Z m Z ddl mZeeZeddd gZd d ZGd d d eZe e ee efdddZdddddddddd e eee ee efedddZd!ddZd"e edddZddZd#dd ZdS)$z9Common utility functions for interacting with subprocess.N)ENOEXEC) TextIOWrapper)ListOptionalUnion) performance SubpResultstdoutstderrcCsg}g}g}|D]}t|trR|ddur8|dd}q|d|kr|d|n6t|trx||ds||n|t|q||q|rtd||d||rt dj |d|d|S) aEnsure user-provided commands start with base_command; warn otherwise. Each command is either a list or string. Perform the following: - If the command is a list, pop the first element if it is None - If the command is a list, insert base_command as the first element if not present. - When the command is a string not starting with 'base-command', warn. Allow flexibility to provide non-base-command environment/config setup if needed. @commands: List of commands. Each command element is a list or string. @return: List of 'fixed up' commands. @raise: TypeError on invalid config item type. rN z Non-%s commands in %s config: %s zHInvalid {name} config. These commands are not a string or list: {errors})nameerrors) isinstancelistinsertstr startswithappendLOGwarningjoin TypeErrorformat)Z base_commandZcommandswarningsrZfixed_commandsZcommandr2/usr/lib/python3.9/site-packages/cloudinit/subp.pyprepend_base_commands:       rc@sHeZdZdZdZd ddZddZd eee feee fd d d Z dS)ProcessExecutionErrorzr%(description)s Command: %(cmd)s Exit code: %(exit_code)s Reason: %(reason)s Stdout: %(stdout)s Stderr: %(stderr)s-Nc Cs|p|j|_|r||_n|s,|tkr,d|_nd|_t|tr@|n|j|_|sb|durZ|jn||_n |||_|s|dur|jn||_ n |||_ |p|j|_ |r||_ |j | |j| |j| |j| |j | |j| |j d}t||dS)Nz(Exec format error. Missing #! in script?z'Unexpected error while running command.) descriptioncmd exit_coder r reason) empty_attrr"r!rrintr#r _indent_textr r$errno MESSAGE_TMPL_ensure_stringIOError__init__) selfr r r#r"r!r$r(messagerrrr,Qs4           zProcessExecutionError.__init__cCst|tr|S|S)z1 if data is bytes object, decode rbytesdecode)r-textrrrr*sz$ProcessExecutionError._ensure_string)r2returncCs>t|ts$|dddd|S|dddd|S)z indent text on all but the first line, allowing for easy to read output remove any newlines at end of text first to prevent unneeded blank line in output r r   )rr0rstripreplace)r-r2Z indent_levelrrrr's z"ProcessExecutionError._indent_text)NNNNNNN)r3) __name__ __module__ __qualname__r)r%r,r*rrr0r'rrrrrFs" /  r)argscCs@|D]6}t|tst|dstd|t|d|dqdS)zcheck argument types to ensure that subp() can run the argument Throw a user-friendly exception which explains the issue. args: list of arguments passed to subp() raises: ProcessExecutionError with information explaining the issue encodezRunning invalid command: %szRunning invalid command: )r"r$N)rr0hasattrrrr)r<Z componentrrrraise_on_invalid_commands   r?TFr8) datarcscaptureshell logstringr1 update_envcwdtimeout)r<r4c  s|durdg}tj} |r&| |td|r4|n||||d} d} |rXtj} tj} |durhtj} ntj} t |t s| }t |t r|}n,t |t r| d}nt |dd|D}zhtd|r|n|:tj|| | | | ||d}|j|| d \}}Wdn1s0YWnPtyt}z6t|||jrJd nd rVd nd d |WYd}~n d}~00rdfd d }||}||}|j}||vrt||||dt||S)aRun a subprocess. :param args: command to run in a list. [cmd, arg1, arg2...] :param data: input to the command, made available on its stdin. :param rcs: a list of allowed return codes. If subprocess exits with a value not in this list, a ProcessExecutionError will be raised. By default, data is returned as a string. See 'decode' parameter. :param capture: boolean indicating if output should be captured. If True, then stderr and stdout will be returned. If False, they will not be redirected. :param shell: boolean indicating if this should be run with a shell. :param logstring: the command will be logged to DEBUG. If it contains info that should not be logged, then logstring will be logged instead. :param decode: if False, no decoding will be done and returned stdout and stderr will be bytes. Other allowed values are 'strict', 'ignore', and 'replace'. These values are passed through to bytes().decode() as the 'errors' parameter. There is no support for decoding to other than utf-8. :param update_env: update the environment for this command with this dictionary. this will not affect the current processes os.environ. :param cwd: change the working directory to cwd before executing the command. :param timeout: maximum time for the subprocess to run, passed directly to the timeout parameter of Popen.communicate() :return if not capturing, return is (None, None) if capturing, stdout and stderr are returned. if decode: entries in tuple will be string if not decode: entries in tuple will be bytes NrzFRunning command %s with allowed return codes %s (shell=%s, capture=%s)utf-8cSs$g|]}t|tr|n|dqS)rH)rr0r=).0xrrr szsubp..z Running {})r r stdinenvrCrF)rGr -)r"r$r(r r cst|tr||S|SNr/)r@mr1rrldecodeszsubp..ldecode)r r r#r")rH)osenvironcopyupdaterdebug subprocessPIPEZDEVNULLrr0r=rr?rZTimedrPopenZ communicateOSErrorrr( returncoder)r<r@rArBrCrDr1rErFrGrMr r rLZ bytes_argsspouterrerRrcrrQrsubps2         6   rbcCs|dvrd}n.PATHrcrdcSsg|]}|dr|qS)rd)rrmrrrrKLcSsg|]}tj|qSr)rSrfrgrmrrrrKOrp) rjrSrfsepis_exerTgetsplitpathsepr)ZprogramsearchripathsrfZppathrrrwhich>s rxcCstj|ot|tjSrO)rSrfisfileaccessX_OK)ZfpathrrrrrYsrrc Cs>|rtj|sdSg}g}|dur*g}n.t|tr@t|g}nt|trP|}ntdtt|D]}tj ||}t |r| |zt ||gddWn8t y}z t|| |WYd}~n d}~00qftj|rtd|qftd|qf|r:|r:tdt|dd |d t|d dS) Nz%exe_prefix must be None, str, or listF)rBzjskipping %s as its not executable or the underlying file system is mounted without executable permissions.zNot executing special file [%s]z Runparts: z failures (,z) in z attempted commands)rSrfisdirrrrrsortedlistdirrrrrrbrrrWryr RuntimeErrorrh) ZdirpZ skip_no_existZ exe_prefixZfailedZ attemptedprefixZexe_nameZexe_pathr`rrrrunparts^s@     "  r)NN)NN)TN)__doc__ collectionsZloggingrSrXr(riortypingrrrZ cloudinitrZ getLoggerr9r namedtuplerrr+rrr0r?rbrjrxrrrrrrrs>    3N