Merge mozilla-b2g34 to 2.1s. a=merge
[gecko.git] / tools / update-packaging / make_incremental_updates.py
blob922fb9697c46a1f3595213fb8b53e45d0cb37406
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 import os
6 import shutil
7 import sha
8 from os.path import join, getsize
9 from stat import *
10 import re
11 import sys
12 import getopt
13 import time
14 import datetime
15 import bz2
16 import string
17 import tempfile
19 class PatchInfo:
20 """ Represents the meta-data associated with a patch
21 work_dir = working dir where files are stored for this patch
22 archive_files = list of files to include in this patch
23 manifestv2 = set of manifest version 2 patch instructions
24 manifestv3 = set of manifest version 3 patch instructions
25 file_exclusion_list =
26 files to exclude from this patch. names without slashes will be
27 excluded anywhere in the directory hiearchy. names with slashes
28 will only be excluded at that exact path
29 """
30 def __init__(self, work_dir, file_exclusion_list, path_exclusion_list):
31 self.work_dir=work_dir
32 self.archive_files=[]
33 self.manifestv2=[]
34 self.manifestv3=[]
35 self.file_exclusion_list=file_exclusion_list
36 self.path_exclusion_list=path_exclusion_list
38 def append_add_instruction(self, filename):
39 """ Appends an add instruction for this patch.
40 if filename starts with distribution/extensions/.*/ this will add an
41 add-if instruction that will add the file if the parent directory
42 of the file exists. This was ported from
43 mozilla/tools/update-packaging/common.sh's make_add_instruction.
44 """
45 m = re.match("((?:|.*/)distribution/extensions/.*)/", filename)
46 if m:
47 # Directory immediately following extensions is used for the test
48 testdir = m.group(1)
49 print ' add-if "'+testdir+'" "'+filename+'"'
50 self.manifestv2.append('add-if "'+testdir+'" "'+filename+'"')
51 self.manifestv3.append('add-if "'+testdir+'" "'+filename+'"')
52 else:
53 print ' add "'+filename+'"'
54 self.manifestv2.append('add "'+filename+'"')
55 self.manifestv3.append('add "'+filename+'"')
57 def append_add_if_not_instruction(self, filename):
58 """ Appends an add-if-not instruction to the version 3 manifest for this patch.
59 This was ported from mozilla/tools/update-packaging/common.sh's
60 make_add_if_not_instruction.
61 """
62 print ' add-if-not "'+filename+'" "'+filename+'"'
63 self.manifestv3.append('add-if-not "'+filename+'" "'+filename+'"')
65 def append_patch_instruction(self, filename, patchname):
66 """ Appends a patch instruction for this patch.
68 filename = file to patch
69 patchname = patchfile to apply to file
71 if filename starts with distribution/extensions/.*/ this will add a
72 patch-if instruction that will patch the file if the parent
73 directory of the file exists. This was ported from
74 mozilla/tools/update-packaging/common.sh's make_patch_instruction.
75 """
76 m = re.match("((?:|.*/)distribution/extensions/.*)/", filename)
77 if m:
78 testdir = m.group(1)
79 print ' patch-if "'+testdir+'" "'+patchname+'" "'+filename+'"'
80 self.manifestv2.append('patch-if "'+testdir+'" "'+patchname+'" "'+filename+'"')
81 self.manifestv3.append('patch-if "'+testdir+'" "'+patchname+'" "'+filename+'"')
82 else:
83 print ' patch "'+patchname+'" "'+filename+'"'
84 self.manifestv2.append('patch "'+patchname+'" "'+filename+'"')
85 self.manifestv3.append('patch "'+patchname+'" "'+filename+'"')
87 def append_remove_instruction(self, filename):
88 """ Appends an remove instruction for this patch.
89 This was ported from
90 mozilla/tools/update-packaging/common.sh/make_remove_instruction
91 """
92 if filename.endswith("/"):
93 print ' rmdir "'+filename+'"'
94 self.manifestv2.append('rmdir "'+filename+'"')
95 self.manifestv3.append('rmdir "'+filename+'"')
96 elif filename.endswith("/*"):
97 filename = filename[:-1]
98 print ' rmrfdir "'+filename+'"'
99 self.manifestv2.append('rmrfdir "'+filename+'"')
100 self.manifestv3.append('rmrfdir "'+filename+'"')
101 else:
102 print ' remove "'+filename+'"'
103 self.manifestv2.append('remove "'+filename+'"')
104 self.manifestv3.append('remove "'+filename+'"')
106 def create_manifest_files(self):
107 """ Create the v2 manifest file in the root of the work_dir """
108 manifest_file_path = os.path.join(self.work_dir,"updatev2.manifest")
109 manifest_file = open(manifest_file_path, "wb")
110 manifest_file.writelines("type \"partial\"\n")
111 manifest_file.writelines(string.join(self.manifestv2, '\n'))
112 manifest_file.writelines("\n")
113 manifest_file.close()
115 bzip_file(manifest_file_path)
116 self.archive_files.append('"updatev2.manifest"')
118 """ Create the v3 manifest file in the root of the work_dir """
119 manifest_file_path = os.path.join(self.work_dir,"updatev3.manifest")
120 manifest_file = open(manifest_file_path, "wb")
121 manifest_file.writelines("type \"partial\"\n")
122 manifest_file.writelines(string.join(self.manifestv3, '\n'))
123 manifest_file.writelines("\n")
124 manifest_file.close()
126 bzip_file(manifest_file_path)
127 self.archive_files.append('"updatev3.manifest"')
129 def build_marfile_entry_hash(self, root_path):
130 """ Iterates through the root_path, creating a MarFileEntry for each file
131 and directory in that path. Excludes any filenames in the file_exclusion_list
133 mar_entry_hash = {}
134 filename_set = set()
135 dirname_set = set()
136 for root, dirs, files in os.walk(root_path):
137 for name in files:
138 # filename is the relative path from root directory
139 partial_path = root[len(root_path)+1:]
140 if name not in self.file_exclusion_list:
141 filename = os.path.join(partial_path, name)
142 if "/"+filename not in self.path_exclusion_list:
143 mar_entry_hash[filename]=MarFileEntry(root_path, filename)
144 filename_set.add(filename)
146 for name in dirs:
147 # dirname is the relative path from root directory
148 partial_path = root[len(root_path)+1:]
149 if name not in self.file_exclusion_list:
150 dirname = os.path.join(partial_path, name)
151 if "/"+dirname not in self.path_exclusion_list:
152 dirname = dirname+"/"
153 mar_entry_hash[dirname]=MarFileEntry(root_path, dirname)
154 dirname_set.add(dirname)
156 return mar_entry_hash, filename_set, dirname_set
159 class MarFileEntry:
160 """Represents a file inside a Mozilla Archive Format (MAR)
161 abs_path = abspath to the the file
162 name = relative path within the mar. e.g.
163 foo.mar/dir/bar.txt extracted into /tmp/foo:
164 abs_path=/tmp/foo/dir/bar.txt
165 name = dir/bar.txt
167 def __init__(self, root, name):
168 """root = path the the top of the mar
169 name = relative path within the mar"""
170 self.name=name.replace("\\", "/")
171 self.abs_path=os.path.join(root,name)
172 self.sha_cache=None
174 def __str__(self):
175 return 'Name: %s FullPath: %s' %(self.name,self.abs_path)
177 def calc_file_sha_digest(self, filename):
178 """ Returns sha digest of given filename"""
179 file_content = open(filename, 'r').read()
180 return sha.new(file_content).digest()
182 def sha(self):
183 """ Returns sha digest of file repreesnted by this _marfile_entry\x10"""
184 if not self.sha_cache:
185 self.sha_cache=self.calc_file_sha_digest(self.abs_path)
186 return self.sha_cache
188 def exec_shell_cmd(cmd):
189 """Execs shell cmd and raises an exception if the cmd fails"""
190 if (os.system(cmd)):
191 raise Exception, "cmd failed "+cmd
194 def copy_file(src_file_abs_path, dst_file_abs_path):
195 """ Copies src to dst creating any parent dirs required in dst first """
196 dst_file_dir=os.path.dirname(dst_file_abs_path)
197 if not os.path.exists(dst_file_dir):
198 os.makedirs(dst_file_dir)
199 # Copy the file over
200 shutil.copy2(src_file_abs_path, dst_file_abs_path)
202 def bzip_file(filename):
203 """ Bzip's the file in place. The original file is replaced with a bzip'd version of itself
204 assumes the path is absolute"""
205 exec_shell_cmd('bzip2 -z9 "' + filename+'"')
206 os.rename(filename+".bz2",filename)
208 def bunzip_file(filename):
209 """ Bzip's the file in palce. The original file is replaced with a bunzip'd version of itself.
210 doesn't matter if the filename ends in .bz2 or not"""
211 if not filename.endswith(".bz2"):
212 os.rename(filename, filename+".bz2")
213 filename=filename+".bz2"
214 exec_shell_cmd('bzip2 -d "' + filename+'"')
217 def extract_mar(filename, work_dir):
218 """ Extracts the marfile intot he work_dir
219 assumes work_dir already exists otherwise will throw osError"""
220 print "Extracting "+filename+" to "+work_dir
221 saved_path = os.getcwd()
222 try:
223 os.chdir(work_dir)
224 exec_shell_cmd("mar -x "+filename)
225 finally:
226 os.chdir(saved_path)
228 def create_partial_patch_for_file(from_marfile_entry, to_marfile_entry, shas, patch_info):
229 """ Creates the partial patch file and manifest entry for the pair of files passed in
231 if not (from_marfile_entry.sha(),to_marfile_entry.sha()) in shas:
232 print 'diffing "'+from_marfile_entry.name+'\"'
233 #bunzip to/from
234 bunzip_file(from_marfile_entry.abs_path)
235 bunzip_file(to_marfile_entry.abs_path)
237 # The patch file will be created in the working directory with the
238 # name of the file in the mar + .patch
239 patch_file_abs_path = os.path.join(patch_info.work_dir,from_marfile_entry.name+".patch")
240 patch_file_dir=os.path.dirname(patch_file_abs_path)
241 if not os.path.exists(patch_file_dir):
242 os.makedirs(patch_file_dir)
244 # Create bzip'd patch file
245 exec_shell_cmd("mbsdiff "+from_marfile_entry.abs_path+" "+to_marfile_entry.abs_path+" "+patch_file_abs_path)
246 bzip_file(patch_file_abs_path)
248 # Create bzip's full file
249 full_file_abs_path = os.path.join(patch_info.work_dir, to_marfile_entry.name)
250 shutil.copy2(to_marfile_entry.abs_path, full_file_abs_path)
251 bzip_file(full_file_abs_path)
253 if os.path.getsize(patch_file_abs_path) < os.path.getsize(full_file_abs_path):
254 # Patch is smaller than file. Remove the file and add patch to manifest
255 os.remove(full_file_abs_path)
256 file_in_manifest_name = from_marfile_entry.name+".patch"
257 file_in_manifest_abspath = patch_file_abs_path
258 patch_info.append_patch_instruction(to_marfile_entry.name, file_in_manifest_name)
259 else:
260 # File is smaller than patch. Remove the patch and add file to manifest
261 os.remove(patch_file_abs_path)
262 file_in_manifest_name = from_marfile_entry.name
263 file_in_manifest_abspath = full_file_abs_path
264 patch_info.append_add_instruction(file_in_manifest_name)
266 shas[from_marfile_entry.sha(),to_marfile_entry.sha()] = (file_in_manifest_name,file_in_manifest_abspath)
267 patch_info.archive_files.append('"'+file_in_manifest_name+'"')
268 else:
269 filename, src_file_abs_path = shas[from_marfile_entry.sha(),to_marfile_entry.sha()]
270 # We've already calculated the patch for this pair of files.
271 if (filename.endswith(".patch")):
272 # print "skipping diff: "+from_marfile_entry.name
273 # Patch was smaller than file - add patch instruction to manifest
274 file_in_manifest_name = to_marfile_entry.name+'.patch';
275 patch_info.append_patch_instruction(to_marfile_entry.name, file_in_manifest_name)
276 else:
277 # File was smaller than file - add file to manifest
278 file_in_manifest_name = to_marfile_entry.name
279 patch_info.append_add_instruction(file_in_manifest_name)
280 # Copy the pre-calculated file into our new patch work aread
281 copy_file(src_file_abs_path, os.path.join(patch_info.work_dir, file_in_manifest_name))
282 patch_info.archive_files.append('"'+file_in_manifest_name+'"')
284 def create_add_patch_for_file(to_marfile_entry, patch_info):
285 """ Copy the file to the working dir, add the add instruction, and add it to the list of archive files """
286 copy_file(to_marfile_entry.abs_path, os.path.join(patch_info.work_dir, to_marfile_entry.name))
287 patch_info.append_add_instruction(to_marfile_entry.name)
288 patch_info.archive_files.append('"'+to_marfile_entry.name+'"')
290 def create_add_if_not_patch_for_file(to_marfile_entry, patch_info):
291 """ Copy the file to the working dir, add the add-if-not instruction, and add it to the list of archive files """
292 copy_file(to_marfile_entry.abs_path, os.path.join(patch_info.work_dir, to_marfile_entry.name))
293 patch_info.append_add_if_not_instruction(to_marfile_entry.name)
294 patch_info.archive_files.append('"'+to_marfile_entry.name+'"')
296 def process_explicit_remove_files(dir_path, patch_info):
297 """ Looks for a 'removed-files' file in the dir_path. If the removed-files does not exist
298 this will throw. If found adds the removed-files
299 found in that file to the patch_info"""
301 # Windows and linux have this file at the root of the dir
302 list_file_path = os.path.join(dir_path, "removed-files")
303 if not os.path.exists(list_file_path):
304 list_file_path = os.path.join(dir_path, "Contents/Resources/removed-files")
306 if (os.path.exists(list_file_path)):
307 list_file = bz2.BZ2File(list_file_path,"r") # throws if doesn't exist
309 lines = []
310 for line in list_file:
311 lines.append(line.strip())
312 list_file.close()
314 lines.sort(reverse=True)
315 for line in lines:
316 # Exclude any blank and comment lines.
317 if line and not line.startswith("#"):
318 # Python on windows uses \ for path separators and the update
319 # manifests expects / for path separators on all platforms.
320 line = line.replace("\\", "/")
321 patch_info.append_remove_instruction(line)
323 def create_partial_patch(from_dir_path, to_dir_path, patch_filename, shas, patch_info, forced_updates, add_if_not_list):
324 """ Builds a partial patch by comparing the files in from_dir_path to those of to_dir_path"""
325 # Cannocolize the paths for safey
326 from_dir_path = os.path.abspath(from_dir_path)
327 to_dir_path = os.path.abspath(to_dir_path)
328 # Create a hashtable of the from and to directories
329 from_dir_hash,from_file_set,from_dir_set = patch_info.build_marfile_entry_hash(from_dir_path)
330 to_dir_hash,to_file_set,to_dir_set = patch_info.build_marfile_entry_hash(to_dir_path)
331 # Create a list of the forced updates
332 forced_list = forced_updates.strip().split('|')
333 # Require that the precomplete file is included in the complete update
334 if "precomplete" in to_file_set:
335 forced_list.append("precomplete")
336 elif "Contents/Resources/precomplete" in to_file_set:
337 forced_list.append("Contents/Resources/precomplete")
338 # The check with \ file separators allows tests for Mac to run on Windows
339 elif "Contents\Resources\precomplete" in to_file_set:
340 forced_list.append("Contents\Resources\precomplete")
341 else:
342 raise Exception, "missing precomplete file in: "+to_dir_path
344 if "removed-files" in to_file_set:
345 forced_list.append("removed-files")
346 elif "Contents/Resources/removed-files" in to_file_set:
347 forced_list.append("Contents/Resources/removed-files")
348 # The check with \ file separators allows tests for Mac to run on Windows
349 elif "Contents\Resources\\removed-files" in to_file_set:
350 forced_list.append("Contents\Resources\\removed-files")
351 else:
352 raise Exception, "missing removed-files file in: "+to_dir_path
354 # Files which exist in both sets need to be patched
355 patch_filenames = list(from_file_set.intersection(to_file_set))
356 patch_filenames.sort(reverse=True)
357 for filename in patch_filenames:
358 from_marfile_entry = from_dir_hash[filename]
359 to_marfile_entry = to_dir_hash[filename]
360 if os.path.basename(filename) in add_if_not_list:
361 # This filename is in the add if not list, explicitly add-if-not
362 create_add_if_not_patch_for_file(to_dir_hash[filename], patch_info)
363 elif filename in forced_list:
364 print 'Forcing "'+filename+'"'
365 # This filename is in the forced list, explicitly add
366 create_add_patch_for_file(to_dir_hash[filename], patch_info)
367 else:
368 if from_marfile_entry.sha() != to_marfile_entry.sha():
369 # Not the same - calculate a patch
370 create_partial_patch_for_file(from_marfile_entry, to_marfile_entry, shas, patch_info)
372 # files in to_dir not in from_dir need to added
373 add_filenames = list(to_file_set - from_file_set)
374 add_filenames.sort(reverse=True)
375 for filename in add_filenames:
376 if os.path.basename(filename) in add_if_not_list:
377 create_add_if_not_patch_for_file(to_dir_hash[filename], patch_info)
378 else:
379 create_add_patch_for_file(to_dir_hash[filename], patch_info)
381 # files in from_dir not in to_dir need to be removed
382 remove_filenames = list(from_file_set - to_file_set)
383 remove_filenames.sort(reverse=True)
384 for filename in remove_filenames:
385 patch_info.append_remove_instruction(from_dir_hash[filename].name)
387 process_explicit_remove_files(to_dir_path, patch_info)
389 # directories in from_dir not in to_dir need to be removed
390 remove_dirnames = list(from_dir_set - to_dir_set)
391 remove_dirnames.sort(reverse=True)
392 for dirname in remove_dirnames:
393 patch_info.append_remove_instruction(from_dir_hash[dirname].name)
395 # Construct the Manifest files
396 patch_info.create_manifest_files()
398 # And construct the mar
399 mar_cmd = 'mar -C '+patch_info.work_dir+' -c output.mar '+string.join(patch_info.archive_files, ' ')
400 exec_shell_cmd(mar_cmd)
402 # Copy mar to final destination
403 patch_file_dir = os.path.split(patch_filename)[0]
404 if not os.path.exists(patch_file_dir):
405 os.makedirs(patch_file_dir)
406 shutil.copy2(os.path.join(patch_info.work_dir,"output.mar"), patch_filename)
408 return patch_filename
410 def usage():
411 print "-h for help"
412 print "-f for patchlist_file"
414 def get_buildid(work_dir):
415 """ extracts buildid from MAR
417 ini = '%s/application.ini' % work_dir
418 if not os.path.exists(ini):
419 ini = '%s/Contents/Resources/application.ini' % work_dir
420 if not os.path.exists(ini):
421 print 'WARNING: application.ini not found, cannot find build ID'
422 return ''
424 file = bz2.BZ2File(ini)
425 for line in file:
426 if line.find('BuildID') == 0:
427 return line.strip().split('=')[1]
428 print 'WARNING: cannot find build ID in application.ini'
429 return ''
431 def decode_filename(filepath):
432 """ Breaks filename/dir structure into component parts based on regex
433 for example: firefox-3.0b3pre.en-US.linux-i686.complete.mar
434 Or linux-i686/en-US/firefox-3.0b3.complete.mar
435 Returns dict with keys product, version, locale, platform, type
437 try:
438 m = re.search(
439 '(?P<product>\w+)(-)(?P<version>\w+\.\w+(\.\w+){0,2})(\.)(?P<locale>.+?)(\.)(?P<platform>.+?)(\.)(?P<type>\w+)(.mar)',
440 os.path.basename(filepath))
441 return m.groupdict()
442 except Exception, exc:
443 try:
444 m = re.search(
445 '(?P<platform>.+?)\/(?P<locale>.+?)\/(?P<product>\w+)-(?P<version>\w+\.\w+)\.(?P<type>\w+).mar',
446 filepath)
447 return m.groupdict()
448 except:
449 raise Exception("could not parse filepath %s: %s" % (filepath, exc))
451 def create_partial_patches(patches):
452 """ Given the patches generates a set of partial patches"""
453 shas = {}
455 work_dir_root = None
456 metadata = []
457 try:
458 work_dir_root = tempfile.mkdtemp('-fastmode', 'tmp', os.getcwd())
459 print "Building patches using work dir: %s" % (work_dir_root)
461 # Iterate through every patch set in the patch file
462 patch_num = 1
463 for patch in patches:
464 startTime = time.time()
466 from_filename,to_filename,patch_filename,forced_updates = patch.split(",")
467 from_filename,to_filename,patch_filename = os.path.abspath(from_filename),os.path.abspath(to_filename),os.path.abspath(patch_filename)
469 # Each patch iteration uses its own work dir
470 work_dir = os.path.join(work_dir_root,str(patch_num))
471 os.mkdir(work_dir)
473 # Extract from mar into from dir
474 work_dir_from = os.path.join(work_dir,"from");
475 os.mkdir(work_dir_from)
476 extract_mar(from_filename,work_dir_from)
477 from_decoded = decode_filename(from_filename)
478 from_buildid = get_buildid(work_dir_from)
479 from_shasum = sha.sha(open(from_filename).read()).hexdigest()
480 from_size = str(os.path.getsize(to_filename))
482 # Extract to mar into to dir
483 work_dir_to = os.path.join(work_dir,"to")
484 os.mkdir(work_dir_to)
485 extract_mar(to_filename, work_dir_to)
486 to_decoded = decode_filename(from_filename)
487 to_buildid = get_buildid(work_dir_to)
488 to_shasum = sha.sha(open(to_filename).read()).hexdigest()
489 to_size = str(os.path.getsize(to_filename))
491 mar_extract_time = time.time()
493 partial_filename = create_partial_patch(work_dir_from, work_dir_to, patch_filename, shas, PatchInfo(work_dir, ['update.manifest','updatev2.manifest','updatev3.manifest'],[]),forced_updates,['channel-prefs.js','update-settings.ini'])
494 partial_buildid = to_buildid
495 partial_shasum = sha.sha(open(partial_filename).read()).hexdigest()
496 partial_size = str(os.path.getsize(partial_filename))
498 metadata.append({
499 'to_filename': os.path.basename(to_filename),
500 'from_filename': os.path.basename(from_filename),
501 'partial_filename': os.path.basename(partial_filename),
502 'to_buildid':to_buildid,
503 'from_buildid':from_buildid,
504 'to_sha1sum':to_shasum,
505 'from_sha1sum':from_shasum,
506 'partial_sha1sum':partial_shasum,
507 'to_size':to_size,
508 'from_size':from_size,
509 'partial_size':partial_size,
510 'to_version':to_decoded['version'],
511 'from_version':from_decoded['version'],
512 'locale':from_decoded['locale'],
513 'platform':from_decoded['platform'],
515 print "done with patch %s/%s time (%.2fs/%.2fs/%.2fs) (mar/patch/total)" % (str(patch_num),str(len(patches)),mar_extract_time-startTime,time.time()-mar_extract_time,time.time()-startTime)
516 patch_num += 1
517 return metadata
518 finally:
519 # If we fail or get a ctrl-c during run be sure to clean up temp dir
520 if (work_dir_root and os.path.exists(work_dir_root)):
521 shutil.rmtree(work_dir_root)
523 def main(argv):
524 patchlist_file = None
525 try:
526 opts, args = getopt.getopt(argv, "hf:", ["help", "patchlist_file="])
527 for opt, arg in opts:
528 if opt in ("-h", "--help"):
529 usage()
530 sys.exit()
531 elif opt in ("-f", "--patchlist_file"):
532 patchlist_file = arg
533 except getopt.GetoptError:
534 usage()
535 sys.exit(2)
537 if not patchlist_file:
538 usage()
539 sys.exit(2)
541 patches = []
542 f = open(patchlist_file, 'r')
543 for line in f.readlines():
544 patches.append(line)
545 f.close()
546 create_partial_patches(patches)
548 if __name__ == "__main__":
549 main(sys.argv[1:])