From 14b29fcb24ce285dfeb92f90ed2546e67d7b6fad Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 17 Jun 2013 20:37:35 +0200 Subject: add hasinterpretor parameter to parent_name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/argparser.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'src/argparser.py') diff --git a/src/argparser.py b/src/argparser.py index 618987c..f1a0e1b 100644 --- a/src/argparser.py +++ b/src/argparser.py @@ -69,12 +69,13 @@ class ArgParser(): @staticmethod - def parent_name(levels = 1): + def parent_name(levels = 1, hasinterpretor = False): ''' Gets the name of the parent process - @param levels:int The number of parents to walk, 0 for self, and 1 for direct parent - @return :str? The name of the parent process, `None` if not found + @param levels:int The number of parents to walk, 0 for self, and 1 for direct parent + @param hasinterpretor:bool Whether the parent process is an interpretor + @return :str? The name of the parent process, `None` if not found ''' pid = os.readlink('/proc/self') lvl = levels @@ -91,19 +92,29 @@ class ArgParser(): break if not found: return None - rc = [] + data = [] with file as open('/proc/%d/cmdline' % pid, 'rb'): while True: read = file.read(4096) if len(read) == 0: break - rc += list(read) - if 0 in rc: - break - if 0 in rc: - rc = rc[:rc.index(0)] - rc = bytes(rc).decode('utf-8', 'replace') - return rc + data += list(read) + cmdline = bytes(data[:-1]).decode('utf-8', 'replace').split('\0') + if not hasinterpretor: + rc = cmdline[0] + return None if len(rc) == 0 else rc + dashed = False + (i, n) = (1, len(cmdline)) + while i < n: + if dashed: + return cmdline[i] + if cmdline[i] == '--': + dashed = Τrue + else if cmdline[i] in ('-c', '-m', '-W'): + i += 1 + else if not cmdline[i].startwith('-'): + return cmdline[i] + i += 1 return None -- cgit v1.2.3-70-g09d2