From: Frederic Massart Date: Wed, 5 Nov 2014 02:20:16 +0000 (+0800) Subject: Abstracting the methods to get URL or extra dir X-Git-Tag: v1.4~15 X-Git-Url: http://git.cameron1729.xyz/?a=commitdiff_plain;h=9efe2159d27e675932bae557435ac9ebb237e6eb;p=mdk.git Abstracting the methods to get URL or extra dir --- diff --git a/mdk/commands/create.py b/mdk/commands/create.py index ba85f9c..f89ccda 100644 --- a/mdk/commands/create.py +++ b/mdk/commands/create.py @@ -199,7 +199,8 @@ class CreateCommand(Command): 'dbname': dbname, 'dropDb': dropDb, 'fullname': fullname, - 'dataDir': self.Wp.getPath(name, 'data') + 'dataDir': self.Wp.getPath(name, 'data'), + 'wwwroot': self.Wp.getUrl(name) } try: M.install(**kwargs) diff --git a/mdk/commands/doctor.py b/mdk/commands/doctor.py index 60352ab..d80d395 100644 --- a/mdk/commands/doctor.py +++ b/mdk/commands/doctor.py @@ -24,7 +24,6 @@ http://github.com/FMCorz/mdk import os import shutil -import imp import subprocess from .. import git from ..command import Command @@ -358,16 +357,13 @@ class DoctorCommand(Command): instances = self.Wp.resolveMultiple(self.Wp.list()) - wwwroot = '%s://%s/' % (self.C.get('scheme'), self.C.get('host')) - if self.C.get('path') != '' and self.C.get('path') != None: - wwwroot = wwwroot + self.C.get('path') + '/' for M in instances: if not M.isInstalled(): continue else: actual = M.get('wwwroot') - expected = wwwroot + M.get('identifier') + expected = self.Wp.getUrl(M.get('identifier')) if actual != expected: print ' %s: Found %s, not %s' % (M.get('identifier'), actual, expected) if args.fix: diff --git a/mdk/commands/install.py b/mdk/commands/install.py index 5e595e0..2fe16e2 100644 --- a/mdk/commands/install.py +++ b/mdk/commands/install.py @@ -93,7 +93,8 @@ class InstallCommand(Command): kwargs = { 'engine': engine, 'fullname': fullname, - 'dataDir': dataDir + 'dataDir': dataDir, + 'wwwroot': self.Wp.getUrl(name) } M.install(**kwargs) diff --git a/mdk/moodle.py b/mdk/moodle.py index 4341b6d..318eb33 100644 --- a/mdk/moodle.py +++ b/mdk/moodle.py @@ -398,12 +398,14 @@ class Moodle(object): info[k] = v return info - def install(self, dbname=None, engine=None, dataDir=None, fullname=None, dropDb=False): + def install(self, dbname=None, engine=None, dataDir=None, fullname=None, dropDb=False, wwwroot=None): """Launch the install script of an Instance""" if self.isInstalled(): raise InstallException('Instance already installed!') + if not wwwroot: + raise InstallException('Cannot install without a value for wwwroot') if dataDir == None or not os.path.isdir(dataDir): raise InstallException('Cannot install instance without knowing where the data directory is') if dbname == None: @@ -430,12 +432,6 @@ class Moodle(object): db.createdb(dbname) db.selectdb(dbname) - # Defining wwwroot. - wwwroot = '%s://%s/' % (C.get('scheme'), C.get('host')) - if C.get('path') != '' and C.get('path') != None: - wwwroot = wwwroot + C.get('path') + '/' - wwwroot = wwwroot + self.identifier - logging.info('Installing %s...' % self.identifier) cli = 'admin/cli/install.php' params = (wwwroot, dataDir, engine, dbname, C.get('db.%s.user' % engine), C.get('db.%s.passwd' % engine), C.get('db.%s.host' % engine), fullname, self.identifier, C.get('login'), C.get('passwd')) diff --git a/mdk/phpunit.py b/mdk/phpunit.py index fa461d4..b72c0a3 100644 --- a/mdk/phpunit.py +++ b/mdk/phpunit.py @@ -63,19 +63,11 @@ class PHPUnit(object): def getCoverageDir(self): """Get the Coverage directory, and create it if required""" - path = os.path.join(self.Wp.getPath(self.M.get('identifier'), 'extra'), 'coverage') - if not os.path.exists(path): - mkdir(path, 0777) - return path + return self.Wp.getExtraDir(self.M.get('identifier'), 'coverage') def getCoverageUrl(self): """Return the code coverage URL""" - # TODO Constructing the URL should be done elsewhere... - wwwroot = '%s://%s/' % (C.get('scheme'), C.get('host')) - if C.get('path') != '' and C.get('path') != None: - wwwroot = wwwroot + C.get('path') + '/' - wwwroot = wwwroot + C.get('mdkDir') + '/' + self.M.get('identifier') + '/coverage' - return wwwroot + return self.Wp.getUrl(self.M.get('identifier'), extra='coverage') def init(self, force=False, prefix=None): """Initialise the PHPUnit environment""" diff --git a/mdk/workplace.py b/mdk/workplace.py index 2e84e5b..a9dff86 100644 --- a/mdk/workplace.py +++ b/mdk/workplace.py @@ -276,6 +276,18 @@ class Workplace(object): else: return os.path.join(self.cache, 'moodle.git') + def getExtraDir(self, name, subdir=None): + """Return the path to the extra directory of an instance + + This also creates the directory if does not exist. + """ + path = self.getPath(name, 'extra') + if subdir: + path = os.path.join(path, subdir) + if not os.path.exists(path): + mkdir(path, 0777) + return path + def getMdkWebDir(self): """Return (and create) the special MDK web directory.""" mdkExtra = os.path.join(self.www, self.mdkDir) @@ -296,6 +308,21 @@ class Workplace(object): else: return base + def getUrl(self, name, extra=None): + """Return the URL to an instance, or to its extra directory if extra is passed""" + base = '%s://%s' % (C.get('scheme'), C.get('host')) + + if C.get('path') != '' and C.get('path') != None: + base = '%s/%s' % (base, C.get('path')) + + wwwroot = None + if not extra: + wwwroot = '%s/%s' % (base, name) + else: + wwwroot = '%s/%s/%s/%s' % (base, self.mdkDir, name, extra) + + return wwwroot + def isMoodle(self, name): """Checks whether a Moodle instance exist under this name""" d = os.path.join(self.path, name)