Skip to content

Commit ae005c5

Browse files
committed
fix pgrep call quoting
1 parent c615310 commit ae005c5

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

pgtree/pgtree.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@
6464
import time
6565
except ImportError:
6666
pass
67+
try:
68+
from shlex import join as shlex_join
69+
except ImportError:
70+
import pipes
71+
def shlex_join(cmd_list):
72+
"""shlex.join for python2"""
73+
return ' '.join([pipes.quote(c) for c in cmd_list])
6774
6875
# impossible detection using ps for AIX/MacOS
6976
# stime is not start time of process
@@ -246,7 +253,7 @@ def pgrep(self, argv):
246253
"""mini built-in pgrep if pgrep command not available
247254
[-f] [-x] [-i] [-u <user>] [pattern]"""
248255
if "PGT_PGREP" not in os.environ or os.environ["PGT_PGREP"]:
249-
_, pgrep = runcmd('pgrep ' +' '.join(argv))
256+
_, pgrep = runcmd('pgrep ' + shlex_join(argv))
250257
return pgrep.split("\n")
251258
252259
try:
@@ -268,7 +275,6 @@ def pgrep(self, argv):
268275
exact = True
269276
elif opt == "-u":
270277
user = arg
271-
272278
if args:
273279
pattern = args[0]
274280
if exact:
@@ -282,7 +288,6 @@ def pgrep(self, argv):
282288
pids.append(pid)
283289
return pids
284290
285-
286291
def get_parents(self):
287292
"""get parents list of pids"""
288293
last_ppid = None
@@ -353,6 +358,8 @@ def _print_tree(self, pids, print_it=True, pre=' '):
353358
354359
def print_tree(self, pids=None, child_only=False, sig=0, confirmed=False):
355360
"""display full or children only process tree"""
361+
if pids == []:
362+
return
356363
if pids:
357364
self.pids = pids
358365
else:

0 commit comments

Comments
 (0)