587
|
1 |
#============================================================================
|
|
2 |
#Name : delta_zip.py
|
|
3 |
#Part of : Helium
|
|
4 |
|
|
5 |
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
6 |
#All rights reserved.
|
|
7 |
#This component and the accompanying materials are made available
|
|
8 |
#under the terms of the License "Eclipse Public License v1.0"
|
|
9 |
#which accompanies this distribution, and is available
|
|
10 |
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
11 |
#
|
|
12 |
#Initial Contributors:
|
|
13 |
#Nokia Corporation - initial contribution.
|
|
14 |
#
|
|
15 |
#Contributors:
|
|
16 |
#
|
|
17 |
#Description:
|
|
18 |
#===============================================================================
|
|
19 |
"""delta zip create the MD5 signature"""
|
|
20 |
|
|
21 |
import os
|
|
22 |
import shutil
|
|
23 |
import re
|
|
24 |
import fileutils
|
|
25 |
import buildtools
|
|
26 |
import logging
|
|
27 |
|
|
28 |
_logger = logging.getLogger('delta_zip')
|
|
29 |
logging.basicConfig(level=logging.INFO)
|
|
30 |
|
|
31 |
class MD5SignatureBuilder(object):
|
|
32 |
""" MD5 CRC creation base class"""
|
|
33 |
def __init__(self, build_area_root, nb_split, temp_dir, exclude_dirs, list_of_files):
|
|
34 |
"""constructor"""
|
|
35 |
if not build_area_root.endswith(os.sep):
|
|
36 |
self.build_area_root = build_area_root + os.sep
|
|
37 |
self.nb_split = int(nb_split)
|
|
38 |
self.temp_dir = temp_dir
|
|
39 |
self.exclude_dirs = exclude_dirs
|
|
40 |
self.list_of_files = list_of_files
|
628
|
41 |
self.dest_dir = None
|
|
42 |
self.list_of_lists = None
|
587
|
43 |
|
|
44 |
def create_file_list(self):
|
|
45 |
"""Create list of files (was list_files.pl)"""
|
|
46 |
#list_of_files_symbol = os.path.join(self.temp_dir, "list_files_sym.txt")
|
|
47 |
|
|
48 |
if not os.path.exists(self.temp_dir):
|
|
49 |
os.mkdir(self.temp_dir)
|
|
50 |
|
|
51 |
fp_filelist = open(self.list_of_files, 'w')
|
|
52 |
#fp_filelist_sym = open(list_of_files_symbol, 'w')
|
|
53 |
|
|
54 |
scanner = fileutils.FileScanner(self.build_area_root)
|
|
55 |
scanner.add_include('**')
|
|
56 |
|
|
57 |
for _dir in self.exclude_dirs.split(','):
|
|
58 |
_dir = _dir.replace(self.build_area_root, "")
|
|
59 |
scanner.add_exclude(_dir)
|
|
60 |
|
|
61 |
for path in scanner.scan():
|
|
62 |
if (not os.path.isdir(path)) or (os.path.isdir(path) and (os.listdir(path) != [] and os.listdir(path) != ['.svn'])):
|
|
63 |
(drive, _) = os.path.splitdrive(path)
|
|
64 |
path = path.replace(drive + os.sep, "")
|
|
65 |
fp_filelist.write(path + "\n")
|
|
66 |
|
|
67 |
def split_file_list(self):
|
|
68 |
"""Split the list of files for parallelalisation"""
|
|
69 |
md5_dir = os.path.join(self.temp_dir, "md5_temp")
|
|
70 |
self.dest_dir = os.path.join(md5_dir, str(self.nb_split))
|
|
71 |
if not os.path.exists(self.dest_dir):
|
|
72 |
os.makedirs(self.dest_dir)
|
|
73 |
fp_split = []
|
|
74 |
#Open files
|
|
75 |
#White list_of_lists.txt
|
|
76 |
self.list_of_lists = self.dest_dir + "/list_of_lists.txt"
|
|
77 |
fp_list_of_lists = open(self.list_of_lists, 'w')
|
|
78 |
for i in range(self.nb_split):
|
|
79 |
filename = self.dest_dir + "/" + str(i) + ".txt"
|
|
80 |
_fp = open(filename, 'w')
|
|
81 |
fp_split.append(_fp)
|
|
82 |
#Write in list_of_lists
|
|
83 |
fp_list_of_lists.write(filename + "\n")
|
|
84 |
|
|
85 |
#Write in files
|
|
86 |
fp_read = open(self.list_of_files, 'r')
|
|
87 |
line = fp_read.readline()
|
|
88 |
line_number = 0
|
|
89 |
while(line != ""):
|
|
90 |
fp_split[line_number % len(fp_split)].write(line)
|
|
91 |
line = fp_read.readline()
|
|
92 |
line_number += 1
|
|
93 |
|
|
94 |
fp_list_of_lists.close()
|
|
95 |
fp_read.close()
|
|
96 |
for _fp in fp_split:
|
|
97 |
_fp.close()
|
|
98 |
|
|
99 |
def create_command_list(self):
|
|
100 |
""" create the command to run evalid on each file in the list of files"""
|
|
101 |
liste = buildtools.CommandList()
|
|
102 |
|
|
103 |
#tools_dir = os.path.join(self.build_area_root, "/epoc32/tools")
|
|
104 |
|
|
105 |
for i in range(self.nb_split):
|
|
106 |
#liste.addCommand(buildtools.Command("perl -I"+tools_dir, tools_dir, [os.path.join(tools_dir,"evalid_multiple.pl"), "-f", self.__get_partial_input_file_name(i), "> "+self.__get_partial_signature_file_name(i) ]))
|
|
107 |
liste.addCommand(buildtools.Command("evalid", os.sep, ["", "-f", self.__get_partial_input_file_name(i) + " "+self.build_area_root, self.__get_partial_signature_file_name(i) ]))
|
|
108 |
|
|
109 |
return liste
|
|
110 |
|
|
111 |
def __get_partial_input_file_name(self, _nb):
|
|
112 |
""" get the input file name string as has been created so far and add .txt to it"""
|
|
113 |
return os.path.join(self.dest_dir, str(_nb) + ".txt")
|
|
114 |
|
|
115 |
def __get_partial_signature_file_name(self, _nb):
|
|
116 |
""" get the signature file name string as has been created so far and add .md5 to it"""
|
|
117 |
return os.path.join(self.dest_dir, str(_nb) + ".md5")
|
|
118 |
|
|
119 |
def concatenate_signature_files(self, signature_file):
|
|
120 |
""" concatenate all the files with the MD5 CRC in """
|
|
121 |
# Get header
|
|
122 |
_fp = open(self.__get_partial_signature_file_name(0), 'r')
|
|
123 |
line = ""
|
|
124 |
header_temp = ""
|
|
125 |
header = ""
|
|
126 |
while (re.search(r'(\S+).*MD5=(\S+)', line) == None):
|
|
127 |
header_temp = header_temp + line
|
|
128 |
line = _fp.readline()
|
|
129 |
|
|
130 |
for line in header_temp.splitlines():
|
|
131 |
if re.match(r'Directory:.*', line):
|
|
132 |
line = "Directory:" + self.build_area_root
|
|
133 |
if re.match(r'FileList:.*', line):
|
|
134 |
line = "FileList:" + self.list_of_files
|
|
135 |
header = header + line + "\n"
|
|
136 |
|
|
137 |
#re.sub(r'(Directory:).*\n', "\1"+self.build_area_root, header)
|
|
138 |
#re.sub(r'(FileList:).*\n', "\1"+self.list_of_files, header)
|
|
139 |
|
|
140 |
header_size = len(header.splitlines())
|
|
141 |
|
|
142 |
fp_md5_signatures_file = open(signature_file, 'w')
|
|
143 |
fp_md5_signatures_file.write(header)
|
|
144 |
for i in range(self.nb_split):
|
|
145 |
_fp = open(self.__get_partial_signature_file_name(i), 'r')
|
|
146 |
|
|
147 |
for i in range(header_size): # Skip header
|
|
148 |
_fp.readline()
|
|
149 |
|
|
150 |
fp_md5_signatures_file.write(_fp.read())
|
|
151 |
_fp.close()
|
|
152 |
fp_md5_signatures_file.close()
|
|
153 |
|
|
154 |
def write_build_file(self):
|
|
155 |
""" create the file of the list of files to have a CRC created"""
|
|
156 |
self.create_file_list()
|
|
157 |
self.split_file_list()
|
|
158 |
self.create_build_file()
|
|
159 |
|
|
160 |
def create_build_file(self):
|
|
161 |
""" there should always be an overloaded version of this method in sub-classes"""
|
|
162 |
raise NotImplementedError()
|
|
163 |
|
|
164 |
def build(self, signature_file):
|
|
165 |
"""create the list of files generate the MD5 CRC and create the final file with CRCs in"""
|
|
166 |
self.write_build_file()
|
|
167 |
self.compute_evalid_MD5()
|
|
168 |
self.concatenate_signature_files(signature_file)
|
|
169 |
|
|
170 |
def compute_evalid_MD5(self):
|
|
171 |
""" there should always be an overlaoded version in the methos sub-class"""
|
|
172 |
raise NotImplementedError()
|
|
173 |
|
|
174 |
class MD5SignatureBuilderEBS(MD5SignatureBuilder):
|
|
175 |
""" build the MD5 CRCs for all the files in the list of files"""
|
628
|
176 |
def __init__(self, build_area_root, nb_split, temp_dir, exclude_dirs, list_of_files):
|
|
177 |
MD5SignatureBuilder.__init__(self, build_area_root, nb_split, temp_dir, exclude_dirs, list_of_files)
|
|
178 |
self.makefile = None
|
|
179 |
|
587
|
180 |
def create_build_file(self):
|
|
181 |
"""Create EBS XML"""
|
|
182 |
liste = self.create_command_list()
|
|
183 |
self.makefile = self.dest_dir + "/ebs.xml"
|
|
184 |
buildtools.convert(liste, self.makefile, "ebs")
|
|
185 |
|
|
186 |
def compute_evalid_MD5(self):
|
|
187 |
"""Compute MD5 using the requested parallel build system"""
|
|
188 |
os.chdir(self.build_area_root)
|
|
189 |
os.system("perl -I%HELIUM_HOME%/tools/common/packages %HELIUM_HOME%/tools/compile/buildjob.pl -d " + self.makefile + " -l " + os.path.join(self.dest_dir, "md5.log") + " -n " + str(int(os.environ['NUMBER_OF_PROCESSORS'])*2))
|
|
190 |
|
|
191 |
|
|
192 |
# Run the delta zipping over the EC build system
|
|
193 |
|
|
194 |
class MD5SignatureBuilderEC(MD5SignatureBuilder):
|
|
195 |
""" The MD5 CRC creation for delta zippinf for use on EC machines"""
|
|
196 |
def __init__(self, build_area_root, nb_split, temp_dir, exclude_dirs, ec_cluster_manager, ec_build_class, list_of_files):
|
|
197 |
MD5SignatureBuilder.__init__(self, build_area_root, nb_split, temp_dir, exclude_dirs, list_of_files)
|
628
|
198 |
self.makefile = None
|
587
|
199 |
self.ec_cluster_manager = ec_cluster_manager
|
|
200 |
self.ec_build_class = ec_build_class
|
|
201 |
|
|
202 |
def create_build_file(self):
|
|
203 |
"""Create makefile"""
|
|
204 |
liste = self.create_command_list()
|
|
205 |
self.makefile = self.dest_dir + "/Makefile"
|
|
206 |
buildtools.convert(liste, self.makefile, "make")
|
|
207 |
|
|
208 |
def compute_evalid_MD5(self):
|
|
209 |
"""Compute MD5 using the requested parallel build system"""
|
|
210 |
root_path = os.environ['EMAKE_ROOT'] +";" + "c:\\apps;"
|
|
211 |
os.chdir(self.build_area_root)
|
|
212 |
|
|
213 |
print "emake --emake-cm=" + self.ec_cluster_manager + " --emake-class=" + self.ec_build_class + " --emake-root="+root_path+ " --emake-emulation-table make=symbian,emake=symbian,nmake=nmake -f " + self.makefile
|
|
214 |
os.system("emake --emake-cm=" + self.ec_cluster_manager + " --emake-annodetail=basic,history,file,waiting --emake-annofile="+self.temp_dir+"\\delta_zip_anno.xml"+ " --emake-class=" + self.ec_build_class + " --emake-root="+root_path+" --emake-emulation-table make=symbian,emake=symbian,nmake=nmake -f " + self.makefile)
|
|
215 |
|
|
216 |
class DeltaZipBuilder(object):
|
|
217 |
"""methods to create the delta zip after all the prep"""
|
|
218 |
def __init__(self, build_area_root, temp_path, old_md5_signature, new_md5_signature):
|
|
219 |
self.build_area_root = os.path.join(build_area_root, os.sep)
|
|
220 |
self.temp_path = temp_path
|
|
221 |
self.old_md5_signature = old_md5_signature
|
|
222 |
self.new_md5_signature = new_md5_signature
|
|
223 |
self.sign_dic = SignaturesDict()
|
|
224 |
|
|
225 |
def __fill_signature_dict(self, signature_file, old_new):
|
|
226 |
""" read each line of signature file search for .MD5"""
|
|
227 |
_fp = open(signature_file, 'r')
|
|
228 |
lines = _fp.read().splitlines()
|
|
229 |
_fp.close()
|
|
230 |
for line in lines:
|
|
231 |
info = re.search(r'([ \S]+) TYPE=.*MD5=(\S+)', line)
|
|
232 |
if info != None:
|
|
233 |
filename = info.group(1)
|
|
234 |
if not self.sign_dic.has_key(filename):
|
|
235 |
self.sign_dic[filename] = ["", ""]
|
|
236 |
self.sign_dic[filename][old_new] = info.group(2)
|
|
237 |
|
|
238 |
def create_delta_zip(self, zip_file, delete_list_file, no_of_zips, ant_file):
|
|
239 |
"""Create Delta zip and list of file to delete."""
|
|
240 |
|
|
241 |
no_of_zips = int(no_of_zips)
|
|
242 |
self.__fill_signature_dict(self.old_md5_signature, 0)
|
|
243 |
self.__fill_signature_dict(self.new_md5_signature, 1)
|
|
244 |
|
|
245 |
#fp_dic = open(zip_file + ".dic.txt", 'w')
|
|
246 |
#fp_dic.write(str(self.sign_dic))
|
|
247 |
#fp_dic.close()
|
|
248 |
|
|
249 |
delete_list = []
|
|
250 |
|
|
251 |
if not os.path.exists(os.path.dirname(delete_list_file)):
|
|
252 |
os.mkdir(os.path.dirname(delete_list_file))
|
|
253 |
if not os.path.exists(self.temp_path):
|
|
254 |
os.mkdir(self.temp_path)
|
|
255 |
|
|
256 |
archive_txt = open(os.path.join(self.temp_path, 'create_zips.txt'), 'w')
|
|
257 |
|
|
258 |
for _file in self.sign_dic.keys():
|
|
259 |
filepath = os.path.join(self.build_area_root, _file)
|
|
260 |
|
|
261 |
signatures = self.sign_dic[_file]
|
|
262 |
|
|
263 |
( _, rest) = os.path.splitdrive(filepath)
|
|
264 |
(frontpath, rest) = os.path.split(rest)
|
|
265 |
|
|
266 |
if (signatures[0] != signatures[1]): #File changed between the 2 BAs
|
|
267 |
if (signatures[0] != "") and (signatures[1] != ""): # File is present in both BAs and has changed
|
|
268 |
if os.path.exists(filepath): # File could have been deleting after running 'build-md5':
|
|
269 |
archive_txt.write(_file + "\n")
|
|
270 |
else:
|
|
271 |
if (signatures[1] != ""): # New file
|
|
272 |
if os.path.exists(filepath):
|
|
273 |
archive_txt.write(_file + "\n")
|
|
274 |
else: # Deleted file
|
|
275 |
delete_list.append(filepath)
|
|
276 |
|
|
277 |
archive_txt.close()
|
|
278 |
|
|
279 |
splitter = MD5SignatureBuilder('', no_of_zips, self.temp_path, '', os.path.join(self.temp_path, 'create_zips.txt'))
|
|
280 |
splitter.split_file_list()
|
|
281 |
|
|
282 |
os.chdir(self.build_area_root)
|
|
283 |
|
|
284 |
(frontpath, rest) = os.path.split(zip_file)
|
|
285 |
stages = buildtools.CommandList()
|
|
286 |
|
|
287 |
for i in range(no_of_zips):
|
|
288 |
md5_dir = os.path.join(self.temp_path, "md5_temp")
|
|
289 |
path = os.path.join(md5_dir, os.path.join(str(no_of_zips), str(i) + '.txt'))
|
|
290 |
output = os.path.join(frontpath, rest.replace(".zip", "_part_%sof%s.zip" % (str(i+1), str(no_of_zips))))
|
|
291 |
|
|
292 |
cmd = buildtools.Command('7za.exe', self.build_area_root)
|
|
293 |
cmd.addArg('a')
|
|
294 |
# Set the format to be zip-compatible
|
|
295 |
cmd.addArg('-tzip')
|
|
296 |
cmd.addArg(output)
|
|
297 |
cmd.addArg('@' + path)
|
|
298 |
|
|
299 |
stages.addCommand(cmd)
|
|
300 |
|
|
301 |
writer = buildtools.AntWriter(ant_file)
|
|
302 |
writer.write(stages)
|
|
303 |
|
|
304 |
fp_delete = open(delete_list_file, 'w')
|
|
305 |
fp_delete.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
|
|
306 |
fp_delete.write("<updateinstructions>\n")
|
|
307 |
for i in delete_list:
|
|
308 |
fp_delete.write("<deletefileaction target=\"" + i[2:] + "\"/>\n")
|
|
309 |
fp_delete.write("</updateinstructions>\n")
|
|
310 |
fp_delete.close()
|
|
311 |
|
|
312 |
|
|
313 |
class SignaturesDict(dict):
|
|
314 |
""" class to handle signature comparison"""
|
|
315 |
def __init__(self):
|
|
316 |
""" constructor"""
|
|
317 |
dict.__init__(self)
|
|
318 |
|
|
319 |
def __str__(self):
|
|
320 |
""" compare the tree structures"""
|
|
321 |
string = ""
|
|
322 |
#o = OldNewBA()
|
|
323 |
both = False
|
|
324 |
only_old = False
|
|
325 |
only_new = False
|
|
326 |
for filename in self.keys():
|
|
327 |
signatures = self[filename]
|
|
328 |
if signatures[0] == signatures[1]: #File did not change
|
|
329 |
both = True
|
|
330 |
elif (signatures[0] != "") and (signatures[1] != ""): # File is present in both BAs and has changed
|
|
331 |
both = False
|
|
332 |
else:
|
|
333 |
if (signatures[1] != ""): # New file
|
|
334 |
only_old = True
|
|
335 |
else: # Deleted file
|
|
336 |
only_new = True
|
|
337 |
|
|
338 |
string = string + filename + " " + str(both) + " " + " " + str(only_old) + " " + str(only_new) + " " + self[filename][0] + " " + self[filename][1] + "\n"
|
|
339 |
|
|
340 |
return string
|
|
341 |
|
628
|
342 |
def readEvalid(dir_):
|
587
|
343 |
"""read E valid"""
|
|
344 |
filesdict = {}
|
628
|
345 |
for root, _, files in os.walk(dir_):
|
587
|
346 |
for name in files:
|
|
347 |
f_file = os.path.join(root, name)
|
|
348 |
directory = None
|
|
349 |
for md5line in open(f_file):
|
|
350 |
if md5line.startswith('Directory:'):
|
|
351 |
directory = md5line.replace('Directory:', '').replace('\n', '')
|
|
352 |
if 'MD5=' in md5line:
|
|
353 |
info = re.search(r'([ \S]+) TYPE=.*MD5=(\S+)', md5line)
|
|
354 |
if info != None:
|
|
355 |
assert directory
|
|
356 |
filesdict[os.path.join(directory, info.group(1))] = info.group(2)
|
|
357 |
return filesdict
|
|
358 |
|
|
359 |
def changedFiles(atsevalidpre, atsevalidpost):
|
|
360 |
"""changed Files"""
|
|
361 |
filesbefore = readEvalid(atsevalidpre)
|
|
362 |
filesafter = readEvalid(atsevalidpost)
|
|
363 |
|
|
364 |
changedfiles = []
|
|
365 |
|
|
366 |
for key in filesafter.keys():
|
|
367 |
if key not in filesbefore:
|
|
368 |
changedfiles.append(key)
|
|
369 |
else:
|
|
370 |
if filesafter[key] != filesbefore[key]:
|
|
371 |
changedfiles.append(key)
|
|
372 |
|
|
373 |
return changedfiles
|
|
374 |
|
|
375 |
def evalidAdomapping(builddrive, dest, adomappingfile):
|
|
376 |
"""evalid Ado mapping """
|
|
377 |
os.chdir(builddrive)
|
|
378 |
i = 0
|
|
379 |
if os.path.exists(dest):
|
|
380 |
shutil.rmtree(dest)
|
|
381 |
os.mkdir(dest)
|
|
382 |
for line in open(adomappingfile):
|
628
|
383 |
dir_ = line.split('=')[0].replace(r'\:', ':')
|
587
|
384 |
tmpfile = os.path.join(dest, str(i))
|
628
|
385 |
os.system('evalid -g ' + dir_ + ' ' + tmpfile)
|
587
|
386 |
i = i + 1 |