fix: 1、用例方法统计分析支持传入日期;

Description:

Log:
This commit is contained in:
mikigo 2024-03-06 14:44:27 +08:00
parent 26a7318354
commit 3a82c8feb4
7 changed files with 162 additions and 38 deletions

View File

@ -89,6 +89,8 @@ class Manage:
url=None, url=None,
commit1=None, commit1=None,
commit2=None, commit2=None,
startdate=None,
enddate=None,
git_user=None, git_user=None,
git_password=None, git_password=None,
depth=None, depth=None,
@ -140,6 +142,8 @@ class Manage:
self.default_url = url self.default_url = url
self.default_commit1 = commit1 self.default_commit1 = commit1
self.default_commit2 = commit2 self.default_commit2 = commit2
self.default_startdate = startdate
self.default_enddate = enddate
self.default_git_user = git_user self.default_git_user = git_user
self.default_git_password = git_password self.default_git_password = git_password
self.default_depth = depth self.default_depth = depth
@ -498,7 +502,7 @@ class Manage:
) )
def git_control(self, parser=None, sub_parser_csv=None): def git_control(self, parser=None, sub_parser_csv=None):
"""csv相关功能命令参数""" """git相关功能命令参数"""
sub_parser_csv.add_argument( sub_parser_csv.add_argument(
"-a", "--app", default="", "-a", "--app", default="",
help="应用名称apps/autotest_deepin_music 或 autotest_deepin_music" help="应用名称apps/autotest_deepin_music 或 autotest_deepin_music"
@ -524,6 +528,12 @@ class Manage:
sub_parser_csv.add_argument( sub_parser_csv.add_argument(
"-c2", "--commit2", default="", help="commit2" "-c2", "--commit2", default="", help="commit2"
) )
sub_parser_csv.add_argument(
"-s", "--startdate", default="", help="统计开始时间"
)
sub_parser_csv.add_argument(
"-e", "--enddate", default="", help="统计结束时间"
)
args = parser.parse_args() args = parser.parse_args()
git_kwargs = { git_kwargs = {
Args.app_name.value: args.app or self.default_app, Args.app_name.value: args.app or self.default_app,
@ -534,6 +544,8 @@ class Manage:
Args.depth.value: args.depth or self.default_depth, Args.depth.value: args.depth or self.default_depth,
Args.commit1.value: args.commit1 or self.default_commit1, Args.commit1.value: args.commit1 or self.default_commit1,
Args.commit2.value: args.commit2 or self.default_commit2, Args.commit2.value: args.commit2 or self.default_commit2,
Args.startdate.value: args.startdate or self.default_startdate,
Args.enddate.value: args.enddate or self.default_enddate,
} }
from src.git.check import check_git_installed from src.git.check import check_git_installed
@ -557,6 +569,8 @@ class Manage:
git_kwargs.get(Args.app_name.value), git_kwargs.get(Args.app_name.value),
git_kwargs.get(Args.commit1.value), git_kwargs.get(Args.commit1.value),
git_kwargs.get(Args.commit2.value), git_kwargs.get(Args.commit2.value),
git_kwargs.get(Args.startdate.value),
git_kwargs.get(Args.enddate.value),
] ]
): ):
from src.git.code_statistics import CodeStatistics from src.git.code_statistics import CodeStatistics

View File

@ -3,9 +3,9 @@
# SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. # SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
# SPDX-License-Identifier: GPL-2.0-only # SPDX-License-Identifier: GPL-2.0-only
import os import os
import sys
from setting import conf from setting import conf
def check_git_installed(): def check_git_installed():
if not os.popen("git --version").read().startswith("git version"): if not os.popen("git --version").read().startswith("git version"):
print("git 没有安装,我们将尝试安装") print("git 没有安装,我们将尝试安装")

View File

@ -7,12 +7,12 @@ from setting import conf
def sslclone( def sslclone(
url: str = None, url: str = None,
user: str = None, user: str = None,
password: str = None, password: str = None,
branch: str = None, branch: str = None,
depth: [str, int] = None, depth: [str, int] = None,
**kwargs **kwargs,
): ):
os.system( os.system(
f"cd {conf.ROOT_DIR}/src/utils && " f"cd {conf.ROOT_DIR}/src/utils && "
@ -20,12 +20,7 @@ def sslclone(
) )
def clone( def clone(url: str = None, branch: str = "", depth: [str, int] = "", **kwargs):
url: str = None,
branch: str = "",
depth: [str, int] = "",
**kwargs
):
os.system( os.system(
f"cd {conf.APPS_PATH} && git clone {url} " f"cd {conf.APPS_PATH} && git clone {url} "
f"{f'-b {branch}' if branch else ''} {f'--depth {depth}' if depth else ''}" f"{f'-b {branch}' if branch else ''} {f'--depth {depth}' if depth else ''}"

View File

@ -4,30 +4,49 @@
# SPDX-License-Identifier: GPL-2.0-only # SPDX-License-Identifier: GPL-2.0-only
import os import os
import json import json
from setting import conf
from difflib import unified_diff from difflib import unified_diff
from setting import conf
from src.rtk._base import transform_app_name from src.rtk._base import transform_app_name
from src.git.commit import Commit
class CodeStatistics: class CodeStatistics(Commit):
__author__ = "mikigo<huangmingqiang@uniontech.com>" __author__ = "mikigo<huangmingqiang@uniontech.com>"
def __init__(self, app_name: str, commit_new: str, commit_old: str, **kwargs): def __init__(
self,
app_name: str,
commit1: str = None,
commit2: str = None,
startdate: str = None,
enddate: str = None,
**kwargs,
):
if (commit1 or commit2) and startdate:
raise ValueError("commitid 和 startdate是两种不同的方式不能同时使用")
if (commit1 and commit2 is None) or (commit1 is None and commit2):
raise ValueError("commit1 和 commit2 两个参数必须同时传入")
self.app_name = transform_app_name(app_name) self.app_name = transform_app_name(app_name)
self.repo_path = f"{conf.APPS_PATH}/{self.app_name}" self.repo_path = f"{conf.APPS_PATH}/{self.app_name}"
self.commit_new = commit_new self.commit1 = commit1
self.commit_old = commit_old self.commit2 = commit2
if startdate:
super().__init__(app_name, startdate=startdate, enddate=enddate)
commitids = self.commit_id()
if isinstance(commitids, tuple):
self.commit1, self.commit2 = commitids
self.ignore_txt = ["from", "import", ""] self.ignore_txt = ["from", "import", ""]
for i in range(100): for i in range(100):
self.ignore_txt.append(" " * i + "#") self.ignore_txt.append(" " * i + "#")
self._debug = []
def get_git_files(self): def get_git_files(self):
diffs = os.popen(f"cd {self.repo_path};git diff {self.commit_old} {self.commit_new}").read().split("\n") diffs = (
os.popen(f"cd {self.repo_path};git diff {self.commit2} {self.commit1}")
.read()
.split("\n")
)
git_files = [] git_files = []
file_info = {} file_info = {}
for diff in diffs: for diff in diffs:
@ -45,6 +64,8 @@ class CodeStatistics:
return git_files return git_files
def compare_files(self): def compare_files(self):
_fix_debug = []
_new_debug = []
new_test_case_num = 0 new_test_case_num = 0
del_test_case_num = 0 del_test_case_num = 0
fix_test_case_num = 0 fix_test_case_num = 0
@ -53,16 +74,26 @@ class CodeStatistics:
fix_method_num = 0 fix_method_num = 0
git_files = self.get_git_files() git_files = self.get_git_files()
for git_file in git_files: for git_file in git_files:
filepath = git_file.get("file").split(" ")[-1].strip('b/') filepath = git_file.get("file").split(" ")[-1].strip("b/")
filename = filepath.split("/")[-1] filename = filepath.split("/")[-1]
if not filename.endswith(".py"): if not filename.endswith(".py"):
print("===== ignoring:", filepath, "\n") print("===== ignored:", filepath, "\n")
continue continue
print("filepath:", filepath, "\n") print("filepath:", filepath, "\n")
old_code = os.popen(f"cd {self.repo_path}/;git show {self.commit_old}:{filepath}").read().splitlines() or "" old_code = (
new_code = os.popen(f"cd {self.repo_path}/;git show {self.commit_new}:{filepath}").read().splitlines() or "" os.popen(f"cd {self.repo_path}/;git show {self.commit2}:{filepath}")
.read()
.splitlines()
or ""
)
new_code = (
os.popen(f"cd {self.repo_path}/;git show {self.commit1}:{filepath}")
.read()
.splitlines()
or ""
)
dif_gen = unified_diff(old_code, new_code) dif_gen = unified_diff(old_code, new_code)
print("=" * 100) print("=" * 100)
# case # case
@ -84,9 +115,7 @@ class CodeStatistics:
methods = [] methods = []
method_info = {} method_info = {}
for line in dif_txt.splitlines(): for line in dif_txt.splitlines():
if line.startswith( if line.startswith(("---", "+++", "@@", "@")):
("---", "+++", "@@", "@")
):
continue continue
if "def " in line: if "def " in line:
if method_info: if method_info:
@ -109,13 +138,14 @@ class CodeStatistics:
if content.startswith(("-", "+")): if content.startswith(("-", "+")):
if content[1:].startswith(tuple(self.ignore_txt)): if content[1:].startswith(tuple(self.ignore_txt)):
continue continue
self._debug.append(method) _fix_debug.append(method)
fix_method_num += 1 fix_method_num += 1
break break
# 正常出现的方法 # 正常出现的方法
# 方法名称是+开头,直接视为新增方法 # 方法名称是+开头,直接视为新增方法
elif method_name.startswith("+"): elif method_name.startswith("+"):
_new_debug.append(method)
new_method_num += 1 new_method_num += 1
# 方法名称是-开头,直接视为删除方法 # 方法名称是-开头,直接视为删除方法
elif method_name.startswith("-"): elif method_name.startswith("-"):
@ -124,7 +154,7 @@ class CodeStatistics:
if method_content: if method_content:
for content in method_content: for content in method_content:
if content.startswith(("-", "+")): if content.startswith(("-", "+")):
self._debug.append(method) _fix_debug.append(method)
fix_method_num += 1 fix_method_num += 1
break break
@ -140,12 +170,24 @@ class CodeStatistics:
def write_result(self): def write_result(self):
res = self.compare_files() res = self.compare_files()
with open(os.path.join(conf.REPORT_PATH, f"{self.app_name}_git_compare_result.json"), "w", encoding="utf-8") as f: if not os.path.exists(conf.REPORT_PATH):
os.makedirs(conf.REPORT_PATH)
result_file = os.path.join(conf.REPORT_PATH, f"{self.app_name}_git_compare_result.json")
with open(result_file, "w", encoding="utf-8") as f:
f.write(json.dumps(res, ensure_ascii=False, indent=2)) f.write(json.dumps(res, ensure_ascii=False, indent=2))
with open(result_file, "r", encoding="utf-8") as f:
print(f.read())
if __name__ == '__main__':
repo = '/home/mikigo/github/deepin-autotest-framework/apps/autotest_deepin_downloader' if __name__ == "__main__":
commit_n = '059632e9e2dfc7fe580abefe3c51f15d8672d213' app_name = "apps/autotest_deepin_downloader"
commit_o = 'c30572e113a2e90cf340426a8c16c44b7605bfd7' commit1 = "059632e9e2dfc7fe580abefe3c51f15d8672d213"
CodeStatistics(repo, commit_n, commit_o).write_result() commit2 = "c30572e113a2e90cf340426a8c16c44b7605bfd7"
CodeStatistics(
app_name=app_name,
# commit1=commit1,
# commit2=commit2,
startdate="2024-02-25",
enddate="2024-02-27",
).write_result()

68
src/git/commit.py Normal file
View File

@ -0,0 +1,68 @@
#!/usr/bin/env python3
# _*_ coding:utf-8 _*_
# SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
# SPDX-License-Identifier: GPL-2.0-only
import os
import re
import locale
from datetime import datetime
from datetime import timedelta
from setting import conf
from src.rtk._base import transform_app_name
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
class Commit:
__author__ = "mikigo<huangmingqiang@uniontech.com>"
def __init__(self, app_name: str, startdate: str, enddate: str = None):
self.app_name = transform_app_name(app_name)
self.startdate = datetime.strptime(startdate, "%Y-%m-%d")
self.enddate = (
self.now_dt
if enddate is None
else datetime.strptime(enddate, "%Y-%m-%d") + timedelta(days=1)
)
@property
def now_dt(self):
return datetime.strptime(datetime.now().strftime("%Y-%m-%d-%H-%M"), "%Y-%m-%d-%H-%M")
@property
def git_logs(self) -> list:
git_logs = re.findall(
r"commit (.*?)\nAuthor: (.*?)\nDate: (.*?)\n",
os.popen(f"cd {conf.APPS_PATH}/{self.app_name} && git log").read(),
)
return git_logs
def commit_id(self):
start_commit_id = None
start_commit_date = None
end_commit_id = None
end_commit_date = None
for commit, author, _time_str in self.git_logs:
time_str = " ".join(_time_str.split(" ")[:-1])
git_dt = datetime.strptime(time_str, "%a %b %d %H:%M:%S %Y")
if self.startdate <= git_dt <= self.enddate:
if end_commit_id is None:
end_commit_id = commit
end_commit_date = git_dt
else:
if end_commit_id and start_commit_id is None:
start_commit_id = commit
start_commit_date = git_dt
if start_commit_id and end_commit_id:
return end_commit_id, start_commit_id
raise ValueError(f"{self.startdate}{self.enddate} 没有获取到有效的 commit id")
if __name__ == "__main__":
Commit(
app_name="apps/autotest_deepin_downloader", startdate="2024-02-25", enddate="2024-02-26"
).commit_id()

View File

@ -73,6 +73,8 @@ class Args(Enum):
url = "url" url = "url"
commit1 = "commit1" commit1 = "commit1"
commit2 = "commit2" commit2 = "commit2"
startdate = "startdate"
enddate = "enddate"
user = "user" user = "user"
password = "password" password = "password"
depth = "depth" depth = "depth"

View File

@ -1,4 +1,7 @@
#!/bin/bash #!/bin/bash
# _*_ coding:utf-8 _*_
# SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
# SPDX-License-Identifier: GPL-2.0-only
CODE_PATH=$1 CODE_PATH=$1
URL=$2 URL=$2