From a5d74356dda2f34d9bebf3a3276f5f7d1869985a Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Mon, 23 Oct 2017 18:24:06 +0200 Subject: [PATCH] simplify version output --- mitmproxy/utils/__init__.py | 10 ---- mitmproxy/utils/debug.py | 73 +++++++++++++----------------- test/mitmproxy/utils/test_debug.py | 10 +++- 3 files changed, 39 insertions(+), 54 deletions(-) diff --git a/mitmproxy/utils/__init__.py b/mitmproxy/utils/__init__.py index 86649864b..e69de29bb 100644 --- a/mitmproxy/utils/__init__.py +++ b/mitmproxy/utils/__init__.py @@ -1,10 +0,0 @@ -import os -from contextlib import contextmanager - - -@contextmanager -def chdir(dir): - orig_dir = os.getcwd() - os.chdir(dir) - yield - os.chdir(orig_dir) diff --git a/mitmproxy/utils/debug.py b/mitmproxy/utils/debug.py index c2eee2b6c..de01b12cd 100644 --- a/mitmproxy/utils/debug.py +++ b/mitmproxy/utils/debug.py @@ -8,60 +8,49 @@ import traceback import subprocess from mitmproxy import version -from mitmproxy import utils from OpenSSL import SSL def dump_system_info(): - git_describe = 'release version' - with utils.chdir(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))): - try: - c = ['git', 'describe', '--tags', '--long'] - git_describe = subprocess.check_output(c, stderr=subprocess.STDOUT) - last_tag, tag_dist, commit = git_describe.decode().strip().rsplit("-", 2) + mitmproxy_version = version.VERSION + here = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) + try: + git_describe = subprocess.check_output( + ['git', 'describe', '--tags', '--long'], + stderr=subprocess.STDOUT, + cwd=here, + ) + except: + pass + else: + last_tag, tag_dist, commit = git_describe.decode().strip().rsplit("-", 2) - if last_tag.startswith('v'): - # remove the 'v' prefix - last_tag = last_tag[1:] - if commit.startswith('g'): - # remove the 'g' prefix added by recent git versions - commit = commit[1:] + commit = commit.lstrip("g") # remove the 'g' prefix added by recent git versions + tag_dist = int(tag_dist) - # build the same version specifier as used for snapshots by rtool - git_describe = "{version}dev{tag:04}-0x{commit}".format( - version=last_tag, - tag=int(tag_dist), - commit=commit, - ) - except: - pass + if tag_dist > 0: + tag_dist = "dev{:04}".format(tag_dist) + else: + tag_dist = "" - bin_indicator = "" # PyInstaller builds indicator, if using precompiled binary + mitmproxy_version += "{tag_dist} ({commit})".format( + tag_dist=tag_dist, + commit=commit, + ) + + # PyInstaller builds indicator, if using precompiled binary if getattr(sys, 'frozen', False): - bin_indicator = "Precompiled Binary" + bin_indicator = "binary" + else: + bin_indicator = "" data = [ - "Mitmproxy version: {} ({}) {}".format(version.VERSION, git_describe, bin_indicator), - "Python version: {}".format(platform.python_version()), - "Platform: {}".format(platform.platform()), - "SSL version: {}".format(SSL.SSLeay_version(SSL.SSLEAY_VERSION).decode()), + "Mitmproxy: {} {}".format(mitmproxy_version, bin_indicator), + "Python: {}".format(platform.python_version()), + "OpenSSL: {}".format(SSL.SSLeay_version(SSL.SSLEAY_VERSION).decode()), + "Platform: {}".format(platform.platform()), ] - d = platform.linux_distribution() - t = "Linux distro: %s %s %s" % d - if d[0]: # pragma: no cover - data.append(t) - - d = platform.mac_ver() - t = "Mac version: %s %s %s" % d - if d[0]: # pragma: no cover - data.append(t) - - d = platform.win32_ver() - t = "Windows version: %s %s %s %s" % d - if d[0]: # pragma: no cover - data.append(t) - return "\n".join(data) diff --git a/test/mitmproxy/utils/test_debug.py b/test/mitmproxy/utils/test_debug.py index 4371ef70f..a8e1054da 100644 --- a/test/mitmproxy/utils/test_debug.py +++ b/test/mitmproxy/utils/test_debug.py @@ -11,13 +11,19 @@ from mitmproxy.utils import debug def test_dump_system_info_precompiled(precompiled): sys.frozen = None with mock.patch.object(sys, 'frozen', precompiled): - assert ("Precompiled Binary" in debug.dump_system_info()) == precompiled + assert ("binary" in debug.dump_system_info()) == precompiled def test_dump_system_info_version(): + with mock.patch('subprocess.check_output') as m: + m.return_value = b"v2.0.0-0-cafecafe" + x = debug.dump_system_info() + assert 'dev' not in x + assert 'cafecafe' in x + with mock.patch('subprocess.check_output') as m: m.side_effect = subprocess.CalledProcessError(-1, 'git describe --tags --long') - assert 'release version' in debug.dump_system_info() + assert 'dev' not in debug.dump_system_info() def test_dump_info():