From b7e0fcba6dc50620528a2be3dd29cc01b2054761 Mon Sep 17 00:00:00 2001 From: Calin Culianu Date: Sat, 9 Feb 2019 15:08:45 +0200 Subject: [PATCH] [Lib] Refactored version related code into version.py util.normalize_version got moved into version.py. Cleaned up re's in version.py to compile on startup. --- lib/network.py | 9 ++++----- lib/util.py | 3 --- lib/version.py | 15 +++++++++++++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/network.py b/lib/network.py index f973c6d62ce8..f449281d9860 100644 --- a/lib/network.py +++ b/lib/network.py @@ -42,7 +42,7 @@ from .i18n import _ from .interface import Connection, Interface from . import blockchain -from .version import PACKAGE_VERSION, PROTOCOL_VERSION +from . import version DEFAULT_AUTO_CONNECT = True @@ -52,7 +52,6 @@ def parse_servers(result): """ parse servers list into dict format""" - from .version import PROTOCOL_VERSION servers = {} for item in result: host = item[1] @@ -79,7 +78,7 @@ def parse_servers(result): def filter_version(servers): def is_recent(version): try: - return util.normalize_version(version) >= util.normalize_version(PROTOCOL_VERSION) + return version.normalize_version(version) >= version.normalize_version(version.PROTOCOL_VERSION) except Exception as e: return False return {k: v for k, v in servers.items() if is_recent(v.get('version'))} @@ -122,7 +121,7 @@ def servers_to_hostmap(servers): m[protocol] = port if need_add: m['pruning'] = '-' # hmm. this info is missing, so give defaults just to make the map complete. - m['version'] = PROTOCOL_VERSION + m['version'] = version.PROTOCOL_VERSION ret[host] = m return ret @@ -814,7 +813,7 @@ def new_interface(self, server_key, socket): self.interfaces[server_key] = interface # server.version should be the first message - params = [PACKAGE_VERSION, PROTOCOL_VERSION] + params = [version.PACKAGE_VERSION, version.PROTOCOL_VERSION] self.queue_request('server.version', params, interface) # The interface will immediately respond with it's last known header. self.queue_request('blockchain.headers.subscribe', [], interface) diff --git a/lib/util.py b/lib/util.py index 20655016b776..3e170940303c 100644 --- a/lib/util.py +++ b/lib/util.py @@ -43,9 +43,6 @@ def inv_dict(d): base_units = {'BCH':8, 'mBCH':5, 'cash':2} fee_levels = [_('Within 25 blocks'), _('Within 10 blocks'), _('Within 5 blocks'), _('Within 2 blocks'), _('In the next block')] -def normalize_version(v): - return [int(x) for x in re.sub(r'(\.0+)*$','', v).split(".")] - class NotEnoughFunds(Exception): pass class ExcessiveFee(Exception): pass diff --git a/lib/version.py b/lib/version.py index 06e70ccac1b5..ccfaff606307 100644 --- a/lib/version.py +++ b/lib/version.py @@ -9,6 +9,11 @@ def seed_prefix(seed_type): assert seed_type == 'standard' return SEED_PREFIX +import re + +_RX_VERSION_PARSE = re.compile(r'(\d+)[.](\d+)[.]?(\d*)\s*(\S*)') +_RX_NORMALIZER = re.compile(r'(\.0+)*$') + def parse_package_version(pvstr): """ Parse a package version string e.g.: @@ -16,12 +21,18 @@ def parse_package_version(pvstr): '3.4.5_iOS' -> (3, 4, 5, '_iOS') '3.3.5' -> (3, 3, 5, '') '3.3' -> (3, 3, 0, '') + and.. perhaps unexpectedly: + '3.3.5.1_iOS' -> (3, 3, 5, '.1_iOS') .. so be sure not to have more than 3 version fields + 1 'extra' field! etc... """ - import re - m = re.search(r'(\d+)[.](\d+)[.]?(\d*)\s*(\S*)', pvstr) + m = _RX_VERSION_PARSE.search(pvstr) if not m: raise ValueError('Failed to parse package version for: ' + str(pvstr)) major, minor, rev, variant = int(m.group(1)), int(m.group(2)), m.group(3), m.group(4) rev = int(rev) if rev else 0 return major, minor, rev, variant + +def normalize_version(v): + """Used for PROTOCOL_VERSION normalization, e.g '1.4.0' -> (1,4) """ + return tuple(int(x) for x in _RX_NORMALIZER.sub('', v.strip()).split(".")) +