Install Command Object
authorFred <fmcell@gmail.com>
Sun, 3 Mar 2013 17:21:53 +0000 (01:21 +0800)
committerFred <fmcell@gmail.com>
Sun, 3 Mar 2013 17:21:53 +0000 (01:21 +0800)
lib/commands/__init__.py
lib/commands/install.py [new file with mode: 0644]

index bcb02f6..b866d7a 100644 (file)
@@ -31,3 +31,4 @@ from create import CreateCommand
 from fix import FixCommand
 from info import InfoCommand
 from init import InitCommand
+from install import InstallCommand
diff --git a/lib/commands/install.py b/lib/commands/install.py
new file mode 100644 (file)
index 0000000..186e8ee
--- /dev/null
@@ -0,0 +1,107 @@
+#!/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
+from lib import db
+from lib.command import Command
+from lib.tools import debug
+
+DB = db.DB
+
+
+class InstallCommand(Command):
+
+    _description = 'Install a Moodle instance'
+
+    def __init__(self, *args, **kwargs):
+        super(InstallCommand, self).__init__(*args, **kwargs)
+        self._arguments = [
+            (
+                ['-e', '--engine'],
+                {
+                'action': 'store',
+                  'choices': ['mysqli', 'pgsql'],
+                  'default': self.C.get('defaultEngine'),
+                  'help': 'database engine to use',
+                  'metavar': 'engine'
+                }
+            ),
+            (
+                ['-f', '--fullname'],
+                {
+                    'action': 'store',
+                    'help': 'full name of the instance',
+                    'metavar': 'fullname'
+                }
+            ),
+            (
+                ['-r', '--run'],
+                {
+                    'action': 'store',
+                    'help': 'scripts to run after installation',
+                    'metavar': 'run',
+                    'nargs': '*'
+                }
+            ),
+            (
+                ['name'],
+                {
+                    'default': None,
+                    'help': 'name of the instance',
+                    'metavar': 'name',
+                    'nargs': '?'
+                })
+        ]
+
+    def run(self, args):
+
+        name = args.name
+        engine = args.engine
+        fullname = args.fullname
+
+        M = self.Wp.resolve(name)
+        if not M:
+            raise Exception('This is not a Moodle instance')
+
+        name = M.get('identifier')
+        dataDir = self.Wp.getPath(name, 'data')
+        if not os.path.isdir(dataDir):
+            os.mkdir(dataDir, 0777)
+
+        kwargs = {
+            'engine': engine,
+            'fullname': fullname,
+            'dataDir': dataDir
+        }
+        M.install(**kwargs)
+
+        # Running scripts
+        if M.isInstalled() and type(args.run) == list:
+            for script in args.run:
+                debug('Running script \'%s\'' % (script))
+                try:
+                    M.runScript(script)
+                except Exception as e:
+                    debug('Error while running the script')
+                    debug(e)