1#!/usr/bin/env python 2 3""" 4Convert the raw message sources from git patch emails to git-am friendly files. 5 6Usage: 7 81. Mail.app -> Save As -> api.eml (Raw Message Source) 92. .../convert.py api.eml 103. git am [--signoff] < api.eml 114. git svn dcommit [--commit-url https://id@llvm.org/svn/llvm-project/lldb/trunk] 12""" 13 14import os, re, sys 15import StringIO 16 17def usage(problem_file=None): 18 if problem_file: 19 print "%s is not a file" % problem_file 20 print "Usage: convert.py raw-message-source [raw-message-source2 ...]" 21 sys.exit(0) 22 23def do_convert(file): 24 """Skip all preceding mail message headers until 'From: ' is encountered. 25 Then for each line ('From: ' header included), replace the dos style CRLF 26 end-of-line with unix style LF end-of-line. 27 """ 28 print "converting %s ..." % file 29 30 with open(file, 'r') as f_in: 31 content = f_in.read() 32 33 # The new content to be written back to the same file. 34 new_content = StringIO.StringIO() 35 36 # Boolean flag controls whether to start printing lines. 37 from_header_seen = False 38 39 # By default, splitlines() don't include line breaks. CRLF should be gone. 40 for line in content.splitlines(): 41 # Wait till we scan the 'From: ' header before start printing the lines. 42 if not from_header_seen: 43 if not line.startswith('From: '): 44 continue 45 else: 46 from_header_seen = True 47 48 print >> new_content, line 49 50 with open(file, 'w') as f_out: 51 f_out.write(new_content.getvalue()) 52 53 print "done" 54 55def main(): 56 if len(sys.argv) == 1: 57 usage() 58 # Convert the raw message source one by one. 59 for file in sys.argv[1:]: 60 if not os.path.isfile(file): 61 usage(file) 62 do_convert(file) 63 64if __name__ == '__main__': 65 main() 66