B u `G@szddlZddlZddlZddlZddlZddlmZyddlmZ Wne k r\dZ YnXdZ GdddZ e ZdS)N)deprecation_warning)TempfileManagerTc@sxeZdZdZdZddZdddZddd Zdd d Zdd dZ d ddZ ddZ ddZ ddZ ddZd!ddZdS)"TempfileManagerClassz%A class that manages temporary files.NcKsgg|_d|_dS)N) _tempfiles_ctr)selfkwdsr :/tmp/pip-unpacked-wheel-n62dbgi3/pyomo/common/tempfiles.py__init__'szTempfileManagerClass.__init__FcCs|dkr d}|dkrd}|dkrP|j}|dkrPtdk rPtj}|dk rPtdddtj||||d}t|}tj|dstj ||d}n|d}t |d |j d krtj ||t |j |}tj |rt|t|||}|j d7_ |jd ||S) zCreate a unique temporary file Returns the absolute path of a temporary filename that is guaranteed to be unique. This function generates the file and returns the filename. NtmpzThe use of the PyUtilib TempfileManager.tempdir to specify the default location for Pyomo temporary files has been deprecated. Please set TempfileManager.tempdir in pyomo.common.tempfilesz5.7.2)version)suffixprefixtextdirrr)tempdir pyutilib_mngrrtempfilemkstemplistospathisabsjoincloserstrexistsremoveshutilmoverappend)rrrrrZansfnameZ new_fnamer r r create_tempfile+s6    z$TempfileManagerClass.create_tempfilecCs|dkr d}|dkrd}|dkrP|j}|dkrPtdk rPtj}|dk rPtdddtj|||d}|jdkrtj||t |j|}tj |rt |t |||}|jd 7_|jd ||S) zCreate a unique temporary directory Returns the absolute path of a temporary directory that is guaranteed to be unique. This function generates the directory and returns the directory name. Nr rzThe use of the PyUtilib TempfileManager.tempdir to specify the default location for Pyomo temporary directories has been deprecated. Please set TempfileManager.tempdir in pyomo.common.tempfilesz5.7.2)r)rrrrrr)rrrrmkdtemprrrrrr r"rmtreer#rr$)rrrrdirnameZ new_dirnamer r r create_tempdirVs,    z#TempfileManagerClass.create_tempdirTcCs<tj|}|r(tj|s(td||jd|dS)z"Declare this file to be temporary.zTemporary file does not exist: rN)rrabspathr IOErrorrr$)rfilenamer rr r r add_tempfile|s  z!TempfileManagerClass.add_tempfilecCs,xt|jdkr||qW||dS)zDelete all temporary files.rN)lenrpop)rr!r r r clear_tempfilessz$TempfileManagerClass.clear_tempfilesrcCs ||_dS)z>Start generating sequential files, using the specified counterN)r)rZctrr r r sequential_filessz%TempfileManagerClass.sequential_filescCs d|_dS)z=Stop generating sequential files, using the specified counterrN)r)rr r r unique_filessz!TempfileManagerClass.unique_filescCs|jg|S)N)rr$)rr r r pushs zTempfileManagerClass.pushcCs |dS)N)r4)rr r r __enter__szTempfileManagerClass.__enter__cCs|jdddS)NT)r!)r0)rtypevalue tracebackr r r __exit__szTempfileManagerClass.__exit__c Cs|j}|rx|D]}tj|rtj|rBtj|t dqyt |Wqt k ryt dt |Wn6t k rtrnt t}|d|fYnXYqXqWt|jdkrgg|_dS)N) ignore_errorsrz"Unable to delete temporary file %sr)rr0rrr isdirr"r(deletion_errors_are_fatalr!Z WindowsErrortimesleeplogging getLogger__name__warningr/)rr!filesr-loggerr r r r0s,       zTempfileManagerClass.pop)NNFN)NNN)T)T)r)T)rA __module__ __qualname____doc__rr r&r*r.r1r2r3r4r5r9r0r r r r r"s + &   r) rr=rr?r"Zpyomo.common.deprecationrZ#pyutilib.component.config.tempfilesrr ImportErrorr<rr r r r s