[TIC-CORE] Modify arguments of exports api
[archive/20170607/tools/tic-core.git] / tic / command.py
1 #!/usr/bin/python
2 # Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
3 #
4 # Contact: 
5 # @author Chulwoo Shin <cw1.shin@samsung.com>
6
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 # Contributors:
20 # - S-Core Co., Ltd
21
22 import os
23 import logging
24
25 from tic.dependency import analyze_dependency, get_installed_packages
26 from tic.parser.recipe_parser import get_default_recipe, convert_recipe_to_yaml
27 from tic.parser.repo_parser import RepodataParser
28 from tic.parser.view_parser import make_view_data
29 from tic.utils.error import TICError
30 from tic.utils.file import copyfile
31 from tic.repo import get_repodata_from_repos
32 from tic.pykickstarter import KSoption, kswriter
33
34
35 DEFAULT_CACHEDIR='/var/tmp/tic-core/cached'
36 DEFAULT_KICKSTARTDIR='/var/tmp/tic-core/kickstart'
37
38 def analyze(repo_list, recipe_list=None):
39     logger = logging.getLogger(__name__)
40     
41     if not repo_list and not recipe_list:
42         raise TICError('No repositories defined')
43     
44     repos = []
45     recipe = None
46     #TODO Repository check
47     # using default recipe (Temporary Code)
48     if recipe_list and recipe_list[0] == 'default':
49         recipe = get_default_recipe()
50         for repo_url in recipe.get('Repositories'):
51             repos.append({'name': repo_url.get('Name'), 
52                           'url': repo_url.get('Url')})
53     else:
54         number=1
55         for repo_url in repo_list:
56             repos.append({'name': 'repository_%d' % number, 
57                           'url': repo_url})
58             number = number + 1
59     
60     #Download repodata from repositories (Remote/Local)
61     repoinfo = get_repodata_from_repos(repos, DEFAULT_CACHEDIR)
62     
63     # Parse the xml files for the analysis of package (.rpm)
64     repo_parser = RepodataParser(repoinfo)
65     pkg_group = repo_parser.parse()
66     logger.info('pkg_list: %d, pkg2id: %d', len(pkg_group['pkg_list']), len(pkg_group['pkg2id']))
67     
68     # package install-dependency analysis
69     analyze_dependency(pkg_group)
70     # Make a data for TIC (Tizen image creation)
71     view_data = make_view_data(pkg_group)
72     inst_packages = get_installed_packages(recipe, repoinfo, pkg_group)
73     
74     result = {'packages': view_data,
75               'repos': repos,
76               'defaultpackages': inst_packages}
77     
78     return result
79
80 def exports(export_type, recipe, packages, outdir):
81     logger = logging.getLogger(__name__)
82     if not export_type:
83         export_type='ks'
84         logger.info('set default export format(.ks)')
85         
86     if not recipe:
87         raise TICError('No recipe defined')
88     if not packages or type(packages) is not list:
89         raise TICError('No packages defined')
90     
91     #TODO recipe parsing
92     # Temporary code for 1st prototype release
93     if recipe.get('name') == 'default':
94         recipe = get_default_recipe()
95         config = recipe.get('Configurations')[0]
96         for key in ['Default', config['Platform']]:
97             recipe[key]['Groups']=[]
98             recipe[key]['ExtraPackages']=[]
99         config['Groups']=[]
100         config['ExtraPackages'] = packages
101     else:
102         raise TICError('No recipes defined')
103     
104     # create the yaml
105     yaml_info = convert_recipe_to_yaml(recipe, DEFAULT_KICKSTARTDIR)
106     
107     # create kickstart(.ks) using kickstarter tool
108     options = KSoption(yaml_info.configs, yaml_info.repos, yaml_info.cachedir)
109     kswriter(options)
110     
111     # check whether the ks exists
112     baseline=recipe['Default'].get('Baseline')
113     ksname= ''.join([config.get('FileName'), '.ks'])
114     kspath=os.path.join(yaml_info.cachedir, baseline, ksname)
115     if not os.path.exists(kspath):
116         raise TICError('No ks file was created from kickstarter')
117     
118     # copy the ks to output directory
119     output=copyfile(kspath, outdir)
120     logger.info('copy the ks file from %s to dst:%s', kspath, output)
121     
122     return output