import re
from tools import debug
+from db import DB
+from config import Conf
+
+C = Conf().get
class Moodle():
- _path = None
- _config = None
- _installed = False
- _settings = {}
+ path = None
+ installed = False
+ settings = {}
+
+ _dbo = None
+ _loaded = False
def __init__(self, path):
+ self.path = path
+ self._load()
+
+ def dbo(self):
+ if self._dbo == None:
+ engine = self.get('dbtype')
+ db = self.get('dbname')
+ if engine != None and db != None:
+ try:
+ self._dbo = DB(engine, C('db.%s' % engine))
+ except:
+ pass
+ return self._dbo
- self._path = path
- self._config = os.path.join(path, 'config.php')
+ def get(self, param, default = None):
+ info = self.info()
+ try:
+ return info[param]
+ except:
+ return default
+
+ def info(self):
+ self._load()
+ info = self.settings.copy()
+ info['path'] = self.path
+ info['installed'] = self.installed
+ return info
- if os.path.isfile(self._config):
- self.load()
- self._installed = True
- else:
- self._installed = False
+ def reload(self):
+ self._loaded = False
+ return self._load()
- def installed(self):
- return self._installed
+ def _load(self):
- def load(self):
+ if self._loaded:
+ return True
- if not self.installed():
+ config = os.path.join(self.path, 'config.php')
+ if not os.path.isfile(config):
return None
# Extracts parameters from config.php, does not handle params over multiple lines
prog = re.compile('^\s*\$CFG->([a-z]+)\s*=\s*(?P<brackets>[\'"])(.+)(?P=brackets)\s*;$', re.I)
try:
- f = open(self._config, 'r')
+ f = open(config, 'r')
for line in f:
match = prog.search(line)
if match == None: continue
- self._settings[match.group(1)] = match.group(3)
- setattr(self, match.group(1), match.group(3))
+ self.settings[match.group(1)] = match.group(3)
f.close()
except Exception as e:
- debug('Error while reading config file')
\ No newline at end of file
+ debug('Error while reading config file')
+
+ self._loaded = True
+ return True
--- /dev/null
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import os
+import shutil
+
+from tools import debug
+import config
+import db
+import moodle
+
+C = config.Conf().get
+
+class Workplace():
+
+ def __init__(self, path = None, wwwDir = None, dataDir = None):
+ if path == None:
+ path = C('dirs.store')
+ if wwwDir == None:
+ wwwDir = C('wwwDir')
+ if dataDir == None:
+ dataDir = C('dataDir')
+
+ if not os.path.isdir(path):
+ raise Exception('Directory %s not found' % path)
+
+ self.path = path
+ self.wwwDir = wwwDir
+ self.dataDir = dataDir
+
+ def delete(self, name):
+ # Instantiating the object also checks if it exists
+ M = self.get(name)
+ DB = M.dbo()
+ dbname = M.get('dbname')
+
+ # Deleting the whole thing
+ shutil.rmtree(os.path.join(self.path, name))
+
+ # Deleting the possible symlink
+ link = os.path.join(C('dirs.www'), name)
+ if os.path.islink(link) and os.path.isdir(link):
+ try:
+ os.remove(link)
+ except:
+ pass
+
+ # Delete db
+ if DB:
+ DB.dropdb(dbname)
+
+ def get(self, name):
+ 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))
+
+ def isMoodle(self, name):
+ d = os.path.join(self.path, name)
+ if not os.path.isdir(d):
+ raise Exception('Directory %s not found' % d)
+
+ wwwDir = os.path.join(d, self.wwwDir)
+ dataDir = os.path.join(d, self.dataDir)
+ if not os.path.isdir(wwwDir) or not os.path.isdir(dataDir):
+ return False
+
+ version = os.path.join(wwwDir, 'version.php')
+ try:
+ f = open(version, 'r')
+ lines = f.readlines()
+ f.close()
+ except:
+ return False
+ found = False
+ for line in lines:
+ if line.find('MOODLE VERSION INFORMATION') > -1:
+ found = True
+ break
+ if not found:
+ return False
+
+ return True
+
+ def list(self):
+ dirs = os.listdir(self.path)
+ names = []
+ for d in dirs:
+ if d == '.' or d == '..': continue
+ if not os.path.isdir(os.path.join(self.path, d)): continue
+ if not self.isMoodle(d): continue
+ names.append(d)
+ return names
+