[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [stem/master] Correcting issues with pid_by_name()
commit 738d4f54fb2559236b810d2ca3ca6b1e0eb5edc1
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date: Tue Feb 17 09:34:58 2015 -0800
Correcting issues with pid_by_name()
This has quite a few bits of weirdness...
* Our comment says call() doesn't work but then uses it.
* Our comment says the command might or might not end with '.exe' but then we
always check for that. Sounds wrong but opting for what the code does and
just dropping the mismatching comment.
* We checked for '.exe' anywhere in the string rather than just the end.
* We checked to see if the netstat command existed to see if we could run
tasklist.
* Unlike the other checks we raised an exception here rather than passing
through when unable to determine the pid.
* Regex handling was a bit more verbose than it needed to be.
* We didn't actually use GET_PID_BY_NAME_TASKLIST.
---
stem/util/system.py | 31 ++++++++-----------------------
test/integ/util/system.py | 6 ++----
2 files changed, 10 insertions(+), 27 deletions(-)
diff --git a/stem/util/system.py b/stem/util/system.py
index efe8663..9ee8ad6 100644
--- a/stem/util/system.py
+++ b/stem/util/system.py
@@ -47,10 +47,10 @@ import ctypes.util
import mimetypes
import os
import platform
+import re
import subprocess
import tarfile
import time
-import re
import stem.util.proc
import stem.util.str_tools
@@ -79,7 +79,6 @@ GET_PID_BY_NAME_PIDOF = 'pidof %s'
GET_PID_BY_NAME_PS_LINUX = 'ps -o pid -C %s'
GET_PID_BY_NAME_PS_BSD = 'ps axc'
GET_PID_BY_NAME_LSOF = 'lsof -tc %s'
-GET_PID_BY_NAME_TASKLIST = 'tasklist | findstr %s'
GET_PID_BY_PORT_NETSTAT = 'netstat -npltu'
GET_PID_BY_PORT_SOCKSTAT = 'sockstat -4l -P tcp -p %s'
GET_PID_BY_PORT_LSOF = 'lsof -wnP -iTCP -sTCP:LISTEN'
@@ -438,48 +437,34 @@ def pid_by_name(process_name, multiple = False):
except ValueError:
pass
- # Calling and Parsing tasklist command on Windows (Because call method doesn't work properly with it)
- # Process name may or may not include .exe
- if is_available('netstat -ano') and is_windows():
-
- if process_name.find(".exe") == -1:
+ if is_available('tasklist') and is_windows():
+ if not process_name.endswith('.exe'):
process_name = process_name + '.exe'
- command = GET_PID_BY_NAME_TASKLIST % process_name
process_ids = []
- try:
- results = stem.util.system.call('tasklist')
- tasklist_regex_str = '^\s*' + process_name + '\s+(?P<pid>[0-9]*)'
- tasklist_regex = re.compile(tasklist_regex_str)
+ results = stem.util.system.call('tasklist', None)
- if not results:
- raise IOError("No results found for tasklist")
+ if results:
+ tasklist_regex = re.compile('^\s*%s\s+(?P<pid>[0-9]*)' % process_name)
for line in results:
match = tasklist_regex.search(line)
+
if match:
- attr = match.groupdict()
- id = int(attr['pid'])
- process_ids.append(id)
+ process_ids.append(int(match.group('pid')))
if multiple:
return process_ids
elif len(process_ids) > 0:
return process_ids[0]
- except OSError as exc:
- log.debug("failed to query '%s': %s" % (command, exc))
- raise IOError("Unable to query '%s': %s" % (command, exc))
-
log.debug("failed to resolve a pid for '%s'" % process_name)
return [] if multiple else None
def pid_by_port(port):
-
"""
-
Attempts to determine the process id for a process with the given port,
using...
diff --git a/test/integ/util/system.py b/test/integ/util/system.py
index aa18f45..e06b569 100644
--- a/test/integ/util/system.py
+++ b/test/integ/util/system.py
@@ -221,7 +221,6 @@ class TestSystem(unittest.TestCase):
Tests the pid_by_name function with a tasklist response.
"""
- runner = test.runner.get_runner()
if self._is_extra_tor_running():
test.runner.skip(self, '(multiple tor instances)')
return
@@ -229,9 +228,8 @@ class TestSystem(unittest.TestCase):
test.runner.skip(self, '(tasklist unavailable)')
return
- tor_pid = test.runner.get_runner().get_pid()
- tor_cmd = test.runner.get_runner().get_tor_command(True)
- self.assertEqual(tor_pid, stem.util.system.pid_by_name(tor_cmd))
+ runner = test.runner.get_runner()
+ self.assertEqual(runner.get_pid(), stem.util.system.pid_by_name(runner.get_tor_command(True)))
def test_pid_by_port(self):
"""
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits