B /g^ޣc@sdZddlZddlZddlZddlZddlZddlZyddlZ[dZWne k r^dZYnXyddl Z [ dZ Wne k rdZ YnXyddl Z [ dZ Wne k rdZ YnXyddlmZWne k rdZYnXyddlmZWne k r dZYnXddd d d d d ddddddddddddddddddgZGd ddeZGd!ddeZGd"ddeZGd#ddeZGd$d%d%eZGd&d'd'eZdd)dZd*d+Zdd,d-dZdd,d.d Zeed/rdd,d0d1Z nd2d1Z dd,d3d Z!dd,d4d Z"dd,d5d Z#d6dZ$dde#dfd7d Z%d8d9Z&d:d;Z'ej(ejej)ej*hej+ko~ej,ej-ko~ejej.kZ/ddZ2e#fd?dZ3d@dAZ4dBdCZ5dDdEZ6ddGdHZ7ddIdJZ8dKe7dLgdMfiZ9ere7dNgdOfe9dP<e8gdQfe9dR<e re7dSgdTfe9dU<e r0e7dVgdWfe9dX<dYdZ:dd[dZ;d\dZd_d`Z?ddadZ@dbdZAdcddZBdedfZCdgdhZDdigeDgdMfdjgeCgdQfdkZEerdldmgeDgdOfeEdP<e rdndogeDgdTfeEdU<e rdpdqgeDgdWfeEdX<drdsZFddtdZGeedurPeHdveIdwdxZJdyeJjK_dzeJjL_d{eJjM_d|dvZNn2ejOd}krddlPZPeHdveIdwdxZJd~dvZNdddZQdddZRejSejTBdfddZUdS)zUtility functions for copying and archiving files and directory trees. XXX The functions here don't copy the resource fork or other metadata on Mac. NTF)getpwnam)getgrnam copyfileobjcopyfilecopymodecopystatcopycopy2copytreemovermtreeErrorSpecialFileError ExecError make_archiveget_archive_formatsregister_archive_formatunregister_archive_formatget_unpack_formatsregister_unpack_formatunregister_unpack_formatunpack_archiveignore_patternschownwhichget_terminal_size SameFileErrorc@s eZdZdS)r N)__name__ __module__ __qualname__rr/usr/lib64/python3.7/shutil.pyr 7c@eZdZdZdS)rz5Raised when source and destination are the same file.Nrrr__doc__rrrr r:r!c@r")r z|Raised when trying to do a kind of operation (e.g. copying) which is not supported on a special file (e.g. a named pipe)Nr#rrrr r =c@r")rz+Raised when a command could not be executedNr#rrrr rAr!c@r") ReadErrorz%Raised when an archive cannot be readNr#rrrr r&Dr!r&c@r") RegistryErrorzVRaised when a registry operation with the archiving and unpacking registries failsNr#rrrr r'Gr%r'@cCs$x||}|sP||qWdS)z=copy data from file-like object fsrc to file-like object fdstN)readwrite)fsrcfdstZlengthZbufrrr rLs  cCsXttjdr0ytj||Stk r.dSXtjtj|tjtj|kS)NsamefileF)hasattrospathr-OSErrornormcaseabspathsrcdstrrr _samefileTs r7follow_symlinksc Cst||rtd||xL||gD]@}yt|}Wntk rJYq$Xt|jr$td|q$W|stj |rt t ||n6t |d&}t |d}t||WdQRXWdQRX|S)zCopy data from src to dst. If follow_symlinks is not set and src is a symbolic link, a new symlink will be created instead of copying the file it points to. z{!r} and {!r} are the same filez`%s` is a named pipeZrbwbN)r7rformatr/statr1ZS_ISFIFOst_moder r0islinksymlinkreadlinkopenr)r5r6r9fnstr+r,rrr r`s    cCsx|sr.lstatrDr<rES_IMODEr=)r5r6r9Z stat_funcZ chmod_funcrCrrr r}s   listxattrc Csytj||d}Wn:tk rL}z|jtjtjtjfkr<dSd}~XYnXxr|D]j}y&tj|||d}tj||||dWqTtk r}z |jtj tjtjtjfkrWdd}~XYqTXqTWdS)zCopy extended filesystem attributes from `src` to `dst`. Overwrite existing attributes. If `follow_symlinks` is false, symlinks won't be followed. r8N) r/rHr1errnoENOTSUPZENODATAZEINVALZgetxattrZsetxattrZEPERM)r5r6r9namesZenameZvaluerrr _copyxattrs   rMcOdSNr)argskwargsrrr rMc s6ddddd|p,tj|o*tj| }|r@fdd}n fdd}|d||d }t|j}|d ||j|jf|dt|||d y|d |||d Wnt k rYnXt |d r2y|d ||j |d WnPt k r0}z0x*dD] }t t |r|j tt |krPqWWdd}~XYnXdS)aCopy file metadata Copy the permission bits, last access time, last modification time, and flags from `src` to `dst`. On Linux, copystat() also copies the "extended attributes" where possible. The file contents, owner, and group are unaffected. `src` and `dst` are path names given as strings. If the optional flag `follow_symlinks` is not set, symlinks aren't followed if and only if both `src` and `dst` are symlinks. N)nsr9cWrNrOr)rSr9rPrrr _noprRzcopystat.._nopcs tt|SrO)getattrr/rLrTrr lookuprRzcopystat..lookupcstt|}|tjkr|SSrO)rUr/supports_follow_symlinks)rLrBrWrr rXs  r<r8ZutimerEst_flagsZchflags)Z EOPNOTSUPPrJ)r/r0r>r<rGr=Z st_atime_nsZ st_mtime_nsrMZNotImplementedErrorr.rZr1rIrU) r5r6r9ZfollowrXrCmodewhyerrrrWr rs,      cCBtj|r"tj|tj|}t|||dt|||d|S)a3Copy data and mode bits ("cp src dst"). Return the file's destination. The destination may be a directory. If follow_symlinks is false, symlinks won't be followed. This resembles GNU's "cp -P src dst". If source and destination are the same file, a SameFileError will be raised. r8)r/r0isdirjoinbasenamerrr5r6r9rrr r cCr^)a1Copy data and metadata. Return the file's destination. Metadata is copied with copystat(). Please see the copystat function for more information. The destination may be a directory. If follow_symlinks is false, symlinks won't be followed. This resembles GNU's "cp -P src dst". r8)r/r0r_r`rarrrbrrr rrccsfdd}|S)zFunction that can be used as copytree() ignore parameter. Patterns is a sequence of glob-style patterns that are used to exclude filescs,g}xD]}|t||q Wt|SrO)extendfnmatchfilterset)r0rK ignored_namesZpatternpatternsrr _ignore_patternss z)ignore_patterns.._ignore_patternsr)rjrkrrir rs c Cst|}|dk r|||}nt}t|g}x@|D]6} | |krJq:tj|| } tj|| } ytj| rt| } |rt| | t | | | dn:tj | s|rw:tj | rt | | |||n || | n(tj | rt | | |||n || | Wq:t k r:} z|| jdWdd} ~ XYq:tk rp}z|| | t|fWdd}~XYq:Xq:Wyt ||WnJtk r}z*t|dddkr|||t|fWdd}~XYnX|rt ||S)aRecursively copy a directory tree. The destination directory must not already exist. If exception(s) occur, an Error is raised with a list of reasons. If the optional symlinks flag is true, symbolic links in the source tree result in symbolic links in the destination tree; if it is false, the contents of the files pointed to by symbolic links are copied. If the file pointed by the symlink doesn't exist, an exception will be added in the list of errors raised in an Error exception at the end of the copy process. You can set the optional ignore_dangling_symlinks flag to true if you want to silence this exception. Notice that this has no effect on platforms that don't support os.symlink. The optional ignore argument is a callable. If given, it is called with the `src` parameter, which is the directory being visited by copytree(), and `names` which is the list of `src` contents, as returned by os.listdir(): callable(src, names) -> ignored_names Since copytree() is called recursively, the callable will be called once for each directory that is copied. It returns a list of names relative to the `src` directory that should not be copied. The optional copy_function argument is a callable that will be used to copy each file. It will be called with the source path and the destination path as arguments. By default, copy2() is used, but any function that supports the same signature (like copy()) can be used. Nr8rZwinerror)r/Zlistdirrgmakedirsr0r`r>r@r?rexistsr_r r rdrPr1appendstrrU)r5r6symlinksZignore copy_functionZignore_dangling_symlinksrKrhZerrorsrLZsrcnameZdstnamelinktor]r\rrr r sL$           *&c CsPy"t|}t|}WdQRXWn*tk rL|tj|tg}YnXx|D]}|j}y|jdd}Wntk rd}YnX|ry|rtdWn*tk r|tjj |twTYnXt ||qTyt |WqTtk r|tj |tYqTXqTWyt |Wn(tk rJ|tj |tYnXdS)NFr8%Cannot call rmtree on a symbolic link) r/scandirlistr1sysexc_infor0is_dirZ is_symlinkr>_rmtree_unsafeunlinkrmdir)r0onerror scandir_itentriesentryfullnamerxrrr ryts8      ryc Csy"t|}t|}WdQRXWn:tk r\}z||_|tj|tdSd}~XYnXx|D]}tj||j }y,|j dd}|r|j dd} t | j }Wntk rd}YnX|rytj|j tj|d} Wn(tk r|tj|tYnXztj| t| rnt| ||ytj|j |dWn(tk rj|tj|tYnXn8y tdWn*tk r|tjj|tYnXWdt| Xqfytj|j |dWqftk r|tj|tYqfXqfWdS)NFr8)Zdir_fdrs)r/rtrur1filenamervrwr0r`rLrxr<ZS_ISDIRr=rAO_RDONLYsamestatfstat_rmtree_safe_fdr{r>closerz) Ztopfdr0r|r}r~r]rrrxorig_stZdirfdrrr rsJ      rc Cs|rdd}n|dkrdd}trPt|tr8t|}yt|}Wn&tk rl|tj|tdSXyt |tj }Wn&tk r|tj|tdSXztj |t |rt|||yt|Wn(tk r|tj|tYnXn8y tdWn*tk r<|tj j|tYnXWdt|XnPytj |rhtdWn*tk r|tj j|tdSXt||SdS)aRecursively delete a directory tree. If ignore_errors is set, errors are ignored; otherwise, if onerror is set, it is called to handle the error with arguments (func, path, exc_info) where func is platform and implementation dependent; path is the argument to that function that caused it to fail; and exc_info is a tuple returned by sys.exc_info(). If ignore_errors is false and onerror is None, an exception is raised. cWrNrOrrPrrr r|rRzrmtree..onerrorNcWsdSrOrrrrr r|rRrs)_use_fd_functions isinstanceZbytesr/ZfsdecoderF ExceptionrvrwrArr0rrrr{r1r>rry)r0Z ignore_errorsr|rZfdrrr r sH       cCs&tjjtjjpd}tj||SN)r/r0sepZaltsepraZrstrip)r0rrrr _basename src Cs|}tj|rTt||r*t||dStj|t|}tj|rTtd|yt||Wnt k rtj |rt |}t ||t |nTtj|rt||rtd||ft|||ddt|n|||t |YnX|S)a+Recursively move a file or directory to another location. This is similar to the Unix "mv" command. Return the file or directory's destination. If the destination is a directory or a symlink to a directory, the source is moved inside the directory. The destination path must not already exist. If the destination already exists but is not a directory, it may be overwritten depending on os.rename() semantics. If the destination is on our current filesystem, then rename() is used. Otherwise, src is copied to the destination and then removed. Symlinks are recreated under the new name if os.rename() fails because of cross filesystem renames. The optional `copy_function` argument is a callable that will be used to copy the source or it will be delegated to `copytree`. By default, copy2() is used, but any function that supports the same signature (like copy()) can be used. A lot more could be done here... A look at a mv.c shows a lot of the issues this implementation glosses over. Nz$Destination path '%s' already existsz.Cannot move a directory '%s' into itself '%s'.T)rqrp)r/r0r_r7Zrenamer`rrmr r1r>r@r?rz _destinsrcr r )r5r6rqZreal_dstrrrrr r s2              cCsVtj|}tj|}|tjjs2|tjj7}|tjjsL|tjj7}||SrO)r/r0r3endswithr startswithr4rrr rHs    rcCNtdks|dkrdSy t|}Wntk r8d}YnX|dk rJ|dSdS)z"Returns a gid, given a group name.N)rKeyErrorrLZresultrrr _get_gidQ  rcCr)z"Returns an uid, given a user name.Nr)rrrrrr _get_uid]rrgzipcs |dkrd}nDtr |dkr d}n2tr2|dkr2d}n trD|dkrDd}ntd|d dl} |rfd |nd} |d | } tj| } | rtj | s|dk r| d | |st | |dk r| d t t fdd} |s| | d|}z|j|| dWd|X| S)aCreate a (possibly compressed) tar file from all the files under 'base_dir'. 'compress' must be "gzip" (the default), "bzip2", "xz", or None. 'owner' and 'group' can be used to define an owner and a group for the archive that is being built. If not provided, the current owner and group will be used. The output tar file will be named 'base_name' + ".tar", possibly plus the appropriate compression extension (".gz", ".bz2", or ".xz"). Returns the output filename. NrrZgzbzip2bz2xzzCbad value for 'compress', or compression format not supported : {0}rZ..tar creating %szCreating tar archivecs,dk r|_|_dk r(|_|_|SrO)gidZgnameuidZuname)Ztarinforgroupownerrrr _set_uid_gidsz#_make_tarball.._set_uid_gidzw|%s)rf)_ZLIB_SUPPORTED_BZ2_SUPPORTED_LZMA_SUPPORTED ValueErrorr;tarfiler/r0dirnamerminforlrrrAaddr) base_namebase_dircompressverbosedry_runrrloggerZtar_compressionrZ compress_extZ archive_name archive_dirrtarrrr _make_tarballis<          rc Cszddl}|d}tj|}|rNtj|sN|dk r@|d||sNt||dk rd|d|||sv|j|d|jd}tj |} | tj kr| | | |dk r|d| xt |D]\} } } xFt | D]:} tj tj| | } | | | |dk r|d| qWxT| D]L} tj tj| | } tj| r| | | |dk r|d| qWqWWdQRX|S) zCreate a zip file from all the files under 'base_dir'. The output zip file will be named 'base_name' + ".zip". Returns the name of the output zip file. rN.ziprz#creating '%s' and adding '%s' to itZw)Z compressionz adding '%s')zipfiler/r0rrmrrlZipFileZ ZIP_DEFLATEDZnormpathcurdirr*ZwalkZsortedr`Zisfile)rrrrrrZ zip_filenamerZzfr0ZdirpathZdirnamesZ filenamesrLrrr _make_zipfiles@             rr)rNzuncompressed tar file)rrzgzip'ed tar-fileZgztarzZIP filezip)rrzbzip2'ed tar-fileZbztar)rrzxz'ed tar-fileZxztarcCddtD}||S)zReturns a list of supported formats for archiving and unarchiving. Each element of the returned sequence is a tuple (name, description) cSsg|]\}}||dfqS)rr).0rLZregistryrrr z'get_archive_formats..)_ARCHIVE_FORMATSitemssortZformatsrrr rs rcCsx|dkr g}t|s td|t|ttfs6tdx.|D]&}t|ttfrZt|dkr|dk r$|d|tj|}|s>t||dkrLtj}||d} y t|} Wn"tk rt d|dYnX| d} x| dD]\} }|| | <qW|dkr|| d<|| d <z| ||f| }Wd|dk r|dk r|d | t| X|S) aCreate an archive file (eg. zip or tar). 'base_name' is the name of the file to create, minus any format-specific extension; 'format' is the archive format: one of "zip", "tar", "gztar", "bztar", or "xztar". Or any other registered format. 'root_dir' is a directory that will be the root directory of the archive; ie. we typically chdir into 'root_dir' before creating the archive. 'base_dir' is the directory where we start archiving from; ie. 'base_dir' will be the common prefix of all files and directories in the archive. 'root_dir' and 'base_dir' both default to the current directory. Returns the name of the archive file. 'owner' and 'group' are used when creating a tar archive. By default, uses the current owner and group. Nzchanging into '%s')rrzunknown archive format '%s'rrrrzchanging back to '%s') r/getcwdZdebugr0r3Zchdirrrrr)rr;Zroot_dirrrrrrrZsave_cwdrQ format_infofuncZargZvalrrrr r s6        cCr)zReturns a list of supported formats for unpacking. Each element of the returned sequence is a tuple (name, extensions, description) cSs"g|]\}}||d|dfqS)rir)rrLrrrr rFrz&get_unpack_formats..)_UNPACK_FORMATSrrrrrr r@s c Csvi}x.tD]"\}}x|dD] }|||<q WqWx,|D]$}||kr:d}t||||fq:Wt|srtddS)z+Checks what gets registered as an unpacker.rz!%s is already registered for "%s"z*The registered function must be a callableN)rrr'rr) extensionsrrZexisting_extensionsrLrext extensionZmsgrrr _check_unpack_optionsKs rcCs,|dkr g}t|||||||ft|<dS)aMRegisters an unpack format. `name` is the name of the format. `extensions` is a list of extensions corresponding to the format. `function` is the callable that will be used to unpack archives. The callable will receive archives to unpack. If it's unable to handle an archive, it needs to raise a ReadError exception. If provided, `extra_args` is a sequence of (name, value) tuples that will be passed as arguments to the callable. description can be provided to describe the format, and will be returned by the get_unpack_formats() function. N)rr)rLrrrrrrr r]s cCs t|=dS)z*Removes the pack format from the registry.N)rrVrrr rsscCs&tj|}tj|s"t|dS)z1Ensure that the parent directory of `path` existsN)r/r0rr_rl)r0rrrr _ensure_directoryws  rc Csddl}||std|||}zx|D]}|j}|ds4d|krRq4tjj |f| d}|spq4t || ds4| |j}t|d}z||Wd|~Xq4WWd|XdS)z+Unpack zip `filename` to `extract_dir` rNz%s is not a zip filez/z..r:)rZ is_zipfiler&rZinfolistrrr/r0r`splitrrr)rAr*r) r extract_dirrrrrLZtargetZdataZfrrr _unpack_zipfile}s*       rcCsXddl}y||}Wn"|jk r8td|YnXz||Wd|XdS)zAUnpack tar/tar.gz/tar.bz2/tar.xz `filename` to `extract_dir` rNz/%s is not a compressed or uncompressed tar file)rrAZTarErrorr&Z extractallr)rrrZtarobjrrr _unpack_tarfilesrrr)rrz.tar.gzz.tgzz.tar.bz2z.tbz2z.tar.xzz.txzcCs:x4tD](\}}x|dD]}||r|SqWq WdS)Nr)rrr)rrLrrrrr _find_unpack_formats   rcCs|dkrt}t|}t|}|dk r~y t|}Wn$tk r\td|dYnX|d}|||ft|dnHt|}|dkrt d|t|d}tt|d}|||f|dS)a Unpack an archive. `filename` is the name of the archive. `extract_dir` is the name of the target directory, where the archive is unpacked. If not provided, the current working directory is used. `format` is the archive format: one of "zip", "tar", "gztar", "bztar", or "xztar". Or any other registered format. If not provided, unpack_archive will use the filename extension and see if an unpacker was registered for that extension. In case none is found, a ValueError is raised. NzUnknown unpack format '{0}'rrzUnknown archive format '{0}') r/rZfspathrrrr;Zdictrr&)rrr;rrrQrrr rs"    statvfs disk_usageZusageztotal used freezTotal space in byteszUsed space in byteszFree space in bytescCs@t|}|j|j}|j|j}|j|j|j}t|||S)zReturn disk usage statistics about the given path. Returned value is a named tuple with attributes 'total', 'used' and 'free', which are the amount of total, used and free space, in bytes. )r/rZf_bavailZf_frsizeZf_blocksZf_bfree_ntuple_diskusage)r0rCfreetotalusedrrr rs    ntcCs"t|\}}||}t|||S)zReturn disk usage statistics about the given path. Returned values is a named tuple with attributes 'total', 'used' and 'free', which are the amount of total, used and free space, in bytes. )rZ _getdiskusager)r0rrrrrr rscCs|dkr|dkrtd|}|}|dkr.d}n(t|trVt|}|dkrVtd||dkrdd}n(t|tst|}|dkrtd|t |||dS)zChange owner user and group of the given path. user and group can be the uid/gid or the user/group names, and in that case, they are converted to their respective uid/gid. Nzuser and/or group must be setizno such user: {!r}zno such group: {!r}) rrrorZ LookupErrorr;intrr/r)r0ZuserrZ_userZ_grouprrr rs"  iPic Csyttjd}Wnttfk r.d}YnXyttjd}Wnttfk r^d}YnX|dksp|dkryttj}Wn$t tt fk rt |}YnX|dkr|j }|dkr|j }t ||fS)aGet the size of the terminal window. For each of the two dimensions, the environment variable, COLUMNS and LINES respectively, is checked. If the variable is defined and the value is a positive integer, it is used. When COLUMNS or LINES is not defined, which is the common case, the terminal connected to sys.__stdout__ is queried by invoking os.get_terminal_size. If the terminal size cannot be successfully queried, either because the system doesn't support querying, or because we are not connected to a terminal, the value given in fallback parameter is used. Fallback defaults to (80, 24) which is the default size used by many terminal emulators. The value returned is a named tuple of type os.terminal_size. ZCOLUMNSrZLINES)rr/environrrrrvZ __stdout__ZfilenoAttributeErrorr1Z terminal_sizecolumnslines)ZfallbackrrZsizerrr r1s$  c s^dd}tjr&||r"SdS|dkrrtjdd}|dkrrytd}Wnttfk rptj}YnX|szdS| tj }t j dkrtj |kr|dtj tjdd  tj }tfd d |Dr܈g}qfd d |D}ng}t}x\|D]T}tj|}||kr||x,|D]$} tj|| } || |r,| Sq,WqWdS)a3Given a command, mode, and a PATH string, return the path which conforms to the given mode on the PATH, or None if there is no such file. `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result of os.environ.get("PATH"), or can be overridden with a custom search path. cSs&tj|o$t||o$tj| SrO)r/r0rmZaccessr_)rBr[rrr _access_checkkszwhich.._access_checkNZPATHZCS_PATHZwin32rZPATHEXTrc3s |]}|VqdSrO)Zlowerrrrcmdrr z szwhich..csg|] }|qSrrrrrr rrzwhich..)r/r0rrZgetZconfstrrrZdefpathrZpathseprvZplatformrZinsertZanyrgr2rr`) rr[r0rZpathextZfilesZseenZdirZnormdirZthefilerLrrr r^sB             )r()FN)rrrNNN)rrNr)NNrrNNNrNNr)r)Vr$r/rvr<reZ collectionsrIZzlibrZ ImportErrorrrZlzmarZpwdrZgrprZ__all__r1r rr rr&rr'rr7rrr.rMrrrrr ryrrArzr{Zsupports_dir_fdrtZ supports_fdrYrr Zavoids_symlink_attacksrr rrrrrrrrrrrrrrrrrrrrrnZ namedtuplerrrrrrLrrrZF_OKZX_OKrrrrr Zs          <  Y#/   = 8    @ -     6  "        (        -