Totally rewrote part of system.py

This commit is contained in:
Luke Rogers 2012-04-01 23:45:45 +12:00
parent d03f6b63c6
commit 81958d63b7

View file

@ -1,28 +1,9 @@
# Plugin by Lukeroge, and neersighted
# Yeah, the code in this sucks. Meh.
import os import os
import re import re
import time
import string
import platform import platform
import subprocess import subprocess
from util import hook from util import hook
def replace(text, wordDic):
rc = re.compile('|'.join(map(re.escape, wordDic)))
def translate(match):
return wordDic[match.group(0)]
return rc.sub(translate, text)
def checkProc(checked_stats):
status_file = open('/proc/self/status').read()
line_pairs = re.findall(r'^(\w+):\s*(.*)\s*$', status_file, re.M)
status = dict(line_pairs)
checked_stats = checked_stats.split()
stats = '\x02, '.join(key + ': \x02' + status[key] for key in checked_stats)
return stats
@hook.command(autohelp=False) @hook.command(autohelp=False)
def system(inp): def system(inp):
@ -33,49 +14,52 @@ def system(inp):
python_ver = platform.python_version() python_ver = platform.python_version()
architecture = '-'.join(platform.architecture()) architecture = '-'.join(platform.architecture())
cpu = platform.machine() cpu = platform.machine()
return 'Hostname: \x02%s\x02, Operating System: \x02%s\x02, Python Version:' \ return "Hostname: \x02%s\x02, Operating System: \x02%s\x02, Python " \
' \x02%s %s\x02, Architecture: \x02%s\x02, CPU: \x02%s\x02' % (hostname, "Version: \x02%s %s\x02, Architecture: \x02%s\x02, CPU: \x02%s" \
os, python_imp, python_ver, architecture, cpu) "\x02" % (hostname, os, python_imp, python_ver, architecture, cpu)
@hook.command(autohelp=False) @hook.command(autohelp=False)
def memory(inp): def memory(inp):
".memory -- Displays the bot's current memory usage." ".memory -- Displays the bot's current memory usage."
if os.name == 'posix': if os.name == "posix":
checked_stats = 'VmRSS VmSize VmPeak VmStk VmData' # get process info
memory = checkProc(checked_stats) status_file = open('/proc/self/status').read()
pretty_names = {'VmRSS': 'Real Memory', 'VmSize': 'Allocated Memory', 'VmPeak': 'Peak Allocated Memory', 'VmStk': 'Stack Size', 'VmData': 'Heap Size'} s = dict(re.findall(r'^(\w+):\s*(.*)\s*$', status_file, re.M))
memory = replace(memory, pretty_names) # get the data we need and process it
memory = string.replace(memory, ' kB', '') data = s['VmRSS'], s['VmSize'], s['VmPeak'], s['VmStk'], s['VmData']
memory = memory.split('\x02') data = [int(i.replace(' kB', '')) for i in data]
numbers = [memory[i] for i in range(len(memory)) if i % 2 == 1] strings = [str(round(float(i) / 1024, 2)) + ' MB' for i in data]
memory = [i for i in memory if i not in numbers] # prepare the output
numbers = [str(round(float(i) / 1024, 2)) + ' MB' for i in numbers] out = "Real Memory: \x02%s\x02, Allocated Memory: \x02%s\x02, Peak " \
memory = [list(i) for i in zip(memory, numbers)] "Allocated Memory: \x02%s\x02, Stack Size: \x02%s\x02, Heap " \
memory = sum(memory, []) "Size: \x02%s\x02" % (strings[0], strings[1], strings[2],
memory = '\x02'.join(memory) strings[3], strings[4])
# return output
return out
elif os.name == 'nt': elif os.name == "nt":
cmd = 'tasklist /FI \"PID eq %s\" /FO CSV /NH' % os.getpid() cmd = 'tasklist /FI "PID eq %s" /FO CSV /NH' % os.getpid()
out = os.popen(cmd).read() out = os.popen(cmd).read()
memory = 0 memory = 0
for amount in re.findall(r'([,0-9]+) K', out): for amount in re.findall(r'([,0-9]+) K', out):
memory += int(amount.replace(',', '')) memory += int(amount.replace(',', ''))
memory = str(round(float(memory) / 1024, 2)) memory = str(round(float(memory) / 1024, 2))
memory = 'Memory Usage: \x02%s MB\x02' % memory return "Memory Usage: \x02%s MB\x02" % memory
else: else:
memory = 'error: operating system not currently supported' return "Operating system not currently supported."
return memory
@hook.command(autohelp=False) @hook.command(autohelp=False)
def uptime(inp): def uptime(inp):
".uptime -- Shows the bot's uptime." ".uptime -- Shows the bot's uptime."
up_time = subprocess.check_output('ps -eo pid,etime | grep %s | awk \'{print $2}\'' % os.getpid(), shell=True) up = subprocess.check_output("ps -eo pid,etime | grep %s | awk " \
up_time = 'Uptime: \x02' + up_time + '\x02' "'{print $2}'" % os.getpid(), shell=True)
return up_time return "Uptime: \x02%s\x02" % up
@hook.command(autohelp=False) @hook.command(autohelp=False)
def pid(inp): def pid(inp):
".pid -- Prints the bot's PID." ".pid -- Prints the bot's PID."
return 'PID: \x02%s\x02' % os.getpid() return "PID: \x02%s\x02" % os.getpid()