Skip to content

Commit

Permalink
move get_terminal_size() in _compat.py
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Apr 5, 2019
1 parent 3204eaf commit 83ed63b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
23 changes: 22 additions & 1 deletion psutil/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
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

Expand Down Expand Up @@ -239,3 +239,24 @@ def _access_check(fn, mode):
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
19 changes: 1 addition & 18 deletions scripts/cpu_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import time

import psutil
from psutil._compat import get_terminal_size


if not hasattr(psutil.Process, "cpu_num"):
Expand All @@ -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:
Expand Down
48 changes: 25 additions & 23 deletions scripts/ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
...
Expand All @@ -16,32 +16,32 @@

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]
Expand All @@ -51,29 +51,31 @@ 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,
nice,
status,
ctime,
cputime,
name
))
cmdline)
print(line[:get_terminal_size()[0]])


if __name__ == '__main__':
Expand Down

0 comments on commit 83ed63b

Please sign in to comment.