Function to compare versions (branch)
authorFrederic Massart <fred@moodle.com>
Wed, 19 Sep 2012 08:15:50 +0000 (16:15 +0800)
committerFrederic Massart <fred@moodle.com>
Wed, 19 Sep 2012 08:59:19 +0000 (16:59 +0800)
lib/moodle.py
moodle-purge.py
moodle-update.py
moodle-upgrade.py

index 1b038bc..39383c4 100644 (file)
@@ -69,6 +69,30 @@ class Moodle(object):
         except:
             raise Exception('Error while writing to config file')
 
+    def branch_compare(self, branch, compare = '>='):
+        """Compare the branch of the current instance with the one passed"""
+        try:
+            branch = int(branch)
+        except:
+            raise Exception('Could not convert branch to int, got %s' % branch)
+        b = self.get('branch')
+        if b == None:
+            raise Exception('Error while reading the branch')
+        elif b == 'master':
+            b = C('masterBranch')
+        b = int(b)
+        if compare == '>=':
+            return b >= branch
+        elif compare == '>':
+            return b > branch
+        elif compare == '=' or compare == '==':
+            return b == branch
+        if compare == '<=':
+            return b <= branch
+        elif compare == '<':
+            return b < branch
+        return False
+
     def cli(self, cli, args = '', **kwargs):
         """Executes a command line tool script"""
         cli = os.path.join(self.get('path'), cli.lstrip('/'))
@@ -128,7 +152,7 @@ class Moodle(object):
     def initPHPUnit(self):
         """Initialise the PHP Unit environment"""
 
-        if self.get('branch') != 'master' and int(self.get('branch')) < 23:
+        if self.branch_compare(23, '<'):
             raise Exception('PHP Unit is only available from Moodle 2.3')
 
         # Set PHP Unit data root
index 08469c4..1a3ad7a 100755 (executable)
@@ -58,7 +58,7 @@ for M in Mlist:
         debug('Instance not installed. Skipping...')
         debug('')
         continue
-    elif M.get('stablebranch') != 'master' and int(M.get('branch')) < 22:
+    elif M.branch_compare('22', '<'):
         debug('Instance does not support cache purging. Skipping...')
         debug('')
         continue
index 4ee6833..49e90ec 100755 (executable)
@@ -63,7 +63,7 @@ for M in Mlist:
        debug('Updating %s...' % M.get('identifier'))
        try:
                M.update()
-               if M.isInstalled() and args.upgrade and (M.get('branch') == 'master' or int(M.get('branch')) >= 20):
+               if M.isInstalled() and args.upgrade and M.branch_compare(20):
                        M.upgrade()
        except Exception as e:
                debug('Error during the update of %s' % M.get('identifier'))
index c56aa99..18c3984 100755 (executable)
@@ -66,7 +66,7 @@ for M in Mlist:
                debug('')
                continue
 
-       if M.get('branch') != 'master' and int(M.get('branch')) < 20:
+       if M.branch_compare(20, '<'):
                debug('Skipping version < 2.0')
        else:
                if not M.upgrade():