2013-08-20 20:55:16 +00:00
#!/usr/bin/env python
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
#
# Copyright (c) 2013, Intel Corporation.
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# DESCRIPTION 'wic' is the OpenEmbedded Image Creator that users can
# use to generate bootable images. Invoking it without any arguments
# will display help screens for the 'wic' command and list the
# available 'wic' subcommands. Invoking a subcommand without any
# arguments will likewise display help screens for the specified
# subcommand. Please use that interface for detailed help.
#
# AUTHORS
# Tom Zanussi <tom.zanussi (at] linux.intel.com>
#
__version__ = "0.1.0"
2013-10-15 21:46:24 +00:00
# Python Standard Library modules
2013-08-20 20:55:16 +00:00
import os
import sys
import optparse
import logging
2015-04-17 13:19:01 +00:00
from distutils import spawn
2013-08-20 20:55:16 +00:00
2013-10-15 21:46:24 +00:00
# External modules
2015-04-09 12:54:35 +00:00
scripts_path = os.path.abspath(os.path.dirname(__file__))
2013-08-20 20:55:16 +00:00
lib_path = scripts_path + '/lib'
2015-04-17 13:19:01 +00:00
sys.path.append(lib_path)
bitbake_exe = spawn.find_executable('bitbake')
if bitbake_exe:
bitbake_path = os.path.join(os.path.dirname(bitbake_exe), '../lib')
sys.path.append(bitbake_path)
from bb import cookerdata
from bb.main import bitbake_main, BitBakeConfigParameters
else:
bitbake_main = None
2013-08-20 20:55:16 +00:00
2015-04-15 20:47:10 +00:00
from wic.utils.oe.misc import find_bitbake_env_lines, set_bitbake_env_lines
from image import engine
from image import help as hlp
2013-08-20 20:55:16 +00:00
2014-03-29 03:12:08 +00:00
def rootfs_dir_to_args(krootfs_dir):
"""
Get a rootfs_dir dict and serialize to string
"""
rootfs_dir = ''
for k, v in krootfs_dir.items():
rootfs_dir += ' '
rootfs_dir += '='.join([k, v])
return rootfs_dir.strip()
def callback_rootfs_dir(option, opt, value, parser):
"""
Build a dict using --rootfs_dir connection=dir
"""
if not type(parser.values.rootfs_dir) is dict:
parser.values.rootfs_dir = dict()
if '=' in value:
(key, rootfs_dir) = value.split('=')
else:
2015-04-07 11:29:25 +00:00
key = 'ROOTFS_DIR'
rootfs_dir = value
2014-03-29 03:12:08 +00:00
parser.values.rootfs_dir[key] = rootfs_dir
2013-08-20 20:55:16 +00:00
def wic_create_subcommand(args, usage_str):
"""
Command-line handling for image creation. The real work is done
by image.engine.wic_create()
"""
2015-04-09 11:36:49 +00:00
parser = optparse.OptionParser(usage=usage_str)
parser.add_option("-o", "--outdir", dest="outdir",
action="store", help="name of directory to create image in")
parser.add_option("-i", "--infile", dest="properties_file",
action="store", help="name of file containing the values for image properties as a JSON file")
parser.add_option("-e", "--image-name", dest="image_name",
action="store", help="name of the image to use the artifacts from e.g. core-image-sato")
parser.add_option("-r", "--rootfs-dir", dest="rootfs_dir",
action="callback", callback=callback_rootfs_dir, type="string",
help="path to the /rootfs dir to use as the .wks rootfs source")
parser.add_option("-b", "--bootimg-dir", dest="bootimg_dir",
action="store", help="path to the dir containing the boot artifacts (e.g. /EFI or /syslinux dirs) to use as the .wks bootimg source")
parser.add_option("-k", "--kernel-dir", dest="kernel_dir",
action="store", help="path to the dir containing the kernel to use in the .wks bootimg")
parser.add_option("-n", "--native-sysroot", dest="native_sysroot",
action="store", help="path to the native sysroot containing the tools to use to build the image")
parser.add_option("-p", "--skip-build-check", dest="build_check",
action="store_false", default=True, help="skip the build check")
parser.add_option("-f", "--build-rootfs", action="store_true", help="build rootfs")
parser.add_option("-D", "--debug", dest="debug", action="store_true",
default=False, help="output debug information")
2013-08-20 20:55:16 +00:00
(options, args) = parser.parse_args(args)
if len(args) != 1:
logging.error("Wrong number of arguments, exiting\n")
parser.print_help()
sys.exit(1)
2015-04-17 13:19:01 +00:00
if options.build_rootfs and not bitbake_main:
logging.error("Can't build roofs as bitbake is not in the $PATH")
sys.exit(1)
2013-10-16 05:14:29 +00:00
if not options.image_name and not (options.rootfs_dir and
options.bootimg_dir and
options.kernel_dir and
options.native_sysroot):
print "Build artifacts not completely specified, exiting."
print " (Use 'wic -e' or 'wic -r -b -k -n' to specify artifacts)"
sys.exit(1)
2013-08-20 20:55:16 +00:00
if not options.image_name:
options.build_check = False
if options.build_check and not options.properties_file:
print "Checking basic build environment..."
2015-04-15 20:47:10 +00:00
if not engine.verify_build_env():
2013-08-20 20:55:16 +00:00
print "Couldn't verify build environment, exiting\n"
sys.exit(1)
else:
print "Done.\n"
2014-02-04 01:16:55 +00:00
bitbake_env_lines = find_bitbake_env_lines(options.image_name)
if not bitbake_env_lines:
print "Couldn't get bitbake environment, exiting."
sys.exit(1)
set_bitbake_env_lines(bitbake_env_lines)
2014-10-29 22:04:23 +00:00
bootimg_dir = ""
2013-08-20 20:55:16 +00:00
if options.image_name:
2015-04-07 09:57:41 +00:00
if options.build_rootfs:
argv = ["bitbake", options.image_name]
if options.debug:
argv.append("--debug")
print "Building rootfs...\n"
if bitbake_main(BitBakeConfigParameters(argv),
cookerdata.CookerConfiguration()):
sys.exit(1)
2014-10-29 22:04:23 +00:00
(rootfs_dir, kernel_dir, bootimg_dir, native_sysroot) \
2015-04-15 20:47:10 +00:00
= engine.find_artifacts(options.image_name)
2013-08-20 20:55:16 +00:00
2015-04-07 09:57:41 +00:00
else:
if options.build_rootfs:
print "Image name is not specified, exiting. (Use -e/--image-name to specify it)\n"
sys.exit(1)
2013-08-20 20:55:16 +00:00
wks_file = args[0]
if not wks_file.endswith(".wks"):
2015-04-15 20:47:10 +00:00
wks_file = engine.find_canned_image(scripts_path, wks_file)
2013-08-20 20:55:16 +00:00
if not wks_file:
print "No image named %s found, exiting. (Use 'wic list images' to list available images, or specify a fully-qualified OE kickstart (.wks) filename)\n" % wks_file
sys.exit(1)
image_output_dir = ""
if options.outdir:
image_output_dir = options.outdir
if not options.image_name:
2014-03-29 03:12:08 +00:00
rootfs_dir = ''
if 'ROOTFS_DIR' in options.rootfs_dir:
rootfs_dir = options.rootfs_dir['ROOTFS_DIR']
2013-08-20 20:55:16 +00:00
bootimg_dir = options.bootimg_dir
kernel_dir = options.kernel_dir
native_sysroot = options.native_sysroot
2014-03-29 03:12:08 +00:00
if rootfs_dir and not os.path.isdir(rootfs_dir):
2013-10-16 05:14:30 +00:00
print "--roofs-dir (-r) not found, exiting\n"
sys.exit(1)
if not os.path.isdir(bootimg_dir):
print "--bootimg-dir (-b) not found, exiting\n"
sys.exit(1)
if not os.path.isdir(kernel_dir):
print "--kernel-dir (-k) not found, exiting\n"
sys.exit(1)
if not os.path.isdir(native_sysroot):
print "--native-sysroot (-n) not found, exiting\n"
sys.exit(1)
2013-10-18 04:45:37 +00:00
else:
not_found = not_found_dir = ""
if not os.path.isdir(rootfs_dir):
(not_found, not_found_dir) = ("rootfs-dir", rootfs_dir)
elif not os.path.isdir(kernel_dir):
(not_found, not_found_dir) = ("kernel-dir", kernel_dir)
elif not os.path.isdir(native_sysroot):
(not_found, not_found_dir) = ("native-sysroot", native_sysroot)
if not_found:
if not not_found_dir:
not_found_dir = "Completely missing artifact - wrong image (.wks) used?"
print "Build artifacts not found, exiting."
print " (Please check that the build artifacts for the machine"
print " selected in local.conf actually exist and that they"
print " are the correct artifacts for the image (.wks file)).\n"
print "The artifact that couldn't be found was %s:\n %s" % \
(not_found, not_found_dir)
sys.exit(1)
2013-08-20 20:55:16 +00:00
2014-03-29 03:12:08 +00:00
krootfs_dir = options.rootfs_dir
if krootfs_dir is None:
2015-04-07 11:29:25 +00:00
krootfs_dir = {}
krootfs_dir['ROOTFS_DIR'] = rootfs_dir
2014-03-29 03:12:08 +00:00
rootfs_dir = rootfs_dir_to_args(krootfs_dir)
2015-04-07 09:57:41 +00:00
print "Creating image(s)...\n"
2015-04-15 20:47:10 +00:00
engine.wic_create(args, wks_file, rootfs_dir, bootimg_dir, kernel_dir,
native_sysroot, scripts_path, image_output_dir,
options.debug, options.properties_file)
2013-08-20 20:55:16 +00:00
def wic_list_subcommand(args, usage_str):
"""
Command-line handling for listing available image properties and
values. The real work is done by image.engine.wic_list()
"""
2015-04-09 11:36:49 +00:00
parser = optparse.OptionParser(usage=usage_str)
2013-08-20 20:55:16 +00:00
2015-04-09 11:36:49 +00:00
parser.add_option("-o", "--outfile", action="store",
dest="properties_file",
help="dump the possible values for image properties to a JSON file")
2013-08-20 20:55:16 +00:00
(options, args) = parser.parse_args(args)
2014-05-15 01:37:27 +00:00
bitbake_env_lines = find_bitbake_env_lines(None)
if not bitbake_env_lines:
print "Couldn't get bitbake environment, exiting."
sys.exit(1)
set_bitbake_env_lines(bitbake_env_lines)
2015-04-15 20:47:10 +00:00
if not engine.wic_list(args, scripts_path, options.properties_file):
2013-08-20 20:55:16 +00:00
logging.error("Bad list arguments, exiting\n")
parser.print_help()
sys.exit(1)
2014-07-07 20:30:12 +00:00
def wic_help_topic_subcommand(args, usage_str):
"""
Command-line handling for help-only 'subcommands'. This is
essentially a dummy command that doesn nothing but allow users to
use the existing subcommand infrastructure to display help on a
particular topic not attached to any particular subcommand.
"""
pass
wic_help_topic_usage = """
"""
2013-08-20 20:55:16 +00:00
subcommands = {
2014-07-11 20:51:27 +00:00
"create": [wic_create_subcommand,
2015-04-15 20:47:10 +00:00
hlp.wic_create_usage,
hlp.wic_create_help],
2014-07-11 20:51:27 +00:00
"list": [wic_list_subcommand,
2015-04-15 20:47:10 +00:00
hlp.wic_list_usage,
hlp.wic_list_help],
2014-07-11 20:51:27 +00:00
"plugins": [wic_help_topic_subcommand,
wic_help_topic_usage,
2015-04-15 20:47:10 +00:00
hlp.wic_plugins_help],
"overview": [wic_help_topic_subcommand,
2014-07-11 20:51:27 +00:00
wic_help_topic_usage,
2015-04-15 20:47:10 +00:00
hlp.wic_overview_help],
"kickstart": [wic_help_topic_subcommand,
2014-07-11 20:51:27 +00:00
wic_help_topic_usage,
2015-04-15 20:47:10 +00:00
hlp.wic_kickstart_help],
2013-08-20 20:55:16 +00:00
}
def start_logging(loglevel):
2015-04-09 11:36:49 +00:00
logging.basicConfig(filname='wic.log', filemode='w', level=loglevel)
2013-08-20 20:55:16 +00:00
2015-04-09 18:41:20 +00:00
def main(argv):
2015-04-09 11:36:49 +00:00
parser = optparse.OptionParser(version="wic version %s" % __version__,
2015-04-15 20:47:10 +00:00
usage=hlp.wic_usage)
2013-08-20 20:55:16 +00:00
parser.disable_interspersed_args()
2015-04-09 18:41:20 +00:00
(options, args) = parser.parse_args(argv)
2013-08-20 20:55:16 +00:00
if len(args):
if args[0] == "help":
if len(args) == 1:
parser.print_help()
sys.exit(1)
2015-04-15 20:47:10 +00:00
hlp.invoke_subcommand(args, parser, hlp.wic_help_usage, subcommands)
2013-08-20 20:55:16 +00:00
if __name__ == "__main__":
try:
2015-04-09 18:41:20 +00:00
ret = main(sys.argv[1:])
2013-08-20 20:55:16 +00:00
except Exception:
ret = 1
import traceback
traceback.print_exc(5)
sys.exit(ret)