2017-08-29 20:29:44 +02:00
|
|
|
#!/usr/bin/env python3
|
2017-06-14 13:36:37 +02:00
|
|
|
#
|
|
|
|
# kas - setup tool for bitbake based projects
|
|
|
|
#
|
2018-09-05 11:13:03 +02:00
|
|
|
# Copyright (c) Siemens AG, 2017-2018
|
2017-06-14 13:36:37 +02:00
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be
|
|
|
|
# included in all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
|
|
|
This module is the main entry point for kas, setup tool for bitbake based
|
|
|
|
projects
|
|
|
|
"""
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
import argparse
|
2017-06-28 12:43:51 +02:00
|
|
|
import atexit
|
|
|
|
import asyncio
|
2017-06-14 13:36:37 +02:00
|
|
|
import traceback
|
|
|
|
import logging
|
2017-06-28 12:43:51 +02:00
|
|
|
import signal
|
2017-06-14 13:36:37 +02:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import pkg_resources
|
|
|
|
|
|
|
|
try:
|
|
|
|
import colorlog
|
2017-06-21 13:32:56 +02:00
|
|
|
HAVE_COLORLOG = True
|
2017-06-14 13:36:37 +02:00
|
|
|
except ImportError:
|
2017-06-21 13:32:56 +02:00
|
|
|
HAVE_COLORLOG = False
|
2017-06-14 13:36:37 +02:00
|
|
|
|
2017-07-17 22:38:06 +02:00
|
|
|
from . import __version__, __file_version__, __compatible_file_version__
|
2017-06-14 13:36:37 +02:00
|
|
|
|
2017-06-28 14:48:41 +02:00
|
|
|
# Import kas plugins
|
|
|
|
# Since they are added by decorators, they don't need to be called,
|
|
|
|
# just imported.
|
2020-10-16 19:31:45 +02:00
|
|
|
from .plugins import kasplugin
|
|
|
|
from .plugins import build
|
|
|
|
from .plugins import shell
|
2017-06-28 14:48:41 +02:00
|
|
|
|
2018-12-07 09:48:42 +01:00
|
|
|
__all__ = ['build', 'shell']
|
|
|
|
|
2017-06-14 13:36:37 +02:00
|
|
|
__license__ = 'MIT'
|
2018-09-05 11:13:03 +02:00
|
|
|
__copyright__ = 'Copyright (c) Siemens AG, 2017-2018'
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def create_logger():
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
|
|
|
Setup the logging environment
|
|
|
|
"""
|
2017-06-14 13:36:37 +02:00
|
|
|
log = logging.getLogger() # root logger
|
|
|
|
log.setLevel(logging.INFO)
|
2017-06-21 13:32:56 +02:00
|
|
|
format_str = '%(asctime)s - %(levelname)-8s - %(message)s'
|
2017-06-14 13:36:37 +02:00
|
|
|
date_format = '%Y-%m-%d %H:%M:%S'
|
2017-06-21 13:32:56 +02:00
|
|
|
if HAVE_COLORLOG and os.isatty(2):
|
|
|
|
cformat = '%(log_color)s' + format_str
|
2017-06-14 13:36:37 +02:00
|
|
|
colors = {'DEBUG': 'reset',
|
|
|
|
'INFO': 'reset',
|
|
|
|
'WARNING': 'bold_yellow',
|
|
|
|
'ERROR': 'bold_red',
|
|
|
|
'CRITICAL': 'bold_red'}
|
2017-06-21 13:32:56 +02:00
|
|
|
formatter = colorlog.ColoredFormatter(cformat, date_format,
|
|
|
|
log_colors=colors)
|
2017-06-14 13:36:37 +02:00
|
|
|
else:
|
2017-06-21 13:32:56 +02:00
|
|
|
formatter = logging.Formatter(format_str, date_format)
|
|
|
|
stream_handler = logging.StreamHandler()
|
|
|
|
stream_handler.setFormatter(formatter)
|
|
|
|
log.addHandler(stream_handler)
|
2017-06-14 13:36:37 +02:00
|
|
|
return logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-06-28 12:43:53 +02:00
|
|
|
def interruption():
|
|
|
|
"""
|
2017-07-03 11:34:33 +02:00
|
|
|
Ignore SIGINT/SIGTERM in kas, let them be handled by our sub-processes
|
2017-06-28 12:43:53 +02:00
|
|
|
"""
|
2017-07-03 11:34:33 +02:00
|
|
|
pass
|
2017-06-28 12:43:53 +02:00
|
|
|
|
|
|
|
|
2017-06-28 14:52:49 +02:00
|
|
|
def _atexit_handler():
|
2017-06-28 12:43:51 +02:00
|
|
|
"""
|
2018-09-05 11:13:03 +02:00
|
|
|
Waits for completion of the event loop
|
2017-06-28 12:43:51 +02:00
|
|
|
"""
|
2017-06-28 19:26:01 +02:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
pending = asyncio.Task.all_tasks()
|
2019-09-25 11:00:45 +02:00
|
|
|
if not loop.is_closed():
|
|
|
|
loop.run_until_complete(asyncio.gather(*pending))
|
|
|
|
loop.close()
|
2017-06-28 19:26:01 +02:00
|
|
|
|
2017-06-28 12:43:51 +02:00
|
|
|
|
2020-06-15 22:03:28 +02:00
|
|
|
def setup_parser_common_args(parser):
|
|
|
|
parser.add_argument('config',
|
|
|
|
help='Config file')
|
|
|
|
parser.add_argument('--skip',
|
|
|
|
help='Skip build steps',
|
|
|
|
default=[])
|
2020-06-15 22:03:30 +02:00
|
|
|
parser.add_argument('--force-checkout', action='store_true',
|
|
|
|
help='Always checkout the desired refspec of each '
|
|
|
|
'repository, discarding any local changes')
|
2020-06-15 22:03:31 +02:00
|
|
|
parser.add_argument('--update', action='store_true',
|
|
|
|
help='Pull new upstream changes to the desired '
|
|
|
|
'refspec even if it is already checked out locally')
|
|
|
|
|
2020-06-15 22:03:28 +02:00
|
|
|
|
2017-06-28 14:48:41 +02:00
|
|
|
def kas_get_argparser():
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
2018-09-05 11:13:03 +02:00
|
|
|
Creates an argparser for kas with all plugins.
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
2017-06-28 14:48:44 +02:00
|
|
|
parser = argparse.ArgumentParser(description='kas - setup tool for '
|
|
|
|
'bitbake based project')
|
2017-06-14 13:36:37 +02:00
|
|
|
|
2017-07-17 22:38:06 +02:00
|
|
|
verstr = '%(prog)s {} (configuration format version {}, ' \
|
|
|
|
'earliest compatible version {})'.format(__version__, __file_version__,
|
|
|
|
__compatible_file_version__)
|
|
|
|
parser.add_argument('--version', action='version', version=verstr)
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
parser.add_argument('-d', '--debug',
|
|
|
|
action='store_true',
|
|
|
|
help='Enable debug logging')
|
|
|
|
|
|
|
|
subparser = parser.add_subparsers(help='sub command help', dest='cmd')
|
2017-06-28 14:48:41 +02:00
|
|
|
for ext_plugin in pkg_resources.iter_entry_points('kas.plugins'):
|
|
|
|
ext_plugin.load()
|
|
|
|
|
2020-10-16 19:31:44 +02:00
|
|
|
for plugin in getattr(kasplugin, 'plugins', {}).values():
|
2020-06-15 22:03:27 +02:00
|
|
|
plugin_parser = subparser.add_parser(plugin.name, help=plugin.helpmsg)
|
2020-06-15 22:03:28 +02:00
|
|
|
setup_parser_common_args(plugin_parser)
|
2020-06-15 22:03:27 +02:00
|
|
|
plugin.setup_parser(plugin_parser)
|
2017-06-28 14:48:41 +02:00
|
|
|
|
|
|
|
return parser
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
|
2017-06-28 14:48:41 +02:00
|
|
|
def kas(argv):
|
|
|
|
"""
|
2018-09-05 11:13:03 +02:00
|
|
|
The actual main entry point of kas.
|
2017-06-28 14:48:41 +02:00
|
|
|
"""
|
|
|
|
create_logger()
|
|
|
|
|
|
|
|
parser = kas_get_argparser()
|
2017-06-14 13:36:37 +02:00
|
|
|
args = parser.parse_args(argv)
|
|
|
|
|
|
|
|
if args.debug:
|
|
|
|
logging.getLogger().setLevel(logging.DEBUG)
|
|
|
|
|
2017-10-19 11:05:49 +02:00
|
|
|
logging.info('%s %s started', os.path.basename(sys.argv[0]), __version__)
|
|
|
|
|
2017-06-28 12:43:51 +02:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
2017-06-28 12:43:53 +02:00
|
|
|
for sig in (signal.SIGINT, signal.SIGTERM):
|
|
|
|
loop.add_signal_handler(sig, interruption)
|
2017-06-28 14:52:49 +02:00
|
|
|
atexit.register(_atexit_handler)
|
2017-06-28 12:43:51 +02:00
|
|
|
|
2020-10-16 19:31:44 +02:00
|
|
|
if args.cmd:
|
|
|
|
plugin = getattr(kasplugin, 'plugins', {})[args.cmd]
|
|
|
|
plugin().run(args)
|
|
|
|
else:
|
|
|
|
parser.print_help()
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
|
|
|
The main function that operates as a wrapper around kas.
|
|
|
|
"""
|
|
|
|
|
2017-06-14 13:36:37 +02:00
|
|
|
try:
|
|
|
|
sys.exit(kas(sys.argv[1:]))
|
|
|
|
except Exception as err:
|
2017-06-21 13:32:56 +02:00
|
|
|
logging.error('%s', err)
|
2017-06-14 13:36:37 +02:00
|
|
|
traceback.print_exc()
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|