[TIC-CORE] fix partition for default recipe
[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 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 from tic.utils import process
34 from tic.utils import misc
35
36 DEFAULT_CACHEDIR='/var/tmp/tic-core'
37 DEFAULT_ANALYSIS_CACHEDIR='/var/tmp/tic-core/analysis'
38 DEFAULT_KICKSTARTDIR='/var/tmp/tic-core/kickstart'
39
40 def analyze(repo_list, recipe_list=None):
41     logger = logging.getLogger(__name__)
42     if not repo_list and not recipe_list:
43         raise TICError('No repositories defined')
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     start_time = misc.get_timestamp()
60     #Download repodata from repositories (Remote/Local)
61     repoinfo = get_repodata_from_repos(repos, DEFAULT_CACHEDIR)
62     logger.info('time to get repodata from repo: %d ms', misc.get_timestamp() - start_time)
63     
64 #     checksum_list=[]
65 #     for repo in repoinfo:
66 #         checksum_list.append(repo['checksum']);
67 #     all_checksum='_'.join(checksum_list);
68 #     analysis_file=os.path.join(DEFAULT_ANALYSIS_CACHEDIR, all_checksum, 'analysis.json')
69 #     if os.path.exists(analysis_file):
70 #         pass
71
72     start_time = misc.get_timestamp()
73     # Parse the xml files for the analysis of package (.rpm)
74     repo_parser = RepodataParser('armv7l', repoinfo)
75     pkg_group = repo_parser.parse()
76     logger.info('packages: %d, provides: %d, files: %d', len(pkg_group['pkg_dict']), len(pkg_group['provides']), len(pkg_group['files']))
77     logger.info('time to parse repodata: %d ms', misc.get_timestamp() - start_time)
78
79     start_time = misc.get_timestamp()
80     # Make a data for TIC (Tizen image creation)
81     view_data = make_view_data(pkg_group)
82     # analyze install-dependency
83     inst_packages = get_installed_packages(recipe, repoinfo, pkg_group)
84     logger.info('installed package: %d', len(inst_packages))
85     logger.info('time to analyze dependency: %d ms', misc.get_timestamp() - start_time)
86
87     result = {'view': view_data,
88               'data': {'packages': pkg_group.get('pkg_dict'),
89                        'provides': pkg_group.get('provides'),
90                        'files': pkg_group.get('files'),
91                        'groups': pkg_group.get('groups'),
92                        'conflicts': pkg_group.get('conflicts')},
93               'repos': repos,
94               'defaultpackages': inst_packages}
95     return result
96
97 def exports(export_type, recipe, packages, outdir, filename=None):
98     logger = logging.getLogger(__name__)
99     #TODO validation should be checked before request
100     if not export_type:
101         export_type='ks'
102         logger.info('set default export format(.ks)')
103
104     if not recipe:
105         raise TICError('No recipe defined')
106     if not packages or type(packages) is not list:
107         raise TICError('No packages defined')
108
109     #TODO recipe parsing
110     # Temporary code for 1st prototype release
111     if recipe.get('name') == 'default':
112         recipe = get_default_recipe()
113         config = recipe.get('Configurations')[0]
114         for key in ['Default', config['Platform']]:
115             recipe[key]['Groups']=[]
116             recipe[key]['ExtraPackages']=[]
117         config['Groups']=[]
118         config['ExtraPackages'] = packages
119     else:
120         raise TICError('No recipes defined')
121     
122     # create the yaml
123     yaml_info = convert_recipe_to_yaml(recipe, DEFAULT_KICKSTARTDIR)
124     
125     # create kickstart(.ks) using kickstarter tool
126     options = KSoption(yaml_info.configs, yaml_info.repos, yaml_info.cachedir)
127     kswriter(options)
128     
129     # check whether the ks exists
130     baseline=recipe['Default'].get('Baseline')
131     ksname= ''.join([config.get('FileName'), '.ks'])
132     kspath=os.path.join(yaml_info.cachedir, baseline, ksname)
133     if not os.path.exists(kspath):
134         raise TICError('No ks file was created from kickstarter')
135     
136     # copy the ks to output directory
137     output=copyfile(kspath, outdir, filename)
138     logger.info('copy the ks file from %s to dst:%s', kspath, output)
139     
140     return output
141
142 def createimage(recipes, ksfile, outdir):
143     logger = logging.getLogger(__name__)
144     
145     if recipes:
146         logger.info('the recipes option is not yet supported')
147         return
148     
149     if not os.path.exists(ksfile) or os.path.isdir(ksfile):
150         raise TICError('kickstart file does not exist')
151     
152     mic_command=['mic', 'cr', 'auto', ksfile]
153     if outdir:
154         mic_command.append('--outdir=%s' % outdir)
155     
156     process.run(mic_command, 2)