From 0e5a8f158e8366b73febdbd16fdfa2bdfbdc8043 Mon Sep 17 00:00:00 2001 From: Jacob Beck Date: Fri, 24 Aug 2018 08:16:02 -0600 Subject: [PATCH 1/5] Remove unconditional calls to get_latest_version --- dbt/version.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dbt/version.py b/dbt/version.py index a50fe85d614..22725d45390 100644 --- a/dbt/version.py +++ b/dbt/version.py @@ -69,4 +69,3 @@ def get_version_information(): __version__ = '0.10.2' installed = get_installed_version() -latest = get_latest_version() From 045fccc8c0f57970955f15c6abe03a2953f2f112 Mon Sep 17 00:00:00 2001 From: Jacob Beck Date: Fri, 24 Aug 2018 08:42:39 -0600 Subject: [PATCH 2/5] remove a sneaky, hidden unconditional call to get_latest_version --- dbt/main.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dbt/main.py b/dbt/main.py index 5c592c8e18d..0e41b8021c0 100644 --- a/dbt/main.py +++ b/dbt/main.py @@ -72,6 +72,9 @@ def handle(args): def handle_and_check(args): parsed = parse_args(args) + if parsed.version: + print(dbt.version.get_version_information()) + return None, True # this needs to happen after args are parsed so we can determine the # correct profiles.yml file @@ -258,8 +261,7 @@ def parse_args(args): p.add_argument( '--version', - action='version', - version=dbt.version.get_version_information(), + action='store_true', help="Show version information") p.add_argument( From 99550700856b17e6a92879579256b30cf121a95a Mon Sep 17 00:00:00 2001 From: Jacob Beck Date: Mon, 27 Aug 2018 14:35:30 -0600 Subject: [PATCH 3/5] make version special the same way argparse does, but with lazy evaluation of the version value. --- dbt/main.py | 35 ++++++++++++++++++++++++++++++----- test/unit/test_version.py | 22 ++++++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/dbt/main.py b/dbt/main.py index 0e41b8021c0..159c54cb859 100644 --- a/dbt/main.py +++ b/dbt/main.py @@ -34,6 +34,34 @@ https://docs.getdbt.com/docs/configure-your-profile """ +class DBTVersion(argparse.Action): + """This is very very similar to the builtin argparse._Version action, + except it just calls dbt.version.get_version_information(). + """ + def __init__(self, + option_strings, + version=None, + dest=argparse.SUPPRESS, + default=argparse.SUPPRESS, + help="show program's version number and exit"): + super(DBTVersion, self).__init__( + option_strings=option_strings, + dest=dest, + default=default, + nargs=0, + help=help) + + def __call__(self, parser, namespace, values, option_string=None): + formatter = parser._get_formatter() + formatter.add_text(dbt.version.get_version_information()) + parser.exit(message=formatter.format_help()) + + +class DBTArgumentParser(argparse.ArgumentParser): + def __init__(self, *args, **kwargs): + super(DBTArgumentParser, self).__init__(*args, **kwargs) + self.register('action', 'dbtversion', DBTVersion) + def main(args=None): if args is None: @@ -72,9 +100,6 @@ def handle(args): def handle_and_check(args): parsed = parse_args(args) - if parsed.version: - print(dbt.version.get_version_information()) - return None, True # this needs to happen after args are parsed so we can determine the # correct profiles.yml file @@ -255,13 +280,13 @@ def invoke_dbt(parsed): def parse_args(args): - p = argparse.ArgumentParser( + p = DBTArgumentParser( prog='dbt: data build tool', formatter_class=argparse.RawTextHelpFormatter) p.add_argument( '--version', - action='store_true', + action='dbtversion', help="Show version information") p.add_argument( diff --git a/test/unit/test_version.py b/test/unit/test_version.py index 9626b8b78eb..4b63844edaf 100644 --- a/test/unit/test_version.py +++ b/test/unit/test_version.py @@ -1,7 +1,9 @@ from mock import patch, MagicMock import unittest +import dbt.main import dbt.version +import sys class VersionTest(unittest.TestCase): @@ -87,3 +89,23 @@ def test_installed_version_lower(self): assert installed_version < latest_version self.assertMultiLineEqual(version_information, expected_version_information) + + # suppress having version info printed to the screen during tests. + @patch('sys.stderr') + def test_dbt_version_flag(self, stderr): + dbt.version.get_remote_version_file_contents = MagicMock( + return_value=""" + [bumpversion] + current_version = 0.10.1 + commit = True + tag = True + + [bumpversion:file:setup.py] + + [bumpversion:file:dbt/version.py] + """) + + with self.assertRaises(SystemExit) as exc: + dbt.main.handle_and_check(['--version']) + self.assertEqual(exc.exception.code, 0) + From 4ab0ec96a80f926a4dfa483a480a21ca4cd6cc19 Mon Sep 17 00:00:00 2001 From: Jacob Beck Date: Mon, 27 Aug 2018 14:46:07 -0600 Subject: [PATCH 4/5] make the case of an inaccessible raw.githubusercontent.com a bit more user-friendly --- dbt/version.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/dbt/version.py b/dbt/version.py index 3f1474387d0..59c64a2624d 100644 --- a/dbt/version.py +++ b/dbt/version.py @@ -35,6 +35,8 @@ def get_remote_version_file_contents(url=REMOTE_VERSION_FILE): def get_latest_version(): contents = get_remote_version_file_contents() + if contents == '': + return None version_string = get_version_string_from_text(contents) return dbt.semver.VersionSpecifier.from_version_string(version_string) @@ -48,11 +50,19 @@ def get_version_information(): latest = get_latest_version() installed_s = installed.to_version_string(skip_matcher=True) - latest_s = latest.to_version_string(skip_matcher=True) + if latest is None: + latest_s = 'unknown' + else: + latest_s = latest.to_version_string(skip_matcher=True) version_msg = ("installed version: {}\n" " latest version: {}\n\n".format(installed_s, latest_s)) + if latest is None: + return ("{}The latest version of dbt could not be determined!\n" + "Make sure that the following URL is accessible:\n{}" + .format(version_msg, REMOTE_VERSION_FILE)) + if installed == latest: return "{}Up to date!".format(version_msg) From d946ac2c99d404a62e597902fb7df8e065baaaa8 Mon Sep 17 00:00:00 2001 From: Jacob Beck Date: Tue, 28 Aug 2018 08:12:56 -0600 Subject: [PATCH 5/5] pep8 --- dbt/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dbt/main.py b/dbt/main.py index 159c54cb859..68a0efef88c 100644 --- a/dbt/main.py +++ b/dbt/main.py @@ -34,6 +34,7 @@ https://docs.getdbt.com/docs/configure-your-profile """ + class DBTVersion(argparse.Action): """This is very very similar to the builtin argparse._Version action, except it just calls dbt.version.get_version_information().