aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2013-06-17 20:37:35 +0200
committerMattias Andrée <maandree@operamail.com>2013-06-17 20:37:35 +0200
commit14b29fcb24ce285dfeb92f90ed2546e67d7b6fad (patch)
tree58857861bc83ae016c4c94e6d3990bf02f18275a
parentm doc (diff)
downloadargparser-14b29fcb24ce285dfeb92f90ed2546e67d7b6fad.tar.gz
argparser-14b29fcb24ce285dfeb92f90ed2546e67d7b6fad.tar.bz2
argparser-14b29fcb24ce285dfeb92f90ed2546e67d7b6fad.tar.xz
add hasinterpretor parameter to parent_name
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r--src/argparser.py33
1 files changed, 22 insertions, 11 deletions
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