<s> import os <EOL> import sys <EOL> import re <EOL> import json <EOL> class CheckProcs ( object ) : <EOL> myPid = <NUM_LIT:0> <EOL> state = "<STR_LIT>" <EOL> name = "<STR_LIT>" <EOL> pid = <NUM_LIT:0> <EOL> allProcs = [ ] <EOL> interestingProcs = [ ] <EOL> procDir = "<STR_LIT>" <EOL> debug = False <EOL> def __init__ ( self ) : <EOL> self . myPid = os . getpid ( ) <EOL> def setup ( self , debug = False , pidlist = False ) : <EOL> self . debug = debug <EOL> self . pidlist = pidlist <EOL> if debug is True : <EOL> print ( "<STR_LIT>" ) <EOL> self . allProcs = [ procs for procs in os . listdir ( self . procDir ) if procs . isdigit ( ) and <EOL> int ( procs ) != int ( self . myPid ) ] <EOL> def process ( self , criteria ) : <EOL> for p in self . allProcs : <EOL> try : <EOL> fh = open ( self . procDir + "<STR_LIT:/>" + p + "<STR_LIT>" ) <EOL> pInfo = fh . readline ( ) . split ( ) <EOL> cmdfh = open ( self . procDir + "<STR_LIT:/>" + p + "<STR_LIT>" ) <EOL> cmd = cmdfh . readline ( ) <EOL> pInfo [ <NUM_LIT:1> ] = cmd <EOL> except : <EOL> continue <EOL> finally : <EOL> cmdfh . close ( ) <EOL> fh . close ( ) <EOL> if criteria == '<STR_LIT:state>' : <EOL> if pInfo [ <NUM_LIT:2> ] == self . state : <EOL> self . interestingProcs . append ( pInfo ) <EOL> elif criteria == '<STR_LIT:name>' : <EOL> if re . search ( self . name , pInfo [ <NUM_LIT:1> ] ) : <EOL> self . interestingProcs . append ( pInfo ) <EOL> elif criteria == '<STR_LIT>' : <EOL> if pInfo [ <NUM_LIT:0> ] == self . pid : <EOL> self . interestingProcs . append ( pInfo ) <EOL> def byState ( self , state ) : <EOL> self . state = state <EOL> self . process ( criteria = '<STR_LIT:state>' ) <EOL> self . show ( ) <EOL> def byPid ( self , pid ) : <EOL> self . pid = pid <EOL> self . process ( criteria = '<STR_LIT>' ) <EOL> self . show ( ) <EOL> def byName ( self , name ) : <EOL> self . name = name <EOL> self . process ( criteria = '<STR_LIT:name>' ) <EOL> self . show ( ) <EOL> def run ( self , foo , criteria ) : <EOL> if foo == '<STR_LIT:state>' : <EOL> self . byState ( criteria ) <EOL> elif foo == '<STR_LIT:name>' : <EOL> self . byName ( criteria ) <EOL> elif foo == '<STR_LIT>' : <EOL> self . byPid ( criteria ) <EOL> def show ( self ) : <EOL> prettyOut = { } <EOL> if len ( self . interestingProcs ) > <NUM_LIT:0> : <EOL> for proc in self . interestingProcs : <EOL> prettyOut [ proc [ <NUM_LIT:0> ] ] = proc [ <NUM_LIT:1> ] <EOL> if self . pidlist is True : <EOL> pidlist = '<STR_LIT:U+0020>' . join ( prettyOut . keys ( ) ) <EOL> sys . stderr . write ( pidlist ) <EOL> print ( json . dumps ( prettyOut ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if "<STR_LIT>" in sys . argv : <EOL> pidlist = True <EOL> else : <EOL> pidlist = False <EOL> foo = CheckProcs ( ) <EOL> foo . setup ( debug = False , pidlist = pidlist ) <EOL> foo . run ( sys . argv [ <NUM_LIT:1> ] , sys . argv [ <NUM_LIT:2> ] ) </s>
