Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 'build' chromite command.""" | |
| 8 | |
| 9 # Python imports | |
| 10 import optparse | |
| 11 import os | |
| 12 | |
| 13 | |
| 14 # Local imports | |
| 15 from chromite.lib import cros_build_lib as cros_lib | |
| 16 from chromite.shell import utils | |
| 17 from chromite.shell import subcmd | |
| 18 | |
| 19 | |
| 20 def _DoMakeChroot(chroot_config, clean_first): | |
| 21 """Build the chroot, if needed. | |
| 22 | |
| 23 Args: | |
| 24 chroot_config: A SafeConfigParser representing the config for the chroot. | |
| 25 clean_first: Delete any old chroot first. | |
| 26 """ | |
| 27 # Skip this whole command if things already exist. | |
| 28 # TODO(dianders): Theoretically, calling make_chroot a second time is OK | |
| 29 # and "fixes up" the chroot. ...but build_packages will do the fixups | |
| 30 # anyway (I think), so this isn't that important. | |
| 31 chroot_dir = utils.GetChrootAbsDir(chroot_config) | |
| 32 if (not clean_first) and utils.DoesChrootExist(chroot_config): | |
| 33 cros_lib.Info('%s already exists, skipping make_chroot.' % chroot_dir) | |
| 34 return | |
| 35 | |
| 36 cros_lib.Info('MAKING THE CHROOT') | |
| 37 | |
| 38 # Put together command. | |
| 39 cmd_list = [ | |
| 40 './make_chroot', | |
| 41 '--chroot="%s"' % chroot_dir, | |
| 42 chroot_config.get('CHROOT', 'make_chroot_flags'), | |
| 43 ] | |
| 44 if clean_first: | |
| 45 cmd_list.insert(1, '--replace') | |
| 46 | |
| 47 # We're going convert to a string and force the shell to do all of the | |
| 48 # splitting of arguments, since we're throwing all of the flags from the | |
| 49 # config file in there. | |
| 50 cmd = ' '.join(cmd_list) | |
| 51 | |
| 52 # We'll put CWD as src/scripts when running the command. Since everyone | |
| 53 # running by hand has their cwd there, it is probably the safest. | |
| 54 cwd = os.path.join(utils.SRCROOT_PATH, 'src', 'scripts') | |
| 55 | |
| 56 # Run it. Exceptions will cause the program to exit. | |
| 57 cros_lib.RunCommand(cmd, shell=True, cwd=cwd, ignore_sigint=True) | |
| 58 | |
| 59 | |
| 60 def _DoSetupBoard(build_config, clean_first): | |
| 61 """Setup the board, if needed. | |
| 62 | |
| 63 This just runs the setup_board command with the proper args, if needed. | |
| 64 | |
| 65 Args: | |
| 66 build_config: A SafeConfigParser representing the build config. | |
| 67 clean_first: Delete any old board config first. | |
| 68 """ | |
| 69 # Skip this whole command if things already exist. | |
| 70 board_dir = utils.GetBoardDir(build_config) | |
| 71 if (not clean_first) and os.path.isdir(board_dir): | |
| 72 cros_lib.Info('%s already exists, skipping setup_board.' % board_dir) | |
| 73 return | |
| 74 | |
| 75 cros_lib.Info('SETTING UP THE BOARD') | |
| 76 | |
| 77 # Put together command. | |
| 78 cmd_list = [ | |
| 79 './setup_board', | |
| 80 '--board="%s"' % build_config.get('BUILD', 'target'), | |
| 81 build_config.get('BUILD', 'setup_board_flags'), | |
| 82 ] | |
| 83 if clean_first: | |
| 84 cmd_list.insert(1, '--force') | |
| 85 | |
| 86 # We're going convert to a string and force the shell to do all of the | |
| 87 # splitting of arguments, since we're throwing all of the flags from the | |
| 88 # config file in there. | |
| 89 cmd = ' '.join(cmd_list) | |
| 90 | |
| 91 # We'll put CWD as src/scripts when running the command. Since everyone | |
| 92 # running by hand has their cwd there, it is probably the safest. | |
| 93 cwd = os.path.join(utils.SRCROOT_PATH, 'src', 'scripts') | |
| 94 | |
| 95 # Run it. Exceptions will cause the program to exit. | |
| 96 cros_lib.RunCommand(cmd, shell=True, cwd=cwd, ignore_sigint=True) | |
| 97 | |
| 98 | |
| 99 def _DoBuildPackages(build_config): | |
| 100 """Build packages. | |
| 101 | |
| 102 This just runs the build_packages command with the proper args. | |
| 103 | |
| 104 Args: | |
| 105 build_config: A SafeConfigParser representing the build config. | |
| 106 """ | |
| 107 cros_lib.Info('BUILDING PACKAGES') | |
| 108 | |
| 109 # Put together command. We're going to force the shell to do all of the | |
| 110 # splitting of arguments, since we're throwing all of the flags from the | |
| 111 # config file in there. | |
| 112 cmd = './build_packages --board="%s" %s' % ( | |
| 113 build_config.get('BUILD', 'target'), | |
| 114 build_config.get('BUILD', 'build_packages_flags') | |
| 115 ) | |
| 116 | |
| 117 # We'll put CWD as src/scripts when running the command. Since everyone | |
| 118 # running by hand has their cwd there, it is probably the safest. | |
| 119 cwd = os.path.join(utils.SRCROOT_PATH, 'src', 'scripts') | |
| 120 | |
| 121 # Run it. Exceptions will cause the program to exit. | |
| 122 cros_lib.RunCommand(cmd, shell=True, cwd=cwd, ignore_sigint=True) | |
| 123 | |
| 124 | |
| 125 def _DoBuildImage(build_config): | |
| 126 """Build an image. | |
| 127 | |
| 128 This just runs the build_image command with the proper args. | |
| 129 | |
| 130 Args: | |
| 131 build_config: A SafeConfigParser representing the build config. | |
| 132 """ | |
| 133 cros_lib.Info('BUILDING THE IMAGE') | |
| 134 | |
| 135 # Put together command. We're going to force the shell to do all of the | |
| 136 # splitting of arguments, since we're throwing all of the flags from the | |
| 137 # config file in there. | |
| 138 cmd = './build_image --board="%s" %s' % ( | |
| 139 build_config.get('BUILD', 'target'), | |
| 140 build_config.get('IMAGE', 'build_image_flags') | |
| 141 ) | |
| 142 | |
| 143 # We'll put CWD as src/scripts when running the command. Since everyone | |
| 144 # running by hand has their cwd there, it is probably the safest. | |
| 145 cwd = os.path.join(utils.SRCROOT_PATH, 'src', 'scripts') | |
| 146 | |
| 147 # Run it. Exceptions will cause the program to exit. | |
| 148 cros_lib.RunCommand(cmd, shell=True, cwd=cwd, ignore_sigint=True) | |
| 149 | |
| 150 | |
| 151 class BuildCmd(subcmd.ChromiteCmd): | |
| 152 """Build the chroot (if needed), the packages for a target, and the image.""" | |
| 153 | |
| 154 def Run(self, raw_argv, chroot_config=None, | |
| 155 loaded_config=False, build_config=None): | |
| 156 """Run the command. | |
| 157 | |
| 158 Args: | |
| 159 raw_argv: Command line arguments, including this command's name, but not | |
| 160 the chromite command name or chromite options. | |
| 161 chroot_config: A SafeConfigParser for the chroot config; or None chromite | |
| 162 was called from within the chroot. | |
| 163 loaded_config: If True, we've already loaded the config. | |
| 164 build_config: None when called normally, but contains the SafeConfigParser | |
| 165 for the build config if we call ourselves with _DoEnterChroot(). Note | |
| 166 that even when called through _DoEnterChroot(), could still be None | |
| 167 if user chose 'HOST' as the target. | |
| 168 """ | |
| 169 # Parse options for command... | |
| 170 usage_str = ('usage: %%prog [chromite_options] %s [options] [target]' % | |
| 171 raw_argv[0]) | |
| 172 parser = optparse.OptionParser(usage=usage_str) | |
| 173 parser.add_option('--clean', default=False, action='store_true', | |
| 174 help='Clean before building.') | |
| 175 (options, argv) = parser.parse_args(raw_argv[1:]) | |
| 176 | |
| 177 # Load the build config if needed... | |
| 178 if not loaded_config: | |
| 179 argv, build_config = utils.GetBuildConfigFromArgs(argv) | |
| 180 if argv: | |
| 181 cros_lib.Die('Unknown arguments: %s' % ' '.join(argv)) | |
| 182 | |
| 183 if not cros_lib.IsInsideChroot(): | |
| 184 # Note: we only want to clean the chroot if they do --clean and have the | |
| 185 # host target. If they do --clean and have a board target, it means | |
| 186 # that they just want to clean the board... | |
| 187 want_clean_chroot = options.clean and build_config is None | |
| 188 | |
| 189 _DoMakeChroot(chroot_config, want_clean_chroot) | |
| 190 | |
| 191 if build_config is not None: | |
| 192 utils.EnterChroot(chroot_config, (self, 'Run'), raw_argv, | |
| 193 build_config=build_config, loaded_config=True) | |
| 194 | |
| 195 cros_lib.Info('Done building.') | |
| 196 else: | |
| 197 if build_config is None: | |
| 198 cros_lib.Die("You can't build the host chroot from within the chroot.") | |
| 199 | |
| 200 _DoSetupBoard(build_config, options.clean) | |
| 201 _DoBuildPackages(build_config) | |
| 202 _DoBuildImage(build_config) | |
| OLD | NEW |