From 19f24a0688d6b54ef4c596890f3da480855ac83f Mon Sep 17 00:00:00 2001 From: Chulwoo Shin Date: Wed, 14 Dec 2016 15:09:59 +0900 Subject: [PATCH] [TIC-CORE] support new api for exporting to specific format create kickstart(.ks) file using the kickstarter module. add thread option for flask Change-Id: Ib6d96e1179fa4750d9655cdd1346217b25a16282 Signed-off-by: Chulwoo Shin --- tic/command.py | 75 +++++++++++++++++++++++++++++++++++++++++++-- tic/dependency.py | 1 + tic/parser/recipe_parser.py | 48 +++++++++++++++++++---------- tic/pykickstarter.py | 20 ++++++------ tic/server/tic_server.py | 8 +++-- 5 files changed, 120 insertions(+), 32 deletions(-) diff --git a/tic/command.py b/tic/command.py index 65570d1..e506e6c 100644 --- a/tic/command.py +++ b/tic/command.py @@ -1,14 +1,39 @@ +#!/usr/bin/python +# Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved. +# +# Contact: +# @author Chulwoo Shin +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Contributors: +# - S-Core Co., Ltd + +import os import logging +import shutil from tic.dependency import analyze_dependency, get_installed_packages -from tic.parser.recipe_parser import get_default_recipe +from tic.parser.recipe_parser import get_default_recipe, convert_recipe_to_yaml from tic.parser.repo_parser import RepodataParser from tic.parser.view_parser import make_view_data - from tic.utils.error import TICError from tic.repo import get_repodata_from_repos +from tic.pykickstarter import KSoption, kswriter + DEFAULT_CACHEDIR='/var/tmp/tic-core/cached' +DEFAULT_KICKSTARTDIR='/var/tmp/tic-core/kickstart' def analyze(repo_list, recipe_list=None): logger = logging.getLogger(__name__) @@ -50,4 +75,48 @@ def analyze(repo_list, recipe_list=None): 'repos': repos, 'defaultpackages': inst_packages} - return result \ No newline at end of file + return result + +def exports(export_type, recipe, packages, output): + logger = logging.getLogger(__name__) + 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: + raise TICError('No packages defined') + + #TODO recipe parsing + # Temporary code for 1st prototype release + if recipe.get('name') == 'default': + recipe = get_default_recipe() + config = recipe.get('Configurations')[0] + for key in ['Default', config['Platform']]: + recipe[key]['Groups']=[] + recipe[key]['ExtraPackages']=[] + config['Groups']=[] + config['ExtraPackages'] = packages + else: + raise TICError('No recipes defined') + + # create the yaml + yaml_info = convert_recipe_to_yaml(recipe, DEFAULT_KICKSTARTDIR) + + # create kickstart(.ks) using kickstarter tool + options = KSoption(yaml_info.configs, yaml_info.repos, yaml_info.cachedir) + kswriter(options) + + # check whether the ks exists + baseline=recipe['Default'].get('Baseline') + ksname= ''.join([config.get('FileName'), '.ks']) + kspath=os.path.join(yaml_info.cachedir, baseline, ksname) + if not os.path.exists(kspath): + raise TICError('No ks file was created from kickstarter') + + # copy the ks to output directory + shutil.copy(kspath, output) + logger.info('copy the ks file from %s to dst:%s', kspath, output) + + return output diff --git a/tic/dependency.py b/tic/dependency.py index ce3c3c9..32b8994 100644 --- a/tic/dependency.py +++ b/tic/dependency.py @@ -18,6 +18,7 @@ # # Contributors: # - S-Core Co., Ltd + from lxml import etree from tic.utils.error import TICError import logging diff --git a/tic/parser/recipe_parser.py b/tic/parser/recipe_parser.py index 7de426a..a2fc3c6 100644 --- a/tic/parser/recipe_parser.py +++ b/tic/parser/recipe_parser.py @@ -19,10 +19,14 @@ # Contributors: # - S-Core Co., Ltd +import collections +from datetime import datetime +import logging import os -import yaml from tic.utils import error -from tic.utils.file import write +from tic.utils.file import write, make_dirs +import yaml + def get_default_recipe(): recipe = dict( @@ -79,6 +83,7 @@ def get_default_recipe(): Schedule= "*", Active= True, Platform= 'Emulator64wayland', + Part= 'mobile-2parts-emulator', Mic2Options= '-f loop --pack-to=@NAME@.tar.gz,', FileName= 'mobile-emulator64-wayland', Repos=['mobile-emulator64-wayland', 'base_emulator64'], @@ -97,7 +102,9 @@ def get_default_recipe(): ], Partitions=[ dict(Name='mobile-mbr', - Contents='part / --fstype="ext4" --size=3584 --ondisk=sda --active --label platform --fsoptions=defaults,noatime') + Contents='part / --fstype="ext4" --size=3584 --ondisk=sda --active --label platform --fsoptions=defaults,noatime'), + dict(Name= 'mobile-2parts-emulator', + Contents='part / --size=2000 --ondisk=sda --fstype=ext4 --label=emulator-rootfs\npart /opt/ --size=2000 --ondisk=sda --fstype=ext4 --label=emulator-sysdata') ] ) return recipe @@ -112,8 +119,8 @@ def load_yaml(path): raise error.TICError('yaml format error of meta file: %s' % path) -def separate_recipe_file_for_ks(recipe): - path = '/home/shinchulwoo/recipe/test' +def convert_recipe_to_yaml(recipe, filepath): + logger = logging.getLogger(__name__) # config.yaml config = dict(Default=None, Configurations=[]) @@ -122,20 +129,30 @@ def separate_recipe_file_for_ks(recipe): config['Configurations'].append(recipe.get('Configurations')[0]) platform_name = config['Configurations'][0].get('Platform') config[platform_name] = recipe.get(platform_name) - with open(os.path.join(path, 'config.yaml'), 'w') as outfile: + + dir_path = os.path.join(filepath, datetime.now().strftime('%Y%m%d%H%M%S%f')) + make_dirs(dir_path) + logger.info('kickstart cache dir=%s' % dir_path) + + yamlinfo = YamlInfo(dir_path, + os.path.join(dir_path, 'configs.yaml'), + os.path.join(dir_path, 'repos.yaml')) + + # configs.yaml + with open(yamlinfo.configs, 'w') as outfile: yaml.dump(config, outfile, default_flow_style=False) - # repo.yaml + # repo.yaml if 'Repositories' in recipe: repos = {} repos['Repositories'] = recipe['Repositories'] - with open(os.path.join(path, 'repos.yaml'), 'w') as outfile: + with open(yamlinfo.repos, 'w') as outfile: yaml.dump(repos, outfile, default_flow_style=False) # partition info if 'Partitions' in recipe: for partition in recipe.get('Partitions'): - partition_path = os.path.join(path, 'partitions') + partition_path = os.path.join(dir_path, 'partitions') file_name = partition.get('Name') temp = os.path.join(partition_path, file_name) write(temp, partition['Contents']) @@ -143,17 +160,16 @@ def separate_recipe_file_for_ks(recipe): # script.post if 'PostScripts' in recipe: for script in recipe.get('PostScripts'): - script_path = os.path.join(path, 'scripts') + script_path = os.path.join(dir_path, 'scripts') script_type = script.get('Type') if script_type and script_type == 'nochroot': file_name = '%s.nochroot' % script.get('Name') else: file_name = '%s.post' % script.get('Name') write(os.path.join(script_path, file_name), script['Contents']) - -if __name__ == "__main__": - get_default_recipe() - recipe = load_yaml('/home/shinchulwoo/recipe/test/test.yaml') - separate_recipe_file_for_ks(recipe) - print('test') + return yamlinfo + +YamlType = collections.namedtuple('YamlInfo', 'cachedir, configs, repos') +def YamlInfo(cachedir, configs, repos): + return YamlType(cachedir, configs, repos) \ No newline at end of file diff --git a/tic/pykickstarter.py b/tic/pykickstarter.py index 59aa938..2e8d246 100644 --- a/tic/pykickstarter.py +++ b/tic/pykickstarter.py @@ -19,27 +19,27 @@ # Contributors: # - S-Core Co., Ltd +from tic.utils.error import TICError from kswriter.KSWriter import KSWriter, KSMetaError class KSoption: - def __init__(self): - self.configsfile = '/home/shinchulwoo/project/meta-mobile/mobile.yaml' - self.repofile = ['/home/shinchulwoo/project/meta-mobile/mobile-repos.yaml'] - self.target = '/home/shinchulwoo/project/meta-mobile/mobile-targets.yaml' - self.external = ['/home/shinchulwoo/project/meta-mobile/ks'] - self.outdir = '/home/shinchulwoo/project/meta-mobile/__test' + def __init__(self, configs, repos, outdir): + self.configsfile = configs + self.repofile = [repos] + self.target = None + self.external = None + self.outdir = outdir self.config = None - self.packages = None + self.packages = False self.targetdefs = None -def kswriter(): +def kswriter(options): try: - options = KSoption() ks = KSWriter(options.configsfile, options.repofile, options.outdir, options.config, options.packages, options.external, options.targetdefs, options.target) ks.generate() except KSMetaError as err: - print('ERROR:', str(err)) + raise TICError(str(err)) if __name__ == "__main__": print('start') diff --git a/tic/server/tic_server.py b/tic/server/tic_server.py index a1a6b4c..4750177 100644 --- a/tic/server/tic_server.py +++ b/tic/server/tic_server.py @@ -45,7 +45,10 @@ def exports(): try: logger = logging.getLogger(__name__) logger.info('%s - %s %s : data=%s' % (request.remote_addr, request.method, request.path, request.data)) - + exportInfo = json.loads(request.data) + type = request.args.get('format') + output = command.exports(type, exportInfo.get('recipe'), exportInfo.get('packages'), exportInfo.get('output')) + resp = makeresponse(output, None) except error.TICError as err: logger.error(err) resp = makeresponse(str(err), err) @@ -55,7 +58,6 @@ def exports(): except Exception as ex: logger.error(ex) resp = makeresponse(str(ex), ex) - return resp @@ -66,7 +68,7 @@ def start(port_num=59001): with app.test_request_context(): print(url_for('index')) print(url_for('analysis')) - app.run(host='0.0.0.0', port=port_num) + app.run(host='0.0.0.0', threaded=True, port=port_num) def makeresponse(data, err): -- 2.7.4