aboutsummaryrefslogtreecommitdiff
path: root/binary_search_tool/cros_pkg/create_cleanup_script.py
blob: abfea5ebd87a441fea569d49bcabc78413a9257c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2020 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""The script to generate a cleanup script after setup.sh.

This script takes a set of flags, telling it what setup.sh changed
during the set up process. Based on the values of the input flags, it
generates a cleanup script, named ${BOARD}_cleanup.sh, which will
undo the changes made by setup.sh, returning everything to its
original state.
"""


import argparse
import sys


def Usage(parser, msg):
    print("ERROR: " + msg)
    parser.print_help()
    sys.exit(1)


def Main(argv):
    """Generate a script to undo changes done by setup.sh

    The script setup.sh makes a change that needs to be
    undone, namely it creates a soft link making /build/${board} point
    to /build/${board}.work.  To do this, it had to see if
    /build/${board} already existed, and if so, whether it was a real
    tree or a soft link.  If it was soft link, it saved the old value
    of the link, then deleted it and created the new link.  If it was
    a real tree, it renamed the tree to /build/${board}.save, and then
    created the new soft link.  If the /build/${board} did not
    previously exist, then it just created the new soft link.

    This function takes arguments that tell it exactly what setup.sh
    actually did, then generates a script to undo those exact changes.
    """

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--board",
        dest="board",
        required=True,
        help="Chromeos board for packages/image.",
    )

    parser.add_argument(
        "--old_tree_missing",
        dest="tree_existed",
        action="store_false",
        help="Did /build/${BOARD} exist.",
        default=True,
    )

    parser.add_argument(
        "--renamed_tree",
        dest="renamed_tree",
        action="store_true",
        help="Was /build/${BOARD} saved & renamed.",
        default=False,
    )

    parser.add_argument(
        "--old_link",
        dest="old_link",
        help=("The original build tree soft link."),
    )

    options = parser.parse_args(argv[1:])

    if options.old_link or options.renamed_tree:
        if not options.tree_existed:
            Usage(
                parser,
                "If --tree_existed is False, cannot have "
                "--renamed_tree or --old_link",
            )

    if options.old_link and options.renamed_tree:
        Usage(parser, "--old_link and --renamed_tree are incompatible options.")

    if options.tree_existed:
        if not options.old_link and not options.renamed_tree:
            Usage(
                parser,
                "If --tree_existed is True, then must have either "
                "--old_link or --renamed_tree",
            )

    out_filename = "cros_pkg/" + options.board + "_cleanup.sh"

    with open(out_filename, "w", encoding="utf-8") as out_file:
        out_file.write("#!/bin/bash\n\n")
        # First, remove the 'new' soft link.
        out_file.write("sudo rm /build/%s\n" % options.board)
        if options.tree_existed:
            if options.renamed_tree:
                # Old build tree existed and was a real tree, so it got
                # renamed.  Move the renamed tree back to the original tree.
                out_file.write(
                    "sudo mv /build/%s.save /build/%s\n"
                    % (options.board, options.board)
                )
            else:
                # Old tree existed and was already a soft link.  Re-create the
                # original soft link.
                original_link = options.old_link
                if original_link[0] == "'":
                    original_link = original_link[1:]
                if original_link[-1] == "'":
                    original_link = original_link[:-1]
                out_file.write(
                    "sudo ln -s %s /build/%s\n" % (original_link, options.board)
                )
        out_file.write("\n")
        # Remove common.sh file
        out_file.write("rm common/common.sh\n")

    return 0


if __name__ == "__main__":
    retval = Main(sys.argv)
    sys.exit(retval)