From e16e0d3a4a4164372cc9c0fb16a1b2fb62e76b55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Pag=C3=A8s?= Date: Tue, 6 Oct 2015 21:35:43 +0200 Subject: [PATCH] Bug 1139487 - Remove mozprocess.pid, mozprocess.wpk. r=jmaher --- testing/mozbase/mozprocess/mozprocess/pid.py | 86 -------------------- testing/mozbase/mozprocess/mozprocess/wpk.py | 56 ------------- testing/mozbase/mozprocess/tests/proctest.py | 9 +- 3 files changed, 5 insertions(+), 146 deletions(-) delete mode 100755 testing/mozbase/mozprocess/mozprocess/pid.py delete mode 100644 testing/mozbase/mozprocess/mozprocess/wpk.py diff --git a/testing/mozbase/mozprocess/mozprocess/pid.py b/testing/mozbase/mozprocess/mozprocess/pid.py deleted file mode 100755 index 809bd9fe7ca..00000000000 --- a/testing/mozbase/mozprocess/mozprocess/pid.py +++ /dev/null @@ -1,86 +0,0 @@ -#!/usr/bin/env python - -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this file, -# You can obtain one at http://mozilla.org/MPL/2.0/. - -from __future__ import absolute_import - -import mozinfo -import shlex -import subprocess -import sys - -# determine the platform-specific invocation of `ps` -if mozinfo.isWin: - psarg='ax' -else: - psarg = 'axwww' - -def ps(arg=psarg): - """ - python front-end to `ps` - http://en.wikipedia.org/wiki/Ps_%28Unix%29 - returns a list of process dicts based on the `ps` header - """ - retval = [] - process = subprocess.Popen(['ps', arg], stdout=subprocess.PIPE) - stdout, _ = process.communicate() - header = None - for line in stdout.splitlines(): - line = line.strip() - if header is None: - # first line is the header - header = line.split() - continue - split = line.split(None, len(header)-1) - process_dict = dict(zip(header, split)) - retval.append(process_dict) - return retval - -def running_processes(name, psarg=psarg, defunct=True): - """ - returns a list of - {'PID': PID of process (int) - 'command': command line of process (list)} - with the executable named `name`. - - defunct: whether to return defunct processes - """ - retval = [] - for process in ps(psarg): - # Support for both BSD and UNIX syntax - # `ps aux` returns COMMAND, `ps -ef` returns CMD - try: - command = process['COMMAND'] - except KeyError: - command = process['CMD'] - - command = shlex.split(command) - if command[-1] == '': - command = command[:-1] - if not command or not defunct: - continue - if 'STAT' in process and not defunct: - if process['STAT'] == 'Z+': - continue - command = subprocess.list2cmdline(command) - if name in command: - retval.append((int(process['PID']), command)) - return retval - -def get_pids(name): - """Get all the pids matching name""" - - if mozinfo.isWin: - # use the windows-specific implementation - from . import wpk - return wpk.get_pids(name) - else: - return [pid for pid,_ in running_processes(name)] - -if __name__ == '__main__': - pids = set() - for i in sys.argv[1:]: - pids.update(get_pids(i)) - for i in sorted(pids): - print i diff --git a/testing/mozbase/mozprocess/mozprocess/wpk.py b/testing/mozbase/mozprocess/mozprocess/wpk.py deleted file mode 100644 index 5d707c2e7b7..00000000000 --- a/testing/mozbase/mozprocess/mozprocess/wpk.py +++ /dev/null @@ -1,56 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this file, -# You can obtain one at http://mozilla.org/MPL/2.0/. - -from __future__ import absolute_import - -from ctypes import sizeof, windll, addressof, create_unicode_buffer -from ctypes.wintypes import DWORD, HANDLE - -PROCESS_TERMINATE = 0x0001 -PROCESS_QUERY_INFORMATION = 0x0400 -PROCESS_VM_READ = 0x0010 - -def get_pids(process_name): - BIG_ARRAY = DWORD * 4096 - processes = BIG_ARRAY() - needed = DWORD() - - pids = [] - result = windll.psapi.EnumProcesses(processes, - sizeof(processes), - addressof(needed)) - if not result: - return pids - - num_results = needed.value / sizeof(DWORD) - - for i in range(num_results): - pid = processes[i] - process = windll.kernel32.OpenProcess(PROCESS_QUERY_INFORMATION | - PROCESS_VM_READ, - 0, pid) - if process: - module = HANDLE() - result = windll.psapi.EnumProcessModules(process, - addressof(module), - sizeof(module), - addressof(needed)) - if result: - name = create_unicode_buffer(1024) - result = windll.psapi.GetModuleBaseNameW(process, module, - name, len(name)) - # TODO: This might not be the best way to - # match a process name; maybe use a regexp instead. - if name.value.startswith(process_name): - pids.append(pid) - windll.kernel32.CloseHandle(module) - windll.kernel32.CloseHandle(process) - - return pids - -def kill_pid(pid): - process = windll.kernel32.OpenProcess(PROCESS_TERMINATE, 0, pid) - if process: - windll.kernel32.TerminateProcess(process, 0) - windll.kernel32.CloseHandle(process) diff --git a/testing/mozbase/mozprocess/tests/proctest.py b/testing/mozbase/mozprocess/tests/proctest.py index 12d70cf8c69..92a510ba238 100644 --- a/testing/mozbase/mozprocess/tests/proctest.py +++ b/testing/mozbase/mozprocess/tests/proctest.py @@ -1,12 +1,11 @@ -import mozinfo import os -import subprocess import sys import unittest -from mozprocess.pid import get_pids +import psutil here = os.path.dirname(os.path.abspath(__file__)) + def check_for_process(processName): """ Use to determine if process of the given name is still running. @@ -16,12 +15,14 @@ def check_for_process(processName): output -- if process exists, stdout of the process, [] otherwise """ name = os.path.basename(processName) - process = get_pids(name) + process = [p.pid for p in psutil.process_iter() + if p.name() == name] if process: return True, process return False, [] + class ProcTest(unittest.TestCase): @classmethod