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