Mercurial

From Enso Wiki

Jump to: navigation, search

litaroelte

[edit] Python

  • NOTE: This command is created for Command Server not as a standalone command

[edit] Code

from base import BaseCommand
import os
import time
 
class Command(BaseCommand):
    def __init__(self, *args):
        BaseCommand.__init__(self, *args)
 
        self._hgdir = None
 
        self.newArbitraryCommand("hgdir {Working Directory}",
                                self._setdir,
                                "Set the Directory to execute Mercurial commands",
                                "<p>Set the Directory to execute Mercurial commands</p>"
        )
        self.newArbitraryCommand("hg {command}",
                                self._runhg,
                                "Execute the given command. You MUST have set the directory first",
                                "<p>Execute the given command. You MUST have set the directory first</p>"
        )
 
 
    #Command Methods
    def _setdir(self, directory):
        if len(directory) == 0 and len(self.getUnicodeSelection()) > 0:
           directory  = self.getUnicodeSelection()
        elif len(self.getFileSelection()) == 1:
            print 'Setting'
            directory = self.getFileSelection()[0]
        else:
            self.displayMessage('You must select a directory to work with', 'Error setting Mercurial Directory')
            return
 
        self._hgdir = directory
 
        os.chdir(self._hgdir)
 
        self.displayMessage(str(self._hgdir), 'Setting Mercurial Directory')
 
    def _runhg(self, command):
        if self._hgdir is None:
            self.displayMessage('You must select a directory to work with', 'Error executing Mercurial command')
 
        try:
            (inp, outp) = os.popen4("hg " + command)
            self.displayMessage(outp.read(), 'hg ' + command)
 
        except Exception, e:
            self.displayMessage(str(e), 'Error executing Mercurial command')
 
        finally:
            #This just ensures the message stays up for a bit
            time.sleep(1)