Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: shell/subcmds/clean_cmd.py

Issue 6250058: Split out the big chromite shell 'main.py' into lots of subfiles. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/chromite.git@master
Patch Set: Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 #!/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.
2 #
3 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 """Implementation of the 'clean' chromite command."""
8
9 # Python imports
10 import optparse
11 import os
12 import sys
13
14
15 # Local imports
16 from chromite.lib import cros_build_lib as cros_lib
17 from chromite.shell import utils
18 from chromite.shell import subcmd
19
20
21 def _DoClean(chroot_config, build_config, want_force_yes):
22 """Clean a target.
23
24 Args:
25 chroot_config: A SafeConfigParser representing the config for the chroot.
26 build_config: A SafeConfigParser representing the build config.
27 want_force_yes: If True, we won't ask any questions--we'll just assume
28 that the user really wants to kill the directory. If False, we'll
29 show UI asking the user to confirm.
30 """
31 # We'll need the directory so we can delete stuff; this is a chroot path.
32 board_dir = utils.GetBoardDir(build_config)
33
34 # If not in the chroot, convert board_dir into a non-chroot path...
35 if not cros_lib.IsInsideChroot():
36 chroot_dir = utils.GetChrootAbsDir(chroot_config)
37
38 # We'll need to make the board directory relative to the chroot.
39 assert board_dir.startswith('/'), 'Expected unix-style, absolute path.'
40 board_dir = board_dir.lstrip('/')
41 board_dir = os.path.join(chroot_dir, board_dir)
42
43 if not os.path.isdir(board_dir):
44 cros_lib.Die("Nothing to clean: the board directory doesn't exist.\n %s" %
45 board_dir)
46
47 if not want_force_yes:
48 sys.stderr.write('\n'
49 'Board dir is at: %s\n'
50 'Are you sure you want to delete it (YES/NO)? ' %
51 board_dir)
52 answer = raw_input()
53 if answer.lower() not in ('y', 'ye', 'yes'):
54 cros_lib.Die("You must answer 'yes' if you want to proceed.")
55
56 # Since we're about to do a sudo rm -rf, these are just extra precautions.
57 # This shouldn't be the only place testing these (assert fails are ugly and
58 # can be turned off), but better safe than sorry.
59 # Note that the restriction on '*' is a bit unnecessary, since no shell
60 # expansion should happen. ...but again, I'd rather be safe.
61 assert os.path.isabs(board_dir), 'Board dir better be absolute'
62 assert board_dir != '/', 'Board dir better not be /'
63 assert '*' not in board_dir, 'Board dir better not have any *s'
64 assert build_config.get('BUILD', 'target'), 'Target better not be blank'
65 assert build_config.get('BUILD', 'target') in board_dir, \
66 'Target name better be in board dir'
67
68 argv = ['sudo', '--', 'rm', '-rf', board_dir]
69 cros_lib.RunCommand(argv)
70 cros_lib.Info('Deleted: %s' % board_dir)
71
72
73 def _DoDistClean(chroot_config, want_force_yes):
74 """Remove the whole chroot.
75
76 Args:
77 chroot_config: A SafeConfigParser representing the config for the chroot.
78 want_force_yes: If True, we won't ask any questions--we'll just assume
79 that the user really wants to kill the directory. If False, we'll
80 show UI asking the user to confirm.
81 """
82 if cros_lib.IsInsideChroot():
83 cros_lib.Die('Please exit the chroot before trying to delete it.')
84
85 chroot_dir = utils.GetChrootAbsDir(chroot_config)
86 if not want_force_yes:
87 sys.stderr.write('\n'
88 'Chroot is at: %s\n'
89 'Are you sure you want to delete it (YES/NO)? ' %
90 chroot_dir)
91 answer = raw_input()
92 if answer.lower() not in ('y', 'ye', 'yes'):
93 cros_lib.Die("You must answer 'yes' if you want to proceed.")
94
95 # Can pass argv and not shell=True, since no user flags. :)
96 argv = ['./make_chroot', '--chroot=%s' % chroot_dir, '--delete']
97
98 # We'll put CWD as src/scripts when running the command. Since everyone
99 # running by hand has their cwd there, it is probably the safest.
100 cwd = os.path.join(utils.SRCROOT_PATH, 'src', 'scripts')
101
102 # Run it. Pass any failures upward.
103 cros_lib.RunCommand(argv, cwd=cwd)
104
105
106 class CleanCmd(subcmd.ChromiteCmd):
107 """Clean out built packages for a target; if target=host, deletes chroot."""
108
109 def Run(self, raw_argv, chroot_config=None):
110 """Run the command.
111
112 Args:
113 raw_argv: Command line arguments, including this command's name, but not
114 the chromite command name or chromite options.
115 chroot_config: A SafeConfigParser for the chroot config; or None chromite
116 was called from within the chroot.
117 """
118 # Parse options for command...
119 usage_str = ('usage: %%prog [chromite_options] %s [options] [target]' %
120 raw_argv[0])
121 parser = optparse.OptionParser(usage=usage_str)
122 parser.add_option('-y', '--yes', default=False, action='store_true',
123 help='Answer "YES" to "are you sure?" questions.')
124 (options, argv) = parser.parse_args(raw_argv[1:])
125
126 # Make sure the chroot exists first, before possibly prompting for board...
127 # ...not really required, but nice for the user...
128 if not cros_lib.IsInsideChroot():
129 if not utils.DoesChrootExist(chroot_config):
130 cros_lib.Die("Nothing to clean: the chroot doesn't exist.\n %s" %
131 utils.GetChrootAbsDir(chroot_config))
132
133 # Load the build config...
134 argv, build_config = utils.GetBuildConfigFromArgs(argv)
135 if argv:
136 cros_lib.Die('Unknown arguments: %s' % ' '.join(argv))
137
138 # If they do clean host, we'll delete the whole chroot
139 if build_config is None:
140 _DoDistClean(chroot_config, options.yes)
141 else:
142 _DoClean(chroot_config, build_config, options.yes)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698