blob: 5faa719df7e6fad3786249d60c3939ef9d6370e6 [file] [log] [blame]
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +08001#!/usr/bin/python -Bu
2#
3# Copyright (c) 2014 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"""Factory toolkit installer.
8
9The factory toolkit is a self-extracting shellball containing factory test
10related files and this installer. This installer is invoked when the toolkit
11is deployed and is responsible for installing files.
12"""
13
14
15import argparse
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +080016from contextlib import contextmanager
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +080017import os
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +080018import sys
Jon Salz4f3ade52014-02-20 17:55:09 +080019import tempfile
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +080020
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +080021import factory_common # pylint: disable=W0611
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +080022from cros.factory.test import factory
Vic Yang196d5242014-08-05 13:51:35 +080023from cros.factory.test import utils
Jon Salz25590302014-07-11 16:07:20 +080024from cros.factory.tools import install_symlinks
Rong Chang6d65fad2014-08-22 16:00:12 +080025from cros.factory.utils import file_utils
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +080026from cros.factory.utils.process_utils import Spawn
Hung-Te Lincdc2a042014-08-27 13:05:16 +080027from cros.factory.utils.sys_utils import MountPartition
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +080028
29
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +080030INSTALLER_PATH = 'usr/local/factory/py/toolkit/installer.py'
Rong Chang6d65fad2014-08-22 16:00:12 +080031MAKESELF_SHELL = '/bin/sh'
32TOOLKIT_NAME = 'install_factory_toolkit.run'
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +080033
Jon Salz4f3ade52014-02-20 17:55:09 +080034# Short and sweet help header for the executable generated by makeself.
35HELP_HEADER = """
36Installs the factory toolkit, transforming a test image into a factory test
37image. You can:
38
39- Install the factory toolkit on a CrOS device that is running a test
40 image. To do this, copy install_factory_toolkit.run to the device and
41 run it. The factory tests will then come up on the next boot.
42
43 rsync -a install_factory_toolkit.run crosdevice:/tmp
44 ssh crosdevice '/tmp/install_factory_toolkit.run && sync && reboot'
45
46- Modify a test image, turning it into a factory test image. When you
47 use the image on a device, the factory tests will come up.
48
49 install_factory_toolkit.run chromiumos_test_image.bin
50"""
51
52HELP_HEADER_ADVANCED = """
53- (advanced) Modify a mounted stateful partition, turning it into a factory
54 test image. This is equivalent to the previous command:
55
56 mount_partition -rw chromiumos_test_image.bin 1 /mnt/stateful
57 install_factory_toolkit.run /mnt/stateful
58 umount /mnt/stateful
59
60- (advanced) Unpack the factory toolkit, modify a file, and then repack it.
61
62 # Unpack but don't actually install
63 install_factory_toolkit.run --target /tmp/toolkit --noexec
64 # Edit some files in /tmp/toolkit
65 emacs /tmp/toolkit/whatever
66 # Repack
67 install_factory_toolkit.run -- --repack /tmp/toolkit \\
68 --pack-into /path/to/new/install_factory_toolkit.run
69"""
70
71# The makeself-generated header comes next. This is a little confusing,
72# so explain.
73HELP_HEADER_MAKESELF = """
74For complete usage information and advanced operations, run
75"install_factory_toolkit.run -- --help" (note the extra "--").
76
77Following is the help message from makeself, which was used to create
78this self-extracting archive.
79
80-----
81"""
82
Vic Yang196d5242014-08-05 13:51:35 +080083# The method to determine whether running on Chrome OS device or not.
84# Override this for unit testing.
85_in_cros_device = utils.in_cros_device
86
Vic Yangdb1e20e2014-10-05 12:10:33 +080087SERVER_FILE_MASK = [
88 # Exclude Umpire server but keep Umpire client
89 '--include', 'py/umpire/__init__.*',
90 '--include', 'py/umpire/common.*',
91 '--include', 'py/umpire/client',
92 '--include', 'py/umpire/client/**',
93 '--exclude', 'py/umpire/**',
94
95 # Lumberjack is only used on Umpire server
96 '--exclude', 'py/lumberjack'
97]
98
Jon Salz4f3ade52014-02-20 17:55:09 +080099
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800100class FactoryToolkitInstaller():
101 """Factory toolkit installer.
102
103 Args:
104 src: Source path containing usr/ and var/.
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800105 dest: Installation destination path. Set this to the mount point of the
106 stateful partition if patching a test image.
107 no_enable: True to not install the tag file.
108 system_root: The path to the root of the file system. This must be left
109 as its default value except for unit testing.
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800110 """
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800111
Jon Salzb7e44262014-05-07 15:53:37 +0800112 # Whether to sudo when rsyncing; set to False for testing.
113 _sudo = True
114
Peter Ammon948b7172014-07-15 12:43:06 -0700115 def __init__(self, src, dest, no_enable, enable_presenter,
Vic Yang7039f422014-07-07 15:38:13 -0700116 enable_device, system_root='/'):
Jon Salz4f3ade52014-02-20 17:55:09 +0800117 self._src = src
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800118 self._system_root = system_root
119 if dest == self._system_root:
120 self._usr_local_dest = os.path.join(dest, 'usr', 'local')
121 self._var_dest = os.path.join(dest, 'var')
Jon Salz4f3ade52014-02-20 17:55:09 +0800122
123 # Make sure we're on a CrOS device.
Vic Yang196d5242014-08-05 13:51:35 +0800124 if not _in_cros_device():
Jon Salz4f3ade52014-02-20 17:55:09 +0800125 sys.stderr.write(
Vic Yang196d5242014-08-05 13:51:35 +0800126 "ERROR: You're not on a CrOS device (for more details, please\n"
127 "check utils.py:in_cros_device), so you must specify a test\n"
128 "image or a mounted stateful partition on which to install the\n"
129 "factory toolkit. Please run\n"
Jon Salz4f3ade52014-02-20 17:55:09 +0800130 "\n"
131 " install_factory_toolkit.run -- --help\n"
132 "\n"
133 "for help.\n")
134 sys.exit(1)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800135 if os.getuid() != 0:
Jon Salz4f3ade52014-02-20 17:55:09 +0800136 raise Exception('You must be root to install the factory toolkit on a '
137 'CrOS device.')
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800138 else:
139 self._usr_local_dest = os.path.join(dest, 'dev_image')
140 self._var_dest = os.path.join(dest, 'var_overlay')
141 if (not os.path.exists(self._usr_local_dest) or
142 not os.path.exists(self._var_dest)):
143 raise Exception(
144 'The destination path %s is not a stateful partition!' % dest)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800145
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800146 self._dest = dest
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800147 self._usr_local_src = os.path.join(src, 'usr', 'local')
148 self._var_src = os.path.join(src, 'var')
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800149 self._no_enable = no_enable
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800150 self._tag_file = os.path.join(self._usr_local_dest, 'factory', 'enabled')
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800151
Peter Ammon948b7172014-07-15 12:43:06 -0700152 self._enable_presenter = enable_presenter
153 self._presenter_tag_file = os.path.join(self._usr_local_dest, 'factory',
154 'init', 'run_goofy_presenter')
Vic Yang7039f422014-07-07 15:38:13 -0700155
156 self._enable_device = enable_device
Ricky Liangd7716912014-07-10 11:52:24 +0800157 self._device_tag_file = os.path.join(self._usr_local_dest, 'factory',
158 'init', 'run_goofy_device')
Vic Yang7039f422014-07-07 15:38:13 -0700159
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800160 if (not os.path.exists(self._usr_local_src) or
161 not os.path.exists(self._var_src)):
162 raise Exception(
163 'This installer must be run from within the factory toolkit!')
164
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800165 def WarningMessage(self, target_test_image=None):
Jon Salz4f3ade52014-02-20 17:55:09 +0800166 with open(os.path.join(self._src, 'VERSION')) as f:
167 ret = f.read()
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800168 if target_test_image:
Jon Salz4f3ade52014-02-20 17:55:09 +0800169 ret += (
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800170 '\n'
171 '\n'
Jon Salz4f3ade52014-02-20 17:55:09 +0800172 '*** You are about to patch the factory toolkit into:\n'
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800173 '*** %s\n'
174 '***' % target_test_image)
175 else:
Jon Salz4f3ade52014-02-20 17:55:09 +0800176 ret += (
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800177 '\n'
178 '\n'
Jon Salz4f3ade52014-02-20 17:55:09 +0800179 '*** You are about to install the factory toolkit to:\n'
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800180 '*** %s\n'
181 '***' % self._dest)
182 if self._dest == self._system_root:
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800183 if self._no_enable:
184 ret += ('\n'
185 '*** Factory tests will be disabled after this process is done, but\n'
Jon Salz4f3ade52014-02-20 17:55:09 +0800186 '*** you can enable them by creating the factory enabled tag:\n'
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800187 '*** %s\n'
188 '***' % self._tag_file)
189 else:
190 ret += ('\n'
191 '*** After this process is done, your device will start factory\n'
192 '*** tests on the next reboot.\n'
193 '***\n'
Jon Salz4f3ade52014-02-20 17:55:09 +0800194 '*** Factory tests can be disabled by deleting the factory enabled\n'
195 '*** tag:\n'
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800196 '*** %s\n'
197 '***' % self._tag_file)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800198 return ret
199
Vic Yang7039f422014-07-07 15:38:13 -0700200 def _SetTagFile(self, name, path, enabled):
201 """Install or remove a tag file."""
202 if enabled:
203 print '*** Installing %s enabled tag...' % name
204 Spawn(['touch', path], sudo=True, log=True, check_call=True)
Ricky Liang8c88a122014-07-11 21:21:22 +0800205 Spawn(['chmod', 'go+r', path], sudo=True, log=True, check_call=True)
Vic Yang7039f422014-07-07 15:38:13 -0700206 else:
207 print '*** Removing %s enabled tag...' % name
208 Spawn(['rm', '-f', path], sudo=True, log=True, check_call=True)
209
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800210 def Install(self):
211 print '*** Installing factory toolkit...'
Jon Salzb7e44262014-05-07 15:53:37 +0800212 for src, dest in ((self._usr_local_src, self._usr_local_dest),
213 (self._var_src, self._var_dest)):
214 # Change the source directory to root, and add group/world read
215 # permissions. This is necessary because when the toolkit was
216 # unpacked, the user may not have been root so the permessions
217 # may be hosed. This is skipped for testing.
Peter Ammon5ac58422014-06-09 14:45:50 -0700218 # --force is necessary to allow goofy directory from prior
219 # toolkit installations to be overwritten by the goofy symlink.
Ricky Liang5e95be22014-07-09 12:52:07 +0800220 try:
221 if self._sudo:
222 Spawn(['chown', '-R', 'root', src],
223 sudo=True, log=True, check_call=True)
224 Spawn(['chmod', '-R', 'go+rX', src],
225 sudo=True, log=True, check_call=True)
226 print '*** %s -> %s' % (src, dest)
Vic Yangdb1e20e2014-10-05 12:10:33 +0800227 Spawn(['rsync', '-a', '--force', ] + SERVER_FILE_MASK +
228 [src + '/', dest], sudo=self._sudo, log=True,
229 check_output=True, cwd=src)
Ricky Liang5e95be22014-07-09 12:52:07 +0800230 finally:
231 # Need to change the source directory back to the original user, or the
232 # script in makeself will fail to remove the temporary source directory.
233 if self._sudo:
234 myuser = os.environ.get('USER')
235 Spawn(['chown', '-R', myuser, src],
236 sudo=True, log=True, check_call=True)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800237
Jon Salz25590302014-07-11 16:07:20 +0800238 print '*** Installing symlinks...'
239 install_symlinks.InstallSymlinks(
240 '../factory/bin',
241 os.path.join(self._usr_local_dest, 'bin'),
242 install_symlinks.MODE_FULL,
243 sudo=self._sudo)
244
245 print '*** Removing factory-mini...'
246 Spawn(['rm', '-rf', os.path.join(self._usr_local_dest, 'factory-mini')],
247 sudo=self._sudo, log=True, check_call=True)
248
Vic Yang7039f422014-07-07 15:38:13 -0700249 self._SetTagFile('factory', self._tag_file, not self._no_enable)
Peter Ammon948b7172014-07-15 12:43:06 -0700250 self._SetTagFile('presenter', self._presenter_tag_file,
251 self._enable_presenter)
Vic Yang7039f422014-07-07 15:38:13 -0700252 self._SetTagFile('device', self._device_tag_file, self._enable_device)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800253
254 print '*** Installation completed.'
255
256
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800257@contextmanager
258def DummyContext(arg):
259 """A context manager that simply yields its argument."""
260 yield arg
261
262
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800263def PrintBuildInfo(src_root):
264 """Print build information."""
265 info_file = os.path.join(src_root, 'REPO_STATUS')
266 if not os.path.exists(info_file):
267 raise OSError('Build info file not found!')
268 with open(info_file, 'r') as f:
269 print f.read()
270
271
Wei-Ning Huangb723d7f2014-11-17 17:26:32 +0800272def PackFactoryToolkit(src_root, output_path, enable_device, enable_presenter):
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800273 """Packs the files containing this script into a factory toolkit."""
274 with open(os.path.join(src_root, 'VERSION'), 'r') as f:
275 version = f.read().strip()
Jon Salz4f3ade52014-02-20 17:55:09 +0800276 with tempfile.NamedTemporaryFile() as help_header:
277 help_header.write(version + "\n" + HELP_HEADER + HELP_HEADER_MAKESELF)
278 help_header.flush()
Wei-Ning Huangb723d7f2014-11-17 17:26:32 +0800279 cmd = [os.path.join(src_root, 'makeself.sh'), '--bzip2', '--nox11',
Jon Salz4f3ade52014-02-20 17:55:09 +0800280 '--help-header', help_header.name,
Wei-Ning Huangb723d7f2014-11-17 17:26:32 +0800281 src_root, output_path, version, INSTALLER_PATH, '--in-exe']
282 if not enable_device:
283 cmd.append('--no-enable-device')
284 if not enable_presenter:
285 cmd.append('--no-enable-presenter')
286 Spawn(cmd, check_call=True, log=True)
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800287 print ('\n'
288 ' Factory toolkit generated at %s.\n'
289 '\n'
290 ' To install factory toolkit on a live device running a test image,\n'
291 ' copy this to the device and execute it as root.\n'
292 '\n'
293 ' Alternatively, the factory toolkit can be used to patch a test\n'
294 ' image. For more information, run:\n'
Jon Salz4f3ade52014-02-20 17:55:09 +0800295 ' %s --help\n'
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800296 '\n' % (output_path, output_path))
297
298
Rong Chang6d65fad2014-08-22 16:00:12 +0800299def InitUmpire(exe_path, src_root, target_board):
300 """Inits Umpire server environment."""
301 if exe_path is None:
302 parent_cmdline = open('/proc/%s/cmdline' % os.getppid(),
303 'r').read().rstrip('\0').split('\0')
304
305 if parent_cmdline > 1 and parent_cmdline[0] == MAKESELF_SHELL:
306 # Get parent script name from parent process.
307 exe_path = parent_cmdline[1]
308 else:
309 # Set to default.
310 exe_path = TOOLKIT_NAME
311
312 if not exe_path.startswith('/'):
313 exe_path = os.path.join(os.environ.get('OLDPWD'), exe_path)
314
315 with file_utils.TempDirectory() as nano_bundle:
316 bundle_toolkit_dir = os.path.join(nano_bundle, 'factory_toolkit')
317 os.mkdir(bundle_toolkit_dir)
318 os.symlink(exe_path, os.path.join(bundle_toolkit_dir,
319 os.path.basename(exe_path)))
320 umpire_bin = os.path.join(src_root, 'usr', 'local', 'factory', 'bin',
321 'umpire')
322 Spawn([umpire_bin, 'init', '--board', target_board, nano_bundle],
323 check_call=True, log=True)
324 print ('\n'
325 ' Umpire initialized successfully. Upstart service is running:\n'
326 ' umpire BOARD=%(board)s.\n'
327 ' For more information, please check umpire command line:\n'
328 '\n'
329 ' umpire-%(board)s --help (if your id is in umpire group)\n'
330 ' or sudo umpire-%(board)s --help\n'
331 '\n' % {'board': target_board})
332
333
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800334def main():
Jon Salz4f3ade52014-02-20 17:55:09 +0800335 import logging
336 logging.basicConfig(level=logging.INFO)
337
338 # In order to determine which usage message to show, first determine
339 # whether we're in the self-extracting archive. Do this first
340 # because we need it to even parse the arguments.
341 if '--in-exe' in sys.argv:
342 sys.argv = [x for x in sys.argv if x != '--in-exe']
343 in_archive = True
344 else:
345 in_archive = False
346
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800347 parser = argparse.ArgumentParser(
Jon Salz4f3ade52014-02-20 17:55:09 +0800348 description=HELP_HEADER + HELP_HEADER_ADVANCED,
349 usage=('install_factory_toolkit.run -- [options]' if in_archive
350 else None),
351 formatter_class=argparse.RawDescriptionHelpFormatter)
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800352 parser.add_argument('dest', nargs='?', default='/',
353 help='A test image or the mount point of the stateful partition. '
354 "If omitted, install to live system, i.e. '/'.")
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800355 parser.add_argument('--no-enable', '-n', action='store_true',
356 help="Don't enable factory tests after installing")
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800357 parser.add_argument('--yes', '-y', action='store_true',
358 help="Don't ask for confirmation")
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800359 parser.add_argument('--build-info', action='store_true',
360 help="Print build information and exit")
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800361 parser.add_argument('--pack-into', metavar='NEW_TOOLKIT',
362 help="Pack the files into a new factory toolkit")
363 parser.add_argument('--repack', metavar='UNPACKED_TOOLKIT',
364 help="Repack from previously unpacked toolkit")
Vic Yang7039f422014-07-07 15:38:13 -0700365
Peter Ammon948b7172014-07-15 12:43:06 -0700366 parser.add_argument('--enable-presenter', dest='enable_presenter',
Vic Yang7039f422014-07-07 15:38:13 -0700367 action='store_true',
Peter Ammon948b7172014-07-15 12:43:06 -0700368 help="Run goofy in presenter mode on startup")
369 parser.add_argument('--no-enable-presenter', dest='enable_presenter',
Vic Yang7039f422014-07-07 15:38:13 -0700370 action='store_false', help=argparse.SUPPRESS)
Vic Yang423c2722014-09-24 16:05:06 +0800371 parser.set_defaults(enable_presenter=True)
Vic Yang7039f422014-07-07 15:38:13 -0700372
373 parser.add_argument('--enable-device', dest='enable_device',
374 action='store_true',
Peter Ammon948b7172014-07-15 12:43:06 -0700375 help="Run goofy in device mode on startup")
Vic Yang7039f422014-07-07 15:38:13 -0700376 parser.add_argument('--no-enable-device', dest='enable_device',
377 action='store_false', help=argparse.SUPPRESS)
Vic Yang423c2722014-09-24 16:05:06 +0800378 parser.set_defaults(enable_device=True)
379
Rong Chang6d65fad2014-08-22 16:00:12 +0800380 parser.add_argument('--init-umpire-board', dest='umpire_board',
381 nargs='?', default=None,
382 help="Locally install Umpire server for specific board")
383 parser.add_argument('--exe-path', dest='exe_path',
384 nargs='?', default=None,
385 help="Current self-extracting archive pathname")
Vic Yang7039f422014-07-07 15:38:13 -0700386
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800387 args = parser.parse_args()
388
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800389 src_root = factory.FACTORY_PATH
390 for _ in xrange(3):
391 src_root = os.path.dirname(src_root)
392
Rong Chang6d65fad2014-08-22 16:00:12 +0800393 # --init-umpire-board creates a nano bundle, then calls umpire command
394 # line utility to install the server code and upstart configurations.
395 if args.umpire_board:
396 InitUmpire(args.exe_path, src_root, args.umpire_board)
397 return
398
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800399 # --pack-into may be called directly so this must be done before changing
400 # working directory to OLDPWD.
401 if args.pack_into and args.repack is None:
Wei-Ning Huangb723d7f2014-11-17 17:26:32 +0800402 PackFactoryToolkit(src_root, args.pack_into, args.enable_device,
403 args.enable_presenter)
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800404 return
405
Jon Salz4f3ade52014-02-20 17:55:09 +0800406 if not in_archive:
407 # If you're not in the self-extracting archive, you're not allowed to
408 # do anything except the above --pack-into call.
409 parser.error('Not running from install_factory_toolkit.run; '
410 'only --pack-into (without --repack) is allowed')
411
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800412 # Change to original working directory in case the user specifies
413 # a relative path.
414 # TODO: Use USER_PWD instead when makeself is upgraded
415 os.chdir(os.environ['OLDPWD'])
416
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800417 if args.repack:
418 if args.pack_into is None:
419 parser.error('Must specify --pack-into when using --repack.')
420 Spawn([os.path.join(args.repack, INSTALLER_PATH),
421 '--pack-into', args.pack_into], check_call=True, log=True)
422 return
423
424 if args.build_info:
425 PrintBuildInfo(src_root)
426 return
427
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800428 if not os.path.exists(args.dest):
429 parser.error('Destination %s does not exist!' % args.dest)
430
431 patch_test_image = os.path.isfile(args.dest)
432
433 with (MountPartition(args.dest, 1, rw=True) if patch_test_image
434 else DummyContext(args.dest)) as dest:
Peter Ammon948b7172014-07-15 12:43:06 -0700435 installer = FactoryToolkitInstaller(src_root, dest, args.no_enable,
436 args.enable_presenter, args.enable_device)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800437
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800438 print installer.WarningMessage(args.dest if patch_test_image else None)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800439
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800440 if not args.yes:
441 answer = raw_input('*** Continue? [y/N] ')
442 if not answer or answer[0] not in 'yY':
443 sys.exit('Aborting.')
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800444
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800445 installer.Install()
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800446
447if __name__ == '__main__':
448 main()