Package grizzled :: Module os
[hide private]
[frames] | no frames]

Module os

source code

The grizzled.os module contains some operating system-related functions and classes. It is a conceptual extension of the standard Python os module.

Classes [hide private]
  DaemonError
Thrown by daemonize() when an error occurs while attempting to create a daemon.
Functions [hide private]
str
path_separator()
Get the path separator for the current operating system.
source code
str
file_separator(*__args, **__kw)
Get the file separator for the current operating system.
source code
 
path_elements(path)
Given a path string value (e.g., the value of the environment variable PATH), this generator function yields each item in the path.
source code
 
working_directory(*args, **kwds)
This function is intended to be used as a with statement context manager.
source code
str
find_command(command_name, path=None)
Determine whether the specified system command exists in the specified path.
source code
 
spawnd(path, args, pidfile=None)
Run a command as a daemon.
source code
 
daemonize(no_close=False, pidfile=None)
Convert the calling process into a daemon.
source code
Variables [hide private]
  UMASK = 0
  WORKDIR = '/'
  MAXFD = 1024
  NULL_DEVICE = '/dev/null'
  PATH_SEPARATOR = {'java': ':', 'nt': ';', 'posix': ':'}
  FILE_SEPARATOR = {'java': '/', 'nt': '\\', 'posix': '/'}
  log = logging.getLogger('grizzled.os')
  __package__ = None
hash(x)
Function Details [hide private]

path_separator()

source code 
Get the path separator for the current operating system. The path separator is used to separate elements of a path string, such as "PATH" or "CLASSPATH". (It's a ":" on Unix-like systems and a ";" on Windows.)
Returns: str
the path separator

file_separator(*__args, **__kw)

source code 

Get the file separator for the current operating system. The file separator is used to separate file elements in a pathname. (It's "/" on Unix-like systems and a "\" on Windows.)

Deprecated. Use the standard Python os.path.sep variable, instead.

Returns: str
the file separator
Decorators:
  • @deprecated(since= '0.7', message= 'Use os.path.sep, instead.')

path_elements(path)

source code 
Given a path string value (e.g., the value of the environment variable PATH), this generator function yields each item in the path.
Parameters:
  • path - the path to break up

working_directory(*args, **kwds)

source code 

This function is intended to be used as a with statement context manager. It allows you to replace code like this:

original_directory = _os.getcwd()
try:
    _os.chdir(some_dir)
    ... bunch of code ...
finally:
    _os.chdir(original_directory)

with something simpler:

from __future__ import with_statement
from grizzled.os import working_directory

with working_directory(some_dir):
    ... bunch of code ...
Parameters:
  • directory (str) - directory in which to execute
Returns:
yields the directory parameter
Decorators:
  • @contextmanager

find_command(command_name, path=None)

source code 
Determine whether the specified system command exists in the specified path.
Parameters:
  • command_name - The (simple) filename of the command to find. May be a glob string.
  • path - The path to search, as a list or a string. If this parameter is a string, then it is split using the operating system-specific path separator. If this parameter is missing, then the PATH environment variable is used
Returns: str
The path to the first command that matches command_name, or None if not found

spawnd(path, args, pidfile=None)

source code 

Run a command as a daemon. This method is really just shorthand for the following code:

daemonize(pidfile=pidfile)
_os.execv(path, args)
Parameters:
  • path (str) - Full path to program to run
  • args (list) - List of command arguments. The first element in this list must be the command name (i.e., arg0).
  • pidfile (str) - Path to file to which to write daemon's process ID. The string may contain a ${pid} token, which is replaced with the process ID of the daemon. e.g.: /var/run/myserver-${pid}

daemonize(no_close=False, pidfile=None)

source code 

Convert the calling process into a daemon. To make the current Python process into a daemon process, you need two lines of code:

from grizzled.os import daemonize
daemonize.daemonize()

If daemonize() fails for any reason, it throws a DaemonError, which is a subclass of the standard OSError exception. also logs debug messages, using the standard Python logging package, to channel "grizzled.os.daemon".

Adapted from: http://software.clapper.org/daemonize/

See Also:

  • Stevens, W. Richard. Unix Network Programming (Addison-Wesley, 1990).
Parameters:
  • no_close (bool) - If True, don't close the file descriptors. Useful if the calling process has already redirected file descriptors to an output file. Warning: Only set this parameter to True if you're sure there are no open file descriptors to the calling terminal. Otherwise, you'll risk having the daemon re-acquire a control terminal, which can cause it to be killed if someone logs off that terminal.
  • pidfile (str) - Path to file to which to write daemon's process ID. The string may contain a ${pid} token, which is replaced with the process ID of the daemon. e.g.: /var/run/myserver-${pid}
Raises: