3 \\A@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 Zeed0rdd-d1d2Z nd3d2Z dd-d4d Z!dd-d5d Z"dd-d6d Z#d7dZ$dde#dfd8d Z%d9d:Z&d;d<Z'ej(ejej)ej*hej+ko~ej,ej-ko~ejej.kZ/dd=dZ0e/e0_1d>d?Z2e#fd@dZ3dAdBZ4dCdDZ5dEdFZ6ddHdIZ7ddJdKZ8dLe7dgdNfiZ9ere7dgdOfe9dP<e8gdQfe9dR<e re7dgdTfe9dU<e r0e7dgdWfe9dX<dYdZ:dd[dZ;d\dZd_d`Z?ddadZ@dbdZAdcddZBdedfZCdgdhZDdigeDgdNfdjgeCgdQfdkZEerdldmgeDgdOfeEdP<e rdndogeDgdTfeEdU<e rdpdqgeDgdWfeEdX<drdsZFddtdZGeedurPejHdvejIdwdxZJdyeJjK_dzeJjL_d{eJjM_d|dvZNn2ejOd}krddlPZPejHdvejIdwdxZJd~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__r r /usr/lib64/python3.6/shutil.pyr 7sc@seZdZdZdS)rz5Raised when source and destination are the same file.N)rrr__doc__r r r r!r:sc@seZdZdZdS)rz|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)N)rrrr"r r r r!r=sc@seZdZdZdS)rz+Raised when a command could not be executedN)rrrr"r r r r!rAsc@seZdZdZdS) ReadErrorz%Raised when an archive cannot be readN)rrrr"r r r r!r#Dsr#c@seZdZdZdS) RegistryErrorzVRaised when a registry operation with the archiving and unpacking registries failsN)rrrr"r r r r!r$Gsr$icCs$x|j|}|sP|j|qWdS)z=copy data from file-like object fsrc to file-like object fdstN)readwrite)fsrcfdstZlengthZbufr r r!rLs  c CsXttjdr0ytjj||Stk r.dSXtjjtjj|tjjtjj|kS)NsamefileF)hasattrospathr*OSErrornormcaseabspath)srcdstr r r! _samefileTs r3)follow_symlinkscCst||rtdj||xL||gD]@}ytj|}Wntk rJYq$Xtj|jr$td|q$W| rtj j |rtj tj ||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 piperbwbN)r3rformatr,statr.S_ISFIFOst_moderr-islinksymlinkreadlinkopenr)r1r2r4fnstr(r)r r r!r`s    cCsz| r>tjj|r>tjj|r>ttdr8tjtj}}q\dSnttdrXtjtj}}ndS||}||tj|j dS)zCopy mode bits from src to dst. If follow_symlinks is not set, symlinks aren't followed if and only if both `src` and `dst` are symlinks. If `lchmod` isn't available (e.g. Linux) this method does nothing. lchmodNchmod) r,r-r;r+lstatrAr8rBS_IMODEr:)r1r2r4Z stat_funcZ chmod_funcr@r r r!r}s   listxattrcCsytj||d}Wn4tk rF}z|jtjtjfkr8dSd}~XnXxn|D]f}y&tj|||d}tj||||dWqNtk r}z|jtjtjtjfkrWYdd}~XqNXqNWdS)zCopy extended filesystem attributes from `src` to `dst`. Overwrite existing attributes. If `follow_symlinks` is false, symlinks won't be followed. )r4N) r,rEr.errnoENOTSUPZENODATAgetxattrsetxattrZEPERM)r1r2r4namesenamevaluer r r! _copyxattrs  rNcOsdS)Nr )argskwargsr r r!rNsc s6ddddd|p,tjj|o*tjj| }|r@fdd}n fdd}|d||d }tj|j}|d ||j|jf|dy|d |||d Wntk rYnXt |d r$y|d ||j |d WnPt k r"}z2x*dD] }t t |r|j t t |krPqWWYdd}~XnXt|||d dS)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)nsr4cWsdS)Nr )rQr4rOr r r!_nopszcopystat.._nopcs tt|S)N)getattrr,)rL)rRr r!lookupszcopystat..lookupcstt|}|tjkr|SS)N)rSr,supports_follow_symlinks)rLr?)rRr r!rTs  r8)r4utimerBst_flagsZchflags EOPNOTSUPPrG)rXrG)r,r-r;r8rDr: st_atime_ns st_mtime_nsNotImplementedErrorr+rWr.rFrSrN) r1r2r4ZfollowrTr@modewhyerrr )rRr!rs,      cCsBtjj|r"tjj|tjj|}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. )r4)r,r-isdirjoinbasenamerr)r1r2r4r r r!rs cCsBtjj|r"tjj|tjj|}t|||dt|||d|S)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". )r4)r,r-r_r`rarr)r1r2r4r r r!r s csfdd}|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]}|jtj||q Wt|S)N)extendfnmatchfilterset)r-rJ ignored_namespattern)patternsr r!_ignore_patternss z)ignore_patterns.._ignore_patternsr )rhrir )rhr!r s cCstj|}|dk r|||}nt}tj|g}xB|D]8} | |krJq:tjj|| } tjj|| } ytjj| rtj| } |rtj| | t | | | dn 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. N)r4rZwinerror)r,listdirremakedirsr-r`r;r=r<rexistsr_r r rbrOr.appendstrrS)r1r2symlinksignore copy_functionZignore_dangling_symlinksrJrferrorsrLZsrcnameZdstnamelinktor^r]r r r!r sL$           *&c$CsLytjj|rtdWn(tk r@|tjj|tjdSXg}ytj|}Wn&tk rz|tj|tjYnXx|D]}tjj||}ytj|j }Wntk rd}YnXt j |rt ||qytj |Wqtk r |tj |tjYqXqWytj|Wn(tk rF|tj|tjYnXdS)Nz%Cannot call rmtree on a symbolic linkr)r,r-r;r.sysexc_inforjr`rCr:r8S_ISDIR_rmtree_unsafeunlinkrmdir)r-onerrorrJrLfullnamer\r r r!rwqs6      rwc 0Csg}ytj|}Wn:tk rL}z||_|tj|tjWYdd}~XnXx|D]x}tjj||}ytj||dd}|j }Wntk rd}YnXtj |rytj |tj |d} Wn&tk r|tj |tjYnXztjj |tj| rJt| ||ytj||dWn(tk rF|tj|tjYnXn8y tdWn*tk r|tjj|tjYnXWdtj| XqVytj||dWqVtk r|tj|tjYqVXqVWdS)NF)dir_fdr4r)r|z%Cannot call rmtree on a symbolic link)r,rjr.filenamertrur-r`r8r:rvr>O_RDONLYsamestatfstat_rmtree_safe_fdryr;closerx) topfdr-rzrJr^rLr{orig_str\dirfdr r r!rsD$     rc!Cs^|rdd}n|dkrdd}trPt|tr8tj|}ytj|}Wn&tk rl|tj|tjdSXytj |tj }Wn&tk r|tj|tjdSXztj j |tj |rt|||ytj|Wn(tk r|tj|tjYnXn8y tdWn*tk r<|tj j|tjYnXWdtj|Xn t||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. cWsdS)Nr )rOr r r!rzszrmtree..onerrorNcWsdS)Nr )rOr r r!rzsz%Cannot call rmtree on a symbolic link)_use_fd_functions isinstancebytesr,fsdecoderC Exceptionrtrur>r~r-rrrryr.r;rrw)r- ignore_errorsrzrfdr r r!r s<      cCs&tjjtjjpd}tjj|j|S)N)r,r-sepaltseprarstrip)r-rr r r! _basenamesrc Cs|}tjj|rTt||r*tj||dStjj|t|}tjj|rTtd|ytj||Wnt k rtjj |rtj |}tj ||tj |nTtjj|rt||rtd||ft|||ddt|n|||tj |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)rqro)r,r-r_r3renamer`rrlr r.r;r=r<rx _destinsrcr r )r1r2rqZreal_dstrsr r r!r s2              cCsVtjj|}tjj|}|jtjjs2|tjj7}|jtjjsL|tjj7}|j|S)N)r,r-r0endswithr startswith)r1r2r r r!r8s    rc CsNtdks|dkrdSy t|}Wntk r8d}YnX|dk rJ|dSdS)z"Returns a gid, given a group name.N)rKeyError)rLresultr r r!_get_gidAs  rc CsNtdks|dkrdSy t|}Wntk r8d}YnX|dk rJ|dSdS)z"Returns an uid, given a user name.Nr)rr)rLrr r r!_get_uidMs  rgzipc s"|dkrd}nDtr |dkr d}n2tr2|dkr2d}n trD|dkrDd}ntdj|d dl} |rfd |nd} |d | } tjj| } | rtjj |  r|dk r|j d | |stj | |dk r|j d t t fdd} |s| j| d|}z|j|| dWd|jX| 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}r.z.tarz creating %szCreating tar archivecs,dk r|_|_dk r(|_|_|S)N)gidZgnameuiduname)Ztarinfo)rgroupownerrr r! _set_uid_gidsz#_make_tarball.._set_uid_gidzw|%s)rd)_ZLIB_SUPPORTED_BZ2_SUPPORTED_LZMA_SUPPORTED ValueErrorr7tarfiler,r-dirnamerlinforkrrr>addr) base_namebase_dircompressverbosedry_runrrloggerZtar_compressionrZ compress_extZ archive_name archive_dirrtarr )rrrrr! _make_tarballYs<          rc Cs|ddl}|d}tjj|}|rPtjj| rP|dk rB|jd||sPtj||dk rf|jd|||sx|j|d|jd}tjj |} | tj kr|j | | |dk r|jd| xtj |D]\} } } xFt | D]:} tjj tjj| | } |j | | |dk r|jd| qWxT| D]L} tjj tjj| | } tjj| r|j | | |dk r|jd| 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. rNz.zipz creating %sz#creating '%s' and adding '%s' to itw)Z compressionz adding '%s')zipfiler,r-rrlrrkZipFileZ ZIP_DEFLATEDnormpathcurdirr'walksortedr`isfile)rrrrrrZ zip_filenamerZzfr-dirpathZdirnames filenamesrLr r r! _make_zipfiles@             rrrzuncompressed tar filezgzip'ed tar-fileZgztarzZIP fileziprzbzip2'ed tar-fileZbztarrzxz'ed tar-fileZxztarcCsddtjD}|j|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 ).0rLregistryr r r! sz'get_archive_formats..)_ARCHIVE_FORMATSitemssort)formatsr r r!rs rcCsz|dkr g}t|s td|t|ttfs6tdx0|D](}t|ttf s\t|dkr|dk r$|jd|tjj|}|s>tj||dkrLtj}||d} y t|} Wn tk rt d|YnX| d} x| dD]\} }|| | <qW|dkr|| d<|| d <z| ||f| }Wd|dk r|dk r|jd | tj| 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,getcwddebugr-r0chdirrrrr)rr7Zroot_dirrrrrrrZsave_cwdrP format_infofuncargvalr}r r r!rs6        cCsddtjD}|j|S)zReturns a list of supported formats for unpacking. Each element of the returned sequence is a tuple (name, extensions, description) cSs"g|]\}}||d|dfqS)rr )rrLrr r r!r6sz&get_unpack_formats..)_UNPACK_FORMATSrr)rr r r!r0s c Csvi}x.tjD]"\}}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 extensionmsgr r r!_check_unpack_options;s 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)rLrrrrr r r!rMs cCs t|=dS)z*Removes the pack format from the registry.N)r)rLr r r!rcscCs&tjj|}tjj|s"tj|dS)z1Ensure that the parent directory of `path` existsN)r,r-rr_rk)r-rr r r!_ensure_directorygs  rc Csddl}|j|std||j|}zx|jD]}|j}|jds4d|krRq4tjj |f|j d}|spq4t ||j ds4|j |j}t|d}z|j|Wd|j~Xq4WWd|jXdS)z+Unpack zip `filename` to `extract_dir` rNz%s is not a zip file/z..r6)rZ is_zipfiler#rZinfolistr}rr,r-r`splitrrr&r>r'r) r} extract_dirrrrrLtargetdatafr r r!_unpack_zipfilems*       rcCsXddl}y|j|}Wn"|jk r8td|YnXz|j|Wd|jXdS)zAUnpack tar/tar.gz/tar.bz2/tar.xz `filename` to `extract_dir` rNz/%s is not a compressed or uncompressed tar file)rr>ZTarErrorr#Z extractallr)r}rrZtarobjr r r!_unpack_tarfilesrz.tarz.zip)rrz.tar.gzz.tgzz.tar.bz2z.tbz2z.tar.xzz.txzcCs:x4tjD](\}}x|dD]}|j|r|SqWq WdS)Nr)rrr)r}rLrrr r r!_find_unpack_formats   rc Cs|dkrtj}|dk rhy t|}Wn"tk rFtdj|YnX|d}|||ft|dnHt|}|dkrtdj|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,rrrrr7dictrr#)r}rr7rrrPr r r!rs  statvfs disk_usageZusageztotal used freezTotal space in byteszUsed space in byteszFree space in bytescCs@tj|}|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,rf_bavailf_frsizef_blocksf_bfree_ntuple_diskusage)r-r@freetotalusedr r r!rs    ntcCs"tj|\}}||}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)r-rrrr r r!rscCs|dkr|dkrtd|}|}|dkr.d}n(t|trVt|}|dkrVtdj||dkrdd}n(t|tst|}|dkrtdj|tj |||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 setrzno such user: {!r}zno such group: {!r}r) rrrnr LookupErrorr7intrr,r)r-userrZ_userZ_groupr r r!rs"  PcCsyttjd}Wnttfk r.d}YnXyttjd}Wnttfk r^d}YnX|dksp|dkrytjtjj}Wn$t tt fk rtj |}YnX|dkr|j }|dkr|j }tj ||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,environrrrrt __stdout__filenoAttributeErrorr. terminal_sizecolumnslines)Zfallbackrrsizer r r!rs$  c s"dd}tjjr&||r"SdS|dkr>tjjdtj}|sFdS|jtj}tj dkrtj |krt|j dtj tjjddjtj}t fd d |Drg}q‡fd d |D}ng}t }xT|D]L}tjj|}||kr|j|x(|D] } tjj|| } || |r| SqWqWdS) 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&tjj|o$tj||o$tjj| S)N)r,r-rlaccessr_)r?r\r r r! _access_checkXszwhich.._access_checkNPATHZwin32rZPATHEXTrc3s |]}jj|jVqdS)N)lowerr)rr)cmdr r! uszwhich..csg|] }|qSr r )rr)r r r!rxszwhich..)r,r-rrgetdefpathrpathseprtplatformrinsertanyrer/rr`) r r\r-rZpathextfilesseendirZnormdirZthefilerLr )r r!rKs8            @)r)FN)rrrNNN)rrN)rN)rr)rr)rr)Nr)NNrrNNN)Nr)NN)NNrr)r)Vr"r,rtr8rc collectionsrFzlibr ImportErrorrrZlzmarpwdrZgrpr__all__r.r rrrr#rr$rr3rrr+rNrrr rr rwrr>rxrysupports_dir_fdrj supports_fdrUrr Zavoids_symlink_attacksrr rrrrrrrrrrrrrrrrrrrrrm namedtuplerrrrrrLrrrF_OKX_OKrr r r r!s          :  Y!,   5 8    @ -     6  "        %        -