#!/usr/bin/env python

# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""
A clone of 'ps -aux' on UNIX.

$ python scripts/ps.py
...
"""

import datetime
import time

import psutil
from psutil._common import bytes2human


PROC_STATUSES_RAW = {
    psutil.STATUS_RUNNING: "R",
    psutil.STATUS_SLEEPING: "S",
    psutil.STATUS_DISK_SLEEP: "D",
    psutil.STATUS_STOPPED: "T",
    psutil.STATUS_TRACING_STOP: "t",
    psutil.STATUS_ZOMBIE: "Z",
    psutil.STATUS_DEAD: "X",
    psutil.STATUS_WAKING: "WA",
    psutil.STATUS_IDLE: "I",
    psutil.STATUS_LOCKED: "L",
    psutil.STATUS_WAITING: "W",
}
if hasattr(psutil, 'STATUS_WAKE_KILL'):
    PROC_STATUSES_RAW[psutil.STATUS_WAKE_KILL] = "WK"
if hasattr(psutil, 'STATUS_SUSPENDED'):
    PROC_STATUSES_RAW[psutil.STATUS_SUSPENDED] = "V"


def main():
    today_day = datetime.date.today()
    templ = "%-10s %5s %5s %5s %7s %7s %5s %-5s %5s %7s  %s"
    attrs = ['pid', 'cpu_percent', 'memory_percent', 'name', 'cpu_times',
             'create_time', 'memory_info', 'status', 'nice', 'username']
    print(templ % ("USER", "PID", "%CPU", "%MEM", "VSZ", "RSS", "NICE",
                   "STAT", "START", "TIME", "COMMAND"))
    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 ctime.date() == today_day:
                ctime = ctime.strftime("%H:%M")
            else:
                ctime = ctime.strftime("%b%d")
        else:
            ctime = ''
        if pinfo['cpu_times']:
            cputime = time.strftime("%M:%S",
                                    time.localtime(sum(pinfo['cpu_times'])))
        else:
            cputime = ''

        user = pinfo['username']
        if not user and psutil.POSIX:
            try:
                user = p.uids()[0]
            except psutil.Error:
                pass
        if user and psutil.WINDOWS and '\\' in user:
            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 ''

        status = PROC_STATUSES_RAW.get(pinfo['status'], pinfo['status'])
        print(templ % (
            user[:10],
            pinfo['pid'],
            pinfo['cpu_percent'],
            memp,
            vms,
            rss,
            nice,
            status,
            ctime,
            cputime,
            name
        ))


if __name__ == '__main__':
    main()

@@ -336,574 +336,8 @@
 n%0A%0A%0A
-PROC_STATUSES_RAW = %7B%0A    psutil.STATUS_RUNNING: %22R%22,%0A    psutil.STATUS_SLEEPING: %22S%22,%0A    psutil.STATUS_DISK_SLEEP: %22D%22,%0A    psutil.STATUS_STOPPED: %22T%22,%0A    psutil.STATUS_TRACING_STOP: %22t%22,%0A    psutil.STATUS_ZOMBIE: %22Z%22,%0A    psutil.STATUS_DEAD: %22X%22,%0A    psutil.STATUS_WAKING: %22WA%22,%0A    psutil.STATUS_IDLE: %22I%22,%0A    psutil.STATUS_LOCKED: %22L%22,%0A    psutil.STATUS_WAITING: %22W%22,%0A%7D%0Aif hasattr(psutil, 'STATUS_WAKE_KILL'):%0A    PROC_STATUSES_RAW%5Bpsutil.STATUS_WAKE_KILL%5D = %22WK%22%0Aif hasattr(psutil, 'STATUS_SUSPENDED'):%0A    PROC_STATUSES_RAW%5Bpsutil.STATUS_SUSPENDED%5D = %22V%22%0A%0A%0A
 def 
@@ -409,20 +409,16 @@
 %255s %255s 
-%255s 
 %257s %257s 
@@ -426,18 +426,17 @@
 5s %25
--5s %255s %257
+6s %256s %256
 s  %25
@@ -461,23 +461,8 @@
 id',
- 'cpu_percent',
  'me
@@ -607,16 +607,8 @@
 ID%22,
- %22%25CPU%22,
  %22%25M
@@ -683,15 +683,12 @@
 %22, %22
-COMMAND
+NAME
 %22))%0A
@@ -1992,24 +1992,25 @@
 '%5D else ''%0A%0A
+#
         stat
@@ -2066,24 +2066,88 @@
 %5B'status'%5D)%0A
+        status = pinfo%5B'status'%5D%5B:5%5D if pinfo%5B'status'%5D else ''%0A
         prin
@@ -2211,42 +2211,8 @@
 '%5D,%0A
-            pinfo%5B'cpu_percent'%5D,%0A
     

improve ps.py script