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

 更新时间:2017年12月20日 13:46:21   作者:南波万  
这篇文章主要介绍了利用 python 和 lint 删除项目无用资源的方法,使用方法是将 python 目录下的 delUnused.py 放到项目目录下,然后直接运行即可,需要的朋友可以参考下

有部分老项目是在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

class _Location(object):
 def __init__(self):
  self.file = ''
  self.line = 0
  self.column = 0

class _LintBean(object):
 def __init__(self):
  self.id = ''
  self.severity = ''
  self.message = ''
  self.location = _Location()

 def print(self):
  print('find a %s, cause: %s. filePath: %s. line: %s' % (
   self.id, self.message, self.location.file, self.location.line))

处理无用资源

解析完数据,可以得到三种资源:

  • Drawable,就一个文件,可以直接删
  • xml中的一个节点,但是这个xml中就这一个节点,直接删文件
  • xml中的一个节点,这个xml中有多个节点,删除节点

对这三种资源进行区分和删除

for lint in lint_result:
 total_unused_resource += 1
 if lint.id != 'UnusedResources':
  continue
 if lint.location.line != '':
  is_single = _is_single_node(lint.location.file)
  if is_single:
   total_del_file += 1
   del_file(lint.location.file)
  else:
   total_remove_attr += 1
   node_name = get_node_name(lint.message)
   del_node(lint.location.file, node_name)
 else:
  total_del_file += 1
  del_file(lint.location.file)

删除文件

def del_file(file_path):
 try:
  os.remove(file_path)
  print('remove %s success.' % file_path)
 except FileNotFoundError:
  print('remove %s error.' % file_path)

删除节点:

def del_node(file_path, node_name):
 file = minidom.parse(file_path)
 root = file.documentElement
 nodes = root.childNodes
 for node in nodes:
  if node.nodeType in (node.TEXT_NODE, node.COMMENT_NODE):
   continue
  if node_name == node.getAttribute('name'):
   root.removeChild(node)
   file.writexml(open(file_path, 'w', encoding='UTF-8'), encoding='UTF-8')
   print('remove %s, node_name:%s. success!' % (file_path, node_name))
   return

总结

以上所述是小编给大家介绍的使用python 和 lint 删除项目无用资源的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • 详解Vue组件动态加载有哪些方式

    详解Vue组件动态加载有哪些方式

    动态加载组件可以显著提高应用的性能,优化用户体验,尤其是在大型应用中,合理的组件加载策略尤为重要,本文将探讨几种在Vue中实现组件动态加载的具体方案,需要的朋友可以参考下
    2024-10-10
  • python3实现往mysql中插入datetime类型的数据

    python3实现往mysql中插入datetime类型的数据

    这篇文章主要介绍了python3实现往mysql中插入datetime类型的数据,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • python图形界面开发之wxPython树控件使用方法详解

    python图形界面开发之wxPython树控件使用方法详解

    这篇文章主要介绍了python图形界面开发之wxPython树控件使用方法详解,需要的朋友可以参考下
    2020-02-02
  • python3+PyQt5实现自定义流体混合窗口部件

    python3+PyQt5实现自定义流体混合窗口部件

    这篇文章主要为大家详细介绍了python3+PyQt5实现自定义流体混合窗口部件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • Python pip指定安装源的方法详解

    Python pip指定安装源的方法详解

    pip是Python包管理工具,该工具提供了对Python包的查找、下载、安装、卸载的功能,这篇文章主要给大家介绍了关于Python pip指定安装源的相关资料,需要的朋友可以参考下
    2023-12-12
  • pytest之assert断言的具体使用

    pytest之assert断言的具体使用

    这篇文章主要介绍了pytest之assert断言的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • Python中的上下文管理器和with语句的使用

    Python中的上下文管理器和with语句的使用

    本篇文章主要介绍了Python中的上下文管理器和with语句的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • Jupyter Lab无法打开终端窗口的解决方法

    Jupyter Lab无法打开终端窗口的解决方法

    本文主要介绍了Jupyter Lab无法打开终端窗口的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • Python学习笔记(一)(基础入门之环境搭建)

    Python学习笔记(一)(基础入门之环境搭建)

    本系列为Python学习相关笔记整理所得,IT人,多学无害,多多探索,激发学习兴趣,开拓思维,不求高大上,只求懂点皮毛,作为知识储备,不至于落后太远。本文主要介绍Python的相关背景,环境搭建。
    2014-06-06
  • 400多行Python代码实现了一个FTP服务器

    400多行Python代码实现了一个FTP服务器

    400多行Python代码实现了一个FTP服务器,实现了比之前的xxftp更多更完善的功能
    2012-05-05

最新评论