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


Posted in Python onDecember 20, 2017

有部分老项目是在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 删除项目无用资源的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
Python中logging模块的用法实例
Sep 29 Python
使用Python读写及压缩和解压缩文件的示例
Jul 08 Python
浅谈django开发者模式中的autoreload是如何实现的
Aug 18 Python
python中如何正确使用正则表达式的详细模式(Verbose mode expression)
Nov 08 Python
OpenCV+Python识别车牌和字符分割的实现
Jan 31 Python
Python实现的旋转数组功能算法示例
Feb 23 Python
Python实现的微信红包提醒功能示例
Aug 22 Python
pyhton中__pycache__文件夹的产生与作用详解
Nov 24 Python
OpenCV 表盘指针自动读数的示例代码
Apr 10 Python
jupyter notebook 实现matplotlib图动态刷新
Apr 22 Python
python数据库批量插入数据的实现(executemany的使用)
Apr 30 Python
使用numpy实现矩阵的翻转(flip)与旋转
Jun 03 Python
python机器学习实战之K均值聚类
Dec 20 #Python
Python绘制3d螺旋曲线图实例代码
Dec 20 #Python
python机器学习实战之最近邻kNN分类器
Dec 20 #Python
python3.6 +tkinter GUI编程 实现界面化的文本处理工具(推荐)
Dec 20 #Python
浅谈Python实现Apriori算法介绍
Dec 20 #Python
利用Python如何生成hash值示例详解
Dec 20 #Python
python 3.6 tkinter+urllib+json实现火车车次信息查询功能
Dec 20 #Python
You might like
让你同时上传 1000 个文件 (二)
2006/10/09 PHP
8个出色的WordPress SEO插件收集
2011/02/26 PHP
php过滤HTML标签、属性等正则表达式汇总
2014/09/22 PHP
简单实现php上传文件功能
2017/09/21 PHP
Laravel框架使用Seeder实现自动填充数据功能
2018/06/13 PHP
PHP判断一个变量是否为整数、正整数的方法示例
2019/09/11 PHP
PHP 对象接口简单实现方法示例
2020/04/13 PHP
如果文字过长,则将过长的部分变成省略号显示
2006/06/26 Javascript
jqeury eval将字符串转换json的方法
2011/01/20 Javascript
input标签内容改变的触发事件介绍
2014/06/18 Javascript
jQuery实现简单的图片查看器
2020/09/11 Javascript
javascript实现简单的全选和反选功能
2016/01/05 Javascript
分享JS数组求和与求最大值的方法
2016/08/11 Javascript
jQuery实现大图轮播
2017/02/13 Javascript
vue源码学习之Object.defineProperty对象属性监听
2018/05/30 Javascript
详解使用Nuxt.js快速搭建服务端渲染(SSR)应用
2019/03/13 Javascript
layui radio单选限制下一个radio单选的实例
2019/09/03 Javascript
Vue实现开心消消乐游戏算法
2019/10/22 Javascript
es6中let和const的使用方法详解
2020/02/24 Javascript
python实时监控cpu小工具
2018/06/21 Python
django与vue的完美结合_实现前后端的分离开发之后在整合的方法
2019/08/12 Python
Python如何使用BeautifulSoup爬取网页信息
2019/11/26 Python
python3的UnicodeDecodeError解决方法
2019/12/20 Python
python matplotlib 绘图 和 dpi对应关系详解
2020/03/14 Python
来自南加州灵感的工作和娱乐服装:TravisMathew
2019/05/01 全球购物
英语文学专业学生的自我评价
2013/10/31 职场文书
党员创先争优心得体会
2014/09/11 职场文书
毕业生实习证明
2014/09/19 职场文书
2014年文艺部工作总结
2014/11/17 职场文书
2015年公民道德宣传日活动总结
2015/03/23 职场文书
2015年机关党建工作总结
2015/05/22 职场文书
MySQL 视图(View)原理解析
2021/05/19 MySQL
浅谈克隆 JavaScript
2021/11/02 Javascript
漫画「你在春天醒来」第10卷封面公开
2022/03/21 日漫
MySQL实现配置主从复制项目实践
2022/03/31 MySQL
一文简单了解MySQL前缀索引
2022/04/03 MySQL