def start_proc_manager(config, logfile): from pycopia import proctools from pycopia import scheduler from pycopia import asyncio asyncio.start_sigio() pm = proctools.get_procmanager() for name, serverlist in config.VHOSTS.items(): for servername in serverlist: print "Starting %s for %s." % (servername, name) p = pm.spawnpipe("%s/fcgi_server -n %s" % (config.LIBEXEC, servername), persistent=True, logfile=logfile) asyncio.poller.register(p) scheduler.sleep(1.0) # give it time to init... if config.USEFRONTEND: lighttpd = proctools.which("lighttpd") pm.spawnpipe("%s -D -f %s" % (lighttpd, LTCONFIG), persistent=True, logfile=logfile) try: while 1: asyncio.poller.loop() for proc in pm.getprocs(): if proc.readable(): print proc.read(4096) except KeyboardInterrupt: asyncio.poller.unregister_all() for proc in pm.getprocs(): proc.kill() proc.wait() if os.path.exists(config.PIDFILE): os.unlink(config.PIDFILE)
def start_proc_manager(config, logfile): from pycopia import proctools from pycopia import scheduler from pycopia import asyncio pm = proctools.get_procmanager() libexec = config.get("LIBEXEC", "/usr/libexec/pycopia") for name, serverlist in config.VHOSTS.items(): for servername in serverlist: print "Starting %s for %s." % (servername, name) p = pm.spawnpipe("%s/fcgi_server -n %s" % (libexec, servername), persistent=True, logfile=logfile) asyncio.poller.register(p) #scheduler.sleep(1.0) # give it time to init... if config.USEFRONTEND: lighttpd = proctools.which("lighttpd") if asyncio.poller: pm.spawnpipe("%s -D -f %s" % (lighttpd, LTCONFIG), persistent=True, logfile=logfile) else: # no servers, just run frontend alone pm.spawnpipe("%s -f %s" % (lighttpd, LTCONFIG)) try: asyncio.poller.loop() print "No servers, exited loop." except KeyboardInterrupt: pass if asyncio.poller: asyncio.poller.unregister_all() for proc in pm.getprocs(): proc.killwait() if os.path.exists(config.PIDFILE): os.unlink(config.PIDFILE)
def check(config): "Check the lighttpd configuration." from pycopia import proctools pm = proctools.get_procmanager() lighttpd = proctools.which("lighttpd") proc = pm.spawnpipe("%s -p -f %s" % (lighttpd, LTCONFIG)) out = proc.read() es = proc.wait() if es: print out else: print "Error: %s" % (es,)
def check(config): "Check the lighttpd configuration." from pycopia import proctools pm = proctools.get_procmanager() lighttpd = proctools.which("lighttpd") proc = pm.spawnpipe("%s -p -f %s" % (lighttpd, LTCONFIG)) out = proc.read() es = proc.wait() if es: print out else: print "Error: %s" % (es, )
# 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. """ Run things as root (or another user) via sudo. """ from __future__ import print_function from __future__ import unicode_literals from __future__ import division from pycopia import proctools from pycopia.aid import IF SUDO = proctools.which("sudo") def sudo(command, user=None, password=None, extraopts=None, logfile=None): opts = "-S %s" % ("-u %s" % user if user else "") cmd = "%s %s %s %s" % (SUDO, opts, extraopts or "", command) proc = proctools.spawnpipe(cmd, logfile=logfile, merge=0) if password: proc.readerr(9) # discard password prompt proc.write("%s\r" % (password, )) proc.readerr(1) # discard newline return proc def sudo_reset(): proc = proctools.spawnpipe("%s -k" % (SUDO, ), merge=0)
# # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. """ Wrapper for the 'rsync' program. See the rsync manpage for more details. """ from pycopia import proctools try: RSYNC = proctools.which("rsync") except ValueError: raise ImportError, "rsync program not found!" TESTED_VERSIONS = ["rsync version 2.5.5 protocol version 26"] def rsync(src, dst, password=None, extraopts=None, logfile=None): """rsync(src, dst, [password, [extraopts, [logfile]]]) Usage: rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST or rsync [OPTION]... [USER@]HOST:SRC DEST or rsync [OPTION]... SRC [SRC]... DEST or rsync [OPTION]... [USER@]HOST::SRC [DEST] or rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST or rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
import os import zipfile import struct import stat from cStringIO import StringIO from pycopia import proctools from pycopia import scheduler from pycopia import socket from pycopia import dictlib from pycopia import timelib from pycopia import aid # Will raise an exception if not found. try: ADB = proctools.which("adb") except proctools.NotFoundError, err: raise ImportError, err class Error(Exception): pass class AdbError(Error): pass class AdbQuit(Error): """Signals that the device connection must quit."""
# version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. """ Use wxcut and wxpaste to get and set the X selection. """ from pycopia import proctools try: WXCOPY = proctools.which("wxcopy") WXPASTE = proctools.which("wxpaste") except ValueError: raise ImportError, "wxcopy or wxpaste program not found! Install WindowMaker for these." WXCOPY_OPTIONS = '-clearselection' WXPASTE_OPTIONS = '' def wxcopy(text, callback=None, logfile=None, extraoptions="", async=False): pm = proctools.get_procmanager() command = "%s %s %s" % (WXCOPY, WXCOPY_OPTIONS, extraoptions) copy = pm.spawnpipe(command, logfile=logfile, async=async) copy.write(text) copy.close() return copy.wait()
# License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. """ Start and control an alsaplayer deamon instance. """ from pycopia import proctools import os try: PLAYER = proctools.which("alsaplayer") except ValueError: raise ImportError, "alsaplayer program not found!" DEVICE = os.environ.get("ALSAPLAYERDEVICE", "default") def get_alsaplayer(session=0, name="alsaplayer", device=DEVICE, extraopts="", logfile=None): """Return a process object for the alsaplayer.""" opts = "-i daemon -q -n %s -s '%s' -d %s --nosave" % (session, name, device) CMD = "%s %s %s" % (PLAYER, opts, extraopts)
configuration file and environment variables. But it's still limited and tedious. The pyOpenSSL based modules are better. Use those if you can. """ from __future__ import absolute_import from __future__ import print_function #from __future__ import unicode_literals from __future__ import division import os import errno from pycopia import environ from pycopia import proctools OPENSSL = proctools.which("openssl") SSL_CONFIG = "/etc/pycopia/ssl/openssl.cnf" # CA config, matches what's in the config file. CA_DIR = "/etc/pycopia/ssl/CA" CAKEY = "cakey.pem" CAREQ = "careq.pem" CACERT = "cacert.pem" CADAYS = 1095 class Error(Exception): pass
# modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. """ Use wxcut and wxpaste to get and set the X selection. """ from pycopia import proctools WXCOPY = proctools.which("wxcopy") WXPASTE = proctools.which("wxpaste") WXCOPY_OPTIONS = '-clearselection' WXPASTE_OPTIONS = '' def wxcopy(text, callback=None, logfile=None, extraoptions="", async=False): pm = proctools.get_procmanager() command = "%s %s %s" % (WXCOPY, WXCOPY_OPTIONS, extraoptions) copy = pm.spawnpipe(command, logfile=logfile, async=async) copy.write(text) copy.close() return copy.wait()
# but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. """ Wrapper for the ssh program. Provides get_ssh, ssh_command, and scp functions. """ import sys, os from pycopia import proctools from pycopia import expect SSH = proctools.which("ssh") SCP = proctools.which("scp") KEYGEN = proctools.which("ssh-keygen") KEYSCAN = proctools.which("ssh-keyscan") class SSHRetry(RuntimeError): pass # |01234567890123456789 TESTED_VERSIONS = [ "OpenSSH_3.4p1, SSH protocols 1.5/2.0, OpenSSL 0x0090605f", "OpenSSH_3.5p1, SSH protocols 1.5/2.0, OpenSSL 0x0090701f", "OpenSSH_3.6.1p2, SSH protocols 1.5/2.0, OpenSSL 0x0090701f", "OpenSSH_3.8.1p1, OpenSSL 0.9.7d 17 Mar 2004",
# # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. """ Wrapper for the netcat program. Allows TCP port forwarding to stdio. If your netcat (nc) is suid to root, you can forward privileged ports, such as SMTP. """ import sys from pycopia import proctools NETCAT = proctools.which("nc") TESTED_VERSIONS = ["[v1.10]"] def get_netcat(host, port, callback=None, logfile=None, extraoptions=""): """get_netcat(host, port, [prompt], [callback], [logfile], [extraoptions]) Returns a Netcat object (an Expect subclass) attached to a netcat client. The logfile parameter should be a file-like object (has a 'write' method). """ cmd = "%s %s %s %s" %(NETCAT, extraoptions, host, port) pm = proctools.get_procmanager() proc = pm.spawnpipe(cmd, callback=callback, logfile=logfile, merge=0) return proc
# 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. """ Wrapper for the netcat program. Allows TCP port forwarding to stdio. If your netcat (nc) is suid to root, you can forward privileged ports, such as SMTP. """ import sys from pycopia import proctools try: NETCAT = proctools.which("nc") except proctools.NotFoundError: NETCAT = proctools.which("netcat") TESTED_VERSIONS = ["[v1.10]"] def get_netcat(host, port, callback=None, logfile=None, extraoptions=""): """get_netcat(host, port, [prompt], [callback], [logfile], [extraoptions]) Returns a Netcat object (an Expect subclass) attached to a netcat client. The logfile parameter should be a file-like object (has a 'write' method). """ cmd = "%s %s %s %s" %(NETCAT, extraoptions, host, port) pm = proctools.get_procmanager() proc = pm.spawnpipe(cmd, callback=callback, logfile=logfile, merge=0)
# 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. """ Wrapper for the 'rsync' program. See the rsync manpage for more details. """ from __future__ import print_function from __future__ import unicode_literals from __future__ import division from pycopia import proctools RSYNC = proctools.which("rsync") TESTED_VERSIONS = ["rsync version 2.5.5 protocol version 26"] def rsync(src, dst, password=None, extraopts=None, logfile=None): """rsync(src, dst, [password, [extraopts, [logfile]]]) Usage: rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST or rsync [OPTION]... [USER@]HOST:SRC DEST or rsync [OPTION]... SRC [SRC]... DEST or rsync [OPTION]... [USER@]HOST::SRC [DEST] or rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST or rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST] You might want to set the RSYNC_RSH environment variable first. """
# 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. """ Wrapper for the ssh program. Provides get_ssh, ssh_command, and scp functions. """ import sys, os from pycopia import proctools from pycopia import expect SSH = proctools.which("ssh") SCP = proctools.which("scp") KEYGEN = proctools.which("ssh-keygen") KEYSCAN = proctools.which("ssh-keyscan") class SSHRetry(RuntimeError): pass # |01234567890123456789 TESTED_VERSIONS = ["OpenSSH_3.4p1, SSH protocols 1.5/2.0, OpenSSL 0x0090605f", "OpenSSH_3.5p1, SSH protocols 1.5/2.0, OpenSSL 0x0090701f", "OpenSSH_3.6.1p2, SSH protocols 1.5/2.0, OpenSSL 0x0090701f", "OpenSSH_3.8.1p1, OpenSSL 0.9.7d 17 Mar 2004", "OpenSSH_4.5p1, OpenSSL 0.9.8a 11 Oct 2005", "OpenSSH_4.3p2" ]
# version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. """ Wrapper for the netcat program. Allows TCP port forwarding to stdio. If your netcat (nc) is suid to root, you can forward privileged ports, such as SMTP. """ import sys from pycopia import proctools NETCAT = proctools.which("nc") TESTED_VERSIONS = ["[v1.10]"] def get_netcat(host, port, callback=None, logfile=None, extraoptions=""): """get_netcat(host, port, [prompt], [callback], [logfile], [extraoptions]) Returns a Netcat object (an Expect subclass) attached to a netcat client. The logfile parameter should be a file-like object (has a 'write' method). """ cmd = "%s %s %s %s" % (NETCAT, extraoptions, host, port) pm = proctools.get_procmanager() proc = pm.spawnpipe(cmd, callback=callback, logfile=logfile, merge=0) return proc
# version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. """ Run things as root (or another user) via sudo. """ from pycopia import proctools from pycopia.aid import IF try: SUDO = proctools.which("sudo") except ValueError: raise ImportError, "'sudo' program not found in PATH" def sudo(command, user=None, password=None, extraopts=None, logfile=None): opts = "-S %s" % (IF(user, "-u %s" % (user,), ""),) cmd = "%s %s %s %s" % (SUDO, opts, extraopts or "", command) proc = proctools.spawnpipe(cmd, logfile=logfile, merge=0) if password: proc.readerr(9) # discard password prompt proc.write("%s\r" % (password,)) proc.readerr(1) # discard newline return proc def sudo_reset(): proc = proctools.spawnpipe("%s -k" % (SUDO,), merge=0)
# version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. """ Use nsupdate to update a DNS server (running Bind version 9.x). """ import sys, os from pycopia import proctools NSUPDATE = proctools.which("nsupdate") class _DNSUpdate(proctools.ProcessPipe): def __init__(self, cmd, logfile=None, env=None, callback=None, merge=0, async=False): proctools.ProcessPipe.__init__(self, cmd, logfile, env, callback, merge, async) self._zone=None self._dnsclass = "IN" # default class self._ttl = 86400 # default ttl def close(self): self.write(chr(4)) # ^D super(_DNSUpdate, self).close() def _readerr_fill(self): try: c = self._readerr(1024)
configuration file and environment variables. But it's still limited and tedious. The pyOpenSSL based modules are better. Use those if you can. """ from __future__ import absolute_import from __future__ import print_function from __future__ import division import os import errno from pycopia import environ from pycopia import proctools OPENSSL = proctools.which("openssl") SSL_CONFIG="/etc/pycopia/ssl/openssl.cnf" # CA config, matches what's in the config file. CA_DIR = "/etc/pycopia/ssl/CA" CAKEY = "cakey.pem" CAREQ = "careq.pem" CACERT = "cacert.pem" CADAYS = 1095 class Error(Exception): pass class SSLExecError(Error):
# This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. """ Wrapper for the netcat program. Allows TCP port forwarding to stdio. If your netcat (nc) is suid to root, you can forward privileged ports, such as SMTP. """ import sys from pycopia import proctools try: NETCAT = proctools.which("nc") except proctools.NotFoundError: NETCAT = proctools.which("netcat") TESTED_VERSIONS = ["[v1.10]"] def get_netcat(host, port, callback=None, logfile=None, extraoptions=""): """get_netcat(host, port, [prompt], [callback], [logfile], [extraoptions]) Returns a Netcat object (an Expect subclass) attached to a netcat client. The logfile parameter should be a file-like object (has a 'write' method). """ cmd = "%s %s %s %s" %(NETCAT, extraoptions, host, port) pm = proctools.get_procmanager() proc = pm.spawnpipe(cmd, callback=callback, logfile=logfile, merge=0)
# 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 Python wrapper for the 'redir' program. Note that to use this to redirect privileged TCP ports as a regular user you must make your 'redir' program suid to root. This is a bad security practice, but is necessary for some scenarios. just be aware. """ from pycopia import proctools try: REDIR = proctools.which("redir") except ValueError: raise ImportError, "'redir' program not found in PATH" def redir(lport, cport, laddr=None, caddr=None, extraopts=None): """redir(lport, cport, laddr=None, caddr=None, extraopts=None) Redirect local port to client port, possible to another host is caddr is given. Optionally bind to a specific IP if laddr is also given. """ opts = "--lport=%d --cport=%d" % (lport, cport) if laddr: opts += " --laddr=%s" % (laddr,) if caddr: opts += " --caddr=%s" % (caddr,) cmd = "%s %s %s" % (REDIR, opts, extraopts or "") proc = proctools.spawnpipe(cmd, merge=0)
# License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. """ Start and control an alsaplayer deamon instance. """ from pycopia import proctools import os try: PLAYER = proctools.which("alsaplayer") except ValueError: raise ImportError, "alsaplayer program not found!" DEVICE = os.environ.get("ALSAPLAYERDEVICE", "default") def get_alsaplayer(session=0, name="alsaplayer", device=DEVICE, extraopts="", logfile=None): """Return a process object for the alsaplayer.""" opts = "-i daemon -q -n %s -s '%s' -d %s --nosave" % (session, name, device) CMD = "%s %s %s" % (PLAYER, opts, extraopts) aplayer = proctools.spawnpipe(CMD, logfile=logfile) return aplayer