Skip to content

Commit

Permalink
Handle bad version numbers gracefully
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jdreed committed Apr 11, 2014
1 parent dca9749 commit 4eb3ed8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions getcluster
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import struct
from optparse import OptionParser

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

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

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


Expand Down Expand Up @@ -73,6 +74,9 @@ def main():
if len(args) != 1:
parser.print_usage()
return 1
if version_re.search(args[0]) is None:
perror("Invalid version number: %s", args[0])
return 1

if options.deprecated:
perror("-f and -l are deprecated and will be ignored.")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from distutils.core import setup

setup(name='getcluster',
version='10.2.1',
version='10.2.2',
author='Debathena Project',
author_email='[email protected]',
scripts=['getcluster'],
Expand Down

0 comments on commit 4eb3ed8

Please sign in to comment.