From f9ed5070bd18659fb75c7bca4ada356ff1ee4f67 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Thu, 30 Aug 2012 21:28:15 +0800 Subject: [PATCH] Command run to execute scripts --- lib/moodle.py | 29 +++++++++++++++++++++++++++++ moodle-run.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ scripts/README.md | 13 +++++++++++++ scripts/dev.php | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100755 moodle-run.py create mode 100644 scripts/README.md create mode 100644 scripts/dev.php diff --git a/lib/moodle.py b/lib/moodle.py index c4c2c30..5dc703d 100644 --- a/lib/moodle.py +++ b/lib/moodle.py @@ -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 index 0000000..1ede5df --- /dev/null +++ b/moodle-run.py @@ -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://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 index 0000000..30aac47 --- /dev/null +++ b/scripts/README.md @@ -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 index 0000000..1a8d779 --- /dev/null +++ b/scripts/dev.php @@ -0,0 +1,37 @@ +