1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2020-2021 Huawei Device Co., Ltd. 5# 6# HDF is dual licensed: you can use it either under the terms of 7# the GPL, or the BSD license, at your option. 8# See the LICENSE file in the root of this repository for complete details. 9 10 11import json 12import os 13import hashlib 14 15from hdf_tool_exception import HdfToolException 16from hdf_tool_settings import HdfToolSettings 17 18 19class WordsConverter(object): 20 def __init__(self, words_str): 21 self.words_str = words_str 22 if len(self.words_str) == 0: 23 raise HdfToolException('empty words') 24 self.words_list = self.split() 25 self.upper = '_'.join([w.upper() for w in self.words_list]) 26 self.lower = '_'.join([w.lower() for w in self.words_list]) 27 self.upper_camel = ''.join([w.capitalize() for w in self.words_list]) 28 if len(self.upper_camel) == 1: 29 self.lower_camel = self.upper_camel[0].lower() 30 else: 31 self.lower_camel = '%s%s' % (self.upper_camel[0].lower(), 32 self.upper_camel[1:]) 33 34 def _capital_pos_generator(self): 35 count = len(self.words_str) 36 for i in range(count): 37 if self.words_str[i].isupper(): 38 yield i 39 yield count 40 41 def split(self): 42 words_list = [] 43 capital_split_words = [] 44 start = 0 45 for i in self._capital_pos_generator(): 46 end = i 47 if end != start: 48 capital_split_words.append(self.words_str[start:end]) 49 start = end 50 for word in capital_split_words: 51 words_list.extend([w for w in word.split('_') if w]) 52 return words_list 53 54 def upper_case(self): 55 return self.upper 56 57 def lower_case(self): 58 return self.lower 59 60 def upper_camel_case(self): 61 return self.upper_camel 62 63 def lower_camel_case(self): 64 return self.lower_camel 65 66 67class SectionRange(object): 68 def __init__(self, start_pos, end_pos): 69 self.start_pos = start_pos 70 self.end_pos = end_pos 71 72 73class SectionContent(object): 74 def __init__(self, begin_flag, content, end_flag): 75 self.begin_flag = begin_flag 76 self.content = content 77 self.end_flag = end_flag 78 79 def to_str(self): 80 return '%s%s%s' % (self.begin_flag, self.content, self.end_flag) 81 82 83def find_section(file_content, section_content): 84 if not file_content or not section_content: 85 return False 86 start_pos = file_content.find(section_content.begin_flag) 87 if start_pos == -1: 88 return False 89 end_pos = file_content.find(section_content.end_flag) 90 if end_pos == -1: 91 return False 92 return SectionRange(start_pos, end_pos + len(section_content.end_flag) - 1) 93 94 95def _write_one_section(file_obj, section_content): 96 if not file_obj or not section_content: 97 return 98 file_obj.write(section_content.begin_flag) 99 file_obj.write(section_content.content) 100 file_obj.write(section_content.end_flag) 101 102 103def add_before_and_save(file_content, file_path, pos_range, new_section): 104 if not file_content or not file_path or not pos_range or not new_section: 105 return 106 with open(file_path, 'w', newline='\n') as file_write: 107 file_write.write(file_content[:pos_range.start_pos]) 108 _write_one_section(file_write, new_section) 109 file_write.write(file_content[pos_range.start_pos:]) 110 111 112def add_after_and_save(file_content, file_path, pos_range, new_section): 113 if not file_content or not file_path or not pos_range or not new_section: 114 return 115 with open(file_path, 'w', newline='\n') as file_write: 116 file_write.write(file_content[:pos_range.end_pos + 1]) 117 _write_one_section(file_write, new_section) 118 if len(file_content) > pos_range.end_pos + 1: 119 file_write.write(file_content[pos_range.end_pos + 1:]) 120 121 122def append_and_save(file_content, file_path, new_section): 123 if not file_content or not file_path or not new_section: 124 return 125 with open(file_path, 'w', newline='\n') as file_write: 126 file_write.write(file_content) 127 _write_one_section(file_write, new_section) 128 129 130def delete_and_save(file_content, file_path, delete_range): 131 if not file_content or not file_path or not delete_range: 132 return 133 length = len(file_content) 134 with open(file_path, 'w', newline='\n') as file_write: 135 file_write.write(file_content[:delete_range.start_pos]) 136 if delete_range.end_pos + 1 < length: 137 file_write.write(file_content[delete_range.end_pos + 1:]) 138 139 140def replace_and_save(file_content, file_path, old_range, new_section): 141 if not file_content or not file_path or not old_range or not new_section: 142 return 143 with open(file_path, 'w', newline='\n') as file_write: 144 file_write.write(file_content[:old_range.start_pos]) 145 _write_one_section(file_write, new_section) 146 file_write.write(file_content[old_range.end_pos + 1:]) 147 148 149def get_id(part2): 150 full_name = part2 151 return hashlib.sha256(full_name.encode('utf8')).hexdigest()[:32] 152 153 154def create_dirs(dir_path): 155 if dir_path and not os.path.exists(dir_path): 156 try: 157 os.makedirs(dir_path) 158 except Exception: 159 raise HdfToolException('create dirs fail: %s' % dir_path) 160 161 162def read_file(file_path): 163 with open(file_path, encoding="utf-8") as file_read: 164 content = file_read.read() 165 return content 166 167 168def read_file_lines(file_path, code_type="utf-8"): 169 with open(file_path, encoding=code_type) as file_read: 170 lines = file_read.readlines() 171 return lines 172 173 174def write_file(file_path, content): 175 with open(file_path, 'w+', newline='\n') as file_write: 176 file_write.write(content) 177 178 179def write_file_lines(file_path, content, code_type="utf-8"): 180 with open(file_path, 'w', encoding=code_type) as file_write: 181 file_write.writelines(content) 182 183 184def get_framework_lite_dir(root): 185 return os.path.join(root, 'drivers', 'adapter', 'khdf', 'liteos') 186 187 188def get_vendor_root_dir(root): 189 return os.path.join(root, 'vendor') 190 191 192def get_vendor_dir(root, vendor): 193 return os.path.join(get_vendor_root_dir(root), vendor) 194 195 196def get_vendor_hdf_dir_framework(root): 197 relative_path = HdfToolSettings().get_drivers_path_framework() 198 return os.path.join(root, relative_path) 199 200 201def get_vendor_hdf_dir_adapter(root, kernel='liteos'): 202 relative_path = HdfToolSettings().get_drivers_path_adapter() 203 return os.path.join(root, relative_path, kernel) 204 205 206def get_vendor_lite_mk_path(root): 207 return os.path.join(get_vendor_hdf_dir_adapter(root), 'hdf_lite.mk') 208 209 210def get_vendor_makefile_path(root, kernel): 211 return os.path.join(get_vendor_hdf_dir_adapter(root, kernel), 'Makefile') 212 213 214def get_dot_configs_path(root, vendor, board): 215 path = os.path.join(root, "vendor", vendor, board, 'kernel_configs') 216 return [os.path.join(path, i) for i in os.listdir(path)] 217 218 219def get_module_dir(root, module=""): 220 return os.path.join(get_vendor_hdf_dir_framework(root), 'model', module) 221 222 223def get_drv_root_dir(root, module): 224 return os.path.join(get_module_dir(root, module), 'driver') 225 226 227def get_drv_dir(root, module, driver): 228 return os.path.join(get_drv_root_dir(root, module), driver) 229 230 231def get_drv_src_dir(root, module): 232 return get_drv_root_dir(root, module) 233 234 235def get_drv_include_dir(root, module, driver): 236 return os.path.join(get_drv_dir(root, module, driver), 'include') 237 238 239def get_vendor_kconfig_path(root, kernel): 240 hdf_dir = get_vendor_hdf_dir_adapter(root, kernel) 241 return os.path.join(hdf_dir, 'Kconfig') 242 243 244def get_module_kconfig_path(root, module): 245 return os.path.join(get_drv_root_dir(root, module), 'Kconfig') 246 247 248def get_module_mk_path(root, module): 249 return os.path.join(get_drv_root_dir(root, module), 'Makefile') 250 251 252def get_liteos_a_dot_config_path(root): 253 return os.path.join(root, 'kernel', 'liteos_a', '.config') 254 255 256def get_resources_dir(): 257 cur_dir = os.path.realpath(os.path.dirname(__file__)) 258 return os.path.join(cur_dir, 'resources') 259 260 261def get_templates_dir(): 262 return os.path.join(get_resources_dir(), 'templates') 263 264 265def get_templates_lite_dir(): 266 return os.path.join(get_templates_dir(), 'lite') 267 268 269def get_template(template_name, type_='lite'): 270 templates_dir = os.path.join(get_templates_dir(), type_) 271 template = os.path.join(templates_dir, template_name) 272 with open(template) as file_read: 273 template_str = file_read.read() 274 return template_str 275 276 277def get_hdf_lite_settings_mk_path(root_dir): 278 return os.path.join(get_framework_lite_dir(root_dir), 279 'hdf_lite_settings.mk') 280 281 282def get_hdf_lite_mk_path(root_dir): 283 return os.path.join(get_framework_lite_dir(root_dir), 284 'hdf_lite.mk') 285 286 287def get_hdf_lite_kconfig_path(root_dir): 288 return os.path.join(get_framework_lite_dir(root_dir), 289 'Kconfig') 290 291 292def is_commented_line(line, comment_start): 293 if line.strip().startswith(comment_start): 294 return True 295 else: 296 return False 297 298 299def get_vendor_gn_path(root): 300 return os.path.join(get_vendor_hdf_dir_adapter(root), 'model', 'BUILD.gn') 301 302 303def get_template_file_path(root): 304 template_relative_path = HdfToolSettings().get_template_path() 305 relative_path2 = HdfToolSettings().get_drivers_path_framework() 306 return os.path.join(root, relative_path2, template_relative_path) 307 308 309def get_hcs_file_path(root, vendor, board): 310 return os.path.join(root, "vendor", vendor, board, 311 "hdf_config", "device_info", "device_info.hcs") 312 313 314def template_filename_filtrate(dir_path, kernal): 315 filename_list = [] 316 for filename in os.listdir(dir_path): 317 if filename.split("_")[0] == kernal.capitalize(): 318 filename_list.append(filename) 319 return filename_list 320 321 322def get_create_model_info(root, create_data): 323 data = json.loads(create_data) 324 out_model_list = [] 325 if not data: 326 return [] 327 file_key_list = list(list(data.items())[0][-1].keys())[:-1] 328 for k, _ in data.items(): 329 model_file_path = {} 330 for key in file_key_list: 331 if key.split("_")[-1] == "path": 332 path_dict = data[k][key] 333 if isinstance(path_dict, dict): 334 for k_filename, file_path in path_dict.items(): 335 if not os.path.exists(os.path.join(root, file_path)): 336 model_file_path[k_filename] = " " 337 else: 338 model_file_path[k_filename] = path_dict[k_filename] 339 else: 340 hcs_file_path = os.path.join(root, path_dict) 341 if not os.path.exists(hcs_file_path): 342 model_file_path[key] = " " 343 else: 344 model_file_path[key] = path_dict 345 out_model_list.append({k: model_file_path}) 346 return out_model_list 347 348 349def get_config_config_path(root, kernel): 350 return os.path.join(root, "kernel", kernel, "config", "linux-5.10")