[28319] in Source-Commits

home help back first fref pref prev next nref lref last post

getcluster commit: Handle bad version numbers gracefully

daemon@ATHENA.MIT.EDU (Jonathan D Reed)
Sun Jul 6 14:59:42 2014

Date: Sun, 6 Jul 2014 14:59:35 -0400
From: Jonathan D Reed <jdreed@MIT.EDU>
Message-Id: <201407061859.s66IxZ6u013780@drugstore.mit.edu>
To: source-commits@MIT.EDU

https://github.com/mit-athena/getcluster/commit/4eb3ed8ec38974c17cc23fc4b6e6c28302507772
commit 4eb3ed8ec38974c17cc23fc4b6e6c28302507772
Author: Jonathan Reed <jdreed@mit.edu>
Date:   Fri Apr 11 17:19:08 2014 -0400

    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.

 getcluster |    8 ++++++--
 setup.py   |    2 +-
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/getcluster b/getcluster
index f53dcd7..727ed67 100755
--- a/getcluster
+++ b/getcluster
@@ -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):
@@ -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)
 
 
@@ -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.")
diff --git a/setup.py b/setup.py
index d1538a2..b464d50 100755
--- a/setup.py
+++ b/setup.py
@@ -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='debathena@mit.edu',
       scripts=['getcluster'],

home help back first fref pref prev next nref lref last post