issue:#I99SM0 通用代码规范告警治理

Signed-off-by: xuezhou_yan <yangang20@huawei.com>
This commit is contained in:
xuezhou_yan
2024-03-19 16:01:54 +08:00
parent 6fd25a3626
commit 843e282bd3
15 changed files with 646 additions and 628 deletions
+5
View File
@@ -18,6 +18,7 @@
from elf_file_mgr import ElfFileMgr
def __createArgParser():
import argparse
@@ -34,6 +35,7 @@ def __createArgParser():
return parser
def _deps_guard_module(out_path, args=None):
mgr = ElfFileMgr(out_path)
mgr.scan_all_files()
@@ -47,6 +49,7 @@ def _deps_guard_module(out_path, args=None):
raise Exception("ERROR: deps_guard failed.")
def _startup_guard_module(out_path, args):
import sys
import os
@@ -60,10 +63,12 @@ def _startup_guard_module(out_path, args):
startup_guard(out_path, args)
def deps_guard(out_path, args=None):
_deps_guard_module(out_path, args)
_startup_guard_module(out_path, args)
if __name__ == '__main__':
parser = __createArgParser()
+70 -70
View File
@@ -17,89 +17,89 @@
#
import os
from stat import *
from stat import ST_SIZE
from .utils import command
class ElfFile(dict):
def __init__(self, file, prefix):
self._f = file
self._f_safe = "'%s'" % file
def __init__(self, file, prefix):
self._f = file
self._f_safe = "'%s'" % file
self["name"] = os.path.basename(file)
self["size"] = os.stat(self._f)[ST_SIZE]
if self["name"].find(".so") > 0:
self["type"] = "lib"
else:
self["type"] = "bin"
self["path"] = file[len(prefix):]
self["name"] = os.path.basename(file)
self["size"] = os.stat(self._f)[ST_SIZE]
if self["name"].find(".so") > 0:
self["type"] = "lib"
else:
self["type"] = "bin"
self["path"] = file[len(prefix):]
def __eq__(self, other):
if not isinstance(other, ElfFile):
return NotImplemented
def __eq__(self, other):
if not isinstance(other, ElfFile):
return NotImplemented
return self["path"] == other["path"]#and self["name"] == other["name"]
return self["path"] == other["path"]
def __extract_soname(self):
soname_data = command("mklibs-readelf", "--print-soname", self._f_safe)
if soname_data:
return soname_data.pop()
return ""
def is_library(self):
if self["name"].find(".so") > 0:
return True
return False
def __extract_elf_size(self):
size_data = command("size", self._f_safe)
if not size_data or len(size_data) < 2:
self["text_size"] = 0
self["data_size"] = 0
self["bss_size"] = 0
return 0
def get_file(self):
return self._f
vals = size_data[1].split()
self["text_size"] = int(vals[0])
self["data_size"] = int(vals[1])
self["bss_size"] = int(vals[2])
return ""
# Return a set of libraries the passed objects depend on.
def library_depends(self):
if not os.access(self._f, os.F_OK):
raise Exception("Cannot find lib: " + self._f)
dynamics = command("readelf", "--dynamic", self._f_safe)
res = []
for line in dynamics:
pos = line.find("(NEEDED)")
if pos <= 0:
continue
line = line[pos + 8:]
line = line.strip()
if not line.startswith("Shared library:"):
continue
line = line[15:]
line = line.strip()
if line.startswith("["):
line = line[1:]
if line.endswith("]"):
line = line[:-1]
line = line.strip()
res.append(line)
return res
def is_library(self):
if self["name"].find(".so") > 0:
return True
return False
def __extract_soname(self):
soname_data = command("mklibs-readelf", "--print-soname", self._f_safe)
if soname_data:
return soname_data.pop()
return ""
def get_file(self):
return self._f
# Return a set of libraries the passed objects depend on.
def library_depends(self):
if not os.access(self._f, os.F_OK):
raise Exception("Cannot find lib: " + self._f)
dynamics = command("readelf", "--dynamic", self._f_safe)
res = []
for line in dynamics:
pos = line.find("(NEEDED)")
if pos <= 0:
continue
line = line[pos + 8:]
line = line.strip()
if not line.startswith("Shared library:"):
continue
line = line[15:]
line = line.strip()
if line.startswith("["):
line = line[1:]
if line.endswith("]"):
line = line[:-1]
line = line.strip()
res.append(line)
return res
def __extract_elf_size(self):
size_data = command("size", self._f_safe)
if not size_data or len(size_data) < 2:
self["text_size"] = 0
self["data_size"] = 0
self["bss_size"] = 0
return 0
vals = size_data[1].split()
self["text_size"] = int(vals[0])
self["data_size"] = int(vals[1])
self["bss_size"] = int(vals[2])
return ""
if __name__ == '__main__':
import elf_walker
import elf_walker
cnt = 0
elfFiles = elf_walker.ELFWalker()
for f in elfFiles.get_elf_files():
if f.find("libskia_ohos.z.so") < 0:
continue
elf = ElfFile(f, elfFiles.get_product_images_path())
print(f)
cnt = 0
elfFiles = elf_walker.ELFWalker()
for f in elfFiles.get_elf_files():
if f.find("libskia_ohos.z.so") < 0:
continue
elf = ElfFile(f, elfFiles.get_product_images_path())
print(f)
+204 -202
View File
@@ -22,251 +22,253 @@ import os
from .elf_file import ElfFile
from .elf_walker import ELFWalker
class ElfFileWithDepsInfo(ElfFile):
def __init__(self, file, prefix):
super(ElfFileWithDepsInfo, self).__init__(file, prefix)
self["deps"] = []
self["dependedBy"] = []
def __eq__(self, other):
if not isinstance(other, ElfFileWithDepsInfo):
return NotImplemented
return self["id"] == other["id"]
def dependsOn(self, mod):
for dep in self["deps"]:
if dep["callee"] == mod:
return True
return False
def __repr__(self):
return self.__str__()
def __str__(self):
return "%s:%d deps(%d) dependedBy(%d)" % (self["name"], self["id"], len(self["deps"]), len(self["dependedBy"]))
class Dependency(dict):
def __init__(self, idx, caller, callee):
self["id"] = idx
self["caller_id"] = caller["id"]
self["callee_id"] = callee["id"]
self["caller"] = caller
self["callee"] = callee
self["external"] = False
self["calls"] = 0
def __eq__(self, other):
if not isinstance(other, Dependency):
return NotImplemented
return self["id"] == other["id"]#and self["name"] == other["name"]
def __repr__(self):
return self.__str__()
def __str__(self):
return "(%s:%s[%d] -%d:%d-> %s:%s[%d])" % (self["caller"]["componentName"], self["caller"]["name"], self["caller"]["id"], int(self["external"]), self["calls"], self["callee"]["componentName"], self["callee"]["name"], self["callee"]["id"])
from .module_info import CompileInfoLoader
from .hdi import HdiParser
from .sa import SAParser
from .innerapi import InnerAPILoader
class ElfFileWithDepsInfo(ElfFile):
def __init__(self, file, prefix):
super(ElfFileWithDepsInfo, self).__init__(file, prefix)
self["deps"] = []
self["dependedBy"] = []
def __eq__(self, other):
if not isinstance(other, ElfFileWithDepsInfo):
return NotImplemented
return self["id"] == other["id"]
def __repr__(self):
return self.__str__()
def __str__(self):
return "%s:%d deps(%d) dependedBy(%d)" % (self["name"], self["id"], len(self["deps"]), len(self["dependedBy"]))
def dependsOn(self, mod):
for dep in self["deps"]:
if dep["callee"] == mod:
return True
return False
class Dependency(dict):
def __init__(self, idx, caller, callee):
self["id"] = idx
self["caller_id"] = caller["id"]
self["callee_id"] = callee["id"]
self["caller"] = caller
self["callee"] = callee
self["external"] = False
self["calls"] = 0
def __eq__(self, other):
if not isinstance(other, Dependency):
return NotImplemented
return self["id"] == other["id"]
def __repr__(self):
return self.__str__()
def __str__(self):
return "(%s:%s[%d] -%d:%d-> %s:%s[%d])" % (self["caller"]["componentName"], self["caller"]["name"], self["caller"]["id"], int(self["external"]), self["calls"], self["callee"]["componentName"], self["callee"]["name"], self["callee"]["id"])
class ElfFileMgr(object):
def __init__(self, product_out_path=None, elfFileClass=None, dependenceClass = None):
self._elfFiles = []
self._path_dict = {}
self._basename_dict = {}
if elfFileClass:
self._elfFileClass = elfFileClass
else:
self._elfFileClass = ElfFileWithDepsInfo
def __init__(self, product_out_path=None, elfFileClass=None, dependenceClass=None):
self._elfFiles = []
self._path_dict = {}
self._basename_dict = {}
if elfFileClass:
self._elfFileClass = elfFileClass
else:
self._elfFileClass = ElfFileWithDepsInfo
self._deps = []
if dependenceClass:
self._dependenceClass = dependenceClass
else:
self._dependenceClass = Dependency
self._depIdx = 1
self._elfIdx = 1
self._deps = []
if dependenceClass:
self._dependenceClass = dependenceClass
else:
self._dependenceClass = Dependency
self._depIdx = 1
self._elfIdx = 1
self._not_found_depened_files = []
self._not_found_depened_files = []
walker = ELFWalker(product_out_path)
self._prefix = walker.get_product_images_path()
self._product_out_path = walker.get_product_out_path()
self._link_file_map = walker.get_link_file_map()
walker = ELFWalker(product_out_path)
self._prefix = walker.get_product_images_path()
self._product_out_path = walker.get_product_out_path()
self._link_file_map = walker.get_link_file_map()
def scan_all_files(self):
walker = ELFWalker(self._product_out_path)
def scan_all_files(self):
walker = ELFWalker(self._product_out_path)
self._scan_all_elf_files(walker)
self._build_deps_tree()
self._scan_all_elf_files(walker)
self._build_deps_tree()
self._maxDepth = 0
self._maxTotalDepends = 0
self._maxDepth = 0
self._maxTotalDepends = 0
print("Load compile information now ...")
CompileInfoLoader.load(self, self._product_out_path)
HdiParser.load(self, self._product_out_path)
SAParser.load(self, self._product_out_path)
print("Load compile information now ...")
CompileInfoLoader.load(self, self._product_out_path)
HdiParser.load(self, self._product_out_path)
SAParser.load(self, self._product_out_path)
def get_product_images_path(self):
return self._prefix
def get_product_images_path(self):
return self._prefix
def get_product_out_path(self):
return self._product_out_path
def get_product_out_path(self):
return self._product_out_path
def add_elf_file(self, elf):
# Append to array in order
elf["id"] = self._elfIdx
self._elfIdx = self._elfIdx + 1
self._elfFiles.append(elf)
def add_elf_file(self, elf):
# Append to array in order
elf["id"] = self._elfIdx
self._elfIdx = self._elfIdx + 1
self._elfFiles.append(elf)
# Add to dictionary with path as key
self._path_dict[elf["path"]] = elf
# Add to dictionary with path as key
self._path_dict[elf["path"]] = elf
# Add to dictionary with basename as key
if elf["name"] in self._basename_dict:
self._basename_dict[elf["name"]].append(elf)
else:
self._basename_dict[elf["name"]] = [ elf ]
# Add to dictionary with basename as key
if elf["name"] in self._basename_dict:
self._basename_dict[elf["name"]].append(elf)
else:
self._basename_dict[elf["name"]] = [elf]
def _scan_all_elf_files(self, walker):
print("Scanning %d ELF files now ..." % len(walker.get_elf_files()))
for f in walker.get_elf_files():
elf = self._elfFileClass(f, self._prefix)
if elf["path"] in self._path_dict:
print("Warning: duplicate " + elf.get_file() + ' skipped.')
continue
def add_dependence(self, caller, callee):
dep = self._dependenceClass(self._depIdx, caller, callee)
caller["deps"].append(dep)
callee["dependedBy"].append(dep)
# Ignore these files
if elf["name"] in [ "ld-musl-aarch64.so.1", "ld-musl-arm.so.1", "hdc_std" ]:
continue
self._deps.append(dep)
self._depIdx = self._depIdx + 1
return dep
self.add_elf_file(elf)
def get_elf_by_path(self, path):
if path not in self._path_dict and path.find("/lib64/") > 0:
path = path.replace("/lib64/", "/lib/")
if path in self._path_dict:
return self._path_dict[path]
if path.find("/platformsdk/") > 0:
return None
# Reorder libraries with same name as defined by LD_LIBRARY_PATH
for bname, val in self._basename_dict.items():
if len(val) < 2:
continue
self._basename_dict[bname] = self.__reorder_library(val)
if path.startswith("system/lib64/"):
path = path.replace("system/lib64/", "system/lib64/platformsdk/")
elif path.startswith("system/lib/"):
path = path.replace("system/lib/", "system/lib/platformsdk/")
else:
return None
def __reorder_library(self, val):
orders = []
idx = 0
for p in val:
orders.append((self.__get_library_order(p["path"]), idx))
idx = idx + 1
orders.sort()
if path not in self._path_dict and path.find("/lib64/") > 0:
path = path.replace("/lib64/", "/lib/")
if path in self._path_dict:
return self._path_dict[path]
return None
res = []
for item in orders:
res.append(val[item[1]])
def get_elf_by_idx(self, idx):
if idx < 1 or idx > len(self._elfFiles):
return None
return self._elfFiles[idx - 1]
return res
def get_elf_by_name(self, name):
if name in self._basename_dict:
return self._basename_dict[name][0]
def __get_library_order(self, path):
if not path.startswith("/"):
path = "/" + path
if path.find("/lib64/") > 0:
pathOrder = "/system/lib64:/vendor/lib64:/vendor/lib64/chipsetsdk:/system/lib64/ndk:/system/lib64/chipset-pub-sdk:/system/lib64/chipset-sdk:/system/lib64/platformsdk:/system/lib64/priv-platformsdk:/system/lib64/priv-module:/system/lib64/module:/system/lib64/module/data:/system/lib64/module/multimedia:/system/lib:/vendor/lib:/system/lib/ndk:/system/lib/chipset-pub-sdk:/system/lib/chipset-sdk:/system/lib/platformsdk:/system/lib/priv-platformsdk:/system/lib/priv-module:/system/lib/module:/system/lib/module/data:/system/lib/module/multimedia:/lib64:/lib:/usr/local/lib:/usr/lib"
else:
pathOrder = "/system/lib:/vendor/lib:/vendor/lib/chipsetsdk:/system/lib/ndk:/system/lib/chipset-pub-sdk:/system/lib/chipset-sdk:/system/lib/platformsdk:/system/lib/priv-platformsdk:/system/lib/priv-module:/system/lib/module:/system/lib/module/data:/system/lib/module/multimedia:/lib:/usr/local/lib:/usr/lib"
return self.__get_link_file(name)
if path.rfind("/") < 0:
return 1000
def get_all(self):
return self._elfFiles
path = path[:path.rfind("/")]
paths = pathOrder.split(':')
idx = 0
for p in paths:
if p == path:
return idx
idx = idx + 1
return 1000
def get_all_deps(self):
return self._deps
def _scan_all_elf_files(self, walker):
print("Scanning %d ELF files now ..." % len(walker.get_elf_files()))
for f in walker.get_elf_files():
elf = self._elfFileClass(f, self._prefix)
if elf["path"] in self._path_dict:
print("Warning: duplicate " + elf.get_file() + ' skipped.')
continue
def _build_deps_tree(self):
print("Build dependence tree for %d ELF files now ..." % len(self._elfFiles))
for elf in self._elfFiles:
self.__build_deps_tree_for_one_elf(elf)
print(" Got %d dependencies" % self._depIdx)
# Ignore these files
if elf["name"] in ["ld-musl-aarch64.so.1", "ld-musl-arm.so.1", "hdc_std"]:
continue
def add_dependence(self, caller, callee):
dep = self._dependenceClass(self._depIdx, caller, callee)
caller["deps"].append(dep)
callee["dependedBy"].append(dep)
self.add_elf_file(elf)
self._deps.append(dep)
self._depIdx = self._depIdx + 1
return dep
# Reorder libraries with same name as defined by LD_LIBRARY_PATH
for bname, val in self._basename_dict.items():
if len(val) < 2:
continue
self._basename_dict[bname] = self.__reorder_library(val)
def __build_deps_tree_for_one_elf(self, elf):
for lib in elf.library_depends():
dep_elf = self.get_elf_by_name(lib)
if not dep_elf:
self._not_found_depened_files.append({"caller": elf["name"], "callee": lib})
print("Warning: can not find depended library [" + lib + "] for " + elf["name"])
break
def __reorder_library(self, val):
orders = []
idx = 0
for p in val:
orders.append((self.__get_library_order(p["path"]), idx))
idx = idx + 1
orders.sort()
self.add_dependence(elf, dep_elf)
res = []
for item in orders:
res.append(val[item[1]])
def get_elf_by_path(self, path):
if path not in self._path_dict and path.find("/lib64/") > 0:
path = path.replace("/lib64/", "/lib/")
if path in self._path_dict:
return self._path_dict[path]
if path.find("/platformsdk/") > 0:
return None
return res
if path.startswith("system/lib64/"):
path = path.replace("system/lib64/", "system/lib64/platformsdk/")
elif path.startswith("system/lib/"):
path = path.replace("system/lib/", "system/lib/platformsdk/")
else:
return None
def __get_library_order(self, path):
if not path.startswith("/"):
path = "/" + path
if path.find("/lib64/") > 0:
pathOrder = "/system/lib64:/vendor/lib64:/vendor/lib64/chipsetsdk:/system/lib64/ndk:/system/lib64/chipset-pub-sdk:/system/lib64/chipset-sdk:/system/lib64/platformsdk:/system/lib64/priv-platformsdk:/system/lib64/priv-module:/system/lib64/module:/system/lib64/module/data:/system/lib64/module/multimedia:/system/lib:/vendor/lib:/system/lib/ndk:/system/lib/chipset-pub-sdk:/system/lib/chipset-sdk:/system/lib/platformsdk:/system/lib/priv-platformsdk:/system/lib/priv-module:/system/lib/module:/system/lib/module/data:/system/lib/module/multimedia:/lib64:/lib:/usr/local/lib:/usr/lib"
else:
pathOrder = "/system/lib:/vendor/lib:/vendor/lib/chipsetsdk:/system/lib/ndk:/system/lib/chipset-pub-sdk:/system/lib/chipset-sdk:/system/lib/platformsdk:/system/lib/priv-platformsdk:/system/lib/priv-module:/system/lib/module:/system/lib/module/data:/system/lib/module/multimedia:/lib:/usr/local/lib:/usr/lib"
if path not in self._path_dict and path.find("/lib64/") > 0:
path = path.replace("/lib64/", "/lib/")
if path in self._path_dict:
return self._path_dict[path]
return None
if path.rfind("/") < 0:
return 1000
def get_elf_by_idx(self, idx):
if idx < 1 or idx > len(self._elfFiles):
return None
return self._elfFiles[idx - 1]
path = path[:path.rfind("/")]
paths = pathOrder.split(':')
idx = 0
for p in paths:
if p == path:
return idx
idx = idx + 1
return 1000
def __get_link_file(self, name):
for src, target in self._link_file_map.items():
tmp_name = os.path.basename(src)
if name != tmp_name:
continue
tmp_name = os.path.dirname(src)
tmp_name = os.path.join(tmp_name, target)
link_elf = ElfFile(tmp_name, self._prefix)
return self.get_elf_by_path(link_elf["path"])
def _build_deps_tree(self):
print("Build dependence tree for %d ELF files now ..." % len(self._elfFiles))
for elf in self._elfFiles:
self.__build_deps_tree_for_one_elf(elf)
print(" Got %d dependencies" % self._depIdx)
def get_elf_by_name(self, name):
if name in self._basename_dict:
return self._basename_dict[name][0]
def __build_deps_tree_for_one_elf(self, elf):
for lib in elf.library_depends():
dep_elf = self.get_elf_by_name(lib)
if not dep_elf:
self._not_found_depened_files.append({"caller": elf["name"], "callee": lib})
print("Warning: can not find depended library [" + lib + "] for " + elf["name"])
break
return self.__get_link_file(name)
self.add_dependence(elf, dep_elf)
def get_all(self):
return self._elfFiles
def __get_link_file(self, name):
for src, target in self._link_file_map.items():
tmp_name = os.path.basename(src)
if name != tmp_name:
continue
tmp_name = os.path.dirname(src)
tmp_name = os.path.join(tmp_name, target)
link_elf = ElfFile(tmp_name, self._prefix)
return self.get_elf_by_path(link_elf["path"])
def get_all_deps(self):
return self._deps
if __name__ == '__main__':
mgr = ElfFileMgr("/home/z00325844/demo/archinfo/assets/rk3568/3.2.7.5")
mgr.scan_all_files()
elf = mgr.get_elf_by_path("system/lib/libskia_ohos.z.so")
print("Get skia now ...")
mgr = ElfFileMgr("./demo/archinfo/assets/rk3568/3.2.7.5")
mgr.scan_all_files()
elf = mgr.get_elf_by_path("system/lib/libskia_ohos.z.so")
print("Get skia now ...")
res = mgr.get_elf_by_path("system/lib/platformsdk/libhmicui18n.z.so")
print(res)
res = mgr.get_elf_by_path("system/lib/platformsdk/libhmicui18n.z.so")
print(res)
+45 -44
View File
@@ -24,56 +24,57 @@ import struct
# find out/rk3568/packages/phone/system/ -type f -print | file -f - | grep ELF | cut -d":" -f1 | wc -l
class ELFWalker():
def __init__(self, product_out_path="/home/z00325844/demo/archinfo/assets/rk3568/3.2.7.5"):
self._files = []
self._links = {}
self._walked = False
self._product_out_path = product_out_path
def __init__(self, product_out_path="./demo/archinfo/assets/rk3568/3.2.7.5"):
self._files = []
self._links = {}
self._walked = False
self._product_out_path = product_out_path
def get_product_images_path(self):
return os.path.join(self._product_out_path, "packages/phone/")
def get_product_images_path(self):
return os.path.join(self._product_out_path, "packages/phone/")
def get_product_out_path(self):
return self._product_out_path
def get_product_out_path(self):
return self._product_out_path
def __walk_path(self, subdir):
for root, subdirs, files in os.walk(os.path.join(self._product_out_path, subdir)):
for _filename in files:
_assetFile = os.path.join(root, _filename)
if os.path.islink(_assetFile):
if _assetFile.find(".so") > 0:
target = os.readlink(_assetFile)
self._links[_assetFile] = target
continue
if not os.path.isfile(_assetFile):
continue
with open(_assetFile, "rb") as f:
data = f.read(4)
try:
magic = struct.unpack("Bccc", data)
if magic[0] == 0x7F and magic[1] == b'E' and magic[2] == b'L' and magic[3] == b'F':
self._files.append(_assetFile)
except:
pass
def get_link_file_map(self):
if not self._walked:
self.__walk_path("packages/phone/system")
self.__walk_path("packages/phone/vendor")
return self._links
self._walked = True
def get_elf_files(self):
if not self._walked:
self.__walk_path("packages/phone/system")
self.__walk_path("packages/phone/vendor")
return self._files
def get_link_file_map(self):
if not self._walked:
self.__walk_path("packages/phone/system")
self.__walk_path("packages/phone/vendor")
return self._links
def __walk_path(self, subdir):
for root, subdirs, files in os.walk(os.path.join(self._product_out_path, subdir)):
for _filename in files:
_assetFile = os.path.join(root, _filename)
if os.path.islink(_assetFile):
if _assetFile.find(".so") > 0:
target = os.readlink(_assetFile)
self._links[_assetFile] = target
continue
if not os.path.isfile(_assetFile):
continue
with open(_assetFile, "rb") as f:
data = f.read(4)
try:
magic = struct.unpack("Bccc", data)
if magic[0] == 0x7F and magic[1] == b'E' and magic[2] == b'L' and magic[3] == b'F':
self._files.append(_assetFile)
except:
pass
def get_elf_files(self):
if not self._walked:
self.__walk_path("packages/phone/system")
self.__walk_path("packages/phone/vendor")
return self._files
self._walked = True
if __name__ == '__main__':
elfFiles = ELFWalker()
for f in elfFiles.get_elf_files():
print(f)
for src, target in elfFiles.get_link_file_map().items():
print('{} -> {}'.format(str, target))
elfFiles = ELFWalker()
for f in elfFiles.get_elf_files():
print(f)
for src, target in elfFiles.get_link_file_map().items():
print('{} -> {}'.format(str, target))
+44 -43
View File
@@ -19,53 +19,54 @@
import os
import subprocess
class HdiParser(object):
@staticmethod
def load(mgr, product_out_path):
# Decode hcb file to get hcs file
hdi_tool = os.path.join(product_out_path, "obj/drivers/hdf_core/framework/tools/hc-gen/hc-gen")
hcs_file = os.path.join(product_out_path, "packages/phone/vendor/etc/hdfconfig/hdf_default.hcb")
out_file = os.path.join(product_out_path, "device_info.hcs")
subprocess.Popen('%s -d "%s" -o "%s"' % (hdi_tool, hcs_file, out_file), shell=True).wait()
try:
with open(out_file) as f:
lines = f.readlines()
except:
try:
out_file = os.path.join(product_out_path, "device_info.d.hcs")
with open(out_file) as f:
lines = f.readlines()
except:
return
@staticmethod
def load(mgr, product_out_path):
# Decode hcb file to get hcs file
hdi_tool = os.path.join(product_out_path, "obj/drivers/hdf_core/framework/tools/hc-gen/hc-gen")
hcs_file = os.path.join(product_out_path, "packages/phone/vendor/etc/hdfconfig/hdf_default.hcb")
out_file = os.path.join(product_out_path, "device_info.hcs")
subprocess.Popen('%s -d "%s" -o "%s"' % (hdi_tool, hcs_file, out_file), shell=True).wait()
try:
with open(out_file) as f:
lines = f.readlines()
except:
try:
out_file = os.path.join(product_out_path, "device_info.d.hcs")
with open(out_file) as f:
lines = f.readlines()
except:
return
modules = []
for line in lines:
line = line.strip()
if line.find("moduleName") < 0:
continue
parts = line.split("=")
parts = [p.strip() for p in parts]
if len(parts) < 2:
continue
name = parts[1]
if name.endswith(";"):
name = name[:-1]
name=name.strip('"')
name=name.strip("'")
if name == "":
continue
modules = []
for line in lines:
line = line.strip()
if line.find("moduleName") < 0:
continue
parts = line.split("=")
parts = [p.strip() for p in parts]
if len(parts) < 2:
continue
name = parts[1]
if name.endswith(";"):
name = name[:-1]
name = name.strip('"')
name = name.strip("'")
if name == "":
continue
if not name.endswith(".so"):
name = "lib%s.z.so" % name
modules.append(name)
if not name.endswith(".so"):
name = "lib%s.z.so" % name
modules.append(name)
if not mgr:
return
if not mgr:
return
for elf in mgr.get_all():
if elf["name"] in modules:
elf["hdiType"] = "hdi_service"
for elf in mgr.get_all():
if elf["name"] in modules:
elf["hdiType"] = "hdi_service"
if __name__ == "__main__":
parser = HdiParser()
parser.load(None, "/home/z00325844/ohos/vendor/hihope/rk3568/hdf_config/uhdf/device_info.hcs")
parser = HdiParser()
parser.load(None, "/home/ohos/vendor/hihope/rk3568/hdf_config/uhdf/device_info.hcs")
@@ -19,30 +19,31 @@
import os
import json
class InnerAPILoader(object):
@staticmethod
def load(mgr, product_out_path):
print("Loading innerapis now ...")
try:
innerapis = InnerAPILoader.__load_innerapi_modules(product_out_path)
except:
innerapis = []
@staticmethod
def load(mgr, product_out_path):
print("Loading innerapis now ...")
try:
innerapis = InnerAPILoader.__load_innerapi_modules(product_out_path)
except:
innerapis = []
if not mgr:
return
if not mgr:
return
for elf in mgr.get_all():
if elf["labelPath"] in innerapis:
elf["innerapi_declared"] = True
for elf in mgr.get_all():
if elf["labelPath"] in innerapis:
elf["innerapi_declared"] = True
def __load_innerapi_modules(product_out_path):
inner_kits_info = os.path.join(product_out_path, "build_configs/parts_info/inner_kits_info.json")
with open(inner_kits_info, "r") as f:
info = json.load(f)
def __load_innerapi_modules(product_out_path):
inner_kits_info = os.path.join(product_out_path, "build_configs/parts_info/inner_kits_info.json")
with open(inner_kits_info, "r") as f:
info = json.load(f)
innerapis = []
for name, component in info.items():
for mod_name, innerapi in component.items():
innerapis.append(innerapi["label"])
innerapis = []
for name, component in info.items():
for mod_name, innerapi in component.items():
innerapis.append(innerapi["label"])
return innerapis
return innerapis
@@ -22,6 +22,66 @@ import json
class CompileInfoLoader(object):
@staticmethod
def load(load_mgr, product_out_path):
info = CompileInfoLoader.__load_output_module_info(product_out_path)
default_info = CompileInfoLoader.__get_default_info()
if info:
for item in info:
elf = load_mgr.get_elf_by_path(item["name"])
if not elf:
continue
for k in default_info.keys():
if k in item:
elf[k] = item[k]
unknown_items = []
for elf in load_mgr.get_all():
if "componentName" not in elf:
print("%s does not match in module info file" % (elf["path"]))
unknown = default_info.copy()
unknown["name"] = elf["path"]
unknown["fileName"] = elf["name"]
for k in default_info.keys():
elf[k] = default_info[k]
unknown_items.append(unknown)
elif elf["componentName"] == "unknown":
print("%s has no componentName info" % (elf["path"]))
unknown = default_info.copy()
unknown["name"] = elf["path"]
for k in default_info.keys():
if k in elf:
default_info[k] = elf[k]
unknown_items.append(unknown)
if elf["path"].startswith("system/lib64/module/") or elf["path"].startswith("system/lib/module/"):
elf["napi"] = True
if not elf["path"].startswith("system/"):
elf["chipset"] = True
# Add if not exists
if "shlib_type" not in elf:
elf["shlib_type"] = ""
if "innerapi_tags" not in elf:
elf["innerapi_tags"] = []
if elf["labelPath"].startswith("//third_party/"):
elf["third_party"] = True
if len(unknown_items) > 0:
print("%d modules has no component info" % len(unknown_items))
with open(os.path.join(product_out_path, "unknown.json"), "w") as f:
res = json.dumps(unknown_items, indent=4)
f.write(res)
# init platformsdk, chipsetsdk, innerapi flags
CompileInfoLoader.__set_elf_default_value(load_mgr)
# for component dependedBy_internal and dependedBy_external
CompileInfoLoader.__update_deps(load_mgr)
@staticmethod
def __get_modules_from_file(product_out_path_):
try:
@@ -93,66 +153,6 @@ class CompileInfoLoader(object):
info_["innerapi"] = False
info_["innerapi_declared"] = False
@staticmethod
def load(load_mgr, product_out_path):
info = CompileInfoLoader.__load_output_module_info(product_out_path)
default_info = CompileInfoLoader.__get_default_info()
if info:
for item in info:
elf = load_mgr.get_elf_by_path(item["name"])
if not elf:
continue
for k in default_info.keys():
if k in item:
elf[k] = item[k]
unknown_items = []
for elf in load_mgr.get_all():
if "componentName" not in elf:
print("%s does not match in module info file" % (elf["path"]))
unknown = default_info.copy()
unknown["name"] = elf["path"]
unknown["fileName"] = elf["name"]
for k in default_info.keys():
elf[k] = default_info[k]
unknown_items.append(unknown)
elif elf["componentName"] == "unknown":
print("%s has no componentName info" % (elf["path"]))
unknown = default_info.copy()
unknown["name"] = elf["path"]
for k in default_info.keys():
if k in elf:
default_info[k] = elf[k]
unknown_items.append(unknown)
if elf["path"].startswith("system/lib64/module/") or elf["path"].startswith("system/lib/module/"):
elf["napi"] = True
if not elf["path"].startswith("system/"):
elf["chipset"] = True
# Add if not exists
if "shlib_type" not in elf:
elf["shlib_type"] = ""
if "innerapi_tags" not in elf:
elf["innerapi_tags"] = []
if elf["labelPath"].startswith("//third_party/"):
elf["third_party"] = True
if len(unknown_items) > 0:
print("%d modules has no component info" % len(unknown_items))
with open(os.path.join(product_out_path, "unknown.json"), "w") as f:
res = json.dumps(unknown_items, indent=4)
f.write(res)
# init platformsdk, chipsetsdk, innerapi flags
CompileInfoLoader.__set_elf_default_value(load_mgr)
# for component dependedBy_internal and dependedBy_external
CompileInfoLoader.__update_deps(load_mgr)
@staticmethod
def __get_default_info():
return {
+19 -16
View File
@@ -22,13 +22,32 @@ import sys
import os
import xml.etree.ElementTree as ET
def xml_node_find_by_name(node, name):
for item in node:
if item.tag == name:
return item.text
return None
class SAParser(object):
@staticmethod
def load(mgr, out_root_path):
all_sa = {}
path = os.path.join(out_root_path, "packages/phone/system/profile")
if not os.path.exists(path):
return
for f in os.listdir(path):
full_name = os.path.join(path, f)
if os.path.isfile(full_name) and f.endswith(".json"):
try:
SAParser.__parse_sa_profile(all_sa, full_name)
except:
pass
SAParser.__add_sa_info(all_sa, mgr)
@staticmethod
def __parse_sa_profile(all_sa, full_name):
with open(full_name, "r") as f:
@@ -50,22 +69,6 @@ class SAParser(object):
continue
mod["sa_id"] = int(all_sa[mod["name"]]["name"])
@staticmethod
def load(mgr, out_root_path):
all_sa = {}
path = os.path.join(out_root_path, "packages/phone/system/profile")
if not os.path.exists(path):
return
for f in os.listdir(path):
full_name = os.path.join(path, f)
if os.path.isfile(full_name) and f.endswith(".json"):
try:
SAParser.__parse_sa_profile(all_sa, full_name)
except:
pass
SAParser.__add_sa_info(all_sa, mgr)
if __name__ == '__main__':
parser = SAParser()
+16 -14
View File
@@ -20,25 +20,27 @@ import os
import sys
import string
DEBUG_NORMAL = 1
DEBUG_NORMAL = 1
DEBUG_VERBOSE = 2
DEBUG_SPAM = 3
DEBUG_SPAM = 3
debuglevel = DEBUG_NORMAL
def debug(level, *msg):
if debuglevel >= level:
print(' '.join(msg))
if debuglevel >= level:
print(' '.join(msg))
# return a list of lines of output of the command
def command(command, *args):
debug(DEBUG_SPAM, "calling", command, ' '.join(args))
pipe = os.popen(command + ' ' + ' '.join(args), 'r')
output = pipe.read().strip()
status = pipe.close()
if status is not None and os.WEXITSTATUS(status) != 0:
print("Command failed with status", os.WEXITSTATUS(status), ":", \
command, ' '.join(args))
print("With output:", output)
sys.exit(1)
return [i for i in output.split('\n') if i]
debug(DEBUG_SPAM, "calling", command, ' '.join(args))
pipe = os.popen(command + ' ' + ' '.join(args), 'r')
output = pipe.read().strip()
status = pipe.close()
if status is not None and os.WEXITSTATUS(status) != 0:
print("Command failed with status", os.WEXITSTATUS(status), ":", \
command, ' '.join(args))
print("With output:", output)
sys.exit(1)
return [i for i in output.split('\n') if i]
+17 -16
View File
@@ -21,23 +21,24 @@ from .sa_rule import SaRule
from .hdi_rule import HdiRule
from .chipsetsdk import ChipsetSDKRule
def check_all_rules(mgr, args):
rules = [
NapiRule,
SaRule,
HdiRule,
ChipsetSDKRule
]
rules = [
NapiRule,
SaRule,
HdiRule,
ChipsetSDKRule
]
passed = True
for rule in rules:
r = rule(mgr, args)
r.log("Do %s rule checking now:" % rule.RULE_NAME)
if not r.check():
passed = False
r.log(" Please refer to: \033[91m%s\x1b[0m" % r.get_help_url())
passed = True
for rule in rules:
r = rule(mgr, args)
r.log("Do %s rule checking now:" % rule.RULE_NAME)
if not r.check():
passed = False
r.log(" Please refer to: \033[91m%s\x1b[0m" % r.get_help_url())
if args and args.no_fail:
return True
if args and args.no_fail:
return True
return passed
return passed
+37 -36
View File
@@ -19,51 +19,52 @@
import os
import json
class BaseRule(object):
RULE_NAME = ""
RULE_NAME = ""
def __init__(self, mgr, args):
self._mgr = mgr
self._args = args
self.__white_lists = self.load_files("whitelist.json")
def __init__(self, mgr, args):
self._mgr = mgr
self._args = args
self.__white_lists = self.load_files("whitelist.json")
def load_files(self, name):
rules_dir = []
rules_dir.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../rules"))
if self._args and self._args.rules:
rules_dir = rules_dir + self._args.rules
def load_files(self, name):
rules_dir = []
rules_dir.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../rules"))
if self._args and self._args.rules:
rules_dir = rules_dir + self._args.rules
res = []
for d in rules_dir:
rules_file = os.path.join(d, self.__class__.RULE_NAME, name)
try:
with open(rules_file, "r") as f:
jsonstr = "".join([ line.strip() for line in f if not line.strip().startswith("//") ])
res = res + json.loads(jsonstr)
except:
pass
res = []
for d in rules_dir:
rules_file = os.path.join(d, self.__class__.RULE_NAME, name)
try:
with open(rules_file, "r") as f:
jsonstr = "".join([line.strip() for line in f if not line.strip().startswith("//")])
res = res + json.loads(jsonstr)
except:
pass
return res
return res
def get_mgr(self):
return self._mgr
def get_mgr(self):
return self._mgr
def get_white_lists(self):
return self.__white_lists
def get_white_lists(self):
return self.__white_lists
def log(self, info):
print(info)
def log(self, info):
print(info)
def warn(self, info):
print("\033[35m[WARNING]\x1b[0m: %s" % info)
def warn(self, info):
print("\033[35m[WARNING]\x1b[0m: %s" % info)
def error(self, info):
print("\033[91m[NOT ALLOWED]\x1b[0m: %s" % info)
def error(self, info):
print("\033[91m[NOT ALLOWED]\x1b[0m: %s" % info)
def get_help_url(self):
return "https://gitee.com/openharmony/developtools_integration_verification/tree/master/tools/deps_guard/rules/%s/README.md" % self.__class__.RULE_NAME
def get_help_url(self):
return "https://gitee.com/openharmony/developtools_integration_verification/tree/master/tools/deps_guard/rules/%s/README.md" % self.__class__.RULE_NAME
# To be override
def check(self):
# Default pass
return True
# To be override
def check(self):
# Default pass
return True
+39 -40
View File
@@ -58,23 +58,45 @@ class ChipsetSDKRule(BaseRule):
return res
def check(self):
self.__load_chipsetsdk_indirects()
# Check if all chipset modules depends on chipsetsdk modules only
passed = self.__check_depends_on_chipsetsdk()
if not passed:
return passed
# Check if all chipsetsdk module depends on chipsetsdk or chipsetsdk_indirect modules only
passed = self.__check_chipsetsdk_indirect()
if not passed:
return passed
# Check if all ChipsetSDK modules are correctly tagged by innerapi_tags
passed = self.__check_if_tagged_correctly()
if not passed:
return passed
self.__write_innerkits_header_files()
return True
def __parser_rules_file(self, rules_file, res):
try:
self.log("****Parsing rules file in {}****".format(rules_file))
with open(rules_file, "r") as f:
contents = f.read()
if not contents:
self.log("****rules file {} is null****".format(rules_file))
return res
json_data = json.loads(contents)
for so in json_data:
so_file_name = so.get("so_file_name")
if so_file_name and so_file_name not in res:
res.append(so_file_name)
except(FileNotFoundError, IOError, UnicodeDecodeError) as file_open_or_decode_err:
self.error(file_open_or_decode_err)
return res
try:
self.log("****Parsing rules file in {}****".format(rules_file))
with open(rules_file, "r") as f:
contents = f.read()
if not contents:
self.log("****rules file {} is null****".format(rules_file))
return res
json_data = json.loads(contents)
for so in json_data:
so_file_name = so.get("so_file_name")
if so_file_name and so_file_name not in res:
res.append(so_file_name)
except(FileNotFoundError, IOError, UnicodeDecodeError) as file_open_or_decode_err:
self.error(file_open_or_decode_err)
return res
def __is_chipsetsdk_tagged(self, mod):
if not "innerapi_tags" in mod:
@@ -117,7 +139,7 @@ class ChipsetSDKRule(BaseRule):
try:
with open(os.path.join(self.get_mgr().get_product_images_path(), "chipsetsdk_info.json"), "w") as f:
json.dump(headers, f, indent = 4)
json.dump(headers, f, indent=4)
except:
pass
@@ -199,7 +221,6 @@ class ChipsetSDKRule(BaseRule):
return passed
def __check_if_tagged_correctly(self):
passed = True
for mod in self.__chipsetsdks:
@@ -222,25 +243,3 @@ class ChipsetSDKRule(BaseRule):
def __load_chipsetsdk_indirects(self):
self.__indirects = self.load_chipsetsdk_json("chipsetsdk_indirect.json")
def check(self):
self.__load_chipsetsdk_indirects()
# Check if all chipset modules depends on chipsetsdk modules only
passed = self.__check_depends_on_chipsetsdk()
if not passed:
return passed
# Check if all chipsetsdk module depends on chipsetsdk or chipsetsdk_indirect modules only
passed = self.__check_chipsetsdk_indirect()
if not passed:
return passed
# Check if all ChipsetSDK modules are correctly tagged by innerapi_tags
passed = self.__check_if_tagged_correctly()
if not passed:
return passed
self.__write_innerkits_header_files()
return True
+3 -3
View File
@@ -24,6 +24,9 @@ from .base_rule import BaseRule
class HdiRule(BaseRule):
RULE_NAME = "NO-Depends-On-HDI"
def check(self):
return self.__check_depends_on_hdi()
def __check_depends_on_hdi(self):
lists = self.get_white_lists()
@@ -68,9 +71,6 @@ class HdiRule(BaseRule):
return passed
def check(self):
return self.__check_depends_on_hdi()
def __ignore_mod(self, mod, is_hdi, lists):
ignore_flag = False
if not is_hdi:
+22 -21
View File
@@ -20,33 +20,34 @@ import json
from .base_rule import BaseRule
class NapiRule(BaseRule):
RULE_NAME = "NO-Depends-On-NAPI"
RULE_NAME = "NO-Depends-On-NAPI"
def __check_depends_on_napi(self):
lists = self.get_white_lists()
def check(self):
return self.__check_depends_on_napi()
passed = True
def __check_depends_on_napi(self):
lists = self.get_white_lists()
# Check if any napi modules has dependedBy
for mod in self.get_mgr().get_all():
if not mod["napi"]:
continue
passed = True
if len(mod["dependedBy"]) == 0:
continue
# Check if any napi modules has dependedBy
for mod in self.get_mgr().get_all():
if not mod["napi"]:
continue
targetName = mod["labelPath"][mod["labelPath"].find(":")+1:]
if targetName in lists:
continue
if len(mod["dependedBy"]) == 0:
continue
self.error("napi module %s depended by:" % mod["name"])
for dep in mod["dependedBy"]:
caller = dep["caller"]
self.log(" module [%s] defined in [%s]" % (caller["name"], caller["labelPath"]))
passed = False
targetName = mod["labelPath"][mod["labelPath"].find(":")+1:]
if targetName in lists:
continue
return passed
self.error("napi module %s depended by:" % mod["name"])
for dep in mod["dependedBy"]:
caller = dep["caller"]
self.log(" module [%s] defined in [%s]" % (caller["name"], caller["labelPath"]))
passed = False
def check(self):
return self.__check_depends_on_napi()
return passed
+43 -42
View File
@@ -20,60 +20,61 @@ import json
from .base_rule import BaseRule
class SaRule(BaseRule):
RULE_NAME = "NO-Depends-On-SA"
RULE_NAME = "NO-Depends-On-SA"
def __check_depends_on_sa(self):
lists = self.get_white_lists()
def check(self):
return self.__check_depends_on_sa()
passed = True
def __check_depends_on_sa(self):
lists = self.get_white_lists()
sa_without_shlib_type = []
non_sa_with_sa_shlib_type = []
passed = True
# Check if any napi modules has dependedBy
for mod in self.get_mgr().get_all():
is_sa = False
if "sa_id" in mod and mod["sa_id"] > 0:
is_sa = True
# Collect non SA modules with shlib_type of value "sa"
if not is_sa and ("shlib_type" in mod and mod["shlib_type"] == "sa"):
non_sa_with_sa_shlib_type.append(mod)
sa_without_shlib_type = []
non_sa_with_sa_shlib_type = []
# Collect SA modules without shlib_type with value of "sa"
if is_sa and ("shlib_type" not in mod or mod["shlib_type"] != "sa"):
if mod["name"] not in lists:
sa_without_shlib_type.append(mod)
# Check if any napi modules has dependedBy
for mod in self.get_mgr().get_all():
is_sa = False
if "sa_id" in mod and mod["sa_id"] > 0:
is_sa = True
# Collect non SA modules with shlib_type of value "sa"
if not is_sa and ("shlib_type" in mod and mod["shlib_type"] == "sa"):
non_sa_with_sa_shlib_type.append(mod)
if not is_sa:
continue
# Collect SA modules without shlib_type with value of "sa"
if is_sa and ("shlib_type" not in mod or mod["shlib_type"] != "sa"):
if mod["name"] not in lists:
sa_without_shlib_type.append(mod)
if len(mod["dependedBy"]) == 0:
continue
if not is_sa:
continue
if mod["name"] in lists:
continue
if len(mod["dependedBy"]) == 0:
continue
# If sa module has version_script to specify exported symbols, it can be depended by others
if "version_script" in mod:
continue
if mod["name"] in lists:
continue
# Check if SA modules is depended by other modules
self.error("sa module %s depended by:" % mod["name"])
for dep in mod["dependedBy"]:
caller = dep["caller"]
self.log(" module [%s] defined in [%s]" % (caller["name"], caller["labelPath"]))
passed = False
# If sa module has version_script to specify exported symbols, it can be depended by others
if "version_script" in mod:
continue
if len(sa_without_shlib_type) > 0:
for mod in sa_without_shlib_type:
self.warn('sa module %s has no shlib_type="sa", add it in %s' % (mod["name"], mod["labelPath"]))
# Check if SA modules is depended by other modules
self.error("sa module %s depended by:" % mod["name"])
for dep in mod["dependedBy"]:
caller = dep["caller"]
self.log(" module [%s] defined in [%s]" % (caller["name"], caller["labelPath"]))
passed = False
if len(non_sa_with_sa_shlib_type) > 0:
for mod in non_sa_with_sa_shlib_type:
self.warn('non sa module %s with shlib_type="sa", %s' % (mod["name"], mod["labelPath"]))
if len(sa_without_shlib_type) > 0:
for mod in sa_without_shlib_type:
self.warn('sa module %s has no shlib_type="sa", add it in %s' % (mod["name"], mod["labelPath"]))
return passed
if len(non_sa_with_sa_shlib_type) > 0:
for mod in non_sa_with_sa_shlib_type:
self.warn('non sa module %s with shlib_type="sa", %s' % (mod["name"], mod["labelPath"]))
def check(self):
return self.__check_depends_on_sa()
return passed