1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright 2020 The ChromiumOS Authors 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""The script to generate a cleanup script after setup.sh. 8 9This script takes a set of flags, telling it what setup.sh changed 10during the set up process. Based on the values of the input flags, it 11generates a cleanup script, named ${BOARD}_cleanup.sh, which will 12undo the changes made by setup.sh, returning everything to its 13original state. 14""" 15 16 17import argparse 18import sys 19 20 21def Usage(parser, msg): 22 print("ERROR: " + msg) 23 parser.print_help() 24 sys.exit(1) 25 26 27def Main(argv): 28 """Generate a script to undo changes done by setup.sh 29 30 The script setup.sh makes a change that needs to be 31 undone, namely it creates a soft link making /build/${board} point 32 to /build/${board}.work. To do this, it had to see if 33 /build/${board} already existed, and if so, whether it was a real 34 tree or a soft link. If it was soft link, it saved the old value 35 of the link, then deleted it and created the new link. If it was 36 a real tree, it renamed the tree to /build/${board}.save, and then 37 created the new soft link. If the /build/${board} did not 38 previously exist, then it just created the new soft link. 39 40 This function takes arguments that tell it exactly what setup.sh 41 actually did, then generates a script to undo those exact changes. 42 """ 43 44 parser = argparse.ArgumentParser() 45 parser.add_argument( 46 "--board", 47 dest="board", 48 required=True, 49 help="Chromeos board for packages/image.", 50 ) 51 52 parser.add_argument( 53 "--old_tree_missing", 54 dest="tree_existed", 55 action="store_false", 56 help="Did /build/${BOARD} exist.", 57 default=True, 58 ) 59 60 parser.add_argument( 61 "--renamed_tree", 62 dest="renamed_tree", 63 action="store_true", 64 help="Was /build/${BOARD} saved & renamed.", 65 default=False, 66 ) 67 68 parser.add_argument( 69 "--old_link", 70 dest="old_link", 71 help=("The original build tree soft link."), 72 ) 73 74 options = parser.parse_args(argv[1:]) 75 76 if options.old_link or options.renamed_tree: 77 if not options.tree_existed: 78 Usage( 79 parser, 80 "If --tree_existed is False, cannot have " 81 "--renamed_tree or --old_link", 82 ) 83 84 if options.old_link and options.renamed_tree: 85 Usage(parser, "--old_link and --renamed_tree are incompatible options.") 86 87 if options.tree_existed: 88 if not options.old_link and not options.renamed_tree: 89 Usage( 90 parser, 91 "If --tree_existed is True, then must have either " 92 "--old_link or --renamed_tree", 93 ) 94 95 out_filename = "cros_pkg/" + options.board + "_cleanup.sh" 96 97 with open(out_filename, "w", encoding="utf-8") as out_file: 98 out_file.write("#!/bin/bash\n\n") 99 # First, remove the 'new' soft link. 100 out_file.write("sudo rm /build/%s\n" % options.board) 101 if options.tree_existed: 102 if options.renamed_tree: 103 # Old build tree existed and was a real tree, so it got 104 # renamed. Move the renamed tree back to the original tree. 105 out_file.write( 106 "sudo mv /build/%s.save /build/%s\n" 107 % (options.board, options.board) 108 ) 109 else: 110 # Old tree existed and was already a soft link. Re-create the 111 # original soft link. 112 original_link = options.old_link 113 if original_link[0] == "'": 114 original_link = original_link[1:] 115 if original_link[-1] == "'": 116 original_link = original_link[:-1] 117 out_file.write( 118 "sudo ln -s %s /build/%s\n" % (original_link, options.board) 119 ) 120 out_file.write("\n") 121 # Remove common.sh file 122 out_file.write("rm common/common.sh\n") 123 124 return 0 125 126 127if __name__ == "__main__": 128 retval = Main(sys.argv) 129 sys.exit(retval) 130