# Copyright 2026 SigmaSRC, Inc. All Rights Reserved.

"""Code for inspecting processes."""
# XXX This is code I wrote but ended up not needed.

class Process(object):
    """Represents a process.

    Attributes are username, pid, ppid, command.

    Used by processes() above.

    """

    def __init__(self, record):
        assert len(record) == 8
        self.username = record[0]
        self.pid = int(record[1])
        self.ppid = int(record[2])
        self.command = record[7]

def processes(selector="-e"):
    """Returns a dict mapping process ids to Process instances."""
    if sys.platform.startswith("win"):
        return {} # XXX (See gather_winps.py for how to do this)
    # XXX Shouldn't this use /proc?
    if sys.platform == "linux2":
        flags = "-fww"
    else:
        flags = "-f"
    pipe = os.popen("ps %s %s" % (selector, flags), "r")
    it = iter(pipe)
    line = next(it)
    assert " ".join(line.split()) == "UID PID PPID C STIME TTY TIME CMD"
    table = {}
    for line in it:
        record = line.split(None, 7) # 7 separators, i.e. 8 fields
                                     # (the last field may contain spaces)
        if len(record) == 8:
            proc = Process(record)
            table[proc.pid] = proc
    pipe.close() # Ignore exit status; if it didn't work, too bad
    return table
