首页 > 编程 > Python > 正文

使用python 和 lint 删除项目无用资源的方法

2020-02-16 11:12:21
字体:
来源:转载
供稿:网友

有部分老项目是在Eclipse环境开发的,最近公司要求应用瘦身,老项目也在其中。如果在 AS 下开发就不会有这样的问题,但是在 Eclipse 中就不太方便了,于是就写了这个脚本。第一次用Python写东西,代码里可能会有许多 Java、C 这样的痕迹,见谅。

使用方法

将 python 目录下的 delUnused.py 放到项目目录下,然后直接运行即可。

代码说明

利用lint进行代码审查

lint --check UnusedResources --xml [resultPath] [projectPath]

命令含义是检查项目中未使用的资源文件,并且用xml格式输出结果,需要提供检查结果输出的路径和项目路径。在脚本中已经自动提供了。

def exec_lint_command(): cmd = 'lint --check UnusedResources --xml %s %s' % (_get_lint_result_path(), _get_project_dir_path()) p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) c = p.stdout.readline().decode() while c:  print(c)  c = p.stdout.readline().decode()

这里给一个检查结果实例吧

<issue  id="UnusedResources"  severity="Warning"  message="The resource `R.layout.activity_all_player` appears to be unused"  category="Performance"  priority="3"  summary="Unused resources"  explanation="Unused resources make applications larger and slow down builds."  errorLine1="<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android""  errorLine2="^"  quickfix="studio">  <location   file="res/layout/activity_all_player.xml"   line="2"   column="1"/> </issue>

我们能用到的信息有 id message location 等。

解析检查结果

我是利用 minidom 解析的,具体的解析方法不多说,参考。

获取根节点

def _parse_lint_report(): file = minidom.parse(_get_lint_result_path()) root = file.documentElement beans = _parse_xml(root) return beans

解析第一层子节点

def _parse_xml(element, beans=None): if beans is None:  beans = [] for node in element.childNodes:  if node.nodeName == ISSUE_KEY and node.nodeType is node.ELEMENT_NODE:   lint_bean = _LintBean()   lint_bean.id = node.getAttribute(ID_KEY)   lint_bean.severity = node.getAttribute(SEVERITY_KEY)   lint_bean.message = node.getAttribute(MESSAGE_KEY)   _parse_location(node, lint_bean)   lint_bean.print()   beans.append(lint_bean) return beans

解析location 子节点

def _parse_location(node, bean): if not node.hasChildNodes():  return for child in node.childNodes:  if child.nodeName == LOCATION_KEY and node.nodeType is node.ELEMENT_NODE:   bean.location.file = child.getAttribute(LOCATION_FILE_KEY)   bean.location.line = child.getAttribute(LOCATION_LINE_KEY)   bean.location.column = child.getAttribute(LOCATION_COLUMN_KEY)

用Java习惯了,解析数据喜欢用Bean

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表