#! /usr/bin/env python3

from getpass import getuser
from datetime import datetime
from re import search

try:
    import psutil
    has_psutil = True
except ImportError:
    import warnings
    warnings.warn('you should install psutil for Python')
    import os
    has_psutil = False

from vcsn_tools.demangle import pretty_plugin


def memory(proc):
    res = proc.memory_info().rss
    for p in proc.children(recursive=True):
        res += p.memory_info().rss
    return res


def print_proc(title, message, proc):
    delta = datetime.now() - datetime.fromtimestamp(proc.create_time())
    mem = memory(proc)
    print('{}:{:0>2}: {:.1f}GB: {:<10} {}'
          .format(int(delta.seconds / 60),
                  delta.seconds % 60,
                  mem / 2 ** 30,
                  title + ':', message))


def user_proc(proc):
    cmd = ' '.join(proc.cmdline())
    # Look for compilations.
    #
    # The cmdline is typically ['sh', '-c', 'vcsn compile...']
    m = search(r'.*vcsn.compile.*\'.*?(/plugins/[^/]+/.*)\.cc\'.*$', cmd)
    if m:
        title, message = pretty_plugin(m.group(1))
        print_proc(title, message, proc)
    # Look for notebook checking.
    #
    # The cmdline is something like
    # ['/opt/local/Library/Frameworks/Python.framework/Versions/3.5/'
    # 'Resources/Python.app/Contents/MacOS/Python',
    # '/Users/akim/src/lrde/2/tests/bin/ipynbdoctest.py', '--tap',
    # '/Users/akim/src/lrde/2/doc/notebooks/Spell-checker.ipynb'].
    # There are three lines for each notebook, the two others are the
    # shell running tap-driver.sh.  So keep only the line without
    # tap-driver.
    m = search(r'ipynbdoctest.py --tap (?:.*?/)?([^/]*\.ipynb)', cmd)
    if m and 'tap-driver.sh' not in cmd:
        title, message = 'CHECK', m.group(1)
        print_proc(title, message, proc)


if has_psutil:
    for proc in psutil.process_iter():
        try:
            if proc.username() == getuser():
                user_proc(proc)
        except (psutil.AccessDenied, psutil.NoSuchProcess):
            pass
else:
    cmd = \
    r'''ps uxww |
        grep -v perl |
        grep "vcsn compile" |
        perl -ne "s{^.*'.*?/lib/plugins/(.*?)\.cc'.*$}{\$1}g && print \$_"'''
    os.system(cmd)
