summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2019-04-05 19:55:46 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2019-04-05 19:55:46 +0200
commit83ed63b168e4c05bf8036e0c72a2460b9e6fa29b (patch)
tree2fe7f9871c876fb18d3a598acbbfecbe1101216f
parent3204eaf4d244f1a76d1c20b2fb9773ad36db4e22 (diff)
downloadpsutil-83ed63b168e4c05bf8036e0c72a2460b9e6fa29b.tar.gz
move get_terminal_size() in _compat.py
-rw-r--r--psutil/_compat.py23
-rwxr-xr-xscripts/cpu_distribution.py19
-rwxr-xr-xscripts/ps.py48
3 files changed, 48 insertions, 42 deletions
diff --git a/psutil/_compat.py b/psutil/_compat.py
index 08aefe4b..c772f61d 100644
--- a/psutil/_compat.py
+++ b/psutil/_compat.py
@@ -10,7 +10,7 @@ import os
import sys
__all__ = ["PY3", "long", "xrange", "unicode", "basestring", "u", "b",
- "lru_cache", "which"]
+ "lru_cache", "which", "get_terminal_size"]
PY3 = sys.version_info[0] == 3
@@ -239,3 +239,24 @@ except ImportError:
if _access_check(name, mode):
return name
return None
+
+
+# python 3.3
+try:
+ from shutil import get_terminal_size
+except ImportError:
+ def get_terminal_size(fallback=(80, 24)):
+ try:
+ import fcntl
+ import termios
+ import struct
+ except ImportError:
+ return fallback
+ else:
+ try:
+ # This should work on Linux.
+ res = struct.unpack(
+ 'hh', fcntl.ioctl(1, termios.TIOCGWINSZ, '1234'))
+ return (res[1], res[0])
+ except Exception:
+ return fallback
diff --git a/scripts/cpu_distribution.py b/scripts/cpu_distribution.py
index 67f25b2d..c509c732 100755
--- a/scripts/cpu_distribution.py
+++ b/scripts/cpu_distribution.py
@@ -46,6 +46,7 @@ import sys
import time
import psutil
+from psutil._compat import get_terminal_size
if not hasattr(psutil.Process, "cpu_num"):
@@ -59,24 +60,6 @@ def clean_screen():
os.system('cls')
-def get_terminal_size(fallback=(80, 24)):
- try:
- # Added in Python 3.3
- from shutil import get_terminal_size as gts
- return gts(fallback=fallback)
- except ImportError:
- try:
- # This should work on Linux.
- import fcntl
- import termios
- import struct
- res = struct.unpack(
- 'hh', fcntl.ioctl(1, termios.TIOCGWINSZ, '1234'))
- return (res[1], res[0])
- except Exception:
- return fallback
-
-
def main():
num_cpus = psutil.cpu_count()
if num_cpus > 8:
diff --git a/scripts/ps.py b/scripts/ps.py
index f7857f21..fd2a8ec4 100755
--- a/scripts/ps.py
+++ b/scripts/ps.py
@@ -5,7 +5,7 @@
# found in the LICENSE file.
"""
-A clone of 'ps -aux' on UNIX.
+A clone of 'ps aux'.
$ python scripts/ps.py
...
@@ -16,32 +16,32 @@ import time
import psutil
from psutil._common import bytes2human
+from psutil._compat import get_terminal_size
def main():
today_day = datetime.date.today()
templ = "%-10s %5s %5s %7s %7s %5s %6s %6s %6s %s"
- attrs = ['pid', 'memory_percent', 'name', 'cpu_times',
+ attrs = ['pid', 'memory_percent', 'name', 'cmdline', 'cpu_times',
'create_time', 'memory_info', 'status', 'nice', 'username']
print(templ % ("USER", "PID", "%MEM", "VSZ", "RSS", "NICE",
- "STAT", "START", "TIME", "NAME"))
+ "STATUS", "START", "TIME", "CMDLINE"))
for p in psutil.process_iter(attrs, ad_value=None):
- pinfo = p.info
- if pinfo['create_time']:
- ctime = datetime.datetime.fromtimestamp(pinfo['create_time'])
+ if p.info['create_time']:
+ ctime = datetime.datetime.fromtimestamp(p.info['create_time'])
if ctime.date() == today_day:
ctime = ctime.strftime("%H:%M")
else:
ctime = ctime.strftime("%b%d")
else:
ctime = ''
- if pinfo['cpu_times']:
+ if p.info['cpu_times']:
cputime = time.strftime("%M:%S",
- time.localtime(sum(pinfo['cpu_times'])))
+ time.localtime(sum(p.info['cpu_times'])))
else:
cputime = ''
- user = pinfo['username']
+ user = p.info['username']
if not user and psutil.POSIX:
try:
user = p.uids()[0]
@@ -51,20 +51,22 @@ def main():
user = user.split('\\')[1]
user = user[:9]
- vms = bytes2human(pinfo['memory_info'].vms) if \
- pinfo['memory_info'] is not None else ''
- rss = bytes2human(pinfo['memory_info'].rss) if \
- pinfo['memory_info'] is not None else ''
- memp = round(pinfo['memory_percent'], 1) if \
- pinfo['memory_percent'] is not None else ''
- nice = int(pinfo['nice']) if pinfo['nice'] else ''
- name = pinfo['name'] if pinfo['name'] else ''
+ 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'], 1) if \
+ p.info['memory_percent'] is not None else ''
+ nice = int(p.info['nice']) if p.info['nice'] else ''
+ if p.info['cmdline']:
+ cmdline = ' '.join(p.info['cmdline'])
+ else:
+ cmdline = p.info['name']
+ status = p.info['status'][:5] if p.info['status'] else ''
-# status = PROC_STATUSES_RAW.get(pinfo['status'], pinfo['status'])
- status = pinfo['status'][:5] if pinfo['status'] else ''
- print(templ % (
+ line = templ % (
user[:10],
- pinfo['pid'],
+ p.info['pid'],
memp,
vms,
rss,
@@ -72,8 +74,8 @@ def main():
status,
ctime,
cputime,
- name
- ))
+ cmdline)
+ print(line[:get_terminal_size()[0]])
if __name__ == '__main__':