x[h1 ZdZddlZddlZddlZddlZddlmZddlmZddl m Z m Z m Z ddl mZmZej eZej&ddd gZd ZGd d eZd e e ee effdZdddddddddd d e eee ee efdefdZddZdde efdZdZddZy)z9Common utility functions for interacting with subprocess.N)ENOEXEC) TextIOWrapper)ListOptionalUnion) performancesignal_handler SubpResultstdoutstderrcg}g}g}|D]}t|tr&|d|dd}nl|d|k7rd|jd|nQt|tr&|j |ds-|j |n|j t||j ||r't jd||dj||r+tdj|dj||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_commandscommands 0/usr/lib/python3/dist-packages/cloudinit/subp.pyprepend_base_commandr$s "H FN ' gt $qz!!!"+|+q,/  %%%a&89( MM#g, ' g& ' /   IIh    BBH&!$))F*;CIC  cXeZdZdZdZ d dZdZ d deee fdeee ffdZ y) ProcessExecutionErrorzr%(description)s Command: %(cmd)s Exit code: %(exit_code)s Reason: %(reason)s Stdout: %(stdout)s Stderr: %(stderr)s-Nc 4|xs |j|_|r||_n|s|tk(rd|_nd|_t |t r|n |j|_|s| |jn||_n|j||_|s| |jn||_ n|j||_ |xs |j|_ |r||_ |j|j|j|j|j|j|j |j|j|j|j|j|jdz}tj||y)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-r1messages r#r5zProcessExecutionError.__init__QsQ)$// *D u/ID HD $Is3I -3^$//DK++F3DK-3^$//DK++F3DK/ DJ##..t/?/?@&&txx0,,T^^<))$++6))$++6))$++6 '   w'r%cFt|tr|jS|S)z1 if data is bytes object, decode rbytesdecode)r6texts r#r3z$ProcessExecutionError._ensure_strings!+4 7t{{}ATAr%r<returnct|ts'|jdjddd|zzS|jdjddd|zzS)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  )rr:rstripreplace)r6r< indent_levels r#r0z"ProcessExecutionError._indent_textsZ$&;;t$,,T4# :L3LM M{{5!))% 9L1LMMr%)NNNNNNN)) __name__ __module__ __qualname__r2r.r5r3rrr:r0r%r#r'r'Fse J -(^B56 N#u*% N sEz  Nr%r'argsc|D]E}t|trt|dr!tj d|t |d|y)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)rr:hasattrrrr')rI components r#raise_on_invalid_commandrNsO 9e, 80L KK5t <'#.ldecodes"-7e-D4;;q&) N$ Nr%)r r r,r+)rX)osenvironcopyupdaterdebug subprocessPIPEDEVNULLrr:rKrrNrTimedrPopen communicateOSErrorr'r1 returncoder )rIrOrPrQrRrSr;rTrUrVrZr r rY bytes_argsxspouterrer_rcs ` r#subprts(d {c **// C :II " D   F F |""$&;;=D $ D# [[)  &FJ ABAu%A188G+< <        Y D A =!!B~~dG~s  F ww{{g&VW)E"F ~"$**.."<"B"B2::"N AGGCL  s]EE(OqQ\\#=N(O +1 1Qbggooa 1F 1   $1 +fe, -L  )P2sD;%E<E&Ectjj|xr$tj|tjSr])r`ryisfileaccessX_OK)fpaths r#rrYs) 77>>% >RYYubgg%>>r%c |r tjj|syg}g}|g}n;t|tr t |g}nt|t r|}n t dttj|D]}tjj||}t|rA|j| tj5t||gzddddotjj%|rt j'd|t j#d||r9|r6t)dt+|ddj|d t+|d yy#1swYxYw#t$r1}t j#||j|Yd}~:d}~wwxYw) Nz%exe_prefix must be None, str, or listF)rQzjskipping %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`ryisdirrrrrsortedlistdirrrrr suspend_crashrtr'rrdrr RuntimeErrorr{) dirp skip_no_exist exe_prefixfailed attemptedprefixexe_nameexe_pathrrs r#runpartsr^sRWW]]40 FI J $j/" J %?@@2::d+,C77<<h/ (    X & (#113=8*,e<= WW^^H % KK*   II7 B%C()V [&1A0B%9~1 3  v==( ( !  h'' (s0F E>+F >F F  G&F??G)NN)TN) __doc__ collectionsloggingr`rer1riortypingrrr cloudinitrr getLoggerrEr namedtupler r$r4r'rr:rNrtr}rrrrHr%r#rs? ((1g! #[ # #L8X2F G 0fKNGKN\5cDK)?#@(     A UDItE{2 3A A H&* 6? ( r%