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

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: Incorporated davidjames and sosa feedback. 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
« no previous file with comments | « shell/subcmds/build_cmd.py ('k') | shell/subcmds/portage_cmds.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 # found in the LICENSE file.
4
5 """Implementation of the 'clean' chromite command."""
6
7 # Python imports
8 import optparse
9 import os
10 import sys
11
12
13 # Local imports
14 import chromite.lib.cros_build_lib as cros_lib
15 from chromite.shell import utils
16 from chromite.shell import subcmd
17
18
19 def _DoClean(chroot_config, build_config, want_force_yes):
20 """Clean a target.
21
22 Args:
23 chroot_config: A SafeConfigParser representing the config for the chroot.
24 build_config: A SafeConfigParser representing the build config.
25 want_force_yes: If True, we won't ask any questions--we'll just assume
26 that the user really wants to kill the directory. If False, we'll
27 show UI asking the user to confirm.
28 """
29 # We'll need the directory so we can delete stuff; this is a chroot path.
30 board_dir = utils.GetBoardDir(build_config)
31
32 # If not in the chroot, convert board_dir into a non-chroot path...
33 if not cros_lib.IsInsideChroot():
34 chroot_dir = utils.GetChrootAbsDir(chroot_config)
35
36 # We'll need to make the board directory relative to the chroot.
37 assert board_dir.startswith('/'), 'Expected unix-style, absolute path.'
38 board_dir = board_dir.lstrip('/')
39 board_dir = os.path.join(chroot_dir, board_dir)
40
41 if not os.path.isdir(board_dir):
42 cros_lib.Die("Nothing to clean: the board directory doesn't exist.\n %s" %
43 board_dir)
44
45 if not want_force_yes:
46 sys.stderr.write('\n'
47 'Board dir is at: %s\n'
48 'Are you sure you want to delete it (YES/NO)? ' %
49 board_dir)
50 answer = raw_input()
51 if answer.lower() not in ('y', 'ye', 'yes'):
52 cros_lib.Die("You must answer 'yes' if you want to proceed.")
53
54 # Since we're about to do a sudo rm -rf, these are just extra precautions.
55 # This shouldn't be the only place testing these (assert fails are ugly and
56 # can be turned off), but better safe than sorry.
57 # Note that the restriction on '*' is a bit unnecessary, since no shell
58 # expansion should happen. ...but again, I'd rather be safe.
59 assert os.path.isabs(board_dir), 'Board dir better be absolute'
60 assert board_dir != '/', 'Board dir better not be /'
61 assert '*' not in board_dir, 'Board dir better not have any *s'
62 assert build_config.get('BUILD', 'target'), 'Target better not be blank'
63 assert build_config.get('BUILD', 'target') in board_dir, \
64 'Target name better be in board dir'
65
66 argv = ['sudo', '--', 'rm', '-rf', board_dir]
67 cros_lib.RunCommand(argv)
68 cros_lib.Info('Deleted: %s' % board_dir)
69
70
71 def _DoDistClean(chroot_config, want_force_yes):
72 """Remove the whole chroot.
73
74 Args:
75 chroot_config: A SafeConfigParser representing the config for the chroot.
76 want_force_yes: If True, we won't ask any questions--we'll just assume
77 that the user really wants to kill the directory. If False, we'll
78 show UI asking the user to confirm.
79 """
80 if cros_lib.IsInsideChroot():
81 cros_lib.Die('Please exit the chroot before trying to delete it.')
82
83 chroot_dir = utils.GetChrootAbsDir(chroot_config)
84 if not want_force_yes:
85 sys.stderr.write('\n'
86 'Chroot is at: %s\n'
87 'Are you sure you want to delete it (YES/NO)? ' %
88 chroot_dir)
89 answer = raw_input()
90 if answer.lower() not in ('y', 'ye', 'yes'):
91 cros_lib.Die("You must answer 'yes' if you want to proceed.")
92
93 # Can pass argv and not shell=True, since no user flags. :)
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')
99
100 # Run it. Pass any failures upward.
101 cros_lib.RunCommand(argv, cwd=cwd)
102
103
104 class CleanCmd(subcmd.ChromiteCmd):
105 """Clean out built packages for a target; if target=host, deletes chroot."""
106
107 def Run(self, raw_argv, chroot_config=None):
108 """Run the command.
109
110 Args:
111 raw_argv: Command line arguments, including this command's name, but not
112 the chromite command name or chromite options.
113 chroot_config: A SafeConfigParser for the chroot config; or None chromite
114 was called from within the chroot.
115 """
116 # Parse options for command...
117 usage_str = ('usage: %%prog [chromite_options] %s [options] [target]' %
118 raw_argv[0])
119 parser = optparse.OptionParser(usage=usage_str)
120 parser.add_option('-y', '--yes', default=False, action='store_true',
121 help='Answer "YES" to "are you sure?" questions.')
122 (options, argv) = parser.parse_args(raw_argv[1:])
123
124 # Make sure the chroot exists first, before possibly prompting for board...
125 # ...not really required, but nice for the user...
126 if not cros_lib.IsInsideChroot():
127 if not utils.DoesChrootExist(chroot_config):
128 cros_lib.Die("Nothing to clean: the chroot doesn't exist.\n %s" %
129 utils.GetChrootAbsDir(chroot_config))
130
131 # Load the build config...
132 argv, build_config = utils.GetBuildConfigFromArgs(argv)
133 if argv:
134 cros_lib.Die('Unknown arguments: %s' % ' '.join(argv))
135
136 # If they do clean host, we'll delete the whole chroot
137 if build_config is None:
138 _DoDistClean(chroot_config, options.yes)
139 else:
140 _DoClean(chroot_config, build_config, options.yes)
OLDNEW
« no previous file with comments | « shell/subcmds/build_cmd.py ('k') | shell/subcmds/portage_cmds.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698