U  g1@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddddgZ ej ej BejBejBejBejBejBZejejBZGdddeZGdddeZdS) a !Change directory, handle temporary directories This module provides a means by which to change to a different directory in a Python "with" block and change back out afterwards, regardless of what happens inside the block. It can, optionally, create a new directory, and optionally delete it at the end of the block. There are two classes: * TempDir - creates a temporary directory with a randomly-generated name, chdirs to the directory, and chdirs back out afterwards. It can be configured to delete the directory afterwards (the default) or not. * NamedDir - a subclass of TempDir that uses a specific directory rather than a randomly-generated one. By default, the directory is NOT deleted at the end of the block. That can be configured.NTempDirNamedDir perm_removeperm_addc @sneZdZdZddddddeeddf ddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZdS)ra !This class is intended to be used with the Python "with TempDir() as t" syntax. Example: @code with TempDir() as t: # we're now in the temporary directory ...do things... # the temporary directory has been deleted now @endcode z.tmpztempdir.NFTc Csd|_||_||_||_|dkr$d}||_d|_||_||_t||_ t||_ | |_ t | |_ |dk slttj|jstjt|j|_tj|jstdS)a!Creates a TempDir. @param suffix,prefix,dir Passed to the tempfile.mkdtemp to generate the directory name. Meanings are the same as in that constructor. See the Python documentation for details. @param keep Controls directory deletion if the "with" block returns without an exception. If False, the directory is deleted. Default: keep=False @param logger A logging.logger for log messages @param print_on_exception Print exceptions before leaving the dir @param add_perms Permissions to add to the directory. Default: 755. @param remove_perms Permissions to remove from the directory. Default: setuid and world write. @param keep_on_error Controls directory deletion if the "with" block raises an Exception or GeneratorExit, or subclass thereof. If False, the directory is deleted under those circumstances. Default: keep_on_error=True. @param cd If True (default), cd to the directory in the "with" block and cd back out afterwards. If False, then only directory creation and deletion happens. N.)dirnamesuffixprefixprint_on_exceptiondirolddir_keep_loggerint _add_perms _remove_perms_keep_on_errorbool_cdAssertionErrorospathisabsjoingetcwd) selfrr r keeploggerr add_perms remove_perms keep_on_errorcdr"8/lfs/h1/ops/prod/packages/hmon.v3.2.7/ush/produtil/cd.py__init__1s$    zTempDir.__init__c Cs|jdk rhz,tj|j}|dk r4|dkr4t|Wn0tk rf}z|jtjkrVW5d}~XYnXtj |j |j|j d|_t |j}t |j|jt|jB|j@dS)z!Decide the name of the directory, and create the directory. Also create any path components leading up to the directory.N)rr r )r rrrmakedirsEnvironmentErrorerrnoEEXISTtempfilemkdtemprr statchmodst_moderrr)rdesr"r"r# name_make_dirms    zTempDir.name_make_dircCsDt|_||jr$t|j|jdk r@|jd|jdS)z!Creates the temporary directory and chdirs the current process into that directory. It calls self.name_make_dir() to do the naming and directory creation.Nzchdir to temporary directory ) rrr r2rchdirrrinforr"r"r#mkdir_cds    zTempDir.mkdir_cdcCs2|jdk r|jd|j|jr.t|jdS)zl!Exit the temporary directory created by mkdir_cd and return to the original directory, if possible.Nzchdir to old directory )rr4r rrr3r5r"r"r#cd_outs zTempDir.cd_outc Cs<zhz|j r| WnPtk rd}z2|jdk rD|jdt||j rTtdW5d}~XYnXW5|jdk r |js tj|jr |jdk r|jd|jf|jdk rtj|j|j dnt|jd|jdk r6tj |jr6|j d|jfn,|jdk r6|jdk r6|jd|jfXd S) a!CD out and remove the old directory. This subroutine exits the temporary directory created by mkdir_cd, and then deletes that temporary directory. After this routine, the process will be in its original directory (from before the call to mkdir_cd) if possible, or otherwise it will be in the root directory (/). It is the caller's responsibility to ensure this function is not called if keep_on_error=True and an error occurs.Nz%s: delete temporary directoryonerrorTz%s: could not delete directoryz$%s: not deleting temporary directoryz8could not chdir, so chdir to root because of exception: /F)rr rrrrr4shutilrmtree_rmerrorexistswarningrr7 Exceptioncriticalreprr3)rr0r"r"r#cd_rmdirs:       zTempDir.cd_rmdircCs&|jdk r"|jdt|f|ddS)ze!Called when a file removal error happens. @param function,path,excinfo exception informationNz%s: cannot remove)exc_info)rstr)rfunctionrexcinfor"r"r#r=s zTempDir._rmerrorcCs^|jdk r4|j|jd|jd|jtntd|jdtt j ddS)zq!Called to dump information to a log, or failing that, the terminal if an unexpected exception is caught.Nz): leaving temp directory due to exceptionz,%s: listing current directory (%s) via ls -lzprodutil.tempdirr) rr?rr4rrlogging getLoggerprintprodutillistingListingr5r"r"r#exception_infos   zTempDir.exception_infocCs ||S)z!This is a simple wrapper around mkdir_cd that is intended to be used with in a "with" block. This subroutine is automatically called at the beginning of the block.)r6r5r"r"r# __enter__szTempDir.__enter__cCsd|dkst|tr|nDt|trXt|tr>|jr>||jrN|q`|n|dS)a5!Exit the 'with' block. This is a simple wrapper around cd_rmdir that is intended to be used with in a "with" block. This subroutine is automatically called at the end of the block. It will call cd_rmdir to delete the directory unless an exception is thrown that is NOT a subclass of Exception or GeneratorExit. The removal is skipped to allow the program to exit quickly in case of a fatal signal (ie.: SIGQUIT, SIGTERM, SIGINT, SIGHUP). @param etype,value,traceback exception informationN) isinstance GeneratorExitrCr@r rNrr7)retypevalue tracebackr"r"r#__exit__s     zTempDir.__exit__)__name__ __module__ __qualname____doc__rrr$r2r6r7rCr=rNrOrUr"r"r"r#r's"  < # cs*eZdZdZd fdd Zdd ZZS) rz!This subclass of TempDir takes a directory name, instead of generating one automatically. By default, it will NOT delete the directory upon __exit__. That can be overridden by specifying keep=False.TNrFcs@t|tstdtt|j|||||d||_t||_dS)a!Create a NamedDir for the specified directory. The given logger is used to log messages. There are two deletion vs. non-deletion options: @param dirname The directory name @param keep If False, the file is deleted upon successful return of the "with" block. If True, the file is kept upon successful return. @param logger A logging.logger for log messages @param add_perms Permissions to add to the directory after cding into it. Default: none. @param remove_perms Permissions to remove from the directory after cding into it. Default: none. @param keep_on_error Controls deletion upon catching of an Exception or GeneratorException (or subclass thereof). @param rm_first If the directory already exists, delete it first and make a new one before cding to it.z6NamedDir requires a string name as its first argument.)rrr rrN) rPrE TypeErrorsuperrr$rr _rm_first)rrrrr rrrm_first __class__r"r#r$s  zNamedDir.__init__cCs|js dS|jr^tj|jr^|jdk rP|jd|jftj |j|j dnt |jdtj|jsxt |j|j dks|j dkrt|j}t|j|jt|j B|j @dS)zj!Replacement for the TempDir.name_make_dir. Uses the directory name specified in the constructor.Nz%s: delete directory treer8Tr)rr\rrr>rrr?r;r<r=r&rrr,r-r.r)rr1r"r"r#r2s"   zNamedDir.name_make_dir)TNTrrF)rVrWrXrYr$r2 __classcell__r"r"r^r#rs)rYr*rrerHsysr;r(r,produtil.listingrK__all__S_IRUSRS_IWUSRS_IXUSRS_IRGRPS_IXGRPS_IROTHS_IXOTHrS_IWOTHS_ISUIDrobjectrrr"r"r"r#s @  ?