Command run to execute scripts
authorFrederic Massart <fred@moodle.com>
Thu, 30 Aug 2012 13:28:15 +0000 (21:28 +0800)
committerFrederic Massart <fred@moodle.com>
Thu, 30 Aug 2012 13:31:36 +0000 (21:31 +0800)
lib/moodle.py
moodle-run.py [new file with mode: 0755]
scripts/README.md [new file with mode: 0644]
scripts/dev.php [new file with mode: 0644]

index c4c2c30..5dc703d 100644 (file)
@@ -24,6 +24,7 @@ http://github.com/FMCorz/mdk
 
 import os
 import re
+import shutil
 
 from tools import debug, process
 from db import DB
@@ -334,6 +335,34 @@ class Moodle(object):
         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"""
 
diff --git a/moodle-run.py b/moodle-run.py
new file mode 100755 (executable)
index 0000000..1ede5df
--- /dev/null
@@ -0,0 +1,50 @@
+#!/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.')
diff --git a/scripts/README.md b/scripts/README.md
new file mode 100644 (file)
index 0000000..30aac47
--- /dev/null
@@ -0,0 +1,13 @@
+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
diff --git a/scripts/dev.php b/scripts/dev.php
new file mode 100644 (file)
index 0000000..1a8d779
--- /dev/null
@@ -0,0 +1,37 @@
+<?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);
+}