欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用python 和 lint 刪除項目無用資源的方法

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

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

使用方法

將 python 目錄下的 delUnused.py 放到項目目錄下,然后直接運行即可。

代碼說明

利用lint進行代碼審查

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

命令含義是檢查項目中未使用的資源文件,并且用xml格式輸出結果,需要提供檢查結果輸出的路徑和項目路徑。在腳本中已經(jī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()

這里給一個檢查結果實例吧

<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 解析的,具體的解析方法不多說,參考。

獲取根節(jié)點

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

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

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é)點

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習慣了,解析數(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))

處理無用資源

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

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

對這三種資源進行區(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é)點:

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 刪除項目無用資源的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關文章

  • Django 再談一談json序列化

    Django 再談一談json序列化

    這篇文章主要介紹了Django json序列化,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • python編譯pyc文件的過程解析

    python編譯pyc文件的過程解析

    pyc是一種二進制文件,是由py文件經(jīng)過編譯后,生成的文件,是一種byte code,這篇文章主要介紹了python編譯pyc文件,需要的朋友可以參考下
    2021-09-09
  • 基于python實現(xiàn)新春煙花盛宴效果

    基于python實現(xiàn)新春煙花盛宴效果

    這篇文章給大家用Python綻放了一場新春煙花盛宴,這里提前祝大家新春快樂呀,文中通過代碼示例給大家介紹的非常詳細,感興趣的小伙伴可以自己動手嘗試一下
    2024-02-02
  • 基于Python對象引用、可變性和垃圾回收詳解

    基于Python對象引用、可變性和垃圾回收詳解

    下面小編就為大家?guī)硪黄赑ython對象引用、可變性和垃圾回收詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Python利用atexit模塊實現(xiàn)優(yōu)雅處理程序退出

    Python利用atexit模塊實現(xiàn)優(yōu)雅處理程序退出

    Python的atexit模塊提供了一種方便的方式來注冊這些退出時執(zhí)行的函數(shù),文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-03-03
  • Python3中函數(shù)參數(shù)傳遞方式實例詳解

    Python3中函數(shù)參數(shù)傳遞方式實例詳解

    這篇文章主要介紹了Python3中函數(shù)參數(shù)傳遞方式,結合實例形式較為詳細的分析了Python3中函數(shù)參數(shù)傳遞的常見操作技巧,需要的朋友可以參考下
    2019-05-05
  • Python Flask RESTful使用demo演示

    Python Flask RESTful使用demo演示

    這篇文章主要為大家介紹了Python Flask RESTful使用demo演示,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • Python GUI自動化實現(xiàn)繞過驗證碼登錄

    Python GUI自動化實現(xiàn)繞過驗證碼登錄

    這篇文章主要介紹了python GUI自動化實現(xiàn)繞過驗證碼登錄,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-01-01
  • pyMySQL SQL語句傳參問題,單個參數(shù)或多個參數(shù)說明

    pyMySQL SQL語句傳參問題,單個參數(shù)或多個參數(shù)說明

    這篇文章主要介紹了pyMySQL SQL語句傳參問題,單個參數(shù)或多個參數(shù)說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • 開源Web應用框架Django圖文教程

    開源Web應用框架Django圖文教程

    Python下有許多款不同的 Web 框架。Django是重量級選手中最有代表性的一位。許多成功的網(wǎng)站和APP都基于Django。Django是一個開放源代碼的Web應用框架,由Python寫成。下面我們來一步步學習下吧
    2017-03-03

最新評論