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