Index: tools/task_kill.py |
=================================================================== |
--- tools/task_kill.py (revision 0) |
+++ tools/task_kill.py (revision 0) |
@@ -0,0 +1,144 @@ |
+#!/usr/bin/env python |
+# |
+# Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
+# for details. All rights reserved. Use of this source code is governed by a |
+# BSD-style license that can be found in the LICENSE file. |
+# |
+ |
+# A script to kill hanging processs. The tool will return non-zero if any |
+# process was actually found. |
+# |
+ |
+import optparse |
+import os |
+import signal |
+import shutil |
+import string |
+import subprocess |
+import sys |
+import utils |
+ |
+os_name = None |
+ |
+POSIX_LIST = 'ps -e -o comm,pid' |
kustermann
2013/08/27 08:42:22
Please add a comment, which gives an example outpu
ricow1
2013/08/27 11:45:25
This has been moved into the platform specific fun
|
+POSIX_INFO = 'ps -p %s -o args' |
+ |
+LIST_COMMAND = { |
+ 'win32': 'wmic process get Caption,Processid', |
+ 'macos': POSIX_LIST, |
+ 'linux': POSIX_LIST, |
+} |
+ |
+INFO_COMMAND = { |
+ 'win32': 'wmic process where Processid=%s get CommandLine', |
+ 'macos': POSIX_INFO, |
+ 'linux': POSIX_INFO, |
+} |
+ |
+def GetOptions(): |
+ parser = optparse.OptionParser("usage: %prog [options]") |
+ parser.add_option("--kill_dart", default=True, |
+ help="Kill all dart processes") |
+ parser.add_option("--kill_browsers", default=False, |
kustermann
2013/08/27 08:42:22
Why the difference in default values?
ricow1
2013/08/27 11:45:25
Because if people use it locally I don't want to k
|
+ help="Kill all browser processes") |
+ (options, args) = parser.parse_args() |
+ return options |
+ |
+def GetPids(process_name): |
+ # We asume that the list command will return lines in the format: |
+ # PROCESS_NAME WHITESPACE* PID |
kustermann
2013/08/27 08:42:22
Don't know if this is working. Often on mac and wi
ricow1
2013/08/27 11:45:25
I changed the order now and just return the last p
|
+ p = subprocess.Popen(LIST_COMMAND[os_name], |
+ stdout=subprocess.PIPE, |
+ stderr=subprocess.PIPE, |
+ shell=True) |
+ output, stderr = p.communicate() |
+ results = [] |
+ |
+ lines = output.splitlines() |
+ # Pop the header |
+ lines.pop(0) |
kustermann
2013/08/27 08:42:22
Do we have this header on windows and linux?
ricow1
2013/08/27 11:45:25
Yes we did, but not with the new command
|
+ for line in lines: |
+ split = line.split() |
+ if len(split) == 2 and split[0] == process_name: |
+ results.append(split[1]) |
kustermann
2013/08/27 08:42:22
see comment above.
ricow1
2013/08/27 11:45:25
Done.
|
+ return results |
+ |
+def PrintPidInfo(pid): |
+ # We asume that the list command will return lines in the format: |
+ # EXECUTABLE_PATH ARGS |
+ # There may be blank strings in the output |
+ p = subprocess.Popen(INFO_COMMAND[os_name] % pid, |
+ stdout=subprocess.PIPE, |
+ stderr=subprocess.PIPE, |
+ shell=True) |
+ output, stderr = p.communicate() |
+ lines = output.splitlines() |
+ |
+ # Pop the header |
+ lines.pop(0) |
+ for line in lines: |
+ # wmic will output a bunch of empty strings, we ignore these |
+ if len(line) >= 1: |
+ print("Hanging process info:") |
+ print(" PID: %s" % pid) |
+ print(" Command line: %s" % line) |
kustermann
2013/08/27 08:42:22
Same comments as above.
ricow1
2013/08/27 11:45:25
Done.
|
+ |
+def KillPosix(pid): |
+ try: |
+ os.kill(int(pid), signal.SIGTERM); |
kustermann
2013/08/27 08:42:22
What if SIGTERM is ignored? We should really make
ricow1
2013/08/27 11:45:25
Done.
ricow1
2013/08/27 11:45:25
Done.
|
+ except: |
+ # Ignore this, the process is already dead from killing another process. |
+ pass |
+ |
+def KillWindows(pid): |
+ # os.kill is not available until python 2.7 |
+ cmd = "taskkill /F /PID %s" % pid |
+ p = subprocess.Popen(cmd, |
+ stdout=subprocess.PIPE, |
+ stderr=subprocess.PIPE, |
+ shell=True) |
+ |
+def GetExecutableName(name): |
+ if (os_name == "win32"): |
+ return "%s.exe" % name |
+ else: |
+ return name |
+ |
+def Kill(name): |
+ print("***************** Killing %s *****************" % name) |
+ pids = GetPids(GetExecutableName(name)) |
+ for pid in pids: |
+ PrintPidInfo(pid); |
+ if (os_name == "win32"): |
+ KillWindows(pid) |
+ else: |
+ KillPosix(pid) |
+ print("Killed pid: %s" % pid) |
kustermann
2013/08/27 08:42:22
You don't know if it was successfull or not at thi
|
+ if (len(pids) == 0): |
+ print(" Nothing to kill") |
kustermann
2013/08/27 08:42:22
We could print this out multiple times (for every
ricow1
2013/08/27 11:45:25
That is what I want, the log will explicitly show
|
+ return len(pids) |
kustermann
2013/08/27 08:42:22
We should return here how many we actually killed.
ricow1
2013/08/27 11:45:25
well, that is hard to figure out without listing a
|
+ |
+def KillBrowsers(): |
+ status = Kill('firefox') |
+ status += Kill('chrome') |
+ status += Kill('iexplore') |
kustermann
2013/08/27 08:42:22
"status" is not the correct name. We should call i
ricow1
2013/08/27 11:45:25
Well, it is the return status :-)
|
+ status += Kill('Safari') |
kustermann
2013/08/27 08:42:22
firefox is probably uppercase on mac. We should ma
ricow1
2013/08/27 11:45:25
No it is not, but chrome is
|
+ return status |
+ |
+def KillDart(): |
+ status = Kill("dart") |
+ return status |
+ |
+def Main(): |
+ global os_name |
+ options = GetOptions() |
+ os_name = utils.GuessOS() |
kustermann
2013/08/27 08:42:22
Don't do it here. Just do assign it at the module
ricow1
2013/08/27 11:45:25
Done.
|
+ status = 0 |
+ if (options.kill_dart): |
+ status += KillDart(); |
+ if (options.kill_browsers): |
+ status += status + KillBrowsers() |
+ return status |
+ |
+if __name__ == '__main__': |
+ sys.exit(Main()) |
Property changes on: tools/task_kill.py |
___________________________________________________________________ |
Added: svn:eol-style |
+ LF |
Added: svn:executable |
+ * |