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