summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2019-04-05 13:00:06 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2019-04-05 13:00:06 +0200
commit275df44b5798ddf09485d7ad503a212636ddf340 (patch)
tree89987f02c493b37616457210b9c5ab896efcc019
parentd4cbce0b3aa7e72deb7409a25850cdd5c3c4d323 (diff)
downloadpsutil-275df44b5798ddf09485d7ad503a212636ddf340.tar.gz
fix #1474: fix formatting of psutil.tests() which mimicks 'ps aux' output
-rw-r--r--HISTORY.rst1
-rw-r--r--psutil/__init__.py39
2 files changed, 30 insertions, 10 deletions
diff --git a/HISTORY.rst b/HISTORY.rst
index 65629f84..5741e247 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -25,6 +25,7 @@
exist. (patch by Cedric Lamoriniere)
- 1471_: [SunOS] Process name() and cmdline() can return SystemError. (patch
by Daniel Beer)
+- 1474_: fix formatting of psutil.tests() which mimicks 'ps aux' output.
- 1475_: [Windows] OSError.winerror attribute wasn't properly checked resuling
in WindowsError being raised instead of AccessDenied.
- 1477_: [Windows] wrong or absent error handling for private NTSTATUS Windows
diff --git a/psutil/__init__.py b/psutil/__init__.py
index 2f33436c..a217a9e7 100644
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -2441,8 +2441,25 @@ def test(): # pragma: no cover
"""List info of all currently running processes emulating ps aux
output.
"""
+ def bytes2human(n):
+ """
+ >>> bytes2human(10000)
+ '9.8 K'
+ >>> bytes2human(100001221)
+ '95.4 M'
+ """
+ symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
+ prefix = {}
+ for i, s in enumerate(symbols):
+ prefix[s] = 1 << (i + 1) * 10
+ for s in reversed(symbols):
+ if n >= prefix[s]:
+ value = float(n) / prefix[s]
+ return '%.1f%s' % (value, s)
+ return '%.1fB' % (n)
+
today_day = datetime.date.today()
- templ = "%-10s %5s %4s %7s %7s %-13s %5s %7s %s"
+ templ = "%-10s %6s %5s %8s %8s %12s %5s %7s %s"
attrs = ['pid', 'memory_percent', 'name', 'cpu_times', 'create_time',
'memory_info']
if POSIX:
@@ -2450,7 +2467,7 @@ def test(): # pragma: no cover
attrs.append('terminal')
print(templ % ("USER", "PID", "%MEM", "VSZ", "RSS", "TTY", "START", "TIME",
"COMMAND"))
- for p in process_iter(attrs=attrs, ad_value=''):
+ for p in process_iter(attrs=attrs, ad_value=None):
if p.info['create_time']:
ctime = datetime.datetime.fromtimestamp(p.info['create_time'])
if ctime.date() == today_day:
@@ -2462,24 +2479,26 @@ def test(): # pragma: no cover
cputime = time.strftime("%M:%S",
time.localtime(sum(p.info['cpu_times'])))
try:
- user = p.username()
+ user = p.username()[:9]
except Error:
user = ''
if WINDOWS and '\\' in user:
user = user.split('\\')[1]
- vms = p.info['memory_info'] and \
- int(p.info['memory_info'].vms / 1024) or '?'
- rss = p.info['memory_info'] and \
- int(p.info['memory_info'].rss / 1024) or '?'
- memp = p.info['memory_percent'] and \
- round(p.info['memory_percent'], 1) or '?'
+
+ vms = bytes2human(p.info['memory_info'].vms) if \
+ p.info['memory_info'] is not None else ''
+ rss = bytes2human(p.info['memory_info'].rss) if \
+ p.info['memory_info'] is not None else ''
+ memp = round(p.info['memory_percent']) if \
+ p.info['memory_percent'] is not None else ''
+
print(templ % (
user[:10],
p.info['pid'],
memp,
vms,
rss,
- p.info.get('terminal', '') or '?',
+ p.info.get('terminal', '') or '',
ctime,
cputime,
p.info['name'].strip() or '?'))