1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright 2020 The Chromium OS Authors. All rights reserved. 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 16from __future__ import print_function 17 18import argparse 19import sys 20 21 22def Usage(parser, msg): 23 print('ERROR: ' + msg) 24 parser.print_help() 25 sys.exit(1) 26 27 28def Main(argv): 29 """Generate a script to undo changes done by setup.sh 30 31 The script setup.sh makes a change that needs to be 32 undone, namely it creates a soft link making /build/${board} point 33 to /build/${board}.work. To do this, it had to see if 34 /build/${board} already existed, and if so, whether it was a real 35 tree or a soft link. If it was soft link, it saved the old value 36 of the link, then deleted it and created the new link. If it was 37 a real tree, it renamed the tree to /build/${board}.save, and then 38 created the new soft link. If the /build/${board} did not 39 previously exist, then it just created the new soft link. 40 41 This function takes arguments that tell it exactly what setup.sh 42 actually did, then generates a script to undo those exact changes. 43 """ 44 45 parser = argparse.ArgumentParser() 46 parser.add_argument( 47 '--board', 48 dest='board', 49 required=True, 50 help='Chromeos board for packages/image.') 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 parser.add_argument( 60 '--renamed_tree', 61 dest='renamed_tree', 62 action='store_true', 63 help='Was /build/${BOARD} saved & renamed.', 64 default=False) 65 66 parser.add_argument( 67 '--old_link', 68 dest='old_link', 69 help=('The original build tree soft link.')) 70 71 options = parser.parse_args(argv[1:]) 72 73 if options.old_link or options.renamed_tree: 74 if not options.tree_existed: 75 Usage( 76 parser, 'If --tree_existed is False, cannot have ' 77 '--renamed_tree or --old_link') 78 79 if options.old_link and options.renamed_tree: 80 Usage(parser, '--old_link and --renamed_tree are incompatible options.') 81 82 if options.tree_existed: 83 if not options.old_link and not options.renamed_tree: 84 Usage( 85 parser, 'If --tree_existed is True, then must have either ' 86 '--old_link or --renamed_tree') 87 88 out_filename = 'cros_pkg/' + options.board + '_cleanup.sh' 89 90 with open(out_filename, 'w', encoding='utf-8') as out_file: 91 out_file.write('#!/bin/bash\n\n') 92 # First, remove the 'new' soft link. 93 out_file.write('sudo rm /build/%s\n' % options.board) 94 if options.tree_existed: 95 if options.renamed_tree: 96 # Old build tree existed and was a real tree, so it got 97 # renamed. Move the renamed tree back to the original tree. 98 out_file.write('sudo mv /build/%s.save /build/%s\n' % (options.board, 99 options.board)) 100 else: 101 # Old tree existed and was already a soft link. Re-create the 102 # original soft link. 103 original_link = options.old_link 104 if original_link[0] == "'": 105 original_link = original_link[1:] 106 if original_link[-1] == "'": 107 original_link = original_link[:-1] 108 out_file.write( 109 'sudo ln -s %s /build/%s\n' % (original_link, options.board)) 110 out_file.write('\n') 111 # Remove common.sh file 112 out_file.write('rm common/common.sh\n') 113 114 return 0 115 116 117if __name__ == '__main__': 118 retval = Main(sys.argv) 119 sys.exit(retval) 120