Lazy loading of workplace to prevent dependency issues. Fixes #79
authorFrederic Massart <fred@moodle.com>
Wed, 9 Apr 2014 10:45:03 +0000 (18:45 +0800)
committerFrederic Massart <fred@moodle.com>
Wed, 9 Apr 2014 10:46:56 +0000 (18:46 +0800)
lib/command.py
lib/commands/config.py
lib/commands/doctor.py
lib/commands/init.py

index 3dbfdc4..1eecfdb 100644 (file)
@@ -24,7 +24,6 @@ http://github.com/FMCorz/mdk
 
 import argparse
 import sys
-import workplace
 
 
 class Command(object):
@@ -46,15 +45,12 @@ class Command(object):
             )
         ]
     _description = 'Undocumented command'
-    _loadWorkplace = True
 
     __C = None
     __Wp = None
 
     def __init__(self, config):
         self.__C = config
-        if self._loadWorkplace:
-            self.__Wp = workplace.Workplace()
 
     def argumentError(self, message):
         raise CommandArgumentError(message)
@@ -76,6 +72,9 @@ class Command(object):
 
     @property
     def Wp(self):
+        if not self.__Wp:
+            import workplace
+            self.__Wp = workplace.Workplace()
         return self.__Wp
 
 
index 9023d9a..054aebf 100644 (file)
@@ -88,7 +88,6 @@ class ConfigCommand(Command):
         )
     ]
     _description = 'Manage your configuration'
-    _loadWorkplace = False
 
     def dictDisplay(self, data, ident=0):
         for name in sorted(data.keys()):
index 5ac2c55..2ffda9c 100644 (file)
@@ -143,7 +143,11 @@ class DoctorCommand(Command):
 
         print 'Checking integration instances branches'
 
+        if not self._checkWorkplace():
+            return
+
         instances = self.Wp.list(integration=True)
+
         for identifier in instances:
             M = self.Wp.get(identifier)
             stablebranch = M.get('stablebranch')
@@ -260,6 +264,10 @@ class DoctorCommand(Command):
         """Check that the correct remotes are used"""
 
         print 'Checking remotes'
+
+        if not self._checkWorkplace():
+            return
+
         remotes = {
             'mine': self.C.get('remotes.mine'),
             'stable': self.Wp.getCachedRemote() if self.C.get('useCacheAsUpstreamRemote') else self.C.get('remotes.stable'),
@@ -291,6 +299,10 @@ class DoctorCommand(Command):
         """Check the wwwroot of the instances"""
 
         print 'Checking wwwroot'
+
+        if not self._checkWorkplace():
+            return
+
         instances = self.Wp.resolveMultiple(self.Wp.list())
 
         wwwroot = '%s://%s/' % (self.C.get('scheme'), self.C.get('host'))
@@ -308,3 +320,12 @@ class DoctorCommand(Command):
                     if args.fix:
                         print '    Setting %s on %s' % (expected, M.get('identifier'))
                         M.updateConfig('wwwroot', expected)
+
+    def _checkWorkplace(self, indent=2):
+        """Returns whether the workplace is available or, and print a message if it is not."""
+        try:
+            self.Wp
+        except ImportError:
+            print ' ' * indent + 'The workplace could not be loaded, did you install the dependencies?'
+            return False
+        return True
index 659342b..48e3b4a 100644 (file)
@@ -45,7 +45,6 @@ class InitCommand(Command):
         )
     ]
     _description = 'Initialise MDK'
-    _loadWorkplace = False
 
     def resolve_directory(self, path, user):
         if path.startswith('~'):