Index: shell/subcmds/clean_cmd.py |
diff --git a/shell/subcmds/clean_cmd.py b/shell/subcmds/clean_cmd.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a9831d940988d064343a4b69b2eb708a65121a32 |
--- /dev/null |
+++ b/shell/subcmds/clean_cmd.py |
@@ -0,0 +1,142 @@ |
+#!/usr/bin/python |
sosa
2011/02/02 01:16:19
Only directly shell-callable python should have th
diandersAtChromium
2011/02/02 01:49:10
Done.
|
+# |
+# Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+"""Implementation of the 'clean' chromite command.""" |
+ |
+# Python imports |
+import optparse |
+import os |
+import sys |
+ |
+ |
+# Local imports |
+from chromite.lib import cros_build_lib as cros_lib |
+from chromite.shell import utils |
+from chromite.shell import subcmd |
+ |
+ |
+def _DoClean(chroot_config, build_config, want_force_yes): |
+ """Clean a target. |
+ |
+ Args: |
+ chroot_config: A SafeConfigParser representing the config for the chroot. |
+ build_config: A SafeConfigParser representing the build config. |
+ want_force_yes: If True, we won't ask any questions--we'll just assume |
+ that the user really wants to kill the directory. If False, we'll |
+ show UI asking the user to confirm. |
+ """ |
+ # We'll need the directory so we can delete stuff; this is a chroot path. |
+ board_dir = utils.GetBoardDir(build_config) |
+ |
+ # If not in the chroot, convert board_dir into a non-chroot path... |
+ if not cros_lib.IsInsideChroot(): |
+ chroot_dir = utils.GetChrootAbsDir(chroot_config) |
+ |
+ # We'll need to make the board directory relative to the chroot. |
+ assert board_dir.startswith('/'), 'Expected unix-style, absolute path.' |
+ board_dir = board_dir.lstrip('/') |
+ board_dir = os.path.join(chroot_dir, board_dir) |
+ |
+ if not os.path.isdir(board_dir): |
+ cros_lib.Die("Nothing to clean: the board directory doesn't exist.\n %s" % |
+ board_dir) |
+ |
+ if not want_force_yes: |
+ sys.stderr.write('\n' |
+ 'Board dir is at: %s\n' |
+ 'Are you sure you want to delete it (YES/NO)? ' % |
+ board_dir) |
+ answer = raw_input() |
+ if answer.lower() not in ('y', 'ye', 'yes'): |
+ cros_lib.Die("You must answer 'yes' if you want to proceed.") |
+ |
+ # Since we're about to do a sudo rm -rf, these are just extra precautions. |
+ # This shouldn't be the only place testing these (assert fails are ugly and |
+ # can be turned off), but better safe than sorry. |
+ # Note that the restriction on '*' is a bit unnecessary, since no shell |
+ # expansion should happen. ...but again, I'd rather be safe. |
+ assert os.path.isabs(board_dir), 'Board dir better be absolute' |
+ assert board_dir != '/', 'Board dir better not be /' |
+ assert '*' not in board_dir, 'Board dir better not have any *s' |
+ assert build_config.get('BUILD', 'target'), 'Target better not be blank' |
+ assert build_config.get('BUILD', 'target') in board_dir, \ |
+ 'Target name better be in board dir' |
+ |
+ argv = ['sudo', '--', 'rm', '-rf', board_dir] |
+ cros_lib.RunCommand(argv) |
+ cros_lib.Info('Deleted: %s' % board_dir) |
+ |
+ |
+def _DoDistClean(chroot_config, want_force_yes): |
+ """Remove the whole chroot. |
+ |
+ Args: |
+ chroot_config: A SafeConfigParser representing the config for the chroot. |
+ want_force_yes: If True, we won't ask any questions--we'll just assume |
+ that the user really wants to kill the directory. If False, we'll |
+ show UI asking the user to confirm. |
+ """ |
+ if cros_lib.IsInsideChroot(): |
+ cros_lib.Die('Please exit the chroot before trying to delete it.') |
+ |
+ chroot_dir = utils.GetChrootAbsDir(chroot_config) |
+ if not want_force_yes: |
+ sys.stderr.write('\n' |
+ 'Chroot is at: %s\n' |
+ 'Are you sure you want to delete it (YES/NO)? ' % |
+ chroot_dir) |
+ answer = raw_input() |
+ if answer.lower() not in ('y', 'ye', 'yes'): |
+ cros_lib.Die("You must answer 'yes' if you want to proceed.") |
+ |
+ # Can pass argv and not shell=True, since no user flags. :) |
+ argv = ['./make_chroot', '--chroot=%s' % chroot_dir, '--delete'] |
+ |
+ # We'll put CWD as src/scripts when running the command. Since everyone |
+ # running by hand has their cwd there, it is probably the safest. |
+ cwd = os.path.join(utils.SRCROOT_PATH, 'src', 'scripts') |
+ |
+ # Run it. Pass any failures upward. |
+ cros_lib.RunCommand(argv, cwd=cwd) |
+ |
+ |
+class CleanCmd(subcmd.ChromiteCmd): |
+ """Clean out built packages for a target; if target=host, deletes chroot.""" |
+ |
+ def Run(self, raw_argv, chroot_config=None): |
+ """Run the command. |
+ |
+ Args: |
+ raw_argv: Command line arguments, including this command's name, but not |
+ the chromite command name or chromite options. |
+ chroot_config: A SafeConfigParser for the chroot config; or None chromite |
+ was called from within the chroot. |
+ """ |
+ # Parse options for command... |
+ usage_str = ('usage: %%prog [chromite_options] %s [options] [target]' % |
+ raw_argv[0]) |
+ parser = optparse.OptionParser(usage=usage_str) |
+ parser.add_option('-y', '--yes', default=False, action='store_true', |
+ help='Answer "YES" to "are you sure?" questions.') |
+ (options, argv) = parser.parse_args(raw_argv[1:]) |
+ |
+ # Make sure the chroot exists first, before possibly prompting for board... |
+ # ...not really required, but nice for the user... |
+ if not cros_lib.IsInsideChroot(): |
+ if not utils.DoesChrootExist(chroot_config): |
+ cros_lib.Die("Nothing to clean: the chroot doesn't exist.\n %s" % |
+ utils.GetChrootAbsDir(chroot_config)) |
+ |
+ # Load the build config... |
+ argv, build_config = utils.GetBuildConfigFromArgs(argv) |
+ if argv: |
+ cros_lib.Die('Unknown arguments: %s' % ' '.join(argv)) |
+ |
+ # If they do clean host, we'll delete the whole chroot |
+ if build_config is None: |
+ _DoDistClean(chroot_config, options.yes) |
+ else: |
+ _DoClean(chroot_config, build_config, options.yes) |