Made psutil optional for the use of the @system command on Windows.

This commit is contained in:
Griatch 2015-03-07 22:11:26 +01:00
parent 5389373776
commit 9e8c307bc4
2 changed files with 22 additions and 20 deletions

View file

@ -668,33 +668,35 @@ class CmdServerLoad(MuxCommand):
os_windows = os.name == "nt"
pid = os.getpid()
loadtable = EvTable("property", "statistic", align="l")
if os_windows:
# Windows requires the psutil module to get statistics like this
try:
import psutil
has_psutil = True
except ImportError:
string = "The psutil module is not installed: Process " \
" listings are only available under Linux/Unix."
self.caller.msg(string)
return
has_psutil = False
loadavg = psutil.cpu_percent()
_mem = psutil.virtual_memory()
rmem = _mem.used
vmem = "N/A on Windows"
pmem = _mem.percent
rusage = "N/A on Windows"
if has_psutil:
loadavg = psutil.cpu_percent()
_mem = psutil.virtual_memory()
rmem = _mem.used
vmem = "N/A on Windows"
pmem = _mem.percent
rusage = "N/A on Windows"
if "mem" in self.switches:
string = "Memory usage: {w%g{n MB (%g%%)"
self.caller.msg(string % (rmem, pmem))
return
# Display table
loadtable.add_row("Server load", "%g" % loadavg)
loadtable.add_row("Process ID", "%g" % pid),
loadtable.add_row("Memory usage","%g MB (%g%%)" % (rmem, pmem))
if "mem" in self.switches:
string = "Memory usage: {w%g{n MB (%g%%)"
self.caller.msg(string % (rmem, pmem))
return
# Display table
loadtable = EvTable("property", "statistic", align="l")
loadtable.add_row("Server load", "%g" % loadavg)
loadtable.add_row("Process ID", "%g" % pid),
loadtable.add_row("Memory usage","%g MB (%g%%)" % (rmem, pmem))
else:
loadtable = "Not available on Windows without 'psutil' library " \
"(install with {wpip install psutil{n)."
else:
# Linux / BSD (OSX)
@ -715,6 +717,7 @@ class CmdServerLoad(MuxCommand):
self.caller.msg(string % (rmem, pmem, vmem))
return
loadtable = EvTable("property", "statistic", align="l")
loadtable.add_row("Server load (1 min)", "%g" % loadavg)
loadtable.add_row("Process ID", "%g" % pid),
loadtable.add_row("Memory usage","%g MB (%g%%)" % (rmem, pmem))

View file

@ -2,7 +2,6 @@
# windows specific
pypiwin32
psutil >= 2.2
# general
django >= 1.7
twisted >= 12.0