Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib/python3.5/site-packages/google/apputils/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib/python3.5/site-packages/google/apputils/__pycache__/file_util.cpython-35.pyc



C��]��@sdZdZddlZddlZddlZddlZddlZddlZddlZGdd�de	�Z
dd�Zd	d
�Zdddd
d�Z
dddd�Zejdd��Zejddddd��Zddd�Zdd�Zddd�ZdS)zSimple file system utilities.�"elaforge@google.com (Evan LaForge)�%matthewb@google.com (Matthew Blecker)�Nc@seZdZdZdS)�PasswdErrorz:Exception class for errors loading a password from a file.N)�__name__�
__module__�__qualname__�__doc__�r	r	�J/opt/alt/python35/lib/python3.5/site-packages/google/apputils/file_util.pyrsrcs �fdd�tj��D�S)zHLike os.listdir with prepended dir_name, which is often more convenient.cs%g|]}tjj�|��qSr	)�os�path�join)�.0�fn)�dir_namer	r
�
<listcomp>#s	zListDirPath.<locals>.<listcomp>)r�listdir)rr	)rr
�ListDirPath!src	Cs$t|��}|j�SWdQRXdS)z2Read entire contents of file with name 'filename'.N)�open�read)�filename�fpr	r	r
�Read&srTi�cCs�tjtjBtjB}|s*|tjO}tj|||�}ztj||�Wdtj|�X|dk	r�tj|d|�dS)aOCreate a file 'filename' with 'contents', with the mode given in 'mode'.

  The 'mode' is modified by the umask, as in open(2).  If
  'overwrite_existing' is False, the file will be opened in O_EXCL mode.

  An optional gid can be specified.

  Args:
    filename: str; the name of the file
    contents: str; the data to write to the file
    overwrite_existing: bool; whether or not to allow the write if the file
                        already exists
    mode: int; permissions with which to create the file (default is 0666 octal)
    gid: int; group id with which to create the file
  N����)	r�O_WRONLY�O_TRUNC�O_CREAT�O_EXCLr�write�close�chown)r�contentsZoverwrite_existing�mode�gid�flags�fdr	r	r
�Write,s
r'c$Cs
tjdtjj|��\}}ztj||�Wdtj|�XyCtj||�|dk	r{tj|d|�tj	||�Wnzt
k
r}zZytj|�Wn;t
k
r�}zt
d||f�}WYdd}~XnX|�WYdd}~XnXdS)a_Create a file 'filename' with 'contents' atomically.

  As in Write, 'mode' is modified by the umask.  This creates and moves
  a temporary file, and errors doing the above will be propagated normally,
  though it will try to clean up the temporary file in that case.

  This is very similar to the prodlib function with the same name.

  An optional gid can be specified.

  Args:
    filename: str; the name of the file
    contents: str; the data to write to the file
    mode: int; permissions with which to create the file (default is 0666 octal)
    gid: int; group id with which to create the file
  �dirNrz%%s. Additional errors cleaning up: %sr)�tempfile�mkstemprr�dirnamerr �chmodr!�rename�OSError�remove)rr"r#r$r&Ztmp_filename�exc�er	r	r
�AtomicWriteHs$)r2cks9tj|�}|j|�|j�|V|j�dS)a[A contextmanager that writes out a string to a file on disk.

  This is useful whenever you need to call a function or command that expects a
  file on disk with some contents that you have in memory. The context manager
  abstracts the writing, flushing, and deletion of the temporary file. This is a
  common idiom that boils down to a single with statement.

  Note:  if you need a temporary file-like object for calling an internal
  function, you should use a StringIO as a file-like object and not this.
  Temporary files should be avoided unless you need a file name or contents in a
  file on disk to be read by some other function or program.

  Args:
    contents: a string with the contents to write to the file.
    **kw: Optional arguments passed on to tempfile.NamedTemporaryFile.
  Yields:
    The temporary file object, opened in 'w' mode.

  N)r)�NamedTemporaryFiler�flushr )r"�kwZtemporary_filer	r	r
�TemporaryFileWithContentsks


r6��tmpccs�tj|||�}z	|VWdytj|�WnGtk
r{}z'|jdkrftj|�n�WYdd}~XnXXdS)a�A context manager to create a temporary directory and clean up on exit.

  The parameters are the same ones expected by tempfile.mkdtemp.
  The directory will be securely and atomically created.
  Everything under it will be removed when exiting the context.

  Args:
    suffix: optional suffix.
    prefix: options prefix.
    base_path: the base path under which to create the temporary directory.
  Yields:
    The absolute path of the new temporary directory.
  Nz%Cannot call rmtree on a symbolic link)r)�mkdtemp�shutil�rmtreer.�messager�unlink)�suffix�prefix�	base_pathZ
temp_dir_pathr1r	r	r
�TemporaryDirectory�s	rAcCs�tjj|�}|jtjj�}x�tdt|��D]�}tjjj|d|d��}y3|r�tj|�|dk	r�tj	||�Wq=t
k
r�}z,|jtjko�tjj
|�s��WYdd}~Xq=Xq=WdS)aZMakes a directory including its parent directories.

  This function is equivalent to os.makedirs() but it avoids a race
  condition that os.makedirs() has.  The race is between os.mkdir() and
  os.path.exists() which fail with errors when run in parallel.

  Args:
    directory: str; the directory to make
    force_mode: optional octal, chmod dir to get rid of umask interaction
  Raises:
    Whatever os.mkdir() raises when it fails for any reason EXCLUDING
    "dir already exists".  If a directory already exists, it does not
    raise anything.  This behaviour is different than os.makedirs()
  rNr)rr�normpath�split�sep�range�lenr
�mkdirr,r.�errno�EEXIST�isdir)�	directoryZ
force_mode�name�dirs�irr0r	r	r
�MkDirs�s#
$rOc$Cs!ytj|�Wn:tk
rM}z|jtjkr;�WYdd}~XnXy�tjj|�}xj|r�ytj|�Wn:tk
r�}z|jtjkr��WYdd}~XnXtjj|�}qfWWnItk
r}z)|jtj	tj
tjfkr
�WYdd}~XnXdS)z�Removes dir_name and every subsequently empty directory above it.

  Unlike os.removedirs and shutil.rmtree, this function doesn't raise an error
  if the directory does not exist.

  Args:
    dir_name: Directory to be removed.
  N)r:r;r.rH�ENOENTrrr+�rmdirZEACCESZ	ENOTEMPTY�EPERM)r�errZparent_directoryr	r	r
�RmDirs�s"		!rTcCsy|dkr$tjtj��}nNt|t�rEtj|�}n-t|t�rftj|�}ntd��|j	S)a4Find the home directory of a user.

  Args:
    user: int, str, or None - the uid or login of the user to query for,
          or None (the default) to query for the current process' effective user

  Returns:
    str - the user's home directory

  Raises:
    TypeError: if user is not int, str, or None.
  Nz.user must be None or an instance of int or str)
�pwd�getpwuidr�geteuid�
isinstance�int�str�getpwnam�	TypeError�pw_dir)�userZ	pw_structr	r	r
�HomeDir�s
r_)rr)r�
__author__�
contextlibrHrrUr:�statr)�	Exceptionrrrr'r2�contextmanagerr6rArOrTr_r	r	r	r
�<module>s(#