U o a/d@s dZddlZddlZddlZddlmZddlZddlZddlZddl m Z zddl Z dZ Wne k rtdZ YnXzddlmZWne k rdZYnXzdd lmZWne k rdZYnXd d d d dddddddddddddddddgZGdddeZGd ddeZGd!ddeZGd"d#d#eZGd$d%d%eZzeWnek rjdZYnXddd'd Zd(d)Zd*d Zd+d Zd,d Z d-dZ!d.dZ"d/dZ#dde"dfd0dZ$ded1dZ%d2d3Z&d4dZ'd5d6Z(d7d8Z)d9d:Z*dfdd?Z,dhd@dAZ-e+dBgdCfe+dDgdEfe+dFgdGfe-gdHfdIZ.e rRe+dDgdEfe.dJ<dKdZ/didMdZ0dNdZ1djdOdZ2dPdZ3dQdRZ4dkdSdZ5dTdZ6dUdVZ7dWdXZ8dYdZZ9d[d\ge9gdCfd]ge9gdGfd^ge8gdHfd_Z:e rd`ge9gdEfe:dJ<dadbZ;dldcdZ)r3r4r=moderrr r ks   c Cst|}t|j}ttdr4t||j|jfttdrJt||ttdrt|drzt ||j Wn:t k r}ztt dr|j t j krW5d}~XYnXdS)zCCopy all stat info (mode bits, atime, mtime, flags) from src to dstutimer>chflagsst_flags EOPNOTSUPPN)r.r8r?r:r-rAst_atimest_mtimer>rBrCr0errnorD)r3r4r=r@whyrrr r rs       cCs:tj|r"tj|tj|}t||t||dS)zVCopy data and mode bits ("cp src dst"). The destination may be a directory. N)r.r/isdirjoinbasenamerr r2rrr r s  cCs:tj|r"tj|tj|}t||t||dS)z]Copy data and all stat info ("cp -p src dst"). The destination may be a directory. N)r.r/rIrJrKrr r2rrr 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}D]}|t||qt|SN)extendfnmatchfilterset)r/names ignored_namespatternpatternsrr _ignore_patternssz)ignore_patterns.._ignore_patternsr)rUrVrrTr rs c Cst|}|dk r|||}nt}t|g}|D] } | |krFq6tj|| } tj|| } zttj| rt| } |rt| | qtj | s|rWq6|| | n(tj | rt | | |||n || | Wq6t k r } z| | jdW5d} ~ XYq6tk r@}z|| | t|fW5d}~XYq6Xq6zt||WnPtk r}z0tdk r~t|tr~n| ||t|fW5d}~XYnX|rt |dS)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. Nr)r.listdirrPmakedirsr/rJislinkreadlinksymlinkexistsrIr rrMargsEnvironmentErrorappendstrr r0 WindowsError isinstance)r3r4symlinksignore copy_functionignore_dangling_symlinksrQrRerrorsnamesrcnamedstnamelinktoerrrHrrr r sD$         (&c Csr|rdd}n|dkrdd}ztj|r4tdWn*tk r`|tjj|tYdSXg}zt|}Wn(tjk r|tj|tYnX|D]}tj||}zt |j }Wntjk rd}YnXt |rt |||qzt|Wqtjk r0|tj|tYqXqzt|Wn*tjk rl|tj|tYnXdS)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 os.listdir, os.remove, or os.rmdir; 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. cWsdSrLrr]rrr onerrorszrmtree..onerrorNcWsdSrLrrmrrr rnsz%Cannot call rmtree on a symbolic linkr)r.r/rYr0sysexc_inforWerrorrJlstatr:r8S_ISDIRrremovermdir)r/ ignore_errorsrnrQrhfullnamer@rrr rs>      cCstj|tjjSrL)r.r/rKrstripsep)r/rrr _basename'srzcCs|}tj|rTt||r*t||dStj|t|}tj|rTtd|zt||Wnft k rtj|rt ||rtd||ft ||ddt |nt ||t|YnXdS)aRecursively move a file or directory to another location. This is similar to the Unix "mv" command. 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. 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)rc)r.r/rIr5renamerJrzr\rr0 _destinsrcr rr unlink)r3r4real_dstrrr r,s$         cCsNt|}t|}|tjjs*|tjj7}|tjjsD|tjj7}||SrL)rendswithr.r/ry startswithr2rrr r|Ts  r|cCsNtdks|dkrdSz t|}Wntk r8d}YnX|dk rJ|dSdS)z"Returns a gid, given a group name.N)rKeyErrorrhresultrrr _get_gid]s  rcCsNtdks|dkrdSz t|}Wntk r8d}YnX|dk rJ|dSdS)z"Returns an uid, given a user name.Nr)rrrrrr _get_uidis  rgzipcsddd}ddi} tr&d|d<d| d<|d k rD|| krDtd ||d | |d} tj| } tj| s|d k r|d | |st | |d k r|d t t fdd} |st | d||} z| j|| dW5| X| S)aCreate a (possibly compressed) tar file from all the files under 'base_dir'. 'compress' must be "gzip" (the default), "bzip2", 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", or ".bz2"). Returns the output filename. gz)rNrz.gzbz2bzip2.bz2NzCbad value for 'compress', or compression format not supported : {0}.tar creating %szCreating tar archivecs,dk r|_|_dk r(|_|_|SrL)gidgnameuiduname)tarinforgroupownerrrr _set_uid_gidsz#_make_tarball.._set_uid_gidzw|%s)rO)_BZ2_SUPPORTED ValueErrorformatgetr.r/dirnamer\inforXrrrr;closeadd) base_namebase_dircompressverbosedry_runrrloggertar_compression compress_ext archive_name archive_dirrtarrrr _make_tarballus6        rcCsd|r d}nd}ddlm}ddlm}z|d|||g|dWn |k r^td|YnXdS) Nz-rz-rqr)DistutilsExecError)spawnzip)rzkunable to create zip file '%s': could neither import the 'zipfile' module nor find a standalone zip utility)distutils.errorsrdistutils.spawnrr)r zip_filenamerr zipoptionsrrrrr _call_external_zips  rcCs|d}tj|}tj|sB|dk r4|d||sBt|z ddl}Wntk rfd}YnX|dkrt||||n|dk r|d|||s|j |d|j d}t |D]V\} } } | D]F} tj tj | | } tj| r|| | |dk r|d| qq||S) amCreate a zip file from all the files under 'base_dir'. The output zip file will be named 'base_name' + ".zip". Uses either the "zipfile" Python module (if available) or the InfoZIP "zip" utility (if installed and found on the default search path). If neither tool is available, raises ExecError. Returns the name of the output zip file. .zipNrrz#creating '%s' and adding '%s' to itw) compressionz adding '%s')r.r/rr\rrXzipfile ImportErrorrZipFile ZIP_DEFLATEDwalknormpathrJisfiler'r)rrrrrrrrrdirpathdirnames filenamesrhr/rrr _make_zipfiles>         r)rrzgzip'ed tar-file)rrzbzip2'ed tar-file)rNzuncompressed tar filezZIP file)gztarbztarrrrcCsddtD}||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).0rhregistryrrr sz'get_archive_formats..)_ARCHIVE_FORMATSitemssortformatsrrr rs rcCsx|dkr g}t|tjs$td|t|ttfs:td|D]&}t|ttfr\t|dkr>tdq>|||ft|<dS)auRegisters an archive format. name is the name of the format. function is the callable that will be used to create archives. 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_archive_formats() function. NzThe %s object is not callablez!extra_args needs to be a sequencerz+extra_args elements are : (arg_name, value))rb collectionsCallable TypeErrortuplelistlenr)rhfunction extra_args descriptionelementrrr rs    cCs t|=dSrL)rrhrrr rsc  Cst} |dk r>|dk r$|d|tj|}|s>t||dkrLtj}||d} z t|} Wn tk rt d|YnX| d} | dD]\} }|| | <q|dkr|| d<|| d <z| ||f| }W5|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", "bztar" or "gztar". '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'rrrrrzchanging back to '%s') r.getcwddebugr/rchdircurdirrrr)rrroot_dirrrrrrrsave_cwdkwargs format_infofuncargvalfilenamerrr r s6        cCsddtD}||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)rrhrrrr r]sz&get_unpack_formats..)_UNPACK_FORMATSrrrrrr rWs c Csni}tD]\}}|dD] }|||<qq |D]$}||kr0d}t||||fq0t|tjsjtddS)z+Checks what gets registered as an unpacker.rz!%s is already registered for "%s"z*The registered function must be a callableN)rrr$rbrrr) extensionsrrexisting_extensionsrhrext extensionmsgrrr _check_unpack_optionsbs    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)rhrrrrrrr rts cCs t|=dS)z*Removes the pack format from the registry.N)rrrrr rscCs&tj|}tj|s"t|dS)z1Ensure that the parent directory of `path` existsN)r.r/rrIrX)r/rrrr _ensure_directorys  rc Csz ddl}Wntk r(tdYnX||s@td|||}z|D]}|j}|dsTd|krrqTt j j |f| d}|sqTt ||dsT||j}t|d}z||W5|~XqTW5|XdS)z+Unpack zip `filename` to `extract_dir` rNz/zlib not supported, cannot unpack this archive.z%s is not a zip file/z..r7)rrr# is_zipfilerrinfolistrrr.r/rJsplitrrr&r;r') r extract_dirrrrrhtargetdatafrrr _unpack_zipfiles0         rcCsPzt|}Wn"tjk r0td|YnXz||W5|XdS)z:Unpack tar/tar.gz/tar.bz2 `filename` to `extract_dir` z/%s is not a compressed or uncompressed tar fileN)rr;TarErrorr#r extractall)rrtarobjrrr _unpack_tarfiles rz.tar.gzz.tgzrr)rrrrcCs:tD],\}}|dD]}||r|SqqdS)Nr)rrr)rrhrrrrr _find_unpack_formats   rcCs|dkrt}|dk rhz t|}Wn"tk rFtd|YnX|d}|||ft|dnHt|}|dkrtd|t|d}tt|d}|||f|dS)aUnpack 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", or "gztar". 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.rrrrrdictrr#)rrrrrrrrr rs  )r%)FN)rrrNNN)FF)rrN)Nr)NNrrNNN)Nr)NN)=r"r.ror8os.pathrrNrrGrrrrrpwdrgrpr__all__r^rrrr# Exceptionr$ra NameErrorrr5rr r r r rr rrzrr|rrrrrrrrrrrrrrrrrrrrrrrr s             R 1(    >  0     7  %