国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2020-02-16 11:12:21
字體:
來源:轉載
供稿:網友

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 淮北市| 眉山市| 云和县| 玛曲县| 墨竹工卡县| 荥经县| 全椒县| 嘉荫县| 涪陵区| 普安县| 呼图壁县| 绥棱县| 渑池县| 淮滨县| 衢州市| 调兵山市| 九龙城区| 平南县| 尉犁县| 昆明市| 东城区| 邛崃市| 南部县| 灵宝市| 如东县| 益阳市| 甘孜| 南昌市| 福鼎市| 枞阳县| 正蓝旗| 高尔夫| 昭苏县| 栖霞市| 金溪县| 凤翔县| 望都县| 昌江| 鲁甸县| 和静县| 理塘县|