Guessing the editor from the environment
authorFrederic Massart <fred@moodle.com>
Tue, 15 Apr 2014 07:56:23 +0000 (15:56 +0800)
committerFrederic Massart <fred@moodle.com>
Tue, 15 Apr 2014 07:56:23 +0000 (15:56 +0800)
config-dist.json
lib/commands/doctor.py
lib/tools.py

index 1a48f46..5211cc3 100644 (file)
     "recess": "/usr/local/bin/recess",
     // Path to lessc
     "lessc": "/usr/local/bin/lessc",
-    // Path to your favourite editor
-    "editor": "/usr/bin/vim",
+    // Path to your favourite editor. Set to null to guess it from the System environment.
+    "editor": null,
 
     // Debug level of MDK. 'debug', 'info', 'warning', 'error' or 'critical'.
     "debug": "info",
index 42b0b79..5112f11 100644 (file)
@@ -25,9 +25,10 @@ http://github.com/FMCorz/mdk
 import os
 import shutil
 import imp
+import subprocess
 from lib import git
 from lib.command import Command
-from lib.tools import mkdir
+from lib.tools import mkdir, resolveEditor
 
 
 class DoctorCommand(Command):
@@ -213,8 +214,9 @@ class DoctorCommand(Command):
 
         print 'Checking dependencies'
 
+        # Check binaries.
         hasErrors = False
-        for k in ['git', 'php', 'java', 'recess', 'lessc', 'editor']:
+        for k in ['git', 'php', 'java', 'recess', 'lessc']:
             path = self.C.get(k)
             if not path or not os.path.isfile(path):
                 print '  The path to \'%s\' is invalid: %s' % (k, path)
@@ -222,6 +224,7 @@ class DoctorCommand(Command):
         if hasErrors and args.fix:
             print '    Please manually fix the paths in your config file'
 
+        # Check PIP modules.
         with open(os.path.join(os.path.dirname(__file__), '..', '..', 'requirements.txt'), 'r') as f:
             hasErrors = False
             for line in f:
@@ -236,6 +239,21 @@ class DoctorCommand(Command):
             if hasErrors and args.fix:
                 print '    Try running \'pip -r requirements.txt\' from MDK\'s installation directory'
 
+        # Checking editor.
+        editor = resolveEditor()
+        if editor:
+            try:
+                # Check if it is callable.
+                proc = subprocess.Popen(editor, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+                proc.kill()
+            except OSError:
+                editor = None
+
+        if not editor:
+            print '  Could not resolve the path to your editor'
+            if args.fix:
+                print '    Set $EDITOR, /usr/bin/editor, or use: mdk config set editor [path]'
+
     def directories(self, args):
         """Check that the directories are valid"""
 
index cbfcda9..0ebad54 100644 (file)
@@ -106,11 +106,14 @@ def launchEditor(filepath=None, suffix='.tmp'):
 
     This returns the path to the saved file.
     """
+    editor = resolveEditor()
+    if not editor:
+        raise Exception('Could not locate the editor')
     with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmpfile:
         with open(filepath, 'r') as f:
             tmpfile.write(f.read())
             tmpfile.flush()
-        subprocess.call([C.get('editor'), tmpfile.name])
+        subprocess.call([editor, tmpfile.name])
     return tmpfile.name
 
 
@@ -156,6 +159,19 @@ def process(cmd, cwd=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
     return (proc.returncode, out, err)
 
 
+def resolveEditor():
+    """Try to resolve the editor that the user would want to use.
+       This does actually checks if it is executable"""
+    editor = C.get('editor')
+    if not editor:
+        editor = os.environ.get('EDITOR')
+    if not editor:
+        editor = os.environ.get('VISUAL')
+    if not editor and os.path.isfile('/usr/bin/editor'):
+        editor = '/usr/bin/editor'
+    return editor
+
+
 def downloadProcessHook(count, size, total):
     """Hook to report the downloading a file using urllib.urlretrieve"""
     if count <= 0: