From 5da36bf987dc80e028d4d17b7b856fb93ffaf0ca Mon Sep 17 00:00:00 2001 From: Chulwoo Shin Date: Fri, 16 Dec 2016 17:23:08 +0900 Subject: [PATCH] [TIC-CORE] support cli commands - add export cli for creating images - add createimage cli using mic Change-Id: Id102b0c5e6a43781f4a9047230c7d35845bbd2d0 Signed-off-by: Chulwoo Shin --- tic/command.py | 21 ++++++++++++++++++++- tic/utils/log.py | 6 +++--- tic/utils/process.py | 33 ++++++++++++++++++++++++++------- tools/tic-core | 26 ++++++++++++++++---------- 4 files changed, 65 insertions(+), 21 deletions(-) diff --git a/tic/command.py b/tic/command.py index c2e80c0..51f41cc 100644 --- a/tic/command.py +++ b/tic/command.py @@ -30,6 +30,7 @@ from tic.utils.error import TICError from tic.utils.file import copyfile from tic.repo import get_repodata_from_repos from tic.pykickstarter import KSoption, kswriter +from tic.utils import process DEFAULT_CACHEDIR='/var/tmp/tic-core/cached' @@ -79,10 +80,12 @@ def analyze(repo_list, recipe_list=None): def exports(export_type, recipe, packages, outdir): logger = logging.getLogger(__name__) + + #TODO validation should be checked before request if not export_type: export_type='ks' logger.info('set default export format(.ks)') - + if not recipe: raise TICError('No recipe defined') if not packages or type(packages) is not list: @@ -120,3 +123,19 @@ def exports(export_type, recipe, packages, outdir): logger.info('copy the ks file from %s to dst:%s', kspath, output) return output + +def createimage(recipes, ksfile, outdir): + logger = logging.getLogger(__name__) + + if recipes: + logger.info('the recipes option is not yet supported') + return + + if not os.path.exists(ksfile) or os.path.isdir(ksfile): + raise TICError('kickstart file does not exist') + + mic_command=['mic', 'cr', 'auto', ksfile] + if outdir: + mic_command.append('--outdir=%s' % outdir) + + process.run(mic_command, 2) diff --git a/tic/utils/log.py b/tic/utils/log.py index d901386..afae483 100644 --- a/tic/utils/log.py +++ b/tic/utils/log.py @@ -34,13 +34,13 @@ def setup(root): formatter.datefmt = '%Y-%m-%d %H:%M:%S' mkdir_p(TIC_LOG_DIR) - fileHandler = logging.handlers.RotatingFileHandler(os.path.join(TIC_LOG_DIR, 'tic-core.log'), maxBytes=LOG_FILE_MAX_BYTES, backupCount=10) + #fileHandler = logging.handlers.RotatingFileHandler(os.path.join(TIC_LOG_DIR, 'tic-core.log'), maxBytes=LOG_FILE_MAX_BYTES, backupCount=10) streamHandler = logging.StreamHandler() - fileHandler.setFormatter(formatter) + #fileHandler.setFormatter(formatter) streamHandler.setFormatter(formatter) - logger.addHandler(fileHandler) + #logger.addHandler(fileHandler) logger.addHandler(streamHandler) def mkdir_p(path): diff --git a/tic/utils/process.py b/tic/utils/process.py index 8fe75cb..d376ed4 100644 --- a/tic/utils/process.py +++ b/tic/utils/process.py @@ -19,13 +19,14 @@ # Contributors: # - S-Core Co., Ltd +import os import subprocess import logging from tic.utils.error import TICError -def run(cmdln): - logger = logging.getLogger() +def run(cmdln, catch=3): + logger = logging.getLogger(__name__) if isinstance(cmdln, list): cmd = cmdln[0] shellType = False @@ -33,21 +34,39 @@ def run(cmdln): import shlex cmd = shlex.split(cmdln)[0] shellType = True + + + if catch == 0: + # silent run + dev_null = os.open("/dev/null", os.O_WRONLY) + sout = dev_null + serr = dev_null + elif catch == 2: + # no redirection + sout = None + serr = None + elif catch == 3: + # both STDOUT and STDERR + sout = subprocess.PIPE + serr = subprocess.STDOUT try: + logger.info('subprocess open: %s', cmd) p = subprocess.Popen(cmdln, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + stdout=sout, + stderr=serr, shell=shellType) + (sout, serr) = p.communicate() out = ''.join(filter(None, [sout, serr])) - logger.info('subprocess open: %s', cmd) except OSError as e: if e.errno == 2: # No such file or directory raise TICError('Cannot run command: %s' % cmd) else: raise + finally: + if catch == 0: + os.close(dev_null) - return (p.returncode, out) - + return (p.returncode, out) \ No newline at end of file diff --git a/tools/tic-core b/tools/tic-core index acfd6d0..6011e5a 100644 --- a/tools/tic-core +++ b/tools/tic-core @@ -54,13 +54,14 @@ def create_parser(): parser_analyze.add_argument('-o', "--outdir", dest="outdir", action="store", help="The result file is distributed in the outdir path", default=os.getcwd()) # create the parser for the 'export' command parser_export = subparsers.add_parser('export', help='export files') - parser_export.add_argument('-f', "--format", dest="format", metavar="recipe/ks", help="exports file foramt", required=True) - parser_export.add_argument('-c', "--recipes", dest="recipes", metavar="paths", nargs='+', help="The path of recipe") - parser_export.add_argument('-o', "--outdir", dest="outdir", action="store", help="The result file is distributed in the outdir path", default=os.getcwd()) + parser_export.add_argument('-f', "--format", dest="format", metavar="recipe/ks", help="exports file format", required=True) + parser_export.add_argument('-c', "--recipes", dest="recipes", metavar="paths", nargs='+', help="recipe files") + parser_export.add_argument('-o', "--outdir", dest="outdir", action="store", help="export file output directory", default=os.getcwd()) # create the parser for the 'create' command - parser_create = subparsers.add_parser('create', help='create an image for tizen') - parser_create.add_argument('-r', "--input", dest="input", metavar="data", nargs='+', help="Input data files (kickstart/recipes)", required=True) - parser_create.add_argument('-o', "--outdir", dest="outdir", action="store", help="The result file is distributed in the output path", default=os.getcwd()) + parser_create = subparsers.add_parser('createimage', help='create an image for tizen') + parser_create.add_argument('-c', "--recipes", dest="recipes", metavar="recipes", nargs='+', help="recipe files to be used for image creation") + parser_create.add_argument('-k', "--ks", dest="kickstart", metavar="kickstart", help="ks file to be used for image creation") + parser_create.add_argument('-o', "--outdir", dest="outdir", action="store", help="image output directory", default=os.getcwd()) parser_start = subparsers.add_parser('start', help='start the tic-core demon on system. port 59001 is used by default ') parser_start.add_argument('-p', "--port", dest="port", action="store", help="port number", default=59001) @@ -80,11 +81,16 @@ def main(argv): output_dir=os.path.abspath(os.path.expanduser(args.outdir)) file.write(os.path.join(output_dir, 'viewdata.json'), json.dumps(view_data)) elif args.subparser_name == 'export': - output=command.exports(args.format, args.recipes, None, args.outdir) + #TODO Temporary code(should be deleted) + recipes={'name':'default'} + packages=['attr', 'filesystem'] + output=command.exports(args.format, recipes, packages, args.outdir) logger.info("export the %s file: %s", args.format, output) - elif args.subparser_name == 'create': - logger.info('create command is yet available') - pass + elif args.subparser_name == 'createimage': + if args.recipes or args.kickstart: + command.createimage(args.recipes, args.kickstart, args.outdir) + else: + logger.info('kickstart or recipes file is required') elif args.subparser_name == 'start': tic_server.start(args.port) return 0 -- 2.7.4