From 27e7e80e922a51f3f4d21aa4d58f84fa9b5c6621 Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Mon, 15 Jul 2019 18:07:01 -0700 Subject: py3: fix up swift-orphans Change-Id: Id1280abd92e8bb02fcaa4701a0e9d211d9d6e33e --- bin/swift-orphans | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'bin') diff --git a/bin/swift-orphans b/bin/swift-orphans index d2a3889ad..ed2b896fa 100755 --- a/bin/swift-orphans +++ b/bin/swift-orphans @@ -15,6 +15,7 @@ from __future__ import print_function import optparse import os +import re import signal import subprocess import sys @@ -56,27 +57,30 @@ Example (sends SIGTERM to all orphaned Swift processes older than two hours): pids.append(open(os.path.join(root, name)).read().strip()) pids.extend(subprocess.Popen( ['ps', '--ppid', pids[-1], '-o', 'pid', '--no-headers'], - stdout=subprocess.PIPE).communicate()[0].split()) + stdout=subprocess.PIPE).communicate()[0].decode().split()) listing = [] + swift_cmd_re = re.compile( + '^/usr/bin/python[23]? /usr(?:/local)?/bin/swift-') for line in subprocess.Popen( ['ps', '-eo', 'etime,pid,args', '--no-headers'], - stdout=subprocess.PIPE).communicate()[0].split('\n'): + stdout=subprocess.PIPE).communicate()[0].split(b'\n'): if not line: continue hours = 0 try: - etime, pid, args = line.split(None, 2) + etime, pid, args = line.decode('ascii').split(None, 2) except ValueError: sys.exit('Could not process ps line %r' % line) if pid in pids: continue - if (not args.startswith('/usr/bin/python /usr/bin/swift-') and - not args.startswith('/usr/bin/python /usr/local/bin/swift-')) or \ - 'swift-orphans' in args or \ - 'once' in args.split(): + if any([ + not swift_cmd_re.match(args), + 'swift-orphans' in args, + 'once' in args.split(), + ]): continue - args = args.split('-', 1)[1] + args = args.split('swift-', 1)[1] etime = etime.split('-') if len(etime) == 2: hours = int(etime[0]) * 24 @@ -105,11 +109,11 @@ Example (sends SIGTERM to all orphaned Swift processes older than two hours): args_len = max(args_len, len(args)) args_len = min(args_len, 78 - hours_len - pid_len) - print(('%%%ds %%%ds %%s' % (hours_len, pid_len)) % - ('Hours', 'PID', 'Command')) + print('%*s %*s %s' % + (hours_len, 'Hours', pid_len, 'PID', 'Command')) for hours, pid, args in listing: - print(('%%%ds %%%ds %%s' % (hours_len, pid_len)) % - (hours, pid, args[:args_len])) + print('%*s %*s %s' % + (hours_len, hours, pid_len, pid, args[:args_len])) if options.signal: try: -- cgit v1.2.1