createprecomplete.py (4065B)
1 # Any copyright is dedicated to the Public Domain. 2 # http://creativecommons.org/publicdomain/zero/1.0/ 3 4 # Creates the precomplete file containing the remove and rmdir application 5 # update instructions which is used to remove files and directories that are no 6 # longer present in a complete update. The current working directory is used for 7 # the location to enumerate and to create the precomplete file. 8 # For symlinks, remove instructions are always generated. 9 10 import os 11 12 # TODO When TOR_BROWSER_DATA_OUTSIDE_APP_DIR is used on all platforms, 13 # we should remove all lines in this file that contain: 14 # TorBrowser/Data 15 16 17 def get_build_entries(root_path): 18 """Iterates through the root_path, creating a list for each file and 19 directory. Excludes any file paths ending with channel-prefs.js. 20 To support Tor Browser updates, excludes: 21 TorBrowser/Data/Browser/profiles.ini 22 TorBrowser/Data/Browser/profile.default/bookmarks.html 23 TorBrowser/Data/Tor/torrc 24 """ 25 rel_file_path_set = set() 26 rel_dir_path_set = set() 27 for root, dirs, files in os.walk(root_path): 28 for file_name in files: 29 parent_dir_rel_path = root[len(root_path) + 1 :] 30 rel_path_file = os.path.join(parent_dir_rel_path, file_name) 31 rel_path_file = rel_path_file.replace("\\", "/") 32 tb_data_files = { 33 "TorBrowser/Data/Browser/profiles.ini", 34 "TorBrowser/Data/Browser/profile.default/bookmarks.html", 35 "TorBrowser/Data/Tor/torrc", 36 } 37 if not ( 38 rel_path_file.endswith("channel-prefs.js") 39 or rel_path_file.endswith("update-settings.ini") 40 or "/ChannelPrefs.framework/" in rel_path_file 41 or rel_path_file.startswith("ChannelPrefs.framework/") 42 or "/UpdateSettings.framework/" in rel_path_file 43 or rel_path_file.startswith("UpdateSettings.framework/") 44 or "distribution/" in rel_path_file 45 or rel_path_file in tb_data_files 46 ): 47 rel_file_path_set.add(rel_path_file) 48 49 for dir_name in dirs: 50 parent_dir_rel_path = root[len(root_path) + 1 :] 51 rel_path_dir = os.path.join(parent_dir_rel_path, dir_name) 52 rel_path_dir = rel_path_dir.replace("\\", "/") + "/" 53 if rel_path_dir.find("distribution/") == -1: 54 if os.path.islink(rel_path_dir[:-1]): 55 rel_file_path_set.add(rel_path_dir[:-1]) 56 else: 57 rel_dir_path_set.add(rel_path_dir) 58 59 rel_file_path_list = list(rel_file_path_set) 60 rel_file_path_list.sort(reverse=True) 61 rel_dir_path_list = list(rel_dir_path_set) 62 rel_dir_path_list.sort(reverse=True) 63 64 return rel_file_path_list, rel_dir_path_list 65 66 67 def generate_precomplete(root_path): 68 """Creates the precomplete file containing the remove and rmdir 69 application update instructions. The given directory is used 70 for the location to enumerate and to create the precomplete file. 71 """ 72 rel_path_precomplete = "precomplete" 73 # If inside a Mac bundle use the root of the bundle for the path. 74 if os.path.basename(root_path) == "Resources": 75 root_path = os.path.abspath(os.path.join(root_path, "../../")) 76 rel_path_precomplete = "Contents/Resources/precomplete" 77 78 precomplete_file_path = os.path.join(root_path, rel_path_precomplete) 79 # Open the file so it exists before building the list of files and open it 80 # in binary mode to prevent OS specific line endings. 81 precomplete_file = open(precomplete_file_path, mode="w", newline="\n") 82 rel_file_path_list, rel_dir_path_list = get_build_entries(root_path) 83 for rel_file_path in rel_file_path_list: 84 precomplete_file.write('remove "' + rel_file_path + '"\n') 85 86 for rel_dir_path in rel_dir_path_list: 87 precomplete_file.write('rmdir "' + rel_dir_path + '"\n') 88 89 precomplete_file.close() 90 91 92 if __name__ == "__main__": 93 generate_precomplete(os.getcwd())