#自己在用的,代码写的比较矬,没什么异常处理,linux下一直在用,适合git生成的patch,win下没用过
#!/bin/python
import sys, os, getopt
#opts, args = getopt.getopt(sys.argv[1:], "hi:o:")
global input_file, output_dir
input_file=""
output_dir="out"
def prepare(opts, args):
global input_file, output_dir
for op, value in opts:
if op == "-i":
input_file = value
elif op == "-o":
output_dir = value
elif op == "-h":
print "python getFileFromPatch.py -i xxx.patch [-o output dir]"
sys.exit()
print "input: ", input_file
if os.path.exists(output_dir):
print "out dir exists"
else:
print "no out dir"
os.mkdir(output_dir)
def dumpFile():
global input_file, output_dir
start = False
line_count = 0
file_line_count = 0
file_size = -1
temp_line = ""
file_buf = ""
for line in open(input_file):
line_count += 1
#if "new file mode" in line:
# index = line.find("new file mode 10")
if "--- /dev/null" in line:
start = True
line_count = 0
#print line
continue
if start:
if line_count == 1:
temp_line = line[5:]
file_path_full = output_dir + temp_line
file_path_full = file_path_full.strip().lstrip().rstrip(',')
index = file_path_full.rfind('/')
file_path = file_path_full[0:index]
file_name = file_path_full[index:]
print "generate file: ", file_path+file_name
if os.path.exists(file_path):
print "out dir exists"
else:
print "no out dir"
os.makedirs(file_path)
continue
if line_count == 2:
line = line[11:]
index2 = line.find(" @@")
file_size = line[:index2]
print "file_size =", file_size
continue
if float(file_line_count) < float(file_size):
file_line_count += 1
file_buf += line[1:]
elif start:
file_obj = open(file_path_full, "w")
file_obj.writelines(file_buf)
file_obj.close()
# clean
start = False
line_count = 0
file_line_count = 0
file_size = -1
temp_line = ""
file_buf = ""
if __name__=="__main__":
opts, args = getopt.getopt(sys.argv[1:], "hi:o:")
prepare(opts, args)
dumpFile()