2a883cb8ffb9cb643e60adec7b38bb24670bf2a9
[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 import time
25
26 from tic.dependency import get_installed_packages
27 from tic.parser.recipe_parser import get_default_recipe, convert_recipe_to_yaml
28 from tic.parser.repo_parser import RepodataParser
29 from tic.parser.view_parser import make_view_data
30 from tic.utils.error import TICError
31 from tic.utils.file import copyfile
32 from tic.repo import get_repodata_from_repos
33 from tic.pykickstarter import KSoption, kswriter
34 from tic.utils import process
35
36
37 DEFAULT_CACHEDIR='/var/tmp/tic-core/cached'
38 DEFAULT_KICKSTARTDIR='/var/tmp/tic-core/kickstart'
39
40 current_milli_time = lambda: int(round(time.time() * 1000))
41
42 def analyze(repo_list, recipe_list=None):
43     logger = logging.getLogger(__name__)
44     
45     if not repo_list and not recipe_list:
46         raise TICError('No repositories defined')
47     
48     repos = []
49     recipe = None
50     #TODO Repository check
51     # using default recipe (Temporary Code)
52     if recipe_list and recipe_list[0] == 'default':
53         recipe = get_default_recipe()
54         for repo_url in recipe.get('Repositories'):
55             repos.append({'name': repo_url.get('Name'), 
56                           'url': repo_url.get('Url')})
57     else:
58         number=1
59         for repo_url in repo_list:
60             repos.append({'name': 'repository_%d' % number, 
61                           'url': repo_url})
62             number = number + 1
63     
64     start_time = current_milli_time()
65     #Download repodata from repositories (Remote/Local)
66     repoinfo = get_repodata_from_repos(repos, DEFAULT_CACHEDIR)
67     logger.info('time to get repodata from repo: %d ms', current_milli_time() - start_time)
68     
69     start_time = current_milli_time()
70     # Parse the xml files for the analysis of package (.rpm)
71     repo_parser = RepodataParser('armv7l', repoinfo)
72     pkg_group = repo_parser.parse()
73     logger.info('packages: %d, provides: %d, files: %d', len(pkg_group['pkg_dict']), len(pkg_group['provides']), len(pkg_group['files']))
74     logger.info('time to parse repodata: %d ms', current_milli_time() - start_time)
75     
76     start_time = current_milli_time()
77     # Make a data for TIC (Tizen image creation)
78     view_data = make_view_data(pkg_group)
79     # analyze install-dependency
80     inst_packages = get_installed_packages(recipe, repoinfo, pkg_group)
81     logger.info('installed package: %d', len(inst_packages))
82     logger.info('time to analyze dependency: %d ms', current_milli_time() - start_time)
83     
84     
85     start_time = current_milli_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     
96     return result
97
98 def exports(export_type, recipe, packages, outdir, filename=None):
99     logger = logging.getLogger(__name__)
100     
101     #TODO validation should be checked before request
102     if not export_type:
103         export_type='ks'
104         logger.info('set default export format(.ks)')
105     
106     if not recipe:
107         raise TICError('No recipe defined')
108     if not packages or type(packages) is not list:
109         raise TICError('No packages defined')
110     
111     #TODO recipe parsing
112     # Temporary code for 1st prototype release
113     if recipe.get('name') == 'default':
114         recipe = get_default_recipe()
115         config = recipe.get('Configurations')[0]
116         for key in ['Default', config['Platform']]:
117             recipe[key]['Groups']=[]
118             recipe[key]['ExtraPackages']=[]
119         config['Groups']=[]
120         config['ExtraPackages'] = packages
121     else:
122         raise TICError('No recipes defined')
123     
124     # create the yaml
125     yaml_info = convert_recipe_to_yaml(recipe, DEFAULT_KICKSTARTDIR)
126     
127     # create kickstart(.ks) using kickstarter tool
128     options = KSoption(yaml_info.configs, yaml_info.repos, yaml_info.cachedir)
129     kswriter(options)
130     
131     # check whether the ks exists
132     baseline=recipe['Default'].get('Baseline')
133     ksname= ''.join([config.get('FileName'), '.ks'])
134     kspath=os.path.join(yaml_info.cachedir, baseline, ksname)
135     if not os.path.exists(kspath):
136         raise TICError('No ks file was created from kickstarter')
137     
138     # copy the ks to output directory
139     output=copyfile(kspath, outdir, filename)
140     logger.info('copy the ks file from %s to dst:%s', kspath, output)
141     
142     return output
143
144 def createimage(recipes, ksfile, outdir):
145     logger = logging.getLogger(__name__)
146     
147     if recipes:
148         logger.info('the recipes option is not yet supported')
149         return
150     
151     if not os.path.exists(ksfile) or os.path.isdir(ksfile):
152         raise TICError('kickstart file does not exist')
153     
154     mic_command=['mic', 'cr', 'auto', ksfile]
155     if outdir:
156         mic_command.append('--outdir=%s' % outdir)
157     
158     process.run(mic_command, 2)