mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-14 22:49:23 +01:00
Switch from using the various popen flavors to subprocess.Popen
Signed-off-by: James Vega <jamessan@users.sourceforge.net>
This commit is contained in:
parent
07e283f450
commit
fbdc44ca52
@ -1,5 +1,6 @@
|
|||||||
###
|
###
|
||||||
# Copyright (c) 2002-2005, Jeremiah Fincher
|
# Copyright (c) 2002-2005, Jeremiah Fincher
|
||||||
|
# Copyright (c) 2009, James Vega
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
||||||
@ -31,6 +32,7 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
|
import subprocess
|
||||||
|
|
||||||
import supybot.conf as conf
|
import supybot.conf as conf
|
||||||
import supybot.utils as utils
|
import supybot.utils as utils
|
||||||
@ -142,12 +144,17 @@ class Status(callbacks.Plugin):
|
|||||||
if plat.startswith('linux') or plat.startswith('sunos') or \
|
if plat.startswith('linux') or plat.startswith('sunos') or \
|
||||||
plat.startswith('freebsd') or plat.startswith('openbsd') or \
|
plat.startswith('freebsd') or plat.startswith('openbsd') or \
|
||||||
plat.startswith('darwin'):
|
plat.startswith('darwin'):
|
||||||
|
cmd = 'ps -o rss -p %s' % pid
|
||||||
try:
|
try:
|
||||||
r = os.popen('ps -o rss -p %s' % pid)
|
inst = subprocess.Popen(cmd.split(), close_fds=True,
|
||||||
r.readline() # VSZ Header.
|
stdin=file(os.devnull),
|
||||||
mem = r.readline().strip()
|
stdout=subprocess.PIPE,
|
||||||
finally:
|
stderr=subprocess.PIPE)
|
||||||
r.close()
|
except OSError:
|
||||||
|
irc.error('Unable to run ps command.', Raise=True)
|
||||||
|
(out, _) = inst.communicate()
|
||||||
|
inst.wait()
|
||||||
|
mem = out.splitlines()[1]
|
||||||
elif sys.platform.startswith('netbsd'):
|
elif sys.platform.startswith('netbsd'):
|
||||||
mem = '%s kB' % os.stat('/proc/%s/mem' % pid)[7]
|
mem = '%s kB' % os.stat('/proc/%s/mem' % pid)[7]
|
||||||
response += ' I\'m taking up %s kB of memory.' % mem
|
response += ' I\'m taking up %s kB of memory.' % mem
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
###
|
###
|
||||||
# Copyright (c) 2002-2005, Jeremiah Fincher
|
# Copyright (c) 2002-2005, Jeremiah Fincher
|
||||||
# Copyright (c) 2008, James Vega
|
# Copyright (c) 2008-2009, James Vega
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
||||||
@ -34,10 +34,10 @@ import pwd
|
|||||||
import sys
|
import sys
|
||||||
import crypt
|
import crypt
|
||||||
import errno
|
import errno
|
||||||
import popen2
|
|
||||||
import random
|
import random
|
||||||
import select
|
import select
|
||||||
import struct
|
import struct
|
||||||
|
import subprocess
|
||||||
|
|
||||||
import supybot.utils as utils
|
import supybot.utils as utils
|
||||||
from supybot.commands import *
|
from supybot.commands import *
|
||||||
@ -130,46 +130,45 @@ class Unix(callbacks.Plugin):
|
|||||||
# We are only checking the first word
|
# We are only checking the first word
|
||||||
spellCmd = self.registryValue('spell.command')
|
spellCmd = self.registryValue('spell.command')
|
||||||
if not spellCmd:
|
if not spellCmd:
|
||||||
irc.error('A spell checking command doesn\'t seem to be '
|
irc.error('The spell checking command is not configured. If one '
|
||||||
'installed on this computer. If one is installed, '
|
'is installed, reconfigure '
|
||||||
'reconfigure supybot.plugins.Unix.spell.command '
|
'supybot.plugins.Unix.spell.command appropriately.',
|
||||||
'appropriately.', Raise=True)
|
Raise=True)
|
||||||
if word and not word[0].isalpha():
|
if word and not word[0].isalpha():
|
||||||
irc.error('<word> must begin with an alphabet character.')
|
irc.error('<word> must begin with an alphabet character.')
|
||||||
return
|
return
|
||||||
if ' ' in word:
|
if ' ' in word:
|
||||||
irc.error('Spaces aren\'t allowed in the word.')
|
irc.error('Spaces aren\'t allowed in the word.')
|
||||||
return
|
return
|
||||||
inst = popen2.Popen4([spellCmd, '-a'])
|
|
||||||
(r, w) = (inst.fromchild, inst.tochild)
|
|
||||||
try:
|
try:
|
||||||
s = r.readline() # Banner, hopefully.
|
inst = subprocess.Popen([spellCmd, '-a'], close_fds=True,
|
||||||
if 'sorry' in s.lower():
|
stdout=subprocess.PIPE,
|
||||||
irc.error(s)
|
stderr=subprocess.PIPE,
|
||||||
return
|
stdin=subprocess.PIPE)
|
||||||
w.write(word)
|
except OSError, e:
|
||||||
w.write('\n')
|
irc.error(e, Raise=True)
|
||||||
w.flush()
|
ret = inst.poll()
|
||||||
try:
|
if ret is not None:
|
||||||
line = pipeReadline(r)
|
s = inst.stderr.readline()
|
||||||
# aspell puts extra whitespace, ignore it
|
if not s:
|
||||||
while not line.strip('\r\n'):
|
s = inst.stdout.readline()
|
||||||
line = pipeReadline(r)
|
s = s.rstrip('\r\n')
|
||||||
# cache an extra line in case aspell's first line says the word
|
s = s.lstrip('Error: ')
|
||||||
# is spelled correctly, but subsequent lines offer spelling
|
irc.error(s, Raise=True)
|
||||||
# suggestions
|
(out, err) = inst.communicate(word)
|
||||||
line2 = pipeReadline(r)
|
inst.wait()
|
||||||
except TimeoutError:
|
lines = filter(None, out.splitlines())
|
||||||
irc.error('The spell command timed out.')
|
lines.pop(0) # Banner
|
||||||
return
|
if not lines:
|
||||||
finally:
|
irc.error('No results found.', Raise=True)
|
||||||
r.close()
|
line = lines.pop(0)
|
||||||
w.close()
|
line2 = ''
|
||||||
inst.wait()
|
if lines:
|
||||||
|
line2 = lines.pop(0)
|
||||||
# parse the output
|
# parse the output
|
||||||
# aspell will sometimes list spelling suggestions after a '*' or '+'
|
# aspell will sometimes list spelling suggestions after a '*' or '+'
|
||||||
# line for complex words.
|
# line for complex words.
|
||||||
if line[0] in '*+' and line2.strip('\r\n'):
|
if line[0] in '*+' and line2:
|
||||||
line = line2
|
line = line2
|
||||||
if line[0] in '*+':
|
if line[0] in '*+':
|
||||||
resp = format('%q may be spelled correctly.', word)
|
resp = format('%q may be spelled correctly.', word)
|
||||||
@ -199,24 +198,23 @@ class Unix(callbacks.Plugin):
|
|||||||
if self.registryValue('fortune.offensive'):
|
if self.registryValue('fortune.offensive'):
|
||||||
args.append('-a')
|
args.append('-a')
|
||||||
args.extend(self.registryValue('fortune.files'))
|
args.extend(self.registryValue('fortune.files'))
|
||||||
inst = popen2.Popen4(args)
|
|
||||||
(r, w) = (inst.fromchild, inst.tochild)
|
|
||||||
try:
|
try:
|
||||||
lines = r.readlines()
|
inst = subprocess.Popen(args, close_fds=True,
|
||||||
lines = map(str.rstrip, lines)
|
stdout=subprocess.PIPE,
|
||||||
lines = filter(None, lines)
|
stderr=subprocess.PIPE,
|
||||||
if lines:
|
stdin=file(os.devnull))
|
||||||
irc.replies(lines, joiner=' ')
|
except OSError, e:
|
||||||
else:
|
irc.error('It seems the configured fortune command was '
|
||||||
irc.error('It seems the configured fortune command was '
|
'not available.', Raise=True)
|
||||||
'not available.')
|
(out, err) = inst.communicate()
|
||||||
finally:
|
inst.wait()
|
||||||
w.close()
|
lines = out.splitlines()
|
||||||
r.close()
|
lines = map(str.rstrip, lines)
|
||||||
inst.wait()
|
lines = filter(None, lines)
|
||||||
|
irc.replies(lines, joiner=' ')
|
||||||
else:
|
else:
|
||||||
irc.error('I couldn\'t find the fortune command on this system. '
|
irc.error('The fortune command is not configured. If fortune is '
|
||||||
'If it is installed on this system, reconfigure the '
|
'installed on this system, reconfigure the '
|
||||||
'supybot.plugins.Unix.fortune.command configuration '
|
'supybot.plugins.Unix.fortune.command configuration '
|
||||||
'variable appropriately.')
|
'variable appropriately.')
|
||||||
|
|
||||||
@ -229,31 +227,27 @@ class Unix(callbacks.Plugin):
|
|||||||
"""
|
"""
|
||||||
wtfCmd = self.registryValue('wtf.command')
|
wtfCmd = self.registryValue('wtf.command')
|
||||||
if wtfCmd:
|
if wtfCmd:
|
||||||
def commandError():
|
|
||||||
irc.error('It seems the configured wtf command '
|
|
||||||
'was not available.')
|
|
||||||
something = something.rstrip('?')
|
something = something.rstrip('?')
|
||||||
inst = popen2.Popen4([wtfCmd, something])
|
|
||||||
(r, w) = (inst.fromchild, inst.tochild)
|
|
||||||
try:
|
try:
|
||||||
response = utils.str.normalizeWhitespace(r.readline().strip())
|
inst = subprocess.Popen([wtfCmd, something], close_fds=True,
|
||||||
if response:
|
stdout=subprocess.PIPE,
|
||||||
irc.reply(response)
|
stderr=file(os.devnull),
|
||||||
else:
|
stdin=file(os.devnull))
|
||||||
commandError()
|
except OSError:
|
||||||
finally:
|
irc.error('It seems the configured wtf command was not '
|
||||||
r.close()
|
'available.', Raise=True)
|
||||||
w.close()
|
(out, _) = inst.communicate()
|
||||||
inst.wait()
|
inst.wait()
|
||||||
|
if out:
|
||||||
|
response = out.splitlines()[0].strip()
|
||||||
|
response = utils.str.normalizeWhitespace(response)
|
||||||
|
irc.reply(response)
|
||||||
else:
|
else:
|
||||||
irc.error('I couldn\'t find the wtf command on this system. '
|
irc.error('The wtf command is not configured. If it is installed '
|
||||||
'If it is installed on this system, reconfigure the '
|
'on this system, reconfigure the '
|
||||||
'supybot.plugins.Unix.wtf.command configuration '
|
'supybot.plugins.Unix.wtf.command configuration '
|
||||||
'variable appropriately.')
|
'variable appropriately.')
|
||||||
wtf = wrap(wtf, [optional(('literal', ['is'])), 'something'])
|
wtf = wrap(wtf, [optional(('literal', ['is'])), 'something'])
|
||||||
|
|
||||||
|
|
||||||
Class = Unix
|
Class = Unix
|
||||||
|
|
||||||
|
|
||||||
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
###
|
###
|
||||||
# Copyright (c) 2005, Jeremiah Fincher
|
# Copyright (c) 2005, Jeremiah Fincher
|
||||||
|
# Copyright (c) 2009, James Vega
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
||||||
@ -59,8 +60,8 @@ if __name__ == '__main__':
|
|||||||
# import supybot.conf as conf
|
# import supybot.conf as conf
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import popen2
|
|
||||||
import optparse
|
import optparse
|
||||||
|
import subprocess
|
||||||
|
|
||||||
parser = optparse.OptionParser(usage='Usage: %prog [options]')
|
parser = optparse.OptionParser(usage='Usage: %prog [options]')
|
||||||
parser.add_option('', '--verbose', action='store_true',
|
parser.add_option('', '--verbose', action='store_true',
|
||||||
@ -120,17 +121,18 @@ if __name__ == '__main__':
|
|||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
debug('Bot not found, starting.')
|
debug('Bot not found, starting.')
|
||||||
home = os.environ['HOME']
|
home = os.environ['HOME']
|
||||||
inst = popen2.Popen4('sh')
|
inst = subprocess.Popen('sh', close_fds=True, stderr=subprocess.STDOUT,
|
||||||
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||||
for filename in ('.login', '.bash_profile', '.profile', '.bashrc'):
|
for filename in ('.login', '.bash_profile', '.profile', '.bashrc'):
|
||||||
filename = os.path.join(home, filename)
|
filename = os.path.join(home, filename)
|
||||||
if os.path.exists(filename):
|
if os.path.exists(filename):
|
||||||
debug('Found %s, sourcing.' % filename)
|
debug('Found %s, sourcing.' % filename)
|
||||||
inst.tochild.write('source %s' % filename + os.linesep)
|
inst.communicate('source %s' % filename + os.linesep)
|
||||||
cmdline = "%s --daemon %s" % (options.supybot, options.conffile)
|
cmdline = "%s --daemon %s" % (options.supybot, options.conffile)
|
||||||
debug('Sending cmdline to sh process.')
|
debug('Sending cmdline to sh process.')
|
||||||
inst.tochild.write(cmdline + os.linesep)
|
(stdout, _) = inst.communicate(cmdline + os.linesep)
|
||||||
inst.tochild.close()
|
inst.stdin.close()
|
||||||
debug('Received from sh process: %r' % inst.fromchild.read())
|
debug('Received from sh process: %r' % stdout)
|
||||||
ret = inst.wait()
|
ret = inst.wait()
|
||||||
debug('Bot started, command line %r returned %s.' % (cmdline, ret))
|
debug('Bot started, command line %r returned %s.' % (cmdline, ret))
|
||||||
sys.exit(ret)
|
sys.exit(ret)
|
||||||
|
Loading…
Reference in New Issue
Block a user