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