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