Utility functions for I/O.
Class |
|
Helper to store file handle's fstat. |
Class |
|
Stores the list of files to be deleted and removes them on demand. |
Function |
|
Converts bytes to mebibytes. |
Function |
|
Calculates the size of a directory recursively. |
Function |
|
Returns True if the user can access (read) the file. |
Function |
|
Creates a backup of a file. |
Function |
|
Compute a ganeti pid file absolute path |
Function |
|
Enforces that given path has given permissions. |
Function |
|
Make required directories, if they don't exist. |
Function |
|
Format an EnvironmentError exception. |
Function |
|
Look for a filesystem object in a given path. |
Function |
|
Returns the file 'id', i.e. the dev/inode and mtime information. |
Function |
|
Returns the total and free space on a filesystem. |
Function |
|
Check whether a path is below a root dir. |
Function |
|
Check whether a path is absolute and also normalized |
Function |
|
Check whether a path is a HTTP URL. |
Function |
|
Returns True if the user belongs to the group. |
Function |
|
Returns a list of visible files in a directory. |
Function |
|
Super-mkdir; create a leaf directory and all intermediate ones. |
Function |
|
Enforces that given path is a dir and has given mode, uid and gid set. |
Function |
|
Returns a random UUID. |
Function |
|
Returns a text I/O object pointing a TTY (/dev/tty by default) |
Function |
|
Safe-join a list of path components. |
Function |
|
Reads a binary file. |
Function |
|
Reads a text file. |
Function |
|
Reads a locked PID file. |
Function |
|
Return the first non-empty line from a file. |
Function |
|
Read a pid from a file. |
Function |
|
Reads the watcher pause file. |
Function |
|
Remove an empty directory. |
Function |
|
Remove a file ignoring some errors. |
Function |
|
Renames a file. |
Function |
|
Wraper over WriteFile that locks the target file. |
Function |
|
Return the last lines from a file. |
Function |
|
Returns the current time formatted for filenames. |
Function |
|
Verifies that two file IDs are matching. |
Function |
|
(Over)write a file atomically. |
Function |
|
Write the current process pidfile. |
Constant | KEEP |
Undocumented |
Constant | KP |
Undocumented |
Constant | KP |
Undocumented |
Constant | KP |
Undocumented |
Constant | URL |
Undocumented |
Function | _ |
Tries to extract a process ID from a PID file's content. |
Constant | _LOST |
Undocumented |
Calculates the size of a directory recursively.
Parameters | |
path:string | Path to directory |
Returns | |
int | Size in mebibytes |
Returns True if the user can access (read) the file.
Parameters | |
username:string | the name of the user |
filename:string | the name of the file |
Returns | |
bool | Undocumented |
Creates a backup of a file.
Parameters | |
file | file to be backed up |
Returns | |
str | the path to the newly created backup |
Raises | |
errors.ProgrammerError | for invalid file names |
Compute a ganeti pid file absolute path
Parameters | |
name:str | the daemon name |
Returns | |
str | the full path to the pidfile corresponding to the given daemon name |
Enforces that given path has given permissions.
Parameters | |
path | The path to the file |
mode | The mode of the file |
uid | The uid of the owner of this file |
gid | The gid of the owner of this file |
must | Specifies if non-existance of path will be an error |
_chmod | chmod function to use (unittest only) |
_chown | chown function to use (unittest only) |
_stat | Undocumented |
Make required directories, if they don't exist.
Parameters | |
dirs:list of (string, integer) | list of tuples (dir_name, dir_mode) |
Format an EnvironmentError exception.
If the err
argument has an errno attribute, it will be looked up and converted into a textual E... description. Otherwise the string representation of the error will be returned.
Parameters | |
err:EnvironmentError | the exception to format |
Look for a filesystem object in a given path.
This is an abstract method to search for filesystem object (files, dirs) under a given search path.
Parameters | |
name:str | the name to look for |
search | locations to start at |
test:callable | a function taking one argument that should return True if the a given object is valid; the default value is os.path.exists, causing only existing files to be returned |
Returns | |
str or None | full path to the object if found, None otherwise |
Returns the file 'id', i.e. the dev/inode and mtime information.
Either the path to the file or the fd must be given.
Parameters | |
path | the file path |
fd | a file descriptor |
Returns | |
a tuple of (device number, inode number, mtime) |
Returns the total and free space on a filesystem.
Parameters | |
path:string | Path on filesystem to be examined |
Returns | |
int | tuple of (Total space, Free space) in mebibytes |
Check whether a path is below a root dir.
This works around the nasty byte-byte comparison of commonprefix.
Check whether a path is absolute and also normalized
This avoids things like /dir/../../other/path to be valid.
Returns True if the user belongs to the group.
Parameters | |
uid:int | the user id |
gid:int | the group id |
Returns | |
bool | Undocumented |
Returns a list of visible files in a directory.
Parameters | |
path:str | the directory to enumerate |
_is | Undocumented |
Returns | |
list | the list of all files not starting with a dot |
Raises | |
ProgrammerError | if path is not an absolue and normalized path |
Super-mkdir; create a leaf directory and all intermediate ones.
This is a wrapper around os.makedirs adding error handling not implemented before Python 2.5.
Enforces that given path is a dir and has given mode, uid and gid set.
Parameters | |
path | The path to the file |
mode | The mode of the file |
uid | The uid of the owner of this file |
gid | The gid of the owner of this file |
_lstat | Stat function to use (unittest only) |
_mkdir | mkdir function to use (unittest only) |
_perm | permission setter function to use (unittest only) |
Returns a random UUID.
Returns | |
str | Undocumented |
Note | |
This is a Linux-specific method as it uses the /proc filesystem. |
Returns a text I/O object pointing a TTY (/dev/tty by default)
As of Python 3.7, /dev/tty cannot be opened in buffered and/or text mode, see https://bugs.python.org/issue20074. Work around this by using TextIOWrapper over a BufferedRWPair to return a line-buffered TTY I/O object.
Parameters | |
device:string | path to the TTY/PTY device to open |
Safe-join a list of path components.
Requirements:
- the first argument must be an absolute path
- no component in the path must have backtracking (e.g. /../), since we check for normalization at the end
Parameters | |
*args | the path components to be joined |
Raises | |
ValueError | for invalid paths |
Reads a binary file.
Parameters | |
file | Undocumented |
size:int | Read at most size bytes (if negative, entire file) |
preread:callable receiving file handle as single parameter | Function called before file is read |
Returns | |
bytes | the (possibly partial) content of the file |
Raises | |
IOError | if the file cannot be opened |
Reads a text file.
Parameters | |
file | Undocumented |
size:int | Read at most size bytes (if negative, entire file) |
preread:callable receiving file handle as single parameter | Function called before file is read |
Returns | |
str | the (possibly partial) content of the file |
Raises | |
IOError | if the file cannot be opened |
Reads a locked PID file.
This can be used together with utils.process.StartDaemon
.
Parameters | |
path:string | Path to PID file |
Returns | |
PID as integer or, if file was unlocked or couldn't be opened, None |
Return the first non-empty line from a file.
Parameters | |
file | Undocumented |
strict:boolean | if True, abort if the file has more than one non-empty line |
Read a pid from a file.
Parameters | |
pidfile:string | path to the file containing the pid |
Returns | |
int | The process id, if the file exists and contains a valid PID, otherwise 0 |
Reads the watcher pause file.
Parameters | |
filename:string | Path to watcher pause file |
now:None, float or int | Current time as Unix timestamp |
remove | Remove watcher pause file after specified amount of seconds past the pause end time |
Remove an empty directory.
Remove a directory, ignoring non-existing ones. Other errors are passed. This includes the case, where the directory is not empty, so it can't be removed.
Parameters | |
dirname:str | the empty directory to be removed |
Remove a file ignoring some errors.
Remove a file, ignoring non-existing ones or directories. Other errors are passed.
Parameters | |
filename:str | the file to be removed |
Renames a file.
This just creates the very least directory if it does not exist and mkdir is set to true.
Parameters | |
old:string | Original path |
new:string | New path |
mkdir:bool | Whether to create target directory if it doesn't exist |
mkdir | Mode for newly created directories |
dir | The uid for the (if fresh created) dir |
dir | The gid for the (if fresh created) dir |
Return the last lines from a file.
Parameters | |
fname | the file name |
lines:int | the (maximum) number of lines to return |
Note | |
this function will only read and parse the last 4KB of the file; if the lines are very long, it could be that less than the requested number of lines are returned |
Returns the current time formatted for filenames.
The format doesn't contain colons as some shells and applications treat them as separators. Uses the local timezone.
Verifies that two file IDs are matching.
Differences in the inode/device are not accepted, but and older timestamp for fi_disk is accepted.
Parameters | |
fi | tuple (dev, inode, mtime) representing the actual file data |
fi | tuple (dev, inode, mtime) representing the last written file data |
Returns | |
boolean | Undocumented |
(Over)write a file atomically.
The file_name and either fn (a function taking one argument, the file descriptor, and which should write the data to it) or data (the contents of the file) must be passed. The other arguments are optional and allow setting the file mode, owner and group, and the mtime/atime of the file.
If the function doesn't raise an exception, it has succeeded and the target file has the new contents. If the function has raised an exception, an existing target file should be unmodified and the temporary file should be removed.
Parameters | |
file | the target filename |
fn:callable | content writing function, called with file descriptor as parameter |
data:str or bytes | contents of the file |
mode:int | file mode |
uid:int | the owner of the file |
gid:int | the group of the file |
atime:int | a custom access time to be set on the file |
mtime:int | a custom modification time to be set on the file |
close:boolean | whether to close file after writing it |
dry | Undocumented |
backup | Undocumented |
prewrite:callable | function to be called before writing content |
postwrite:callable | function to be called after writing content |
keepKEEP_PERMS_VALUES | if KP_NEVER (default), owner, group, and mode are taken from the other parameters; if KP_ALWAYS , owner, group, and mode are copied from the existing file; if KP_IF_EXISTS , owner, group, and mode are taken from the file, and if the file doesn't exist, they are taken from the other parameters. It is an error to pass KP_ALWAYS when the file doesn't exist or when uid, gid, or mode are set to non-default values. |
Returns | |
None or int | None if the 'close' parameter evaluates to True, otherwise the file descriptor |
Raises | |
errors.ProgrammerError | if any of the arguments are not valid |
Write the current process pidfile.
Parameters | |
pidfile:string | the path to the file to be written |
Returns | |
int | the file descriptor of the lock file; do not close this unless you want to unlock the pid file |
Raises | |
errors.LockError | if the pid file already exists and points to a live process |