#!/usr/bin/python # by dexen Wed Apr 2 22:16:59 CEST 2008 import sys import os import re from os import path def tr( s ): print "trace:", s pass def toFileHgStyle( s ): return s.split( ' ' )[5].strip()+'.diff' matchDotDot = re.compile( "[.][.]" ) def toFile( s ): if matchDotDot.search( s ): raise exception( "DotDot in diff str, giving up: " + s ) if True: return toFileHgStyle( s ) return 'foo' def handleDiffChunk( s ): #print s filePN = toFile( s[0] ) tr( filePN ) matchDiff = re.compile( "diff " ) def processInput( input, chunks ): mLine = '' while True: if len( mLine ) == 0: l = input.readline() else: l = mLine if len( l ) == 0: return # raise "Empty input" if matchDiff.match( l ): pass else: raise "failed to match diff: "+l currentC = [] d = {'chunks':currentC} chunks.append( d ) currentC.append( l ) d['filePN'] = toFile( l ) while True: l = input.readline() #tr( l ) if len( l ) == 0: return if matchDiff.match( l ): mLine = l break currentC.append( l ) def createStuff( ch ): f = ch['filePN'] tr( f ) d = path.dirname( f ) if not path.exists( d ): os.makedirs( path.dirname( f ) ) content = ''.join( ch['chunks'] ) fd = os.open( f, os.O_EXCL | os.O_CREAT | os.O_WRONLY ) os.fdopen( fd, "w" ).write(content) def main(): print sys.argv chunks = [] if len( sys.argv[1:] ): for i in sys.argv[1:]: print "Processing file ",i processInput( open( i ), chunks ) #else: #processInput( sys.stdin ) for i in chunks: createStuff( i ) main()