From a899c0e8ae413c1f5d922a567a02c4bb795cf2f9 Mon Sep 17 00:00:00 2001 From: Fred Date: Mon, 4 Mar 2013 01:21:53 +0800 Subject: [PATCH] Install Command Object --- lib/commands/__init__.py | 1 + lib/commands/install.py | 107 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 lib/commands/install.py diff --git a/lib/commands/__init__.py b/lib/commands/__init__.py index bcb02f6..b866d7a 100644 --- a/lib/commands/__init__.py +++ b/lib/commands/__init__.py @@ -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 index 0000000..186e8ee --- /dev/null +++ b/lib/commands/install.py @@ -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://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) -- 2.11.0