o x[h1 @s dZddlZddlZddlZddlZddlmZddlmZddl m Z m Z m Z ddl mZmZeeZeddd gZd d ZGd d d eZde e ee effddZdddddddddd de eee ee efdefddZd ddZd de efddZddZd!ddZdS)"z9Common utility functions for interacting with subprocess.N)ENOEXEC) TextIOWrapper)ListOptionalUnion) performancesignal_handler SubpResultstdoutstderrcCsg}g}g}|D]A}t|tr)|ddur|dd}n(|d|kr(|d|nt|tr<||ds;||n|t|q||q|rWtd||d||ret 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) base_commandcommandswarningsrfixed_commandscommandr!0/usr/lib/python3/dist-packages/cloudinit/subp.pyprepend_base_commands@        r#c@sZeZdZdZdZ       d ddZddZ dd eee fd eee ffd 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|tkrd|_nd|_t|tr |n|j|_|s1|dur-|jn||_n|||_|sD|dur@|jn||_ n|||_ |pN|j|_ |rU||_ |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-messager!r!r"r1Qs4           zProcessExecutionError.__init__cCst|tr |S|S)z1 if data is bytes object, decode rbytesdecode)r2textr!r!r"r/sz$ProcessExecutionError._ensure_stringr7returncCs>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 rr   )rr5rstripreplace)r2r7 indent_levelr!r!r"r,s z"ProcessExecutionError._indent_text)NNNNNNN)r8) __name__ __module__ __qualname__r.r*r1r/rrr5r,r!r!r!r"r$Fs& /  r$argscCs@|D]}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)rr5hasattrrrr$)rB componentr!r!r"raise_on_invalid_commands  rFTFr=) datarcscaptureshell logstringr6 update_envcwdtimeoutr9c  s|durdg}tj} |r| |td|r|n||||d} d} |r,tj} tj} |dur4tj} n tj} t |t s@| }t |t rH|}nt |t rS| d}n t |dd|D}z3td|rg|n|tj|| | | | ||d}|j|| d \}}Wdn1swYWn"ty}zt|||jrd nd rd d |d d |d}~wwrdfd 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|dqSrO)rr5rC).0xr!r!r" szsubp..z Running {})r r stdinenvrJrM)rNr%-)r'r)r-r r cst|tr ||S|SNr4)rGmr6r!r"ldecodeszsubp..ldecode)r r r(r'rP)osenvironcopyupdaterdebug subprocessPIPEDEVNULLrr5rCrrFrTimedrPopen communicateOSErrorr$r- returncoder )rBrGrHrIrJrKr6rLrMrNrUr r rT bytes_argsspouterrerZrcr!rYr"subps2            rncCs|dvrd}nt|tstd|tj|}|dr$|dd}|s(|St|rB|ddkrB|dd}t|rB|ddks2tj||S)N)N/zUnexpected input for target: z//r r) rr ValueErrorr[pathabspathrlenr)targetrrr!r!r" target_path)s     rvcCst|}tjj|vrtt||r|S|dur4ddtjddtjD}|dkr-|ndd|D}dd|D}|D]}tjj ||f}tt||rS|Sq=dS)NcSsg|]}|dqS)")striprQpr!r!r"rSHs zwhich..PATHrorpcSsg|] }|dr|qS)rp)rryr!r!r"rSLscSsg|]}tj|qSr!)r[rrrsryr!r!r"rSOs) rvr[rrsepis_exer\getsplitpathsepr)programsearchrupathsrrppathr!r!r"which>s rcCstj|o t|tjSrW)r[rrisfileaccessX_OK)fpathr!r!r"r}Ysr}c Csd|r tj|s dSg}g}|durg}nt|tr t|g}n t|tr(|}ntdtt|D]a}tj ||}t |r| |zt t||gddWdn1s^wYWq3ty}zt|| |WYd}~q3d}~wwtj|rtd|q3td|q3|r|rtdt|dd |d t|d dSdS) Nz%exe_prefix must be None, str, or listF)rIzjskipping %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)r[rrisdirrrrrsortedlistdirrr}rr suspend_crashrnr$rr_rr RuntimeErrorrt) dirp skip_no_exist exe_prefixfailed attemptedprefixexe_nameexe_pathrlr!r!r"runparts^sL       r)NN)TN) __doc__ collectionsloggingr[r`r-riortypingrrr cloudinitrr getLoggerr?r namedtupler r#r0r$rr5rFrnrvrr}rr!r!r!r"sB   3N