Check command made useful
authorFred <fmcell@gmail.com>
Mon, 4 Mar 2013 09:24:27 +0000 (17:24 +0800)
committerFred <fmcell@gmail.com>
Mon, 4 Mar 2013 09:24:27 +0000 (17:24 +0800)
lib/commands/__init__.py
lib/commands/check.py [new file with mode: 0644]
lib/git.py

index 90c9d8b..5ca2d87 100644 (file)
@@ -33,6 +33,7 @@ commandsList = [
     'backport',
     'backup',
     'behat',
+    'check',
     'config',
     'create',
     'fix',
diff --git a/lib/commands/check.py b/lib/commands/check.py
new file mode 100644 (file)
index 0000000..d26037e
--- /dev/null
@@ -0,0 +1,112 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+Moodle Development Kit
+
+Copyright (c) 2013 Frédéric Massart - FMCorz.net
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+http://github.com/FMCorz/mdk
+"""
+
+import os
+import shutil
+from lib import git
+from lib.command import Command
+from lib.tools import debug
+
+
+class CheckCommand(Command):
+
+    _arguments = [
+        (
+            ['--fix'],
+            {
+                'action': 'store_true',
+                'help': 'Automatically fix the identified problems'
+            }
+        )
+    ]
+    _description = 'Perform several checks on your current installation'
+
+    def run(self, args):
+
+        # Check directories
+        self.directories(args)
+
+        # Check the cached remotes
+        self.cachedRepositories(args)
+
+    def cachedRepositories(self, args):
+        """Ensure that the cached repositories are valid"""
+
+        debug('Checking cached repositories')
+        cache = os.path.abspath(os.path.realpath(os.path.expanduser(self.C.get('dirs.mdk'))))
+
+        dirs = [
+            {
+                'dir': os.path.join(cache, 'moodle.git'),
+                'url': self.C.get('remotes.stable')
+            },
+            {
+                'dir': os.path.join(cache, 'integration.git'),
+                'url': self.C.get('remotes.integration')
+            },
+        ]
+
+        for d in dirs:
+            directory = d['dir']
+            name = os.path.split(directory)[1]
+
+            if os.path.isdir(directory):
+                if os.path.isdir(os.path.join(directory, '.git')):
+                    debug('  %s is not a bare repository' % name)
+                    if args.fix:
+                        debug('    Renaming %s/.git directory to %s' % (directory, directory))
+                        os.rename(directory, directory + '.tmp')
+                        os.rename(os.path.join(directory + '.tmp', '.git'), directory)
+                        shutil.rmtree(directory + '.tmp')
+
+                repo = git.Git(directory, self.C.get('git'))
+                if repo.getConfig('core.bare') != 'true':
+                    debug('  %s core.bare is not set to true' % name)
+                    if args.fix:
+                        debug('    Setting core.bare to true')
+                        repo.setConfig('core.bare', 'true')
+
+                if repo.getConfig('remote.origin.url') != d['url']:
+                    debug('  %s uses an different origin (%s)' % (name, repo.getConfig('remote.origin.url')))
+                    if args.fix:
+                        debug('    Setting remote.origin.url to %s' % d['url'])
+                        repo.setConfig('remote.origin.url', d['url'])
+
+                if repo.getConfig('remote.origin.fetch') != '+refs/*:refs/*':
+                    debug('  %s fetch value is invalid (%s)' % (name, repo.getConfig('remote.origin.fetch')))
+                    if args.fix:
+                        debug('    Setting remote.origin.fetch to %s' % '+refs/*:refs/*')
+                        repo.setConfig('remote.origin.fetch', '+refs/*:refs/*')
+
+    def directories(self, args):
+        """Check that the directories are valid"""
+
+        debug('Checking directories')
+        for k, d in self.C.get('dirs').items():
+            d = os.path.abspath(os.path.realpath(os.path.expanduser(d)))
+            if not os.path.isdir(d):
+                debug('  %s does not exist' % d)
+                if args.fix:
+                    debug('    Creating %s' % d)
+                    os.mkdir(d, 0777)
index 2c139ad..265b762 100644 (file)
@@ -224,6 +224,11 @@ class Git(object):
         result = self.execute(cmd)
         return result[0] == 0
 
+    def setConfig(self, name, value):
+        cmd = 'config %s %s' % (name, value)
+        result = self.execute(cmd)
+        return result[0] == 0
+
     def setRemote(self, remote, url):
         if not self.getRemote(remote):
             cmd = 'remote add %s %s' % (remote, url)