#!/usr/bin/env python """ diffback : a diff merger diffback is a diff merger script aimed to glue any version controls system with any merger application. (c) 2005 Brian Ray (bray@sent.com) free to use,modify,change, at your own risk. Leave this disclaimer. """ #...................................................................... # Source control type SCMTYPE = 'svn' # 'svn' or 'cvs' #...................................................................... # Macintosh opendiff command DIFFCOMMAND = 'opendiff' DIFFARGS = '' #...................................................................... # vim command (example) # DIFFCOMMAND = 'vim' # DIFFARGS = '-d' #...................................................................... # degub settings DEBUG = 1 DEBUGLEVEL = 0 # and above DEBUGTYPES = ( 'Note', 'Warning', 'Cation' ) import sys,os,tempfile,traceback,re from time import sleep def dprint(str,level = 0 ): """Wrapper to print debug messages.""" if DEBUG == 1: if level <= DEBUGLEVEL: print "%s: %s" % (DEBUGTYPES[level] ,str) def runit(cmd): """Executes any command""" dprint(cmd) rtrncode = os.system(cmd) if rtrncode != 0: traceback.print_stack() raise Exception, "could not run command:\n\t'%s' : %s" % (cmd,rtrncode) def checkfileisthere(filename): """Checks to see if the file specified exists.""" if not os.path.isfile(filename): traceback.print_stack() raise Exceptions, "could not find file:\n\t'%s'" % filename def getsufx(filename): """Trys and gets the dot suffix""" reo = re.compile(r'\.[^\.]*$') res = reo.search(filename) if res != None: return res.group() else: return '.txt' def maketmpcopy(filename): """ creates a temp file of BASE @returns str filepath """ checkfileisthere(filename) sufx = getsufx(filename) tempfilepath = tempfile.mktemp(suffix=sufx,prefix='BASE_') if SCMTYPE == 'svn': cmd = "svn cat -r BASE %s > %s" % (filename,tempfilepath) elif SCMTYPE == 'cvs': cmd = "cvs update -p %s > %s" % (filename,tempfilepath) runit(cmd) return runopendiff, ( tempfilepath , filename ), False def removefile(inf): """Removes a file, as last step""" os.unlink(inf) return None, None, True def waitonhitkey(inf): """Waits for user input EOL""" print "Diff done. hit " raw_input(); return removefile, inf, False def runopendiff(inf): """Run open diff command""" cmd = "%s %s %s %s" % (DIFFCOMMAND,DIFFARGS,inf[0], inf[1]) runit(cmd) # wait a could seconds sleep(3) return waitonhitkey, inf[0], False def checkargs(): """ Parses commandline arguments and takes the appropriate action. """ print sys.argv print "-"*72 len = sys.argv.__len__() if len > 1: return maketmpcopy,(sys.argv[len-1]) # len -1 should give last argument else: traceback.print_stack() raise Exception, "\tUsage :\n\t ./svnfm " if __name__ == "__main__": try: func,impargs = checkargs() done = False while done != True: func,impargs,isdone = func(impargs) done = isdone print "Done." except Exception,e: print '_'*72 print '-'*72 print str(e)