/usr/lib/python3/dist-packages/google/apputils/file_util.py is in python3-google-apputils 0.4.1-1ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | #!/usr/bin/env python
# Copyright 2007 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Simple file system utilities."""
__author__ = ('elaforge@google.com (Evan LaForge)',
'matthewb@google.com (Matthew Blecker)')
import contextlib
import errno
import os
import pwd
import shutil
import stat
import tempfile
class PasswdError(Exception):
"""Exception class for errors loading a password from a file."""
def ListDirPath(dir_name):
"""Like os.listdir with prepended dir_name, which is often more convenient."""
return [os.path.join(dir_name, fn) for fn in os.listdir(dir_name)]
def Read(filename):
"""Read entire contents of file with name 'filename'."""
with open(filename) as fp:
return fp.read()
def Write(filename, contents, overwrite_existing=True, mode=0o666, gid=None):
"""Create 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 0o666 octal)
gid: int; group id with which to create the file
"""
flags = os.O_WRONLY | os.O_TRUNC | os.O_CREAT
if not overwrite_existing:
flags |= os.O_EXCL
fd = os.open(filename, flags, mode)
try:
os.write(fd, contents)
finally:
os.close(fd)
if gid is not None:
os.chown(filename, -1, gid)
def AtomicWrite(filename, contents, mode=0o666, gid=None):
"""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 0o666 octal)
gid: int; group id with which to create the file
"""
fd, tmp_filename = tempfile.mkstemp(dir=os.path.dirname(filename))
try:
os.write(fd, contents)
finally:
os.close(fd)
try:
os.chmod(tmp_filename, mode)
if gid is not None:
os.chown(tmp_filename, -1, gid)
os.rename(tmp_filename, filename)
except OSError as exc:
try:
os.remove(tmp_filename)
except OSError as e:
exc = OSError('%s. Additional errors cleaning up: %s' % (exc, e))
raise exc
@contextlib.contextmanager
def TemporaryFileWithContents(contents, **kw):
"""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.
"""
temporary_file = tempfile.NamedTemporaryFile(**kw)
temporary_file.write(contents)
temporary_file.flush()
yield temporary_file
temporary_file.close()
# TODO(user): remove after migration to Python 3.2.
# This context manager can be replaced with tempfile.TemporaryDirectory in
# Python 3.2 (http://bugs.python.org/issue5178,
# http://docs.python.org/dev/library/tempfile.html#tempfile.TemporaryDirectory).
@contextlib.contextmanager
def TemporaryDirectory(suffix='', prefix='tmp', base_path=None):
"""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.
"""
temp_dir_path = tempfile.mkdtemp(suffix, prefix, base_path)
try:
yield temp_dir_path
finally:
try:
shutil.rmtree(temp_dir_path)
except OSError as e:
if e.message == 'Cannot call rmtree on a symbolic link':
# Interesting synthetic exception made up by shutil.rmtree.
# Means we received a symlink from mkdtemp.
# Also means must clean up the symlink instead.
os.unlink(temp_dir_path)
else:
raise
def MkDirs(directory, force_mode=None):
"""Makes 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()
"""
name = os.path.normpath(directory)
dirs = name.split(os.path.sep)
for i in range(0, len(dirs)):
path = os.path.sep.join(dirs[:i+1])
try:
if path:
os.mkdir(path)
# only chmod if we created
if force_mode is not None:
os.chmod(path, force_mode)
except OSError as exc:
if not (exc.errno == errno.EEXIST and os.path.isdir(path)):
raise
def RmDirs(dir_name):
"""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.
"""
try:
shutil.rmtree(dir_name)
except OSError as err:
if err.errno != errno.ENOENT:
raise
try:
parent_directory = os.path.dirname(dir_name)
while parent_directory:
try:
os.rmdir(parent_directory)
except OSError as err:
if err.errno != errno.ENOENT:
raise
parent_directory = os.path.dirname(parent_directory)
except OSError as err:
if err.errno not in (errno.EACCES, errno.ENOTEMPTY, errno.EPERM):
raise
def HomeDir(user=None):
"""Find 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.
"""
if user is None:
pw_struct = pwd.getpwuid(os.geteuid())
elif isinstance(user, int):
pw_struct = pwd.getpwuid(user)
elif isinstance(user, str):
pw_struct = pwd.getpwnam(user)
else:
raise TypeError('user must be None or an instance of int or str')
return pw_struct.pw_dir
|