Pycopia Process package

These are modules control or interact with other processes.

Proctools

Process manager and process objects. Presents a Pythonic file-like interface to subprocesses.

exception pycopia.proctools.AuthenticationError[source]
class pycopia.proctools.CoProcessPipe(method, logfile=None, env=None, callback=None, merge=False, async=False, pwent=None, _pgid=0)[source]
class pycopia.proctools.CoProcessPty(method, logfile=None, env=None, callback=None, async=False, pwent=None, _pgid=0)[source]
exception pycopia.proctools.NotFoundError[source]

Raised when the which function cannot find the given command.

class pycopia.proctools.ProcManager[source]

An instance of ProcManager manages a collection of child processes. It is a singleton, and you should use the get_procmanager() factory function to get the instance.

clone(proc=None)[source]

clone([proc]) clones the supplied process object and manages it as well. If no process object is supplied then clone the first managed process found in this ProcManager.

coprocess(method, args=(), logfile=None, env=None, callback=None, async=False)[source]
flushlogs()[source]

Force flushing all process logs.

getbyname(name)[source]

getbyname(procname) Returns a list of process objects that match the given name.

getbypid(pid)[source]

getbypid(pid) Returns the process object that matches the given PID.

getpids()[source]

getpids() Returns a list of managed PIDs (which are integers).

getprocs()[source]

getprocs() Returns a list of managed process objects.

getstats()[source]

getstats() Returns a list of process status objects (ProcStat) for each managed process.

kill(proc, sig=2)[source]
killall(name=None, sig=15)[source]

killall([name, [SIG]]) Kills all managed processes with the name ‘name’. If ‘name’ not given kill ALL processes. Default signal is SIGTERM.

loop(timeout=-1.0, callback=NULL)[source]
respawn_callback(deadproc)[source]

Callback that performs a respawn, for persistent services.

spawn(cmd, logfile=None, env=None, callback=None, persisten=None)

Start a child process, connected by pipes.

spawnpipe(cmd, logfile=None, env=None, callback=None, persistent=False, merge=True, pwent=None, async=False, devnull=False)[source]

spawn(cmd, logfile=None, env=None, callback=None, persisten=None) Start a child process, connected by pipes.

spawnprocess(pklass, cmd, logfile=None, env=None, callback=None, persistent=False, merge=True, pwent=None, async=False, devnull=False)[source]

spawnclass(classobj, cmd, logfile=None, env=None, callback=None, persistent=0) Start a child process using a user supplied subclass of ProcessPty or ProcessPipe.

spawnpty(cmd, logfile=None, env=None, callback=None, persisten=None)[source]

Start a child process using a pty. The <persistent> variable is the number of times the process will be respawned if the previous invocation dies.

stopall()[source]

stopall() sends STOP to all managed processes. To restart get the process objects and invoke the cont() method.

submethod(_method, args=None, kwargs=None, pwent=None)[source]
subprocess(_method, *args, **kwargs)[source]
waitpid(pid, option=0)[source]
waitproc(process[, option])[source]

Waits for a process object to finish. Works like os.waitpid, but takes a process object instead of a process ID.

class pycopia.proctools.Process(cmdline, logfile=None, callback=None, async=False, flags=0, devnull=False)[source]

Abstract base class for Processes. Handles all process handling, and some common functionality. I/O is handled in subclasses.

basename[source]
clone()[source]

Spawns a copy of this process. Note that the log file is not inherited.

close()[source]
cont()[source]
dead()[source]

dead() Called when the child dies. Usually only the ProcManager uses this.

environment
error_handler()[source]
exception_handler(ex, val, tb)[source]
flush()[source]
flushlog()[source]
fstat()[source]
getlog()[source]
hangup()[source]
hangup_handler()[source]
isdead()[source]
kill(sig=2)[source]
killwait(sig=2)[source]
log(entry)[source]
logfile

logfile object

newlog(newlog)[source]
pri_handler()[source]
priority()[source]
read(amt=2147483646)[source]
read_handler()[source]
readable()[source]
readerr(amt=2147483646)[source]
readline(amt=2147483646)[source]
readlines(sizehint=2147483646)[source]
removelog()[source]
restart(flag=1)[source]
rewind()[source]
seek(pos, whence=0)[source]
send(data)
set_callback(cb=None)[source]

set_callback(cb) Sets the callback function that will be called when child dies.

set_exitstatus(exitstatus)[source]
setlog(newlog)
setpgid(pgid)[source]
stat()[source]
stop()[source]
tell()[source]
wait(option=0)[source]

wait() retrieves process exit status. Note that this may block if the process is still running.

writable()[source]
write(data)[source]
write_handler()[source]
exception pycopia.proctools.ProcessError[source]
class pycopia.proctools.ProcessNamedPipe(cmdline, logfile=None, callback=None, async=False, flags=0, devnull=False)[source]
class pycopia.proctools.ProcessPipe(cmdline, logfile=None, env=None, callback=None, merge=1, pwent=None, async=False, devnull=None, _pgid=0)[source]

Process(<commandline>, [<logfile>], [environ]) Forks and execs a process as given by the command line argument. The process’s stdio is connected to this instance via pipes, and can be read and written to by the instances read() and write() methods.

close()[source]
fileno()[source]
filenos()[source]

filenos() Returns tuple of all file descriptors used in this object.

interrupt()[source]
isatty()[source]
nonblocking(flag=1)[source]
class pycopia.proctools.ProcessPipeline(cmdline, logfile=None, env=None, callback=None, merge=None, pwent=None, async=False, devnull=None, _pgid=0)[source]

Connects two commands via a pipe, they appear as one process object.

class pycopia.proctools.ProcessPty(cmdline, logfile=None, env=None, callback=None, merge=1, pwent=None, async=False, devnull=False, _pgid=0)[source]

ProcessPty(<commandline>, [<logfilename>], [environ]) Forks and execs a process as given by the command line argument. The process’s stdio is connected to this instance via a pty, and can be read and written to by the instances read() and write() methods. That pty becomes the processes controlling terminal.

close()[source]
fileno()[source]
filenos()[source]

filenos() Returns tuple of all file descriptors used in this object.

interrupt()[source]

Like pressing Ctl-C on most terminals.

isatty()[source]
nonblocking(flag=1)[source]
send_eof()[source]

Like pressing Ctl-D on most terminals.

class pycopia.proctools.SubProcess(pwent=None, _pgid=0)[source]
pycopia.proctools.call(*args, **kwargs)[source]
pycopia.proctools.coprocess(func, args[, logfile, callback])[source]

Works like fork(), but connects the childs stdio to a pty. Returns a file-like object connected to the master end of the child pty.

pycopia.proctools.get_poller()[source]
pycopia.proctools.get_procmanager()[source]

get_procmanager() returns the procmanager. A ProcManager is a singleton instance. Always use this factory function to get it.

pycopia.proctools.getstatusoutput(cmd, logfile=None, env=None, callback=None)[source]
pycopia.proctools.remove_poller()[source]
pycopia.proctools.remove_procmanager()[source]
pycopia.proctools.run_as(pwent, umask=18)[source]

Drop privileges to given user’s password entry, and set up environment. Assumes the parent process has root privileges.

pycopia.proctools.set_nonblocking(fd, flag=1)[source]
pycopia.proctools.setpgid(pid_or_proc, pgrp)[source]
pycopia.proctools.spawnpipe(cmd, logfile=None, env=None, callback=None, persistent=False, merge=True, pwent=None, async=False)[source]

spawn(cmd, logfile=None, env=None) Start a child process, connected by pipes.

pycopia.proctools.spawnpty(cmd, logfile=None, env=None)[source]

Start a child process using a pty.

pycopia.proctools.submethod(_method, args=None, kwargs=None, pwent=None)[source]
pycopia.proctools.subprocess(method, *args, **kwargs)[source]
pycopia.proctools.system(cmd)[source]
pycopia.proctools.waitproc(proc, option=0)[source]
pycopia.proctools.which(basename)[source]

Returns the fully qualified path name (by searching PATH) of the given program name.

Expect

Interact with processes and other io objects using command-response interaction. Provids a powerful interaction engine that can invoke callback function for different events.

class pycopia.expect.Expect(fo=None, prompt=u'$', timeout=90.0, logfile=None, engine=None)[source]

Expect wraps a file-like object and provides enhanced read, write, readline, send, and expect methods. This is very useful when combined with proctool objects running interactive programs (A Process object is a file-like object as well).

The wrapped object need only implement the following methods:

Mandatory:
read(n) write(s) close() fileno()
Optional:
restart(bool) - Turn on or off system call restart. dup() - Duplicate the object and file descriptor (for cloning) interrupt() - Interrupt the wrapped object (usually a process object)
clear_cache()[source]

Clears the pattern cache.

clone(klass=None)[source]
close()[source]
closelog()[source]
delay(time)[source]
expect(patt, mtype=1, callback=None, timeout=None)[source]
expect_exact(patt, callback=None, timeout=None)[source]
expect_glob(patt, callback=None, timeout=None)[source]
expect_regex(patt, callback=None, timeout=None)[source]
fileno()[source]
fileobject()[source]
flushlog()[source]
fstat()[source]
interact(msg=None, escape=None, cmd_interp=None)[source]
interrupt()[source]

interrupt() sends the INTR character to the stream. Actually, delegates this to the wrapped Process object. Otherwise, does nothing.

is_open()[source]
isatty()[source]
openlog(fname)[source]
prompt
read(amt=-1, timeout=None)[source]
read_until(patt=None, timeout=None)[source]
readline(timeout=None)[source]
readlines([N][, filter])[source]

Return a list of lines of input. Read up to N lines, optionally filterered through a filter function.

rewind()[source]
run(engine=None)[source]
seek(pos, whence=0)[source]
send(data)
send_slow(data, delay=0.1)[source]
sendfile(filename, wait_for_prompt=0)[source]
sendfileobject(fp, wait_for_prompt=0)[source]
sendline(text)
set_engine(engine)[source]
set_prompt(prompt)[source]
setlog(fo)[source]
sleep(time)
step()[source]
tcgetpgrp()[source]
ttyname()[source]
wait_for_prompt(timeout=None)[source]
write(data)[source]
writeeol(text)[source]
writeline(text)
writeln(text)[source]
exception pycopia.expect.ExpectError[source]

Raised when the unexpected happens.

pycopia.expect.glob_translate(pat)[source]

Translate a shell (glob style) pattern to a regular expression. There is no way to quote meta-characters.

Utility Modules

These modules use the Expect object to provide easy access to some common programs.

Wrapper for the ssh program.

Provides get_ssh, ssh_command, and scp functions.

pycopia.sshlib.check_version()[source]

Checks that the installed ssh program is the same as this module was tested with (and written for).

pycopia.sshlib.get_procs()[source]

get_ssh_list() Returns list of managed ssh processes.

pycopia.sshlib.get_ssh(host[, user][, password][, prompt][, callback][, logfile])[source]

Uses ssh to get a shell on the given host, and automatically authenticate by password if a password is given. Returns an SSHExpect object.

The logfile parameter should be a file-like object (has a ‘write’ method).

pycopia.sshlib.get_ssh_unsafe(host, ...)[source]

Like get_ssh(), but automatically removes any stale known_hosts entry, if required.

pycopia.sshlib.keygen(keytype='dsa', bits=1024, comment='', filename=None, passphrase=None, logfile=None, async=0, safe=1)[source]

Generate a new ssh user key of the specified keytype.

pycopia.sshlib.keyscan(host, keytype='dsa', logfile=None, async=0)[source]

Run ssh-keyscan. Return key, and program exit status.

pycopia.sshlib.location(host=None, user=None, path=None, forssh=False)[source]

Construct an appropriate ssh/scp path spec based on the combination of parameters. Supply host, user, and path.

pycopia.sshlib.scp(source, destination[, password])[source]

Copies the file from source to destination. these parameters are strings that are passed directly to the scp command, and should follow the syntax for this command.

pycopia.sshlib.ssh_command(host, command[, user][, password][, prompt][, logfile])[source]

Runs the command on the given host via SSH, and return the result.

pycopia.sshlib.ssh_version()[source]

ssh_version() Return the version string for the ssh command on this system.

pycopia.sudo.sudo_command(cmd, password=None, logfile=None)[source]

Run a command with sudo and return the output.

Interface to crontab. You can read, add, change, and submit crontab files to the cron system.

class pycopia.crontab.CrontabFile(text=None, filename=None, username=None)[source]

Represents a user crontab file. You can edit it with the editing methods. It also has list-like methods.

add_event(ical)[source]

Add and event from a iCal entry object. The body of the entry is the command-line to run.

append(obj, lineno=None)[source]

Append any crontab object.

delete_comment(**kwargs)[source]

Delete the variable matching pattern.

delete_crontab(**kwargs)[source]

Delete the CrontabLine matching pattern.

delete_variable(**kwargs)[source]

Delete the variable matching pattern.

find(pattern, start=0, end=-1)[source]

Return a crontab line matching the given pattern.

replace(selection, cronobjects)[source]

Replace a line matching pattern with a new CrontabLine object.

select(start, end)[source]

Select a range of the crontab lines.

class pycopia.crontab.IntSet(data=None, sep=', ', rng='-')[source]

Class implementing sets of integers.

This is an efficient representation for sets consisting of several continuous ranges, e.g. 1-100,200-400,402-1000 is represented internally as a list of three pairs: [(1,100), (200,400), (402,1000)]. The internal representation is always kept normalized.

The constructor has up to three arguments: - the string used to initialize the set (default ‘’), - the separator between ranges (default ‘,’) - the separator between begin and end of a range (default ‘-‘) The separators must be strings (not regexprs) and should be different.

pycopia.crontab.submit(crontab, username=None, password=None)[source]

Submit a crontab via the crontab program. Supply a crontab object. If it is to be installed for a different user supply the username parameter. If this is not run as root, then sudo is used and you must supply your own password for sudo.

Table Of Contents

Previous topic

Pycopia enhanced debugger package

Next topic

Pycopia SMI package