blob: fc479c927ae81620e01862f8b9ffb41efa4cd47f [file] [log] [blame]
#!/usr/bin/python2
#
# Copyright 2015 Google Inc. All Rights Reserved
"""The script to generate a cleanup script after cros_pkg_setup.sh.
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.
"""
from __future__ import print_function
import argparse
import sys
def Usage(parser, msg):
print('ERROR: ' + msg)
parser.print_help()
sys.exit(1)
def Main(argv):
"""Generate a script to undo changes done by cros_pkg_setup.sh
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)