commit 8737fa984a52f216e514747d0562e38b695276dd Author: Damian Johnson atagar@torproject.org Date: Fri Sep 8 09:39:44 2017 -0700
Change 'man command has --encoding arg' check to a constant
We might want to make this a dynamic check later but if we do it'll be done differently. --- stem/manual.py | 3 ++- stem/util/system.py | 42 ++++++++++++------------------------------ test/integ/manual.py | 2 +- test/unit/manual.py | 11 +++++------ 4 files changed, 20 insertions(+), 38 deletions(-)
diff --git a/stem/manual.py b/stem/manual.py index ede02faf..e436c866 100644 --- a/stem/manual.py +++ b/stem/manual.py @@ -84,6 +84,7 @@ Category = stem.util.enum.Enum('GENERAL', 'CLIENT', 'RELAY', 'DIRECTORY', 'AUTHO GITWEB_MANUAL_URL = 'https://gitweb.torproject.org/tor.git/plain/doc/tor.1.txt' CACHE_PATH = os.path.join(os.path.dirname(__file__), 'cached_tor_manual.sqlite') DATABASE = None # cache database connections +HAS_ENCODING_ARG = stem.util.system.is_mac() or stem.util.system.is_bsd() or stem.util.system.is_slackware()
SCHEMA_VERSION = 1 # version of our scheme, bump this if you change the following SCHEMA = ( @@ -485,7 +486,7 @@ class Manual(object): :raises: **IOError** if unable to retrieve the manual """
- man_cmd = 'man %s -P cat %s' % ('' if (stem.util.system.is_mac() or stem.util.system.is_bsd() or stem.util.system.is_slackware()) else '--encoding=ascii', man_path) + man_cmd = 'man %s -P cat %s' % ('' if HAS_ENCODING_ARG else '--encoding=ascii', man_path)
try: man_output = stem.util.system.call(man_cmd, env = {'MANWIDTH': '10000000'}) diff --git a/stem/util/system.py b/stem/util/system.py index 53c06ac8..3a40e13d 100644 --- a/stem/util/system.py +++ b/stem/util/system.py @@ -21,10 +21,8 @@ best-effort, providing **None** if the lookup fails. is_windows - checks if we're running on windows is_mac - checks if we're running on a mac is_gentoo - checks if we're running on gentoo - is_bsd - checks if we're running on the bsd family of operating systems is_slackware - checks if we're running on slackware - - has_encoding_man - checks if the system's man command has --encoding=ascii available + is_bsd - checks if we're running on the bsd family of operating systems
is_available - determines if a command is available on this system is_running - determines if a given process is running @@ -313,18 +311,6 @@ class DaemonTask(object): conn.close()
-def has_encoding_man(): - """ - Checks if --encoding=ascii is available for man - """ - retval = True - if is_available('man'): - result = call('man --encoding=ascii man', [], error_return=True) - if 'unrecognized option' in result: - retval = False - return retval - - def is_windows(): """ Checks if we are running on Windows. @@ -355,25 +341,25 @@ def is_gentoo(): return os.path.exists('/etc/gentoo-release')
-def is_bsd(): +def is_slackware(): """ - Checks if we are within the BSD family of operating systems. This currently - recognizes Macs, FreeBSD, and OpenBSD but may be expanded later. + Checks if we are running on a Slackware system.
- :returns: **bool** to indicate if we're on a BSD OS + :returns: **bool** to indicate if we're on a Slackware system """
- return platform.system() in ('Darwin', 'FreeBSD', 'OpenBSD') + return os.path.exists('/etc/slackware-version')
-def is_slackware(): +def is_bsd(): """ - Checks if we are running on a Slackware system. + Checks if we are within the BSD family of operating systems. This currently + recognizes Macs, FreeBSD, and OpenBSD but may be expanded later.
- :returns: **bool** to indicate if we're on a Slackware system + :returns: **bool** to indicate if we're on a BSD OS """
- return os.path.exists('/etc/slackware-version') + return platform.system() in ('Darwin', 'FreeBSD', 'OpenBSD')
def is_available(command, cached=True): @@ -1238,8 +1224,7 @@ def files_with_suffix(base_path, suffix): yield os.path.join(root, filename)
-def call(command, default = UNDEFINED, ignore_exit_status = False, timeout = None, - cwd = None, env = None, error_return = False): +def call(command, default = UNDEFINED, ignore_exit_status = False, timeout = None, cwd = None, env = None): """ call(command, default = UNDEFINED, ignore_exit_status = False)
@@ -1311,10 +1296,7 @@ def call(command, default = UNDEFINED, ignore_exit_status = False, timeout = Non exit_status = process.poll()
if not ignore_exit_status and exit_status != 0: - if error_return: - return stderr - else: - OSError('%s returned exit status %i' % (command, exit_status)) + raise OSError('%s returned exit status %i' % (command, exit_status))
if stdout: return stdout.decode('utf-8', 'replace').splitlines() diff --git a/test/integ/manual.py b/test/integ/manual.py index 85de28c9..4c000b98 100644 --- a/test/integ/manual.py +++ b/test/integ/manual.py @@ -107,7 +107,7 @@ class TestManual(unittest.TestCase): stem.manual.download_man_page(file_handle = tmp) self.man_path = tmp.name
- man_cmd = 'man %s -P cat %s' % ('' if not stem.util.system.has_encoding_man() else '--encoding=ascii', self.man_path) + man_cmd = 'man %s -P cat %s' % ('' if not stem.manual.HAS_ENCODING_ARG else '--encoding=ascii', self.man_path) self.man_content = stem.util.system.call(man_cmd, env = {'MANWIDTH': '10000000'}) except Exception as exc: self.download_error = 'Unable to download the man page: %s' % exc diff --git a/test/unit/manual.py b/test/unit/manual.py index 9850e827..6640baf2 100644 --- a/test/unit/manual.py +++ b/test/unit/manual.py @@ -153,7 +153,7 @@ class TestManual(unittest.TestCase): expand our example (or add another). """
- if not stem.util.system.has_encoding_man(): + if not stem.manual.HAS_ENCODING_ARG: self.skipTest('(man lacks --encoding arg on OSX, BSD, and Slackware #18660)') return
@@ -174,7 +174,7 @@ class TestManual(unittest.TestCase): options. Unlike most other tests this doesn't require network access. """
- if not stem.util.system.has_encoding_man(): + if not stem.manual.HAS_ENCODING_ARG: self.skipTest('(man lacks --encoding arg on OSX and BSD and Slackware, #18660)') return
@@ -202,7 +202,7 @@ class TestManual(unittest.TestCase): Check that we can save and reload manuals as a config. """
- if not stem.util.system.has_encoding_man(): + if not stem.manual.HAS_ENCODING_ARG: self.skipTest('(man lacks --encoding arg on OSX, BSD and Slackware, #18660)') return
@@ -219,7 +219,7 @@ class TestManual(unittest.TestCase): Check that we can save and reload manuals as sqlite. """
- if not stem.util.system.has_encoding_man(): + if not stem.manual.HAS_ENCODING_ARG: self.skipTest('(man lacks --encoding arg on OSX, BSD, and Slackware #18660)') return
@@ -297,9 +297,8 @@ class TestManual(unittest.TestCase): self.assertEqual(b'a2x output', output.getvalue()) call_mock.assert_called_once_with('a2x -f manpage /no/such/path/tor.1.txt')
- @patch('stem.util.system.has_encoding_man', Mock(return_value = True)) - @patch('stem.util.system.is_bsd', Mock(return_value = False)) @patch('stem.util.system.is_mac', Mock(return_value = False)) + @patch('stem.util.system.is_bsd', Mock(return_value = False)) @patch('stem.util.system.is_slackware', Mock(return_value = False)) @patch('stem.util.system.call', Mock(side_effect = OSError('man --encoding=ascii -P cat tor returned exit status 16'))) def test_from_man_when_manual_is_unavailable(self):