• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
6 This script takes a set of flags, telling it what setup.sh changed
7 during the set up process. Based on the values of the input flags, it
8 generates a cleanup script, named ${BOARD}_cleanup.sh, which will
9 undo the changes made by setup.sh, returning everything to its
10 original state.
11 """
12 
13 from __future__ import print_function
14 
15 import argparse
16 import sys
17 
18 
19 def Usage(parser, msg):
20   print('ERROR: ' + msg)
21   parser.print_help()
22   sys.exit(1)
23 
24 
25 def 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(
44       '--board',
45       dest='board',
46       required=True,
47       help='Chromeos board for packages/image.')
48 
49   parser.add_argument(
50       '--old_tree_missing',
51       dest='tree_existed',
52       action='store_false',
53       help='Did /build/${BOARD} exist.',
54       default=True)
55 
56   parser.add_argument(
57       '--renamed_tree',
58       dest='renamed_tree',
59       action='store_true',
60       help='Was /build/${BOARD} saved & renamed.',
61       default=False)
62 
63   parser.add_argument(
64       '--old_link',
65       dest='old_link',
66       help=('The original build tree soft link.'))
67 
68   options = parser.parse_args(argv[1:])
69 
70   if options.old_link or options.renamed_tree:
71     if not options.tree_existed:
72       Usage(parser, 'If --tree_existed is False, cannot have '
73             '--renamed_tree or --old_link')
74 
75   if options.old_link and options.renamed_tree:
76     Usage(parser, '--old_link and --renamed_tree are incompatible options.')
77 
78   if options.tree_existed:
79     if not options.old_link and not options.renamed_tree:
80       Usage(parser, 'If --tree_existed is True, then must have either '
81             '--old_link or --renamed_tree')
82 
83   out_filename = 'cros_pkg/' + options.board + '_cleanup.sh'
84 
85   with open(out_filename, 'w') as out_file:
86     out_file.write('#!/bin/bash\n\n')
87     # First, remove the 'new' soft link.
88     out_file.write('sudo rm /build/%s\n' % options.board)
89     if options.tree_existed:
90       if options.renamed_tree:
91         # Old build tree existed and was a real tree, so it got
92         # renamed.  Move the renamed tree back to the original tree.
93         out_file.write('sudo mv /build/%s.save /build/%s\n' %
94                        (options.board, options.board))
95       else:
96         # Old tree existed and was already a soft link.  Re-create the
97         # original soft link.
98         original_link = options.old_link
99         if original_link[0] == "'":
100           original_link = original_link[1:]
101         if original_link[-1] == "'":
102           original_link = original_link[:-1]
103         out_file.write('sudo ln -s %s /build/%s\n' % (original_link,
104                                                       options.board))
105     out_file.write('\n')
106     # Remove common.sh file
107     out_file.write('rm common/common.sh\n')
108 
109   return 0
110 
111 
112 if __name__ == '__main__':
113   retval = Main(sys.argv)
114   sys.exit(retval)
115