import os
import re
+import shutil
from tools import debug, process
from db import DB
self._loaded = False
return self._load()
+ def runScript(self, scriptname, **kwargs):
+ """Runs a script on the instance"""
+ supported = ['php']
+ path = os.path.join(os.path.dirname(__file__), '..', 'scripts')
+ f = os.path.join(path, scriptname)
+
+ script = None
+ type = None
+ if os.path.isfile(f) and scriptname.rsplit('.', 1) in supported:
+ script = f
+ type = scriptname.rsplit('.', 1)[1]
+ else:
+ for ext in supported:
+ if os.path.isfile(f + '.' + ext):
+ script = f + '.' + ext
+ type = ext
+ break
+
+ if not script:
+ raise Exception('Could not find the script, or format not supported')
+
+ if type == 'php':
+ dest = os.path.join(self.get('path'), 'mdkrun.php')
+ shutil.copyfile(script, dest)
+ result = self.cli('mdkrun.php', **kwargs)
+ os.remove(dest)
+ return result[0]
+
def update(self, remote = 'origin'):
"""Update the instance from the remote"""
--- /dev/null
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+Moodle Development Kit
+
+Copyright (c) 2012 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 sys
+import argparse
+from lib import config, workplace, moodle, tools
+from lib.tools import debug
+
+C = config.Conf().get
+Wp = workplace.Workplace()
+
+# Arguments
+parser = argparse.ArgumentParser(description='Run a script on a Moodle instance')
+parser.add_argument('script', metavar='script', help='the name of the script to run on the instance')
+parser.add_argument('name', metavar='name', default=None, nargs='?', help='name of the instance')
+args = parser.parse_args()
+
+M = Wp.resolve(args.name)
+if not M:
+ debug('This is not a Moodle instance')
+ sys.exit(1)
+
+try:
+ debug('Running \'%s\' on \'%s\'' % (args.script, M.get('identifier')))
+ M.runScript(args.script, stderr=None, stdout=None)
+except Exception as e:
+ debug(e)
+
+debug('Done.')
--- /dev/null
+Custom scripts
+==============
+
+This directory is meant to host scripts to be run on an instance. They are called using the command `run`.
+
+The format of the script is recognised using its extension.
+
+Formats
+-------
+
+### PHP
+
+PHP scripts will be executed from the web directory of an instance. They will be executed as any other CLI script.
\ No newline at end of file
--- /dev/null
+<?php
+/**
+ * Sets the instance ready for developers
+ */
+
+define('CLI_SCRIPT', true);
+require(dirname(__FILE__).'/config.php');
+
+// Set developer level.
+set_config('debug', DEBUG_DEVELOPER);
+
+// Disply debug messages
+set_config('debugdisplay', 1);
+
+// Any kind of password is allowed.
+set_config('passwordpolicy', 0);
+
+// Debug the performance.
+set_config('perfdebug', 15);
+
+// Debug the information of the page.
+set_config('debugpageinfo', 1);
+
+// Allow themes to be changed from the URL.
+set_config('allowthemechangeonurl', 1);
+
+// Adds FirePHP
+$firephp = "
+// FirePHP
+if (@include_once('FirePHPCore/fb.php')) {
+ ob_start();
+}
+";
+if ($f = fopen(dirname(__FILE__).'/config.php', 'a')) {
+ fputs($f, $firephp);
+ fclose($f);
+}