commit 666a831342cb1bc9311ae6e8767a10c4fd1dac5c Author: Damian Johnson atagar@torproject.org Date: Mon Sep 1 16:49:04 2014 -0700
Dropping get_* prefix from proc functions
Just about all the proc functions started with get_*. Bad habbit from my days of doing java. Dropping them, with an alias for the old names for backward compatability. --- stem/control.py | 1 + stem/util/proc.py | 50 ++++++++++++++++++++++-------------- test/integ/util/proc.py | 32 ++++++++++++------------ test/unit/util/proc.py | 64 +++++++++++++++++++++++------------------------ 4 files changed, 80 insertions(+), 67 deletions(-)
diff --git a/stem/control.py b/stem/control.py index 8571a7f..7c5d7bd 100644 --- a/stem/control.py +++ b/stem/control.py @@ -128,6 +128,7 @@ If you're fine with allowing your script to raise exceptions then this can be mo BaseController - Base controller class asynchronous message handling |- msg - communicates with the tor process |- is_alive - reports if our connection to tor is open or closed + |- connection_time - time when we last connected or disconnected |- is_authenticated - checks if we're authenticated to tor |- connect - connects or reconnects to tor |- close - shuts down our connection to the tor process diff --git a/stem/util/proc.py b/stem/util/proc.py index f9b2a59..ac1f6f6 100644 --- a/stem/util/proc.py +++ b/stem/util/proc.py @@ -20,18 +20,18 @@ future, use them at your own risk.** ::
is_available - checks if proc utilities can be used on this system - get_system_start_time - unix timestamp for when the system started - get_physical_memory - memory available on this system - get_cwd - provides the current working directory for a process - get_uid - provides the user id a process is running under - get_memory_usage - provides the memory usage of a process - get_stats - queries statistics about a process - get_file_descriptors_used - number of file descriptors used by a process - get_connections - provides the connections made by a process + system_start_time - unix timestamp for when the system started + physical_memory - memory available on this system + cwd - provides the current working directory for a process + uid - provides the user id a process is running under + memory_usage - provides the memory usage of a process + stats - queries statistics about a process + file_descriptors_used - number of file descriptors used by a process + connections - provides the connections made by a process
.. data:: Stat (enum)
- Types of data available via the :func:`~stem.util.proc.get_stats` function. + Types of data available via the :func:`~stem.util.proc.stats` function.
============== =========== Stat Description @@ -94,7 +94,7 @@ def is_available():
@lru_cache() -def get_system_start_time(): +def system_start_time(): """ Provides the unix time (seconds since epoch) when the system started.
@@ -117,7 +117,7 @@ def get_system_start_time():
@lru_cache() -def get_physical_memory(): +def physical_memory(): """ Provides the total physical memory on the system in bytes.
@@ -139,7 +139,7 @@ def get_physical_memory(): raise exc
-def get_cwd(pid): +def cwd(pid): """ Provides the current working directory for the given process.
@@ -167,7 +167,7 @@ def get_cwd(pid): return cwd
-def get_uid(pid): +def uid(pid): """ Provides the user ID the given process is running under.
@@ -192,7 +192,7 @@ def get_uid(pid): raise exc
-def get_memory_usage(pid): +def memory_usage(pid): """ Provides the memory usage in bytes for the given process.
@@ -225,7 +225,7 @@ def get_memory_usage(pid): raise exc
-def get_stats(pid, *stat_types): +def stats(pid, *stat_types): """ Provides process specific information. See the :data:`~stem.util.proc.Stat` enum for valid options. @@ -281,19 +281,19 @@ def get_stats(pid, *stat_types): results.append(str(float(stat_comp[14]) / CLOCK_TICKS)) elif stat_type == Stat.START_TIME: if pid == 0: - return get_system_start_time() + return system_start_time() else: # According to documentation, starttime is in field 21 and the unit is # jiffies (clock ticks). We divide it for clock ticks, then add the # uptime to get the seconds since the epoch. p_start_time = float(stat_comp[21]) / CLOCK_TICKS - results.append(str(p_start_time + get_system_start_time())) + results.append(str(p_start_time + system_start_time()))
_log_runtime(parameter, stat_path, start_time) return tuple(results)
-def get_file_descriptors_used(pid): +def file_descriptors_used(pid): """ Provides the number of file descriptors currently being used by a process.
@@ -318,7 +318,7 @@ def get_file_descriptors_used(pid): raise IOError("Unable to check number of file descriptors used: %s" % exc)
-def get_connections(pid): +def connections(pid): """ Queries connection related information from the proc contents. This provides similar results to netstat, lsof, sockstat, and other connection resolution @@ -524,3 +524,15 @@ def _log_failure(parameter, exc): """
log.debug('proc call failed (%s): %s' % (parameter, exc)) + +# TODO: drop with stem 2.x +# We renamed our methods to drop a redundant 'get_*' prefix, so alias the old +# names for backward compatability. + +get_system_start_time = system_start_time +get_physical_memory = physical_memory +get_cwd = cwd +get_uid = uid +get_memory_usage = memory_usage +get_stats = stats +get_connections = connections diff --git a/test/integ/util/proc.py b/test/integ/util/proc.py index 8b76dd6..cad0114 100644 --- a/test/integ/util/proc.py +++ b/test/integ/util/proc.py @@ -12,9 +12,9 @@ from stem.util import proc
class TestProc(unittest.TestCase): - def test_get_cwd(self): + def test_cwd(self): """ - Checks that stem.util.proc.get_cwd matches our tor instance's cwd. + Checks that stem.util.proc.cwd matches our tor instance's cwd. """
if not proc.is_available(): @@ -26,11 +26,11 @@ class TestProc(unittest.TestCase):
runner = test.runner.get_runner() runner_pid, tor_cwd = runner.get_pid(), runner.get_tor_cwd() - self.assertEquals(tor_cwd, proc.get_cwd(runner_pid)) + self.assertEquals(tor_cwd, proc.cwd(runner_pid))
- def test_get_uid(self): + def test_uid(self): """ - Checks that stem.util.proc.get_uid matches our tor instance's uid. + Checks that stem.util.proc.uid matches our tor instance's uid. """
if not proc.is_available(): @@ -38,11 +38,11 @@ class TestProc(unittest.TestCase): return
tor_pid = test.runner.get_runner().get_pid() - self.assertEquals(os.geteuid(), proc.get_uid(tor_pid)) + self.assertEquals(os.geteuid(), proc.uid(tor_pid))
- def test_get_memory_usage(self): + def test_memory_usage(self): """ - Checks that stem.util.proc.get_memory_usage looks somewhat reasonable. + Checks that stem.util.proc.memory_usage looks somewhat reasonable. """
if not proc.is_available(): @@ -50,15 +50,15 @@ class TestProc(unittest.TestCase): return
tor_pid = test.runner.get_runner().get_pid() - res_size, vir_size = proc.get_memory_usage(tor_pid) + res_size, vir_size = proc.memory_usage(tor_pid)
# checks that they're larger than a kilobyte self.assertTrue(res_size > 1024) self.assertTrue(vir_size > 1024)
- def test_get_stats(self): + def test_stats(self): """ - Checks that stem.util.proc.get_stats looks somewhat reasonable. + Checks that stem.util.proc.stats looks somewhat reasonable. """
if not proc.is_available(): @@ -66,16 +66,16 @@ class TestProc(unittest.TestCase): return
tor_pid = test.runner.get_runner().get_pid() - command, utime, stime, start_time = proc.get_stats(tor_pid, 'command', 'utime', 'stime', 'start time') + command, utime, stime, start_time = proc.stats(tor_pid, 'command', 'utime', 'stime', 'start time')
self.assertEquals('tor', command) self.assertTrue(float(utime) > 0) self.assertTrue(float(stime) >= 0) - self.assertTrue(float(start_time) > proc.get_system_start_time()) + self.assertTrue(float(start_time) > proc.system_start_time())
- def test_get_connections(self): + def test_connections(self): """ - Checks for our control port in the stem.util.proc.get_connections output if + Checks for our control port in the stem.util.proc.connections output if we have one. """
@@ -95,7 +95,7 @@ class TestProc(unittest.TestCase): with runner.get_tor_socket(): tor_pid = test.runner.get_runner().get_pid()
- for conn in proc.get_connections(tor_pid): + for conn in proc.connections(tor_pid): if ('127.0.0.1', test.runner.CONTROL_PORT) == conn[:2]: return
diff --git a/test/unit/util/proc.py b/test/unit/util/proc.py index 1786a99..c8defd7 100644 --- a/test/unit/util/proc.py +++ b/test/unit/util/proc.py @@ -17,45 +17,45 @@ except ImportError:
class TestProc(unittest.TestCase): @patch('stem.util.proc._get_line') - def test_get_system_start_time(self, get_line_mock): + def test_system_start_time(self, get_line_mock): """ - Tests the get_system_start_time function. + Tests the system_start_time function. """
get_line_mock.side_effect = lambda *params: { ('/proc/stat', 'btime', 'system start time'): 'btime 1001001', }[params]
- self.assertEquals(1001001, proc.get_system_start_time()) + self.assertEquals(1001001, proc.system_start_time())
@patch('stem.util.proc._get_line') - def test_get_physical_memory(self, get_line_mock): + def test_physical_memory(self, get_line_mock): """ - Tests the get_physical_memory function. + Tests the physical_memory function. """
get_line_mock.side_effect = lambda *params: { ('/proc/meminfo', 'MemTotal:', 'system physical memory'): 'MemTotal: 12345 kB', }[params]
- self.assertEquals((12345 * 1024), proc.get_physical_memory()) + self.assertEquals((12345 * 1024), proc.physical_memory())
@patch('os.readlink') - def test_get_cwd(self, readlink_mock): + def test_cwd(self, readlink_mock): """ - Tests the get_cwd function with a given pid. + Tests the cwd function with a given pid. """
readlink_mock.side_effect = lambda param: { '/proc/24019/cwd': '/home/directory/TEST' }[param]
- self.assertEquals('/home/directory/TEST', proc.get_cwd(24019)) + self.assertEquals('/home/directory/TEST', proc.cwd(24019))
@patch('stem.util.proc._get_line') - def test_get_uid(self, get_line_mock): + def test_uid(self, get_line_mock): """ - Tests the get_uid function with a given pid. + Tests the uid function with a given pid. """
for test_value in [(24019, 11111), (0, 22222)]: @@ -65,12 +65,12 @@ class TestProc(unittest.TestCase): ('/proc/%s/status' % pid, 'Uid:', 'uid'): 'Uid: %s' % uid, }[params]
- self.assertEquals(uid, proc.get_uid(pid)) + self.assertEquals(uid, proc.uid(pid))
@patch('stem.util.proc._get_lines') - def test_get_memory_usage(self, get_lines_mock): + def test_memory_usage(self, get_lines_mock): """ - Tests the get_memory_usage function with a given pid. + Tests the memory_usage function with a given pid. """
get_lines_mock.side_effect = lambda *params: { @@ -78,14 +78,14 @@ class TestProc(unittest.TestCase): {'VmRSS:': 'VmRSS: 100 kB', 'VmSize:': 'VmSize: 1800 kB'} }[params]
- self.assertEqual((0, 0), proc.get_memory_usage(0)) - self.assertEqual((100 * 1024, 1800 * 1024), proc.get_memory_usage(1111)) + self.assertEqual((0, 0), proc.memory_usage(0)) + self.assertEqual((100 * 1024, 1800 * 1024), proc.memory_usage(1111))
@patch('stem.util.proc._get_line') - @patch('stem.util.proc.get_system_start_time', Mock(return_value = 10)) - def test_get_stats(self, get_line_mock): + @patch('stem.util.proc.system_start_time', Mock(return_value = 10)) + def test_stats(self, get_line_mock): """ - Tests get_stats() with all combinations of stat_type arguments. + Tests stats() with all combinations of stat_type arguments. """
# list of all combinations of args with respective return values @@ -105,7 +105,7 @@ class TestProc(unittest.TestCase): (stat_path, '24062', 'process '): stat }[params]
- self.assertEquals((), proc.get_stats(24062)) + self.assertEquals((), proc.stats(24062))
for stats in stat_combinations: # the stats variable is... @@ -120,7 +120,7 @@ class TestProc(unittest.TestCase): (stat_path, '24062', 'process %s' % ', '.join(args)): stat }[params]
- self.assertEquals(response, proc.get_stats(24062, *args)) + self.assertEquals(response, proc.stats(24062, *args))
# tests the case where pid = 0
@@ -141,18 +141,18 @@ class TestProc(unittest.TestCase): ('/proc/0/stat', '0', 'process %s' % ', '.join(args)): stat }[params]
- self.assertEquals(response, proc.get_stats(0, *args)) + self.assertEquals(response, proc.stats(0, *args))
@patch('os.listdir') - def test_get_file_descriptors_used(self, listdir_mock): + def test_file_descriptors_used(self, listdir_mock): """ - Tests the get_file_descriptors_used function. + Tests the file_descriptors_used function. """
# check that we reject bad pids
for arg in (None, -100, 'hello',): - self.assertRaises(IOError, proc.get_file_descriptors_used, arg) + self.assertRaises(IOError, proc.file_descriptors_used, arg)
# when proc directory doesn't exist
@@ -160,7 +160,7 @@ class TestProc(unittest.TestCase): listdir_mock.side_effect = OSError(error_msg)
try: - proc.get_file_descriptors_used(2118) + proc.file_descriptors_used(2118) self.fail("We should raise when listdir() fails") except IOError as exc: expected = "Unable to check number of file descriptors used: %s" % error_msg @@ -171,15 +171,15 @@ class TestProc(unittest.TestCase): listdir_mock.return_value = ['0', '1', '2', '3', '4', '5'] listdir_mock.side_effect = None
- self.assertEqual(6, proc.get_file_descriptors_used(2118)) - self.assertEqual(6, proc.get_file_descriptors_used('2118')) + self.assertEqual(6, proc.file_descriptors_used(2118)) + self.assertEqual(6, proc.file_descriptors_used('2118'))
@patch('os.listdir') @patch('os.readlink') @patch('stem.util.proc.open', create = True) - def test_get_connections(self, open_mock, readlink_mock, listdir_mock): + def test_connections(self, open_mock, readlink_mock, listdir_mock): """ - Tests the get_connections function. + Tests the connections function. """
pid = 1111 @@ -204,11 +204,11 @@ class TestProc(unittest.TestCase): }[param]
# tests the edge case of pid = 0 - self.assertEquals([], proc.get_connections(0)) + self.assertEquals([], proc.connections(0))
expected_results = [ ('17.17.17.17', 4369, '34.34.34.34', 8738, 'tcp'), ('187.187.187.187', 48059, '204.204.204.204', 52428, 'udp'), ]
- self.assertEquals(expected_results, proc.get_connections(pid)) + self.assertEquals(expected_results, proc.connections(pid))
tor-commits@lists.torproject.org