1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3""" 4Copyright (c) 2021 Huawei Device Co., Ltd. 5Licensed under the Apache License, Version 2.0 (the "License"); 6you may not use this file except in compliance with the License. 7You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11Unless required by applicable law or agreed to in writing, software 12distributed under the License is distributed on an "AS IS" BASIS, 13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14See the License for the specific language governing permissions and 15limitations under the License. 16 17""" 18import re 19 20 21class Module(): 22 def __init__(self, module_name: str, subsystem_name: str, part_name: str, deps: set, external_deps: set, raw_data): 23 self._module_name = module_name 24 self._subsystem_name = subsystem_name 25 self._part_name = part_name 26 self._deps = deps 27 self._external_deps = external_deps 28 self._raw_data = raw_data 29 30 @property 31 def module_name(self): 32 return self._module_name 33 34 @module_name.setter 35 def module_name(self, value): 36 self._module_name = value 37 38 @property 39 def subsystem_name(self): 40 return self._subsystem_name 41 42 @subsystem_name.setter 43 def subsystem_name(self, value): 44 self._subsystem_name = value 45 46 @property 47 def part_name(self): 48 return self._part_name 49 50 @part_name.setter 51 def part_name(self, value): 52 self._part_name = value 53 54 @property 55 def deps(self): 56 return self._deps 57 58 @deps.setter 59 def deps(self, value): 60 self._deps = value 61 62 @property 63 def external_deps(self): 64 return self._external_deps 65 66 @external_deps.setter 67 def external_deps(self, value): 68 self._external_deps = value 69 70 @property 71 def raw_data(self): 72 return self._raw_data 73 74 @raw_data.setter 75 def raw_data(self, value): 76 self._raw_data = value 77 78 @staticmethod 79 def create_module_by_string(ohos_string, is_so): 80 module_name = '' 81 subsystem_name = '' 82 part_name = '' 83 84 module_pattern = re.compile(r'(?<=ohos_shared_library\(").+(?="\))') if is_so else \ 85 re.compile(r'(?<=ohos_executable\(").+(?="\))') 86 try: 87 module_name = module_pattern.search(ohos_string).group() 88 except AttributeError as e: 89 print('The BUILD.gn file is not written in a standard format and cannot be parsed, The error snippet is :') 90 print(ohos_string) 91 92 subsystem_pattern = re.compile(r'(?<=subsystem_name = ").+?(?="\n)') 93 if subsystem_pattern.search(ohos_string) != None: 94 subsystem_name = subsystem_pattern.search(ohos_string).group() 95 96 part_pattern = re.compile(r'(?<=part_name = ").+?(?="\n)') 97 if part_pattern.search(ohos_string) != None: 98 part_name = part_pattern.search(ohos_string).group() 99 100 deps_pattern = re.compile(r'(?<=deps = \[).+?(?=])', re.DOTALL) 101 deps_raw = deps_pattern.search(ohos_string).group() if deps_pattern.search(ohos_string) != None \ 102 else '' 103 deps_list = [] 104 deps_raw_list = deps_raw.replace('\n', '').replace(' ', '').replace('"', '').split(',') 105 for dep in deps_raw_list: 106 dep = dep.split('/')[-1] 107 deps_list.append(dep) 108 109 external_deps_pattern = re.compile(r'(?<=external_deps = \[).+?(?=])', re.DOTALL) 110 external_deps_raw = external_deps_pattern.search(ohos_string).group() if external_deps_pattern.search(ohos_string) != None \ 111 else '' 112 external_deps_list = [] 113 external_deps_raw_list = external_deps_raw.replace('\n', '').replace(' ', '').replace('"', '').split(',') 114 for dep in external_deps_raw_list: 115 dep = dep.split('/')[-1] 116 external_deps_list.append(dep) 117 118 if external_deps_list.__contains__(''): 119 external_deps_list.remove('') 120 if deps_list.__contains__(''): 121 deps_list.remove('') 122 external_deps = set(external_deps_list) 123 deps = set(set(deps_list) - external_deps) 124 return Module(module_name, subsystem_name, part_name, deps, external_deps, ohos_string) 125 126 127class Node(): 128 def __init__(self, component_name: str): 129 self._components_name = component_name 130 self._modules = {} 131 self._deps = set() 132 self._external_deps = set() 133 134 @property 135 def components_name(self): 136 return self._components_name 137 138 @components_name.setter 139 def components_name(self, value): 140 self._components_name = value 141 142 @property 143 def deps(self): 144 return self._deps 145 146 @deps.setter 147 def deps(self, value): 148 self._deps = value 149 150 def add_module(self, module): 151 self._modules[module.module_name] = module 152 self._deps = self._deps | module.deps 153 self._external_deps = self._external_deps | module.external_deps 154 155