最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用python 和 lint 刪除項(xiàng)目無(wú)用資源的方法

 更新時(shí)間:2017年12月20日 13:46:21   作者:南波萬(wàn)  
這篇文章主要介紹了利用 python 和 lint 刪除項(xiàng)目無(wú)用資源的方法,使用方法是將 python 目錄下的 delUnused.py 放到項(xiàng)目目錄下,然后直接運(yùn)行即可,需要的朋友可以參考下

有部分老項(xiàng)目是在Eclipse環(huán)境開(kāi)發(fā)的,最近公司要求應(yīng)用瘦身,老項(xiàng)目也在其中。如果在 AS 下開(kāi)發(fā)就不會(huì)有這樣的問(wèn)題,但是在 Eclipse 中就不太方便了,于是就寫(xiě)了這個(gè)腳本。第一次用Python寫(xiě)東西,代碼里可能會(huì)有許多 Java、C 這樣的痕跡,見(jiàn)諒。

使用方法

將 python 目錄下的 delUnused.py 放到項(xiàng)目目錄下,然后直接運(yùn)行即可。

代碼說(shuō)明

利用lint進(jìn)行代碼審查

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

命令含義是檢查項(xiàng)目中未使用的資源文件,并且用xml格式輸出結(jié)果,需要提供檢查結(jié)果輸出的路徑和項(xiàng)目路徑。在腳本中已經(jīng)自動(dòng)提供了。

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()

這里給一個(gè)檢查結(jié)果實(shí)例吧

<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 等。

解析檢查結(jié)果

我是利用 minidom 解析的,具體的解析方法不多說(shuō),參考。

獲取根節(jié)點(diǎn)

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

解析第一層子節(jié)點(diǎn)

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 子節(jié)點(diǎn)

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習(xí)慣了,解析數(shù)據(jù)喜歡用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))

處理無(wú)用資源

解析完數(shù)據(jù),可以得到三種資源:

  • Drawable,就一個(gè)文件,可以直接刪
  • xml中的一個(gè)節(jié)點(diǎn),但是這個(gè)xml中就這一個(gè)節(jié)點(diǎn),直接刪文件
  • xml中的一個(gè)節(jié)點(diǎn),這個(gè)xml中有多個(gè)節(jié)點(diǎn),刪除節(jié)點(diǎn)

對(duì)這三種資源進(jìn)行區(qū)分和刪除

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)

刪除節(jié)點(diǎn):

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

總結(jié)

以上所述是小編給大家介紹的使用python 和 lint 刪除項(xiàng)目無(wú)用資源的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 詳解Vue組件動(dòng)態(tài)加載有哪些方式

    詳解Vue組件動(dòng)態(tài)加載有哪些方式

    動(dòng)態(tài)加載組件可以顯著提高應(yīng)用的性能,優(yōu)化用戶(hù)體驗(yàn),尤其是在大型應(yīng)用中,合理的組件加載策略尤為重要,本文將探討幾種在Vue中實(shí)現(xiàn)組件動(dòng)態(tài)加載的具體方案,需要的朋友可以參考下
    2024-10-10
  • python3實(shí)現(xiàn)往mysql中插入datetime類(lèi)型的數(shù)據(jù)

    python3實(shí)現(xiàn)往mysql中插入datetime類(lèi)型的數(shù)據(jù)

    這篇文章主要介紹了python3實(shí)現(xiàn)往mysql中插入datetime類(lèi)型的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • python圖形界面開(kāi)發(fā)之wxPython樹(shù)控件使用方法詳解

    python圖形界面開(kāi)發(fā)之wxPython樹(shù)控件使用方法詳解

    這篇文章主要介紹了python圖形界面開(kāi)發(fā)之wxPython樹(shù)控件使用方法詳解,需要的朋友可以參考下
    2020-02-02
  • python3+PyQt5實(shí)現(xiàn)自定義流體混合窗口部件

    python3+PyQt5實(shí)現(xiàn)自定義流體混合窗口部件

    這篇文章主要為大家詳細(xì)介紹了python3+PyQt5實(shí)現(xiàn)自定義流體混合窗口部件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Python?pip指定安裝源的方法詳解

    Python?pip指定安裝源的方法詳解

    pip是Python包管理工具,該工具提供了對(duì)Python包的查找、下載、安裝、卸載的功能,這篇文章主要給大家介紹了關(guān)于Python?pip指定安裝源的相關(guān)資料,需要的朋友可以參考下
    2023-12-12
  • pytest之a(chǎn)ssert斷言的具體使用

    pytest之a(chǎn)ssert斷言的具體使用

    這篇文章主要介紹了pytest之a(chǎn)ssert斷言的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Python中的上下文管理器和with語(yǔ)句的使用

    Python中的上下文管理器和with語(yǔ)句的使用

    本篇文章主要介紹了Python中的上下文管理器和with語(yǔ)句的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Jupyter Lab無(wú)法打開(kāi)終端窗口的解決方法

    Jupyter Lab無(wú)法打開(kāi)終端窗口的解決方法

    本文主要介紹了Jupyter Lab無(wú)法打開(kāi)終端窗口的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Python學(xué)習(xí)筆記(一)(基礎(chǔ)入門(mén)之環(huán)境搭建)

    Python學(xué)習(xí)筆記(一)(基礎(chǔ)入門(mén)之環(huán)境搭建)

    本系列為Python學(xué)習(xí)相關(guān)筆記整理所得,IT人,多學(xué)無(wú)害,多多探索,激發(fā)學(xué)習(xí)興趣,開(kāi)拓思維,不求高大上,只求懂點(diǎn)皮毛,作為知識(shí)儲(chǔ)備,不至于落后太遠(yuǎn)。本文主要介紹Python的相關(guān)背景,環(huán)境搭建。
    2014-06-06
  • 400多行Python代碼實(shí)現(xiàn)了一個(gè)FTP服務(wù)器

    400多行Python代碼實(shí)現(xiàn)了一個(gè)FTP服務(wù)器

    400多行Python代碼實(shí)現(xiàn)了一個(gè)FTP服務(wù)器,實(shí)現(xiàn)了比之前的xxftp更多更完善的功能
    2012-05-05

最新評(píng)論

新昌县| 天柱县| 湖州市| 沭阳县| 尉犁县| 宜君县| 东兰县| 衡东县| 灵寿县| 明溪县| 霍林郭勒市| 舒兰市| 平远县| 土默特左旗| 宣武区| 阜宁县| 米泉市| 历史| 许昌县| 嘉定区| 金堂县| 堆龙德庆县| 安吉县| 江山市| 休宁县| 红河县| 桐庐县| 孟州市| 周宁县| 鹰潭市| 枞阳县| 阿城市| 鄂州市| 横峰县| 仁布县| 阳高县| 南涧| 安庆市| 巴林左旗| 顺昌县| 镶黄旗|