.t_c@s'dZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z ddl m Z mZejddefd YZd efd YZd efd YZdefdYZdefdYZdS(s FileUtils - Provides dmmgmnt file-related capabilities. $Id: //eng/vdo-releases/magnesium/src/python/vdo/utils/FileUtils.py#1 $ i(tprint_functionNi(tCommand(tTimeoutt TimeoutErrortutilstFileBasecBsheZdZejdZedZdZdZ dZ edZ e j dZ RS(sThe FileBase object; provides basic file control. Class attributes: log (logging.Logger) - logger for this class Attributes: None sutils.FileBasecCs|jS(N(t_FileBase__filePath(tself((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pytpath:scOsAtt|jtjj||_|jdd|_ dS(s: Arguments: None Returns: Nothing tfdN( tsuperRt__init__tosRtrealpathRtgettNonet _FileBase__fd(RtfilePathtargstkwargs((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR AscCs|S(N((R((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyt __enter__MscCstS(N(tFalse(Rt exceptionTypetexceptionValuet traceback((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyt__exit__QscCs|jS(N(R(R((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyt_fdXscCs ||_dS(N(R(Rtvalue((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR_s( t__name__t __module__t__doc__tloggingt getLoggertlogtpropertyRR RRRtsetter(((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR-s  t FileTouchcBs8eZdZejdZdZdZdZRS(sThe FileTouch object; touches the file. Class attributes: log (logging.Logger) - logger for this class Attributes: None sutils.FileTouchcOs tt|j|||dS(s: Arguments: None Returns: Nothing N(R R$R (RRRR((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR yscCstt|j|jdkrtjj|j}tj|tj snt dd|g}|j ntj|jtj s|j qn|S(s0Make certain the file exists and return ourself.tmkdirs-pN( R R$RRRR RtdirnametaccesstF_OKRtrunt _createFile(RtdirPathtcmd((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyRs cCs#td|jg}|jdS(sCreates the targe file.ttouchN(RRR)(RR,((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR*s( RRRRR R!R RR*(((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR$hs  tFileOpencBseZdZejdZedZdZddZ ddZ ddZ e jdZdd Zd Zd Zd Zd ZdZddZdZedZRS(sThe FileOpen object; provides basic access to a file. Class attributes: log (logging.Logger) - logger for this class Attributes: None sutils.FileOpencCs|jS(N(t_FileOpen__file(R((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pytfilescCs|jjdS(N(R0tflush(R((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR1sicCs|jj|S(N(R0tread(Rt numberOfBytes((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR2scCs|jj|S(N(R0treadline(RR3((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR4scCs-|dkr|jjS|jj|SdS(N(RR0t readlines(RtnumberOfBytesHint((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR5s  cCs|jj||dS(N(R0tseek(Rtoffsettwhence((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR7scCs0|dkr|jjn|jj|dS(N(RR0ttruncate(Rtsize((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR:s cCs|jj|dS(N(R0twrite(Rtstring((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR<scCs|jj|dS(N(R0t writeline(RtsequenceOfStrings((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyt writelinesscCs |jjS(N(R0tnext(R((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyRAscCs_tt|j|jdkr@tj|j|j|_ntj |j|j |_ |S(s!Open the file and return ourself.N( R R.RRRR topenRt_osModetfdopent_FileOpen__modeR/(R((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyRs cCs)|jjtt|j|||S(s Close the file.(R0tcloseR R.R(RRRR((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyRs trcOstt|j|||d}t|dkrPd|dkrPtj}nH|ddkrltj}n,|ddkrtjtj B}n tj}|ddkr|tj B}nd|_ ||_ ||_ dS(s: Arguments: None Returns: Nothing it+iRGtwtaN(R R.R RtlenR tO_RDWRtO_RDONLYtO_WRONLYtO_TRUNCtO_APPENDR/REt_FileOpen__osMode(RRtmodeRRtosMode((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR s"     cCs|S(N((R((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyt__iter__scCs|jS(N(RQ(R((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyRCsN(RRRRR R!R"R0R1R2R4RR5R tSEEK_SETR7R:R<R@RARRR RTRC(((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR.s"          tFileLockcBs;eZdZejdZddZdZdZ RS(sThe FileLock object; a context manager providing interlocked access on a file. The file is created, if necessary. Class attributes: log (logging.Logger) - logger for this class Attributes: _timeout - timeout in seconds (None = no timeout) sutils.FileLockcOs&tt|j||||_dS(s Arguments: filePath - (str) path to file mode - (str) open mode timeout - (int) timeout in seconds; may be None Returns: Nothing N(R RVR t_timeout(RRRRttimeoutRR((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR :s cCs tt|j|jtjkr7tj}d}ntj}d}|j dk r|j j dj d|jd|j d|t|j tdj d|jd|j tj|j|WdQXn8|j j d j d|jd|tj|j||S( s_If the open mode is read-only the file is locked shared else it is locked exclusively. tsharedt exclusives'attempting to lock {f} in {s}s mode {m}tftstms!Could not lock {f} in {s} secondsNsattempting to lock {f} mode {m}(R RVRRCR RMtfcntltLOCK_SHtLOCK_EXRWRR!tdebugtformatRRt_tflockR0(Rt flockModetlockModeString((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyRGs(         cCs`tj|jtj|tk rD|jjdjd|jnt t |j |||S(s Unlocks and closes the file.sreleased lock {f}R[( R^RdR0tLOCK_UNRR!RaRbRR RVR(RRRR((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyRds  "N( RRRRR R!RR RR(((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyRV&s   tFileTempcBs8eZdZejdZddddZdZRS(sThe FileTemp object; a context manager providing temporary files with specified (or default) owner and permissions. An optional destination parameter specifies the location to which the temp file should be moved at exit, if no exception is encountered. The move, if specified, is performed after performing the owner manipulations. Class attributes: log (logging.Logger) - logger for this class Attributes: None sutils.FileTempcOs|tj}tt|j|ddd|d|sNttj}n|s]d}n||_||_ ||_ dS(s Arguments: owner - (str) the owner to set for the file ownerPerm - (str) the permissions to set for the owner destination (str) the path to which to move the temp file on exit Returns: Nothing isr+R itrwN( ttempfiletmkstempR RhR tstrR tgeteuidt_FileTemp__ownert_FileTemp__ownerPermt_FileTemp__destination(Rtownert ownerPermt destinationRRttmpFile((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyR s '   cCs6|}|dkr|jdk rytd|j|jg}|jtddjd|jg|jg}|j|jj td|j|jg}|jWqt k r}|}qXn|dk s|jdkry#td|jg}|jWqqXnt t |j |||S(Ntchowntchmodt=tutmvtrm(RRpRRnRR)tjoinRoR0RFt ExceptionR RhR(RRRRt exceptionR,tex((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyRs* *   N( RRRRR R!RR R(((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyRhws  (Rt __future__RterrnoR^tgettexttgrpRR tstatRjttimeRRRtinstalltobjectRR$R.RVRh(((s7/usr/lib/python2.7/site-packages/vdo/utils/FileUtils.pyts$          ;=Q