Some more improvements
authorFred <fmcell@gmail.com>
Sun, 29 Jul 2012 15:26:10 +0000 (23:26 +0800)
committerFred <fmcell@gmail.com>
Sun, 29 Jul 2012 15:26:10 +0000 (23:26 +0800)
lib/moodle.py
lib/workplace.py
moodle-fix.py
moodle-install.py
moodle-phpunit.py

index 402624d..61ff7b9 100644 (file)
@@ -3,9 +3,8 @@
 
 import os
 import re
-import subprocess
 
-from tools import debug
+from tools import debug, process
 from db import DB
 from config import Conf
 from git import Git
@@ -14,6 +13,7 @@ C = Conf().get
 
 class Moodle():
 
+    identifier = None
     path = None
     installed = False
     version = {}
@@ -23,8 +23,9 @@ class Moodle():
     _git = None
     _loaded = False
 
-    def __init__(self, path):
+    def __init__(self, path, identifier = None):
         self.path = path
+        self.identifier = identifier
         self._load()
 
     def addConfig(self, name, value):
@@ -45,13 +46,14 @@ class Moodle():
     def branch(self):
         return self._git().currentBranch()
 
-    def cli(self, cli):
+    def cli(self, cli, args = ''):
         cli = os.path.join(self.get('path'), cli.lstrip('/'))
         if not os.path.isfile(cli):
             raise Exception('Could not find script to call')
-        proc = subprocess.Popen([C('php'), cli], cwd=self.get('path'), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-        (stdout, stderr) = proc.communicate()
-        return (proc.returncode, stdout, stderr)
+        if type(args) == 'list':
+            args = ' '.join(args)
+        cmd = '%s %s %s' % (C('php'), cli, args)
+        return process(cmd, cwd=self.get('path'))
 
     def dbo(self):
         if self._dbo == None:
@@ -95,6 +97,7 @@ class Moodle():
     def info(self):
         self._load()
         info = {}
+        info['identifier'] = self.identifier
         info['path'] = self.path
         info['installed'] = self.installed
         for (k, v) in self.config.items():
index 88e80e7..b0be048 100644 (file)
@@ -24,7 +24,7 @@ class Workplace():
         if not os.path.isdir(path):
             raise Exception('Directory %s not found' % path)
 
-        self.path = path
+        self.path = os.path.abspath(os.path.realpath(path))
         self.wwwDir = wwwDir
         self.dataDir = dataDir
 
@@ -46,13 +46,21 @@ class Workplace():
                 pass
 
         # Delete db
-        if DB:
+        if DB and dbname:
             DB.dropdb(dbname)
 
     def get(self, name):
+
+        # Extracts name from path
+        if os.sep in name:
+            path = os.path.abspath(os.path.realpath(name))
+            if not path.startswith(self.path):
+                raise Exception('Could not find Moodle instance at %s' % name)
+            (head, name) = os.path.split(path)
+
         if not self.isMoodle(name):
             raise Exception('Could not find Moodle instance %s' % name)
-        return moodle.Moodle(os.path.join(self.path, name, self.wwwDir))
+        return moodle.Moodle(os.path.join(self.path, name, self.wwwDir), identifier = name)
 
     def isMoodle(self, name):
         d = os.path.join(self.path, name)
index 8082834..7f72bed 100644 (file)
@@ -4,8 +4,6 @@
 import os
 import argparse
 import re
-import subprocess
-import shlex
 
 from lib import config, git, tools, moodle, workplace
 from lib.tools import debug
@@ -57,5 +55,3 @@ if not repo.checkout(branch):
        exit()
 
 debug('Branch %s checked out' % branch)
-
-
index 2239f74..16e6ea4 100644 (file)
@@ -7,11 +7,12 @@ import shutil
 import argparse
 import re
 
-from lib import config, tools, db, moodle
-Moodle = moodle.Moodle
-debug = tools.debug
+from lib import config, db, moodle, workplace
+from lib.tools import debug, process
+
 DB = db.DB
 C = config.Conf().get
+Wp = workplace.Workplace()
 
 # Arguments
 parser = argparse.ArgumentParser(description='Install a Moodle instance')
@@ -31,11 +32,9 @@ cacheIntegration = os.path.join(C('dirs.cache'), 'integration.git')
 
 # Cloning/caching repositories if necessary
 if not os.path.isdir(cacheStable):
-       os.chdir(C('dirs.cache'))
-       os.system('git clone ' + C('remotes.stable') + ' moodle.git')
+       result = process('%s clone %s %s' % (C('git'), C('remotes.stable'), cacheStable))
 if not os.path.isdir(cacheIntegration):
-       os.chdir(C('dirs.cache'))
-       os.system('git clone ' +C('remotes.integration') + ' integration.git')
+       result = process('%s clone %s %s' % (C('git'), C('remotes.integration'), cacheIntegration))
 
 # Wording version
 prefixVersion = version
@@ -65,6 +64,7 @@ dataDir = os.path.join(installDir, C('dataDir'))
 linkDir = os.path.join(C('dirs.www'), name)
 
 # Cloning the repository
+debug('Preparing instance directories...')
 if os.path.isdir(installDir):
        debug('Installation directory exists (%s)' % installDir)
        # sys.exit()
@@ -80,12 +80,12 @@ else:
        os.mkdir(installDir, 0755)
        os.mkdir(dataDir, 0777)
        if C('useCacheAsRemote'):
-               os.chdir(installDir)
-               os.system('git clone ' + repository + ' ' + C('wwwDir'))
+               result = process('%s clone %s %s' % (C('git'), repository, wwwDir))
        else:
                shutil.copytree(repository, wwwDir)
 
 # Checking database
+debug('Preparing database...')
 dbname = re.sub(r'[^a-zA-Z0-9]', '', name).lower()[:28]
 db = DB(engine, C('db.%s' % engine))
 if db.dbexists(dbname):
@@ -96,8 +96,6 @@ else:
 db.selectdb(dbname)
 
 # Installing
-debug('Installing %s...' % name)
-os.chdir(wwwDir)
 if os.path.islink(linkDir):
        os.remove(linkDir)
 if os.path.isfile(linkDir) or os.path.isdir(linkDir):  # No elif!
@@ -106,36 +104,36 @@ else:
        os.symlink(wwwDir, linkDir)
 
 # Creating, fetch, pulling branches
-os.system('git fetch origin')
+debug('Setting up repository...')
+M = Wp.get(name)
+git = M.git()
+result = git.fetch('origin')
 if version == 'master':
-       branch = 'origin/master'
-       os.system('git checkout master')
+       git.checkout('master')
 else:
-       branch = 'origin/MOODLE_%s_STABLE' % version
-       os.system('git checkout -b MOODLE_%s_STABLE %s' % (version, branch))
-os.system('git pull')
-os.system('git remote add mine %s' % C('remotes.mine'))
-os.system('git config --local moodle.name %s' % name)
-os.system('git config --local moodle.branch %s' % branch)
-os.system('git config --local moodle.version %s' % version)
-os.system('git config --local moodle.engine %s' % engine)
+       track = 'origin/MOODLE_%s_STABLE' % version
+       branch = 'MOODLE_%s_STABLE' % version
+       git.createBranch(branch, track)
+       git.checkout(branch)
+git.pull()
+git.addRemote('mine', C('remotes.mine'))
 
 # Launching installation process
 if not args.noinstall:
 
-       os.chdir(wwwDir)
-       configFile = os.path.join(wwwDir, 'config.php')
-       params = (C('phpEnv'), C('host'), name, dataDir, engine, dbname, C('db.%s.user' % engine), C('db.%s.passwd' % engine), C('db.%s.host' % engine), fullname, name, C('login'), C('passwd'))
-       status = os.system('%s admin/cli/install.php --wwwroot="http://%s/%s/" --dataroot="%s" --dbtype="%s" --dbname="%s" --dbuser="%s" --dbpass="%s" --dbhost="%s" --fullname="%s" --shortname="%s" --adminuser="%s" --adminpass="%s" --allow-unstable --agree-license --non-interactive' % params)
-       if status != 0:
+       debug('Installing %s...' % name)
+       cli = 'admin/cli/install.php'
+       params = (C('host'), name, dataDir, engine, dbname, C('db.%s.user' % engine), C('db.%s.passwd' % engine), C('db.%s.host' % engine), fullname, name, C('login'), C('passwd'))
+       args = '--wwwroot="http://%s/%s/" --dataroot="%s" --dbtype="%s" --dbname="%s" --dbuser="%s" --dbpass="%s" --dbhost="%s" --fullname="%s" --shortname="%s" --adminuser="%s" --adminpass="%s" --allow-unstable --agree-license --non-interactive' % params
+       result = M.cli(cli, args, stdout=None, stderr=None)
+       if result[0] != 0:
                raise Exception('Error while running the install, please manually fix the problem.')
 
+       configFile = os.path.join(wwwDir, 'config.php')
        os.chmod(configFile, 0666)
        try:
-               f = open(configFile, 'a')
-               f.seek(0, os.SEEK_END)
-               f.write("\n$CFG->sessioncookiepath = '/%s/';\n" % name)
-               f.close()
+               M.addConfig('sessioncookiepath', '/%s/' % name)
        except Exception:
                debug('Could not append $CFG->sessioncookiepath to config.php')
 
+debug('Process complete!')
index 9c4adf3..04204aa 100644 (file)
@@ -4,7 +4,6 @@
 import sys
 import os
 import argparse
-import subprocess
 from lib import moodle, workplace, config
 from lib.tools import debug