Bug 1139487 - Remove mozprocess.pid, mozprocess.wpk. r=jmaher

This commit is contained in:
Julien Pagès 2015-10-06 21:35:43 +02:00
parent 7907a32929
commit e16e0d3a4a
3 changed files with 5 additions and 146 deletions

View File

@ -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] == '<defunct>':
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

View File

@ -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)

View File

@ -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