Skip to content

Commit 4eb3ed8

Browse files
committed
Handle bad version numbers gracefully
Pre-compile the regexp for a valid version number, and check args[0] against it before it fails later in vercmp(). Note that we could easily use .match() instead of .search(), since the regexp has a start-of-line anchor in it, but this is a bit more verbose.
1 parent dca9749 commit 4eb3ed8

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

getcluster

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import struct
2222
from optparse import OptionParser
2323

2424
cluster_re = re.compile('^\S+ \S+( \S+){0,2}$')
25+
version_re = re.compile('^(\d+)(?:\.(\d+)){0,1}')
2526
debugMode = os.getenv('DEBUG_GETCLUSTER', 'no') == 'yes'
2627

2728
def debug(msg, *args):
@@ -42,8 +43,8 @@ def cleanup(l):
4243
return filter(lambda x: cluster_re.match(x), map(lambda x: x.rstrip(), l))
4344

4445
def vercmp(v1, v2):
45-
v1l = map(int, map(lambda x: 0 if x is None else x, re.search('^(\d+)(?:\.(\d+)){0,1}',v1).groups()))
46-
v2l = map(int, map(lambda x: 0 if x is None else x, re.search('^(\d+)(?:\.(\d+)){0,1}',v2).groups()))
46+
v1l = map(int, map(lambda x: 0 if x is None else x, version_re.search(v1).groups()))
47+
v2l = map(int, map(lambda x: 0 if x is None else x, version_re.search(v2).groups()))
4748
return cmp(v1l, v2l)
4849

4950

@@ -73,6 +74,9 @@ def main():
7374
if len(args) != 1:
7475
parser.print_usage()
7576
return 1
77+
if version_re.search(args[0]) is None:
78+
perror("Invalid version number: %s", args[0])
79+
return 1
7680

7781
if options.deprecated:
7882
perror("-f and -l are deprecated and will be ignored.")

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from distutils.core import setup
44

55
setup(name='getcluster',
6-
version='10.2.1',
6+
version='10.2.2',
77
author='Debathena Project',
88
author_email='[email protected]',
99
scripts=['getcluster'],

0 commit comments

Comments
 (0)