netsa.files — File and Path Manipulation

The routines in netsa.files are intended to help with manipulation of files in the filesystem as well as pathnames.

Paths

netsa.files.relpath(p : str, base : str) → str

Given a target path along with a reference path, return the relative path from the target to the reference.

This is a logical operation that does not consult the physical filesystem.

os.path.relpath in Python 2.6 adds something similar to this.

netsa.files.is_relpath(p : str, base : str) → bool

Given a target path along with a base reference path, return whether or not the base path subsumes the target path.

This is a logical operation that does not consult the physical filesystem.

Process ID Locks

This form of lock is generally used by services that wish to ensure that only one copy of the service is running at a time.

netsa.files.acquire_pidfile_lock(path : str) → bool

Attempts to acquire a locking PID file at the requested pathname path. If the file does not exist, creates it with the current process ID. If the file does exist but refers to a no-longer-existing process, attempts to replace it. If the file does exist and refers to a running process, does nothing.

Registers the lock to be released (as with release_pidfile_lock) when the currently running process exits, if it has not already been released.

Returns True if this process holds the lock, or False if another running process holds the lock.

netsa.files.examine_pidfile_lock(path : str) → (int, bool) or None

Examines the state of a locking PID file at path and returns it. If the file does not exist or is not in the proper format, returns None. If the file does exist and contains a PID, returns (pid, state) where pid is the process ID holding the lock, and state is True if a running process has that process ID, or False otherwise.

netsa.files.release_pidfile_lock(path : str)

Attempts to release a locking PID file at the requested pathname path. If the file does not exist or contains a lock for a different PID, does nothing. Otherwise, unlinks the file.

Temporary Files

netsa.files.get_temp_file_name([file_name : str]) → str

Return the path to a file named file_name in a temporary directory that will be cleaned up when the process exits. If file_name is None then a new file name is created that has not been used before.

A temporary file at the named location will be cleaned up as long as the Python interpreter exits normally.

netsa.files.get_temp_file([file_name : str, mode='r']) → file

Returns an open file object for the file named file_name in the temporary working directory, with the given mode (as described in open. If file_name is None then a new file name is used that has not been used before.

The resulting temporary file will be cleaned up as long as the Python interpreter exits normally.

netsa.files.get_temp_pipe_name([pipe_name : str]) → str

Returns the path to a named pipe file_name that has been created in a temporary directory that will be cleaned up when the process exits. If file_name is None then a new file name is created that has not been used before.