| OLD | NEW |
| 1 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 1 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Implementation of the 'clean' chromite command.""" | 5 """Implementation of the 'clean' chromite command.""" |
| 6 | 6 |
| 7 # Python imports | 7 # Python imports |
| 8 import optparse | 8 import optparse |
| 9 import os | 9 import os |
| 10 import sys | 10 import sys |
| 11 | 11 |
| 12 | 12 |
| 13 # Local imports | 13 # Local imports |
| 14 import chromite.lib.cros_build_lib as cros_lib | 14 import chromite.lib.cros_build_lib as cros_lib |
| 15 from chromite.shell import utils | 15 from chromite.shell import utils |
| 16 from chromite.shell import subcmd | 16 from chromite.shell import subcmd |
| 17 | 17 |
| 18 | 18 |
| 19 def _DoClean(cros_env, chroot_config, build_config, want_force_yes): | 19 def _DoClean(chroot_config, build_config, want_force_yes): |
| 20 """Clean a target. | 20 """Clean a target. |
| 21 | 21 |
| 22 Args: | 22 Args: |
| 23 cros_env: Chromite environment to use for this command. | |
| 24 chroot_config: A SafeConfigParser representing the config for the chroot. | 23 chroot_config: A SafeConfigParser representing the config for the chroot. |
| 25 build_config: A SafeConfigParser representing the build config. | 24 build_config: A SafeConfigParser representing the build config. |
| 26 want_force_yes: If True, we won't ask any questions--we'll just assume | 25 want_force_yes: If True, we won't ask any questions--we'll just assume |
| 27 that the user really wants to kill the directory. If False, we'll | 26 that the user really wants to kill the directory. If False, we'll |
| 28 show UI asking the user to confirm. | 27 show UI asking the user to confirm. |
| 29 """ | 28 """ |
| 30 # We'll need the directory so we can delete stuff; this is a chroot path. | 29 # We'll need the directory so we can delete stuff; this is a chroot path. |
| 31 board_dir = utils.GetBoardDir(build_config) | 30 board_dir = utils.GetBoardDir(build_config) |
| 32 | 31 |
| 33 # If not in the chroot, convert board_dir into a non-chroot path... | 32 # If not in the chroot, convert board_dir into a non-chroot path... |
| (...skipping 23 matching lines...) Expand all Loading... |
| 57 # can be turned off), but better safe than sorry. | 56 # can be turned off), but better safe than sorry. |
| 58 # Note that the restriction on '*' is a bit unnecessary, since no shell | 57 # Note that the restriction on '*' is a bit unnecessary, since no shell |
| 59 # expansion should happen. ...but again, I'd rather be safe. | 58 # expansion should happen. ...but again, I'd rather be safe. |
| 60 assert os.path.isabs(board_dir), 'Board dir better be absolute' | 59 assert os.path.isabs(board_dir), 'Board dir better be absolute' |
| 61 assert board_dir != '/', 'Board dir better not be /' | 60 assert board_dir != '/', 'Board dir better not be /' |
| 62 assert '*' not in board_dir, 'Board dir better not have any *s' | 61 assert '*' not in board_dir, 'Board dir better not have any *s' |
| 63 assert build_config.get('DEFAULT', 'target'), 'Target better not be blank' | 62 assert build_config.get('DEFAULT', 'target'), 'Target better not be blank' |
| 64 assert build_config.get('DEFAULT', 'target') in board_dir, \ | 63 assert build_config.get('DEFAULT', 'target') in board_dir, \ |
| 65 'Target name better be in board dir' | 64 'Target name better be in board dir' |
| 66 | 65 |
| 67 arg_list = ['--', 'rm', '-rf', board_dir] | 66 argv = ['sudo', '--', 'rm', '-rf', board_dir] |
| 68 cros_env.RunScript('DELETING: %s' % board_dir, 'sudo', arg_list) | 67 cros_lib.RunCommand(argv) |
| 68 cros_lib.Info('Deleted: %s' % board_dir) |
| 69 | 69 |
| 70 | 70 |
| 71 def _DoDistClean(cros_env, chroot_config, want_force_yes): | 71 def _DoDistClean(chroot_config, want_force_yes): |
| 72 """Remove the whole chroot. | 72 """Remove the whole chroot. |
| 73 | 73 |
| 74 Args: | 74 Args: |
| 75 cros_env: Chromite environment to use for this command. | |
| 76 chroot_config: A SafeConfigParser representing the config for the chroot. | 75 chroot_config: A SafeConfigParser representing the config for the chroot. |
| 77 want_force_yes: If True, we won't ask any questions--we'll just assume | 76 want_force_yes: If True, we won't ask any questions--we'll just assume |
| 78 that the user really wants to kill the directory. If False, we'll | 77 that the user really wants to kill the directory. If False, we'll |
| 79 show UI asking the user to confirm. | 78 show UI asking the user to confirm. |
| 80 """ | 79 """ |
| 81 if cros_lib.IsInsideChroot(): | 80 if cros_lib.IsInsideChroot(): |
| 82 cros_lib.Die('Please exit the chroot before trying to delete it.') | 81 cros_lib.Die('Please exit the chroot before trying to delete it.') |
| 83 | 82 |
| 84 chroot_dir = utils.GetChrootAbsDir(chroot_config) | 83 chroot_dir = utils.GetChrootAbsDir(chroot_config) |
| 85 if not want_force_yes: | 84 if not want_force_yes: |
| 86 sys.stderr.write('\n' | 85 sys.stderr.write('\n' |
| 87 'Chroot is at: %s\n' | 86 'Chroot is at: %s\n' |
| 88 'Are you sure you want to delete it (YES/NO)? ' % | 87 'Are you sure you want to delete it (YES/NO)? ' % |
| 89 chroot_dir) | 88 chroot_dir) |
| 90 answer = raw_input() | 89 answer = raw_input() |
| 91 if answer.lower() not in ('y', 'ye', 'yes'): | 90 if answer.lower() not in ('y', 'ye', 'yes'): |
| 92 cros_lib.Die("You must answer 'yes' if you want to proceed.") | 91 cros_lib.Die("You must answer 'yes' if you want to proceed.") |
| 93 | 92 |
| 94 # Can pass argv and not shell=True, since no user flags. :) | 93 # Can pass argv and not shell=True, since no user flags. :) |
| 95 arg_list = ['--chroot=%s' % chroot_dir, '--delete'] | 94 argv = ['./make_chroot', '--chroot=%s' % chroot_dir, '--delete'] |
| 95 |
| 96 # We'll put CWD as src/scripts when running the command. Since everyone |
| 97 # running by hand has their cwd there, it is probably the safest. |
| 98 cwd = os.path.join(utils.SRCROOT_PATH, 'src', 'scripts') |
| 96 | 99 |
| 97 # Run it. Pass any failures upward. | 100 # Run it. Pass any failures upward. |
| 98 cros_env.RunScript('REMOVING CHROOT', './make_chroot', arg_list, cwd=cwd) | 101 cros_lib.RunCommand(argv, cwd=cwd) |
| 99 | 102 |
| 100 | 103 |
| 101 class CleanCmd(subcmd.ChromiteCmd): | 104 class CleanCmd(subcmd.ChromiteCmd): |
| 102 """Clean out built packages for a target; if target=host, deletes chroot.""" | 105 """Clean out built packages for a target; if target=host, deletes chroot.""" |
| 103 | 106 |
| 104 def Run(self, raw_argv, chroot_config=None): | 107 def Run(self, raw_argv, chroot_config=None): |
| 105 """Run the command. | 108 """Run the command. |
| 106 | 109 |
| 107 Args: | 110 Args: |
| 108 raw_argv: Command line arguments, including this command's name, but not | 111 raw_argv: Command line arguments, including this command's name, but not |
| (...skipping 16 matching lines...) Expand all Loading... |
| 125 cros_lib.Die("Nothing to clean: the chroot doesn't exist.\n %s" % | 128 cros_lib.Die("Nothing to clean: the chroot doesn't exist.\n %s" % |
| 126 utils.GetChrootAbsDir(chroot_config)) | 129 utils.GetChrootAbsDir(chroot_config)) |
| 127 | 130 |
| 128 # Load the build config... | 131 # Load the build config... |
| 129 argv, build_config = utils.GetBuildConfigFromArgs(argv) | 132 argv, build_config = utils.GetBuildConfigFromArgs(argv) |
| 130 if argv: | 133 if argv: |
| 131 cros_lib.Die('Unknown arguments: %s' % ' '.join(argv)) | 134 cros_lib.Die('Unknown arguments: %s' % ' '.join(argv)) |
| 132 | 135 |
| 133 # If they do clean host, we'll delete the whole chroot | 136 # If they do clean host, we'll delete the whole chroot |
| 134 if build_config is None: | 137 if build_config is None: |
| 135 _DoDistClean(self.cros_env, chroot_config, options.yes) | 138 _DoDistClean(chroot_config, options.yes) |
| 136 else: | 139 else: |
| 137 _DoClean(self.cros_env, chroot_config, build_config, options.yes) | 140 _DoClean(chroot_config, build_config, options.yes) |
| OLD | NEW |