侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Python  /  管理员 发布于 7年前   179

有部分老项目是在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 = 0class _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机器学习实战之K均值聚类
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客