[TIC-CORE] support caching for analysis data
[archive/20170607/tools/tic-core.git] / tic / parser / recipe_parser.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 collections
23 from datetime import datetime
24 import logging
25 import os
26 from tic.utils import error
27 from tic.utils.file import write, make_dirs
28 import yaml
29
30
31 def get_default_recipe():
32     recipe = dict(
33         Default=dict(
34             Baseline= 'tizen-3.0',
35             Active= True,
36             Mic2Options= '-f raw --fstab=uuid --copy-kernel --compress-disk-image=bz2 --generate-bmap',
37             Part='headless',
38             Language= 'en_US.UTF-8',
39             Keyboard= 'us',
40             Timezone= 'Asia/Seoul',
41             RootPass= 'tizen',
42             DefaultUser= 'guest',
43             DefaultUserPass= 'tizen',
44             BootLoader= True,
45             BootloaderAppend= "rw vga=current splash rootwait rootfstype=ext4 plymouth.enable=0",
46             BootloaderTimeout= 3,
47             BootloaderOptions= '--ptable=gpt --menus="install:Wipe and Install:systemd.unit=system-installer.service:test"',
48             StartX= False,
49             Desktop= 'None',
50             SaveRepos= False,
51             UserGroups= "audio,video"
52         ),
53         Wayland=dict(
54             Part='headless',
55             UserGroups='audio,video',
56             Groups=[],
57             PostScripts=[],
58             Repos= [],
59             NoChrootScripts=[]
60         ),
61         Configurations=[
62             dict(
63                 Name='tizen-headless',
64                 Architecture='armv7l',
65                 Schedule= "*",
66                 Active= True,
67                 Platform= 'Wayland',
68                 Part= 'headless',
69                 Mic2Options= '-f loop --pack-to=@NAME@.tar.gz',
70                 FileName= 'tizen-headless-tm1',
71                 Repos=['tizen-unified', 'tizen-base'],
72                 Groups=[],
73                 ExtraPackages= [],
74                 RemovePackages=[]
75             )
76         ],
77         Repositories=[
78             dict(Name='tizen-unified',
79                  Url='http://download.tizen.org/live/devel:/Tizen:/Unified/standard/',
80                  Options='--ssl_verify=no'),
81             dict(Name='tizen-base',
82                  Url='http://download.tizen.org/snapshots/tizen/base/latest/repos/arm/packages/',
83                  Options='--ssl_verify=no')
84         ],
85         Partitions=[
86             dict(Name='headless',
87                  Contents='part / --size=2000 --ondisk mmcblk0p --fstype=ext4 --label=rootfs --extoptions=\"-J size=16\" \n\
88 part /opt/ --size=1000 --ondisk mmcblk0p --fstype=ext4 --label=system-data --extoptions="-m 0" \n\
89 part /boot/kernel/mod_tizen_tm1/lib/modules --size=12 --ondisk mmcblk0p --fstype=ext4 --label=modules \n')
90         ]
91     )
92     return recipe
93
94 def load_yaml(path):
95     try:
96         with file(path) as f:
97             return yaml.load(f)
98     except IOError:
99         raise error.TICError('cannot read meta file: %s' % path)
100     except:
101         raise error.TICError('yaml format error of meta file: %s' % path)
102     
103
104 def convert_recipe_to_yaml(recipe, filepath):
105     logger = logging.getLogger(__name__)
106     
107     # config.yaml
108     config = dict(Default=None, Configurations=[])
109     config['Default'] = recipe.get('Default')
110     # targets (only one target)
111     config['Configurations'].append(recipe.get('Configurations')[0])
112     platform_name = config['Configurations'][0].get('Platform')
113     config[platform_name] = recipe.get(platform_name)
114     
115     dir_path = os.path.join(filepath, datetime.now().strftime('%Y%m%d%H%M%S%f'))
116     make_dirs(dir_path)
117     logger.info('kickstart cache dir=%s' % dir_path)
118     
119     yamlinfo = YamlInfo(dir_path,
120                         os.path.join(dir_path, 'configs.yaml'),
121                         os.path.join(dir_path, 'repos.yaml'))
122     
123     # configs.yaml
124     with open(yamlinfo.configs, 'w') as outfile:
125         yaml.safe_dump(config, outfile, default_flow_style=False)
126
127     # repo.yaml
128     if 'Repositories' in recipe:
129         repos = {}
130         repos['Repositories'] = recipe['Repositories']
131         with open(yamlinfo.repos, 'w') as outfile:
132             yaml.safe_dump(repos, outfile, default_flow_style=False)
133     
134     # partition info
135     if 'Partitions' in recipe:
136         for partition in recipe.get('Partitions'):
137             partition_path = os.path.join(dir_path, 'partitions')
138             file_name = partition.get('Name')
139             temp = os.path.join(partition_path, file_name)
140             write(temp, partition['Contents'])
141     
142     # script.post
143     if 'PostScripts' in recipe:
144         for script in recipe.get('PostScripts'):
145             script_path = os.path.join(dir_path, 'scripts')
146             script_type = script.get('Type')
147             if script_type and script_type == 'nochroot':
148                 file_name = '%s.nochroot' % script.get('Name')
149             else:
150                 file_name = '%s.post' % script.get('Name')
151             write(os.path.join(script_path, file_name), script['Contents'])
152     
153     return yamlinfo
154
155 YamlType = collections.namedtuple('YamlInfo', 'cachedir, configs, repos')
156 def YamlInfo(cachedir, configs, repos):
157     return YamlType(cachedir, configs, repos)