[TIC-CORE] Modify data structure with tic-web
[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='mobile-mbr',
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         Emulator64wayland=dict(
54             Part='mobile-mbr',
55             UserGroups='audio,video',
56             Groups=[
57                 'Generic Base'],
58 #                 'Mobile Base',
59 #                 'Mobile Console Tools',
60 #                 'Mobile Adaptation',
61 #                 'Mobile Wayland',
62 #                 'Mobile Middleware',
63 #                 'Mobile Applications',
64 #                 'Generic Multimedia',
65 #                 'Mobile Multimedia',
66 #                 'Generic Desktop Applications',
67 #                 'Mobile Dali',
68 #                 'Mobile EFL',
69 #                 'Mobile Enlightenment',
70 #                 'Mobile Input Framework',
71 #                 'Mobile Connectivity Framework',
72 #                 'Mobile Bluetooth',
73 #                 'Mobile Web Framework',
74 #                 'Mobile Telephony'],
75             PostScripts=[],
76             Repos= [],
77             NoChrootScripts=[]
78         ),
79         Configurations=[
80             dict(
81                 Name='mobile-emulator64-wayland',
82                 Architecture='x86_64',
83                 Schedule= "*",
84                 Active= True,
85                 Platform= 'Emulator64wayland',
86                 Part= 'mobile-2parts-emulator',
87                 Mic2Options= '-f loop --pack-to=@NAME@.tar.gz',
88                 FileName= 'mobile-emulator64-wayland',
89                 Repos=['mobile-emulator64-wayland', 'base_emulator64'],
90                 Groups=[],
91 #                 Groups=['Mobile Adaptation Emulator'],
92                 ExtraPackages= [],
93                 RemovePackages=[]
94             )
95         ],
96         Repositories=[
97             dict(Name='mobile-emulator64-wayland',
98                  Url='http://download.tizen.org/snapshots/tizen/mobile/latest/repos/emulator64-wayland/packages/',
99                  Options='--ssl_verify=no'),
100             dict(Name='base_emulator64',
101                  Url='http://download.tizen.org/snapshots/tizen/base/latest/repos/emulator64/packages/',
102                  Options='--ssl_verify=no')
103         ],
104         Partitions=[
105             dict(Name='mobile-mbr',
106                  Contents='part / --fstype="ext4" --size=3584 --ondisk=sda --active --label platform --fsoptions=defaults,noatime'),
107             dict(Name= 'mobile-2parts-emulator',
108                  Contents='part / --size=2000 --ondisk=sda --fstype=ext4 --label=emulator-rootfs\npart /opt/ --size=2000 --ondisk=sda --fstype=ext4 --label=emulator-sysdata')
109         ]
110     )
111     return recipe
112
113 def load_yaml(path):
114     try:
115         with file(path) as f:
116             return yaml.load(f)
117     except IOError:
118         raise error.TICError('cannot read meta file: %s' % path)
119     except:
120         raise error.TICError('yaml format error of meta file: %s' % path)
121     
122
123 def convert_recipe_to_yaml(recipe, filepath):
124     logger = logging.getLogger(__name__)
125     
126     # config.yaml
127     config = dict(Default=None, Configurations=[])
128     config['Default'] = recipe.get('Default')
129     # targets (only one target)
130     config['Configurations'].append(recipe.get('Configurations')[0])
131     platform_name = config['Configurations'][0].get('Platform')
132     config[platform_name] = recipe.get(platform_name)
133     
134     dir_path = os.path.join(filepath, datetime.now().strftime('%Y%m%d%H%M%S%f'))
135     make_dirs(dir_path)
136     logger.info('kickstart cache dir=%s' % dir_path)
137     
138     yamlinfo = YamlInfo(dir_path,
139                         os.path.join(dir_path, 'configs.yaml'),
140                         os.path.join(dir_path, 'repos.yaml'))
141     
142     # configs.yaml
143     with open(yamlinfo.configs, 'w') as outfile:
144         yaml.dump(config, outfile, default_flow_style=False)
145
146     # repo.yaml
147     if 'Repositories' in recipe:
148         repos = {}
149         repos['Repositories'] = recipe['Repositories']
150         with open(yamlinfo.repos, 'w') as outfile:
151             yaml.dump(repos, outfile, default_flow_style=False)
152     
153     # partition info
154     if 'Partitions' in recipe:
155         for partition in recipe.get('Partitions'):
156             partition_path = os.path.join(dir_path, 'partitions')
157             file_name = partition.get('Name')
158             temp = os.path.join(partition_path, file_name)
159             write(temp, partition['Contents'])
160     
161     # script.post
162     if 'PostScripts' in recipe:
163         for script in recipe.get('PostScripts'):
164             script_path = os.path.join(dir_path, 'scripts')
165             script_type = script.get('Type')
166             if script_type and script_type == 'nochroot':
167                 file_name = '%s.nochroot' % script.get('Name')
168             else:
169                 file_name = '%s.post' % script.get('Name')
170             write(os.path.join(script_path, file_name), script['Contents'])
171     
172     return yamlinfo
173
174 YamlType = collections.namedtuple('YamlInfo', 'cachedir, configs, repos')
175 def YamlInfo(cachedir, configs, repos):
176     return YamlType(cachedir, configs, repos)