25 Merge the patches of several patch files into one file
27 The files are processed as bytes. A file with format Binary holds raw
28 doubles between its text lines, and we copy those verbatim. The header
29 of the merged file is the header of the first partial file, i.e. all
30 partial files have to have the same format. Everything from the first
31 "begin patch" in a file on is a patch, and everything before it is the
32 header: A header holds text only, so the first occurrence is the right
33 one, even in a binary file.
35 @param partial_files: [str]
36 Files to merge. Their patches end up in the merged file in this order.
38 @param merged_file_path: str
42 print(
"Start to merge file:", merged_file_path, end=
"...")
43 patch_marker = b
"begin patch"
45 with open(merged_file_path,
"wb")
as merged_patch_file:
49 with open(partial_files[0],
"rb")
as patch_file:
50 content = patch_file.read()
51 first_patch = content.find(patch_marker)
52 merged_patch_file.write(content
if first_patch < 0
else content[:first_patch])
54 for infile
in partial_files:
55 with open(infile,
"rb")
as patch_file:
56 content = patch_file.read()
57 first_patch = content.find(patch_marker)
59 merged_patch_file.write(b
"\n")
60 merged_patch_file.write(content[first_patch:])
69 merged_file_number = 0
73 os.path.dirname(path_to_metafile),
"merged_meta_file.peano-patch-file"
78 "# \n" "# Peano patch file \n" "# Version 0.2 \n" "# \n" "format ASCII \n" "\n"
81 with open(path_to_metafile,
"r")
as metafile:
82 for line
in metafile.readlines():
83 if "end dataset" in line
and (
84 merged_file_number < start
or merged_file_number > end
87 print(
"jump snapshot", merged_file_number)
88 merged_file_number += 1
89 elif "end dataset" in line:
92 "merged_patch_file_" + str(merged_file_number) +
".peano-patch-file"
95 if os.path.isabs(path_to_metafile):
96 merged_file_name = os.path.join(
97 os.path.dirname(path_to_metafile), merged_file_name
102 partial_files.clear()
103 merged_file_number += 1
107 "begin dataset\n" + timestamp_line +
"\n"
108 f
' include "{merged_file_name}"\n'
113 if "timestamp" in line
and merged_file_number >= start:
114 timestamp_line = line
119 if "include" in words:
120 file_path = words[1].strip(
'"')
125 if not os.path.isabs(file_path):
126 file_path = os.path.join(
127 os.path.dirname(path_to_metafile), file_path
130 partial_files.append(file_path)