Cached repositories are now mirrored
authorFred <fmcell@gmail.com>
Mon, 4 Mar 2013 08:24:50 +0000 (16:24 +0800)
committerFred <fmcell@gmail.com>
Mon, 4 Mar 2013 08:24:50 +0000 (16:24 +0800)
config-dist.json
lib/commands/rebase.py
lib/commands/update.py
lib/commands/upgrade.py
lib/workplace.py

index e22240d..a6c42c5 100644 (file)
     // Path to Java
     "java": "/usr/bin/java",
 
-    // Experimental setting to use a local cache as your upstream remotes.
-    // Can be useful to prevent fetch from a slow remote.
-    "useCacheAsRemote": false,
-
     // Name of the symlink pointing to the data directory to create in the www directory
     // during an instance creation. If false, the symlink won't be created.
     "symlinkToData": false,
index b9a56fc..4ff3f65 100644 (file)
@@ -111,6 +111,10 @@ class RebaseCommand(Command):
         # Getting instances
         Mlist = self.Wp.resolveMultiple(names)
 
+        # Updating cache remotes
+        print 'Updating cached repositories'
+        self.Wp.updateCachedClones()
+
         # Loops over instances to rebase
         for M in Mlist:
             debug('Working on %s' % (M.get('identifier')))
index 4c174b3..f5c37ae 100644 (file)
@@ -63,22 +63,6 @@ class UpdateCommand(Command):
             }
         ),
         (
-            ['-c', '--update-cache'],
-            {
-                'action': 'store_true',
-                'dest': 'updatecache',
-                'help': 'update the cached remotes. Useful when useCacheAsRemote is enabled.'
-            }
-        ),
-        (
-            ['-p', '--proceed'],
-            {
-                'action': 'store_true',
-                'dest': 'process',
-                'help': 'do not exit the process after updating the cache'
-            }
-        ),
-        (
             ['names'],
             {
                 'default': None,
@@ -92,15 +76,6 @@ class UpdateCommand(Command):
 
     def run(self, args):
 
-        # Updating cache only
-        if args.updatecache:
-            debug('Updating cached remote')
-            self.Wp.updateCachedClones()
-            debug('Done.')
-            if not args.process:
-                return
-            debug('')
-
         # Updating instances
         names = args.names
         if args.all:
@@ -112,6 +87,10 @@ class UpdateCommand(Command):
         if len(Mlist) < 1:
             raise Exception('No instances to work on. Exiting...')
 
+        # Updating cache
+        print 'Updating cached repositories'
+        self.Wp.updateCachedClones(verbose=False)
+
         errors = []
 
         for M in Mlist:
index 1d25c99..b4082d0 100644 (file)
@@ -94,6 +94,11 @@ class UpgradeCommand(Command):
         if len(Mlist) < 1:
             raise Exception('No instances to work on. Exiting...')
 
+        # Updating cache if required
+        if args.update:
+            print 'Updating cached repositories'
+            self.Wp.updateCachedClones(verbose=False)
+
         errors = []
 
         for M in Mlist:
index 51478ff..dd88563 100644 (file)
@@ -26,7 +26,7 @@ import os
 import shutil
 from distutils.dir_util import copy_tree
 
-from tools import debug, process
+from tools import debug, process, stableBranch
 from config import Conf
 import db
 import git
@@ -57,26 +57,31 @@ class Workplace(object):
         self.wwwDir = wwwDir
         self.dataDir = dataDir
 
-    def checkCachedClones(self, stable = True, integration = True):
+    def checkCachedClones(self, stable=True, integration=True):
         """Clone the official repository in a local cache"""
         cacheStable = os.path.join(self.cache, 'moodle.git')
         cacheIntegration = os.path.join(self.cache, 'integration.git')
         if not os.path.isdir(cacheStable) and stable:
             debug('Cloning stable repository into cache...')
-            result = process('%s clone %s %s' % (C.get('git'), C.get('remotes.stable'), cacheStable))
-            result = process('%s fetch -a' % C.get('git'), cacheStable)
+            debug('That\'s going to take a while...')
+            process('%s clone --mirror %s %s' % (C.get('git'), C.get('remotes.stable'), cacheStable))
+        else:
+            # TODO Ensure mirror repo
+            pass
         if not os.path.isdir(cacheIntegration) and integration:
             debug('Cloning integration repository into cache...')
-            result = process('%s clone %s %s' % (C.get('git'), C.get('remotes.integration'), cacheIntegration))
-            result = process('%s fetch -a' % C.get('git'), cacheIntegration)
+            debug('Have a break, this operation is slow...')
+            process('%s clone --mirror %s %s' % (C.get('git'), C.get('remotes.integration'), cacheIntegration))
+        else:
+            # TODO Ensure mirror repo
+            pass
 
-    def create(self, name = None, version = 'master', integration = False, useCacheAsRemote = False):
-        """Creates a new instance of Moodle"""
+    def create(self, name=None, version='master', integration=False, useCacheAsRemote=False):
+        """Creates a new instance of Moodle.
+        The parameter useCacheAsRemote has been deprecated.
+        """
         if name == None:
-            if integration:
-                name = C.get('wording.prefixIntegration') + prefixVersion
-            else:
-                name = C.get('wording.prefixStable') + prefixVersion
+            name = self.generateInstanceName(version, integration=integration)
 
         installDir = os.path.join(self.path, name)
         wwwDir = os.path.join(installDir, self.wwwDir)
@@ -89,29 +94,24 @@ class Workplace(object):
             raise Exception('Installation path exists: %s' % installDir)
 
         self.checkCachedClones(not integration, integration)
+        self.updateCachedClones(stable=not integration, integration=integration, verbose=False)
         os.mkdir(installDir, 0755)
         os.mkdir(wwwDir, 0755)
         os.mkdir(dataDir, 0777)
 
         if integration:
             repository = os.path.join(self.cache, 'integration.git')
-            upstreamRepository = C.get('remotes.integration')
         else:
             repository = os.path.join(self.cache, 'moodle.git')
-            upstreamRepository = C.get('remotes.stable')
 
         # Clone the instances
         debug('Cloning repository...')
-        if useCacheAsRemote:
-            result = process('%s clone %s %s' % (C.get('git'), repository, wwwDir))
-            upstreamRepository = repository
-        else:
-            copy_tree(repository, wwwDir)
+        process('%s clone %s %s' % (C.get('git'), repository, wwwDir))
 
         # Symbolic link
         if os.path.islink(linkDir):
             os.remove(linkDir)
-        if os.path.isfile(linkDir) or os.path.isdir(linkDir): # No elif!
+        if os.path.isfile(linkDir) or os.path.isdir(linkDir):  # No elif!
             debug('Could not create symbolic link')
             debug('Please manually create: ln -s %s %s' (wwwDir, linkDir))
         else:
@@ -128,20 +128,17 @@ class Workplace(object):
 
         # Setting up the correct remote names
         repo.setRemote(C.get('myRemote'), C.get('remotes.mine'))
-        repo.setRemote(C.get('upstreamRemote'), upstreamRepository)
+        repo.setRemote(C.get('upstreamRemote'), repository)
 
         # Creating, fetch, pulling branches
-        result = repo.fetch(C.get('upstreamRemote'))
-        if version == 'master':
-            repo.checkout('master')
+        repo.fetch(C.get('upstreamRemote'))
+        branch = stableBranch(version)
+        track = '%s/%s' % (C.get('upstreamRemote'), branch)
+        if not repo.hasBranch(branch) and not repo.createBranch(branch, track):
+            debug('Could not create branch %s tracking %s' % (branch, track))
         else:
-            track = '%s/MOODLE_%s_STABLE' % (C.get('upstreamRemote'), version)
-            branch = 'MOODLE_%s_STABLE' % version
-            if not repo.hasBranch(branch) and not repo.createBranch(branch, track):
-                debug('Could not create branch %s tracking %s' % (branch, track))
-            else:
-                repo.checkout(branch)
-        repo.pull(remote = C.get('upstreamRemote'))
+            repo.checkout(branch)
+        repo.pull(remote=C.get('upstreamRemote'))
 
         M = self.get(name)
         return M
@@ -292,11 +289,10 @@ class Workplace(object):
                 debug('Could not find instance called %s' % name)
         return result
 
-    def updateCachedClones(self, integration = True, stable = True, verbose = True):
+    def updateCachedClones(self, integration=True, stable=True, verbose=True):
         """Update the cached clone of the repositories"""
 
         caches = []
-        remote = 'origin'
 
         if integration:
             caches.append(os.path.join(self.cache, 'integration.git'))
@@ -310,23 +306,8 @@ class Workplace(object):
             repo = git.Git(cache, C.get('git'))
 
             verbose and debug('Working on %s...' % cache)
-            verbose and debug('Fetching %s' % remote)
-            if not repo.fetch(remote):
-                raise Exception('Could not fetch %s in repository %s' % (remote, cache))
-
-            remotebranches = repo.remoteBranches(remote)
-            for hash, branch in remotebranches:
-                verbose and debug('Updating branch %s' % branch)
-                track = '%s/%s' % (remote, branch)
-                if not repo.hasBranch(branch) and not repo.createBranch(branch, track = track):
-                    raise Exception('Could not create branch %s tracking %s in repository %s' % (branch, track, cache))
-
-                if not repo.checkout(branch):
-                    raise Exception('Error while checking out branch %s in repository %s' % (branch, cache))
-
-                if not repo.reset(to = track, hard = True):
-                    raise Exception('Could not hard reset to %s in repository %s' % (branch, cache))
-
-            verbose and debug('')
+            verbose and debug('Fetching...')
+            if not repo.fetch():
+                raise Exception('Could not fetch in repository %s' % (cache))
 
         return True