Skip to content

Instantly share code, notes, and snippets.

@starenka
Created February 15, 2011 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starenka/828350 to your computer and use it in GitHub Desktop.
Save starenka/828350 to your computer and use it in GitHub Desktop.
Mercurial "shell"
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# simple mercurial shell inspired by "bzr shell"
# @TODO: autocomplete args, aliases
# show repo info
#
# @author: starenka
# @email: 'moc]tod[liamg].T.E[0aknerats'[::-1]
# @version: 1.0
# @since 2/15/11
# @depends
import cmd,readline,subprocess,sys,os,types
from mercurial import commands
def is_command(function):
if function.startswith('debug'): return False
if function.endswith('_'): return False
if function in ['init']: return False
if isinstance(commands.__dict__.get(a),types.FunctionType): return True
COMMANDS = [a for a in dir(commands) if is_command(a)]
class HgShell(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.set_prompt()
for c in COMMANDS: setattr(self.__class__,'do_%s'%c,self.get_method(c))
def set_prompt(self): self.prompt = 'hg@%s> '%os.getcwd()
def do_EOF(self,line): sys.exit('\nBye')
def get_method(self,name):
def method(self,line):
self.default('%s %s' % (name, line))
return method
def default(self,line):
line = line.split(' ')
if line[0] == 'hg': del(line[0])
cmd = ['hg'] + line
if line[0] not in COMMANDS: cmd = line
try: subprocess.call(cmd)
except Exception,e: print e
def do_cd(self,dir):
try:
os.chdir(dir)
self.set_prompt()
except Exception,e: print e
if __name__ == "__main__":
prompt = HgShell()
while True:
try:
try: prompt.cmdloop()
except KeyboardInterrupt: print
finally: pass #prompt.save_history()
except StopIteration: pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment