aboutsummaryrefslogtreecommitdiff
path: root/binary_search_tool/cros_pkg/cros_pkg_create_cleanup_script.py
blob: 640ca658830c7e71ffca0683154f422244879b95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/python2
#
#  Copyright 2015 Google Inc. All Rights Reserved
#
#  This script takes a set of flags, telling it what cros_pkg_setup.sh changed
#  during the set up process.  Based on the values of the input flags, it
#  generates a cleanup script, named cros_pkg_${BOARD}_cleanup.sh, which will
#  undo the changes made by cros_pkg_setup.sh, returning everything to its
#  original state.
#

import argparse
import sys


def Usage(parser, msg):
  print 'ERROR: ' + msg
  parser.print_help()
  sys.exit(1)


def Main(argv):
  """
    The script cros_pkg_setup.sh make two main changes that need to be
    undone: 1).  It creates a soft link making /build/${board} point to
    /build/${board}.work, and 2). It saves a copy of the build_image
    script, then updates build_image to not execute 'eclean'.  For the first
    change, it had to see if /build/${board} already existed, and if so, whether
    it was a real tree or a soft link.  If it was soft link, it saved the old
    value of the link, then deleted it and created the new link.  If it was a
    real tree, it renamed the tree to /build/${board}.save, and then created the
    new soft link.  If the /build/${board} did not previously exist, then it
    just
    created the new soft link.

    This function takes arguments that tell it exactly what cros_pkg_setup.sh
    actually did, then generates a script to undo those exact changes.
    """

  parser = argparse.ArgumentParser()
  parser.add_argument('--board',
                      dest='board',
                      required=True,
                      help='Chromeos board for packages/image.')

  parser.add_argument('--old_tree_missing',
                      dest='tree_existed',
                      action='store_false',
                      help='Did /build/${BOARD} exist.',
                      default=True)

  parser.add_argument('--renamed_tree',
                      dest='renamed_tree',
                      action='store_true',
                      help='Was /build/${BOARD} saved & renamed.',
                      default=False)

  parser.add_argument('--old_link',
                      dest='old_link',
                      help=('The original build tree soft link.'))

  options = parser.parse_args(argv[1:])

  if options.old_link or options.renamed_tree:
    if not options.tree_existed:
      Usage(parser, 'If --tree_existed is False, cannot have '
            '--renamed_tree or --old_link')

  if options.old_link and options.renamed_tree:
    Usage(parser, '--old_link and --renamed_tree are incompatible options.')

  if options.tree_existed:
    if not options.old_link and not options.renamed_tree:
      Usage(parser, 'If --tree_existed is True, then must have either '
            '--old_link or --renamed_tree')

  out_filename = 'cros_pkg_' + options.board + '_cleanup.sh'

  with open(out_filename, 'w') as out_file:
    out_file.write('#!/bin/bash\n\n')
    # First, remove the 'new' soft link.
    out_file.write('sudo rm /build/%s\n' % options.board)
    if options.tree_existed:
      if options.renamed_tree:
        # Old build tree existed and was a real tree, so it got
        # renamed.  Move the renamed tree back to the original tree.
        out_file.write('sudo mv /build/%s.save /build/%s\n' %
                       (options.board, options.board))
      else:
        # Old tree existed and was already a soft link.  Re-create the
        # original soft link.
        original_link = options.old_link
        if original_link[0] == "'":
          original_link = original_link[1:]
        if original_link[-1] == "'":
          original_link = original_link[:-1]
        out_file.write('sudo ln -s %s /build/%s\n' % (original_link,
                                                      options.board))
    out_file.write('\n')
    # Restore the original saved version of build_image script.
    out_file.write('mv ~/trunk/src/scripts/build_image.save '
                   '~/trunk/src/scripts/build_image\n\n')
    # Remove cros_pkg_common.sh file
    out_file.write('rm cros_pkg_common.sh\n')

  return 0


if __name__ == '__main__':
  retval = Main(sys.argv)
  sys.exit(retval)