createprecomplete.py 3.8 KB
Newer Older
1
2
3
# Any copyright is dedicated to the Public Domain.
# http://creativecommons.org/publicdomain/zero/1.0/

4
5
6
7
# Creates the precomplete file containing the remove and rmdir application
# update instructions which is used to remove files and directories that are no
# longer present in a complete update. The current working directory is used for
# the location to enumerate and to create the precomplete file.
8
# For symlinks, remove instructions are always generated.
9

10
from __future__ import absolute_import
11
12
from __future__ import unicode_literals

13
import os
14
import io
15

16

17
18
19
20
21
# TODO When TOR_BROWSER_DATA_OUTSIDE_APP_DIR is used on all platforms,
# we should remove all lines in this file that contain:
#      TorBrowser/Data


22
def get_build_entries(root_path):
23
24
    """Iterates through the root_path, creating a list for each file and
    directory. Excludes any file paths ending with channel-prefs.js.
25
26
27
28
        To support Tor Browser updates, excludes:
          TorBrowser/Data/Browser/profiles.ini
          TorBrowser/Data/Browser/profile.default/bookmarks.html
          TorBrowser/Data/Tor/torrc
29
30
31
32
33
    """
    rel_file_path_set = set()
    rel_dir_path_set = set()
    for root, dirs, files in os.walk(root_path):
        for file_name in files:
34
            parent_dir_rel_path = root[len(root_path) + 1 :]
35
36
            rel_path_file = os.path.join(parent_dir_rel_path, file_name)
            rel_path_file = rel_path_file.replace("\\", "/")
37
38
39
            if not (
                rel_path_file.endswith("channel-prefs.js")
                or rel_path_file.endswith("update-settings.ini")
40
41
42
43
                or rel_path_file == "TorBrowser/Data/Browser/profiles.ini"
                or rel_path_file
                == "TorBrowser/Data/Browser/profile.default/bookmarks.html"
                or rel_path_file == "TorBrowser/Data/Tor/torrc"
44
45
                or rel_path_file.find("distribution/") != -1
            ):
46
47
48
                rel_file_path_set.add(rel_path_file)

        for dir_name in dirs:
49
            parent_dir_rel_path = root[len(root_path) + 1 :]
50
            rel_path_dir = os.path.join(parent_dir_rel_path, dir_name)
51
            rel_path_dir = rel_path_dir.replace("\\", "/") + "/"
52
            if rel_path_dir.find("distribution/") == -1:
53
54
55
56
                if os.path.islink(rel_path_dir[:-1]):
                    rel_file_path_set.add(rel_path_dir[:-1])
                else:
                    rel_dir_path_set.add(rel_path_dir)
57
58
59
60
61
62
63
64

    rel_file_path_list = list(rel_file_path_set)
    rel_file_path_list.sort(reverse=True)
    rel_dir_path_list = list(rel_dir_path_set)
    rel_dir_path_list.sort(reverse=True)

    return rel_file_path_list, rel_dir_path_list

65

66
def generate_precomplete(root_path):
67
68
69
    """Creates the precomplete file containing the remove and rmdir
    application update instructions. The given directory is used
    for the location to enumerate and to create the precomplete file.
70
    """
71
    rel_path_precomplete = "precomplete"
72
    # If inside a Mac bundle use the root of the bundle for the path.
73
    if os.path.basename(root_path) == "Resources":
74
        root_path = os.path.abspath(os.path.join(root_path, "../../"))
75
        rel_path_precomplete = "Contents/Resources/precomplete"
76

77
    precomplete_file_path = os.path.join(root_path, rel_path_precomplete)
78
79
    # Open the file so it exists before building the list of files and open it
    # in binary mode to prevent OS specific line endings.
80
    precomplete_file = io.open(precomplete_file_path, mode="wt", newline="\n")
81
    rel_file_path_list, rel_dir_path_list = get_build_entries(root_path)
82
    for rel_file_path in rel_file_path_list:
83
        precomplete_file.write('remove "' + rel_file_path + '"\n')
84
85

    for rel_dir_path in rel_dir_path_list:
86
        precomplete_file.write('rmdir "' + rel_dir_path + '"\n')
87
88
89

    precomplete_file.close()

90

91
if __name__ == "__main__":
92
    generate_precomplete(os.getcwd())