[TIC-CORE] support cli commands
[archive/20170607/tools/tic-core.git] / tic / command.py
index 65570d1..51f41cc 100644 (file)
@@ -1,14 +1,40 @@
+#!/usr/bin/python
+# Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
+#
+# Contact: 
+# @author Chulwoo Shin <cw1.shin@samsung.com>
+# 
+# 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
 
 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.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'
+DEFAULT_KICKSTARTDIR='/var/tmp/tic-core/kickstart'
 
 def analyze(repo_list, recipe_list=None):
     logger = logging.getLogger(__name__)
@@ -50,4 +76,66 @@ 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, 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:
+        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
+    output=copyfile(kspath, 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)