From ea5d31b530d48c34a7e674560e428cce477d81f2 Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Mon, 25 Jul 2016 12:19:19 -0500 Subject: [PATCH 1/5] OCD: alphabetize and group imports I'm not the only one: https://google.github.io/styleguide/pyguide.html?showone=Imports_formatting#Imports_formatting --- lbrynet/lbrynet_daemon/LBRYDaemon.py | 29 +++++++++++++--------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index 8811211aa..fceacec15 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -1,19 +1,23 @@ -import string +import binascii +from datetime import datetime +from decimal import Decimal import locale +import logging.handlers import mimetypes import os +import platform +import random import re +import socket +import string import subprocess import sys -import random -import simplejson as json -import binascii -import logging.handlers -import requests -import base58 -import platform -import socket +from urllib2 import urlopen +from appdirs import user_data_dir +import base58 +import requests +import simplejson as json from twisted.web import server from twisted.internet import defer, threads, error, reactor from twisted.internet.task import LoopingCall @@ -21,11 +25,6 @@ from txjsonrpc import jsonrpclib from txjsonrpc.web import jsonrpc from txjsonrpc.web.jsonrpc import Handler -from datetime import datetime -from decimal import Decimal -from appdirs import user_data_dir -from urllib2 import urlopen - from lbrynet import __version__ as lbrynet_version from lbryum.version import LBRYUM_VERSION as lbryum_version from lbrynet.core.PaymentRateManager import PaymentRateManager @@ -53,8 +52,6 @@ from lbrynet.lbryfilemanager.LBRYFileManager import LBRYFileManager from lbrynet.lbryfile.LBRYFileMetadataManager import DBLBRYFileMetadataManager, TempLBRYFileMetadataManager # from lbryum import LOG_PATH as lbryum_log -log = logging.getLogger(__name__) - if sys.platform != "darwin": log_dir = os.path.join(os.path.expanduser("~"), ".lbrynet") From d0bc383a79fd77f1f4a402dde0a2566329b09716 Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Mon, 25 Jul 2016 12:25:10 -0500 Subject: [PATCH 2/5] bug fix: version comparison is not lexographic. Use distutils.version to compare version strings. --- lbrynet/lbrynet_daemon/LBRYDaemon.py | 3 ++- tests/lbrynet/lbrynet_daemon/test_LBRYDaemon.py | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index fceacec15..bf5cfd317 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -1,6 +1,7 @@ import binascii from datetime import datetime from decimal import Decimal +import distutils.version import locale import logging.handlers import mimetypes @@ -2314,4 +2315,4 @@ def get_version_from_tag(tag): def compare_versions(a, b): """Returns True if version a is more recent than version b""" - return a > b + return distutils.version.StrictVersion(a) > distutils.version.StrictVersion(b) diff --git a/tests/lbrynet/lbrynet_daemon/test_LBRYDaemon.py b/tests/lbrynet/lbrynet_daemon/test_LBRYDaemon.py index 104e06c01..a0d1c346f 100644 --- a/tests/lbrynet/lbrynet_daemon/test_LBRYDaemon.py +++ b/tests/lbrynet/lbrynet_daemon/test_LBRYDaemon.py @@ -36,3 +36,6 @@ class MiscTests(unittest.TestCase): def test_error_is_thrown_when_version_cant_be_parsed(self): with self.assertRaises(Exception): LBRYDaemon.get_version_from_tag('garbage') + + def test_compare_versions_isnot_lexographic(self): + self.assertTrue(LBRYDaemon.compare_versions('0.3.10', '0.3.6')) From 1eae3fe5dc5e330e2751db24d3df13e09163c1ce Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Mon, 25 Jul 2016 14:53:07 -0500 Subject: [PATCH 3/5] pylint doesn't like distutils --- packaging/travis/install_dependencies_and_run_tests.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packaging/travis/install_dependencies_and_run_tests.sh b/packaging/travis/install_dependencies_and_run_tests.sh index 6014087e1..422e74229 100755 --- a/packaging/travis/install_dependencies_and_run_tests.sh +++ b/packaging/travis/install_dependencies_and_run_tests.sh @@ -42,5 +42,6 @@ pip install mock pylint trial tests # TODO: submit coverage report to coveralls +# Ignoring distutils because: https://github.com/PyCQA/pylint/issues/73 # TODO: as code quality improves, make pylint be more strict -pylint -E --disable=inherit-non-class --disable=no-member lbrynet +pylint -E --disable=inherit-non-class --disable=no-member --ignored-modules=distutils lbrynet From 3814912adf264cd714d07ca3083bb450a8b88550 Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Mon, 25 Jul 2016 16:09:13 -0500 Subject: [PATCH 4/5] Move to core.utils and expand to check lbryum too --- lbrynet/core/utils.py | 15 +++++++++++-- lbrynet/lbrynet_daemon/LBRYDaemon.py | 22 ++++++++++--------- .../lbrynet/lbrynet_daemon/test_LBRYDaemon.py | 3 --- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/lbrynet/core/utils.py b/lbrynet/core/utils.py index 89b57fb0c..34ea99ba3 100644 --- a/lbrynet/core/utils.py +++ b/lbrynet/core/utils.py @@ -1,6 +1,9 @@ -from lbrynet.core.cryptoutils import get_lbry_hash_obj +import distutils.version import random +from lbrynet.core.cryptoutils import get_lbry_hash_obj + + blobhash_length = get_lbry_hash_obj().digest_size * 2 # digest_size is in bytes, and blob hashes are hex encoded @@ -25,4 +28,12 @@ def is_valid_blobhash(blobhash): for l in blobhash: if l not in "0123456789abcdef": return False - return True \ No newline at end of file + return True + + +def version_is_greater_than(a, b): + """Returns True if version a is more recent than version b""" + try: + return distutils.version.StrictVersion(a) > distutils.version.StrictVersion(b) + except ValueError: + return distutils.version.LooseVersion(a) > distutils.version.LooseVersion(b) diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index bf5cfd317..b5d3c81d7 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -39,6 +39,7 @@ from lbrynet.lbryfile.client.LBRYFileOptions import add_lbry_file_to_sd_identifi from lbrynet.lbrynet_daemon.LBRYUIManager import LBRYUIManager from lbrynet.lbrynet_daemon.LBRYDownloader import GetStream from lbrynet.lbrynet_daemon.LBRYPublisher import Publisher +from lbrynet.core import utils from lbrynet.core.utils import generate_id from lbrynet.lbrynet_console.LBRYSettings import LBRYSettings from lbrynet.conf import MIN_BLOB_DATA_PAYMENT_RATE, DEFAULT_MAX_SEARCH_RESULTS, KNOWN_DHT_NODES, DEFAULT_MAX_KEY_FEE, \ @@ -573,8 +574,11 @@ class LBRYDaemon(jsonrpc.JSONRPC): version = next(line.split("=")[1].split("#")[0].replace(" ", "") for line in r if "LBRYUM_VERSION" in line) version = version.replace("'", "") - log.info("remote lbryum " + str(version) + " > local lbryum " + str(lbryum_version) + " = " + str( - version > lbryum_version)) + log.info( + "remote lbryum %s > local lbryum %s = %s", + version, lbryum_version, + utils.version_is_greater_than(version, lbryum_version) + ) self.git_lbryum_version = version return defer.succeed(None) except: @@ -587,7 +591,8 @@ class LBRYDaemon(jsonrpc.JSONRPC): version = get_lbrynet_version_from_github() log.info( "remote lbrynet %s > local lbrynet %s = %s", - version, lbrynet_version, compare_versions(version, lbrynet_version) + version, lbrynet_version, + utils.version_is_greater_than(version, lbrynet_version) ) self.git_lbrynet_version = version return defer.succeed(None) @@ -1491,8 +1496,10 @@ class LBRYDaemon(jsonrpc.JSONRPC): 'ui_version': self.ui_version, 'remote_lbrynet': self.git_lbrynet_version, 'remote_lbryum': self.git_lbryum_version, - 'lbrynet_update_available': lbrynet_version < self.git_lbrynet_version, - 'lbryum_update_available': lbryum_version < self.git_lbryum_version + 'lbrynet_update_available': utils.version_is_greater_than( + self.git_lbrynet_version, lbrynet_version), + 'lbryum_update_available': utils.version_is_greater_than( + self.git_lbryum_version, lbryum_version) } log.info("Get version info: " + json.dumps(msg)) @@ -2311,8 +2318,3 @@ def get_version_from_tag(tag): return match.group(1) else: raise Exception('Failed to parse version from tag {}'.format(tag)) - - -def compare_versions(a, b): - """Returns True if version a is more recent than version b""" - return distutils.version.StrictVersion(a) > distutils.version.StrictVersion(b) diff --git a/tests/lbrynet/lbrynet_daemon/test_LBRYDaemon.py b/tests/lbrynet/lbrynet_daemon/test_LBRYDaemon.py index a0d1c346f..104e06c01 100644 --- a/tests/lbrynet/lbrynet_daemon/test_LBRYDaemon.py +++ b/tests/lbrynet/lbrynet_daemon/test_LBRYDaemon.py @@ -36,6 +36,3 @@ class MiscTests(unittest.TestCase): def test_error_is_thrown_when_version_cant_be_parsed(self): with self.assertRaises(Exception): LBRYDaemon.get_version_from_tag('garbage') - - def test_compare_versions_isnot_lexographic(self): - self.assertTrue(LBRYDaemon.compare_versions('0.3.10', '0.3.6')) From 927b9afe7cff22ca8246ea66b6eb436150b47359 Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Tue, 26 Jul 2016 09:34:00 -0500 Subject: [PATCH 5/5] forgot to add the test file --- .gitignore | 3 +++ tests/lbrynet/core/test_utils.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 tests/lbrynet/core/test_utils.py diff --git a/.gitignore b/.gitignore index 1bd0eea94..249826fac 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,6 @@ lbrynet.egg-info/PKG-INFO *.decTest .coverage + +# temporary files from the twisted.trial test runner +_trial_temp/ diff --git a/tests/lbrynet/core/test_utils.py b/tests/lbrynet/core/test_utils.py new file mode 100644 index 000000000..9fc14f93a --- /dev/null +++ b/tests/lbrynet/core/test_utils.py @@ -0,0 +1,19 @@ +from lbrynet.core import utils + +from twisted.trial import unittest + + +class CompareVersionTest(unittest.TestCase): + def test_compare_versions_isnot_lexographic(self): + self.assertTrue(utils.version_is_greater_than('0.3.10', '0.3.6')) + + def test_same_versions_return_false(self): + self.assertFalse(utils.version_is_greater_than('1.3.9', '1.3.9')) + + def test_same_release_is_greater_then_beta(self): + self.assertTrue(utils.version_is_greater_than('1.3.9', '1.3.9b1')) + + def test_version_can_have_four_parts(self): + self.assertTrue(utils.version_is_greater_than('1.3.9.1', '1.3.9')) + +