YfpA@srdZddlZddlZddlZddlZddlZddlZyddlZ[dZWne k rdZYnXyddl Z [ dZ Wne k rdZ YnXyddl Z [ dZ Wne k rdZ YnXyddlmZWne k rdZYnXyddlmZWne k rGdZYnXddd d d d d ddddddddddddddddddgZGd ddeZGd!ddeZGd"ddeZGd#ddeZGd$d%d%eZGd&d'd'eZdd*dZd+d,Zd-dd.dZd-dd/d Zeed0rd-dd1d2Z n d3d2Z d-dd4d Z!d-dd5d Z"d-dd6d Z#d7dZ$dde#dd8d Z%d9d:Z&d;d<Z'ej(ejej)ej*hej+koFej,ej-koFejej.kZ/ddd=dZ0e/e0_1d>d?Z2e#d@dZ3dAdBZ4dCdDZ5dEdFZ6dGddddddHdIZ7ddddJdKZ8dLe7dgdNfiZ9ere7dgdOfe9dPd_d`Z?ddZdadZ@dbdZAdcddZBdedfZCdgdhZDdLdigeDgdNfdRdjgeCgdQfiZEerLdkdlgeDgdOfeEdP._nopcstt|S)N)getattrr,)rL)rRr r!lookupszcopystat..lookupcs)tt|}|tjkr%|SS)N)rSr,supports_follow_symlinks)rLr?)rRr r!rTsr8utimerBst_flagsZchflags EOPNOTSUPPrG)rXrG)r,r-r;r8rDr: st_atime_ns st_mtime_nsNotImplementedErrorr+rWr.rFrSrN) r1r2r4ZfollowrTr@modewhyerrr )rRr!rs,+!   'cCs]tjj|r3tjj|tjj|}t||d|t||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 !cCs]tjj|r3tjj|tjj|}t||d|t||d||S)zCopy data and all stat info ("cp -p 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". 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}x'D]}|jtj||q Wt|S)N)extendfnmatchfilterset)r-rJ ignored_namespattern)patternsr r!_ignore_patterns s z)ignore_patterns.._ignore_patternsr )rhrir )rhr!rscCsstj|}|dk r-|||}n t}tj|g}x|D]} | |kreqPtjj|| } tjj|| } ytjj| r7tj| } |rtj| | t | | d| qotjj |  r|rwPtjj | r't | | |||qo|| | n8tjj | rbt | | |||n || | WqPt k r} z|j| jdWYdd} ~ XqPtk r}z!|j| | t|fWYdd}~XqPXqPWyt ||WnYtk r\}z9t|dddkrJ|j||t|fWYdd}~XnX|rot ||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. Nr4rZwinerror)r,listdirremakedirsr-r`r;r=r<rexistsr_r r rbrOr.appendstrrS)r1r2symlinksignore copy_functionZignore_dangling_symlinksrJrferrorsrLZsrcnameZdstnamelinktor^r]r r r!r sL$      &3/ c$Csy"tjj|r!tdWn2tk rV|tjj|tjdSYnXg}ytj|}Wn+tk r|tj|tjYnXx|D]}tjj||}ytj|j }Wntk rd}YnXt j |rt ||qytj |Wqtk rN|tj |tjYqXqWytj|Wn+tk r|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!rwks6       " rwc 0Cs@g}ytj|}WnGtk rb}z'||_|tj|tjWYdd}~XnXx|D]}tjj||}y(tj|d|dd}|j }Wntk rd}YnXtj |rytj |tj d|} Wn+tk r#|tj |tjYq8Xztjj |tj| rt| ||ytj|d|Wqtk r|tj|tjYqXnAytdWn.tk r|tjj|tjYnXWdtj| Xqjytj|d|Wqjtk r7|tj|tjYqjXqjWdS)Ndir_fdr4Frz%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|dkr-dd}trt|trQtj|}ytj|}Wn/tk r|tj|tjdSYnXytj |tj }Wn/tk r|tj|tjdSYnXztj j |tj |rTt|||ytj|Wqtk rP|tj|tjYqXnAytdWn.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<        ! %cCs5tjjtjjpd}tjj|j|S)N)r,r-sepaltseprarstrip)r-rr r r! _basenamesrc Cs]|}tjj|rxt||r;tj||dStjj|t|}tjj|rxtd|ytj||Wnt k rXtjj |rtj |}tj ||tj |nwtjj|r:t||rtd||ft||d|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'.rqroT)r,r-r_r3renamer`rrlr r.r;r=r<rx _destinsrcr r )r1r2rqZreal_dstrsr r r!r s2   cCs{tjj|}tjj|}|jtjjsI|tjj7}|jtjjsn|tjj7}|j|S)N)r,r-r0endswithr startswith)r1r2r r r!r2src Cs_tdks|dkrdSyt|}Wntk rFd}YnX|dk r[|dSdS)z"Returns a gid, given a group name.N)rKeyError)rLresultr r r!_get_gid;s   rc Cs_tdks|dkrdSyt|}Wntk rFd}YnX|dk r[|dSdS)z"Returns an uid, given a user name.Nr)rr)rLrr r r!_get_uidGs   rgzipc s|dkrd}nftr0|dkr0d}nKtrK|dkrKd}n0trf|dkrfd}ntdj|d dl} |rd |nd} |d | } tjj| } | rtjj |  r|dk r|j d | |stj | |dk r|j d t t fdd} |s| j| d|}z|j|d| Wd|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_gids      z#_make_tarball.._set_uid_gidzw|%srd)_ZLIB_SUPPORTED_BZ2_SUPPORTED_LZMA_SUPPORTED ValueErrorr7tarfiler,r-dirnamerlinforkrrr>addr) base_namebase_dircompressverbosedry_runrrloggerZtar_compressionrZ compress_extZ archive_name archive_dirrtarr )rrrrr! _make_tarballSs<                rc Csddl}|d}tjj|}|rptjj| rp|dk r]|jd||sptj||dk r|jd|||s|j|dd|jK}tjj |} | tj kr|j | | |dk r|jd| xtj |D]\} } } xat | D]S} tjj tjj| | } |j | | |dk r,|jd| q,Wxm| D]e} 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 itwZ 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-fileZxztarcCs'ddtjD}|j|S)zReturns a list of supported formats for archiving and unarchiving. Each element of the returned sequence is a tuple (name, description) cSs&g|]\}}||dfqS)rr ).0rLregistryr r r! s z'get_archive_formats..)_ARCHIVE_FORMATSitemssort)formatsr r r!rs  rcCs|dkrg}t|s.td|t|ttfsOtdxB|D]:}t|ttf st|dkrVtdqVW|||ft|.)_UNPACK_FORMATSrr)rr r r!r*s  c Csi}x9tjD]+\}}x|dD]}|||r'r) r} extract_dirrrrrLtargetdatafr r r!_unpack_zipfilegs*     rcCsjddl}y|j|}Wn%|jk rFtd|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_tarfiles rz.tarz.zipz.tar.gzz.tgzz.tar.bz2z.tbz2z.tar.xzz.txzcCsIxBtjD]4\}}x%|dD]}|j|r$|Sq$Wq WdS)Nr)rrr)r}rLrrr r r!_find_unpack_formats  rcCs|dkrtj}|dk ryt|}Wn'tk r[tdj|YnX|d}|||t|dn_t|}|dkrtdj|t|d}tt|d}||||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 freecCsVtj|}|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!rs cCs|dkr$|dkr$td|}|}|dkrEd}n<t|trt|}|dkrtdj||dkrd}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 r7d}YnXyttjd}Wnttfk rod}YnX|dks|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 sdd}tjjr5||r1SdS|dkrYtjjdtj}|scdS|jtj}tj dkrtj |kr|j dtj tjjddjtj}t fd d |Drg}qfd d |D}n g}t }xr|D]j}tjj|}||kr'|j|x6|D].} tjj|| } || |r_| Sq_Wq'WdS) 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. cSs5tjj|o4tj||o4tjj| S)N)r,r-rlaccessr_)r?r\r r r! _access_checkOs$zwhich.._access_checkNPATHwin32rZPATHEXTrc3s*|] }jj|jVqdS)N)lowerr)rr)cmdr r! lszwhich..csg|]}|qSr r )rr)r r r!ros zwhich..)r,r-rrgetdefpathrpathseprtplatformrinsertanyrer/rr`) r r\r-rZpathextfilesseendirZnormdirZthefilerLr )r r!rBs8  !        i@)rN)rr)rr)rr)rr)Sr"r,rtr8rc collectionsrFzlibr ImportErrorrrZlzmarpwdrZgrpr__all__r.r rrrr#rr$rr3rrr+rNrrr rr rwrr>rxrysupports_dir_fdrj supports_fdrUrr Zavoids_symlink_attacksrr rrrrrrrrrrrrrrrrrrrrrm namedtuplerrrLrrrF_OKX_OKrr r r r!s                           6 Y ! , 5  8 @-         6    "     %     -