fix: 统计功能修复了一些Bug

Description:

Log:
This commit is contained in:
mikigo 2024-03-08 13:29:36 +08:00
parent a4677cc801
commit 1470297c16
7 changed files with 151 additions and 65 deletions

View File

@ -140,8 +140,8 @@ class Manage:
self.default_pms_link_csv = pms_link_csv self.default_pms_link_csv = pms_link_csv
self.default_send2task = send2task self.default_send2task = send2task
self.default_url = url self.default_url = url
self.default_commit1 = commit1 self.default_start_commit_id = commit1
self.default_commit2 = commit2 self.default_end_commit_id = commit2
self.default_startdate = startdate self.default_startdate = startdate
self.default_enddate = enddate self.default_enddate = enddate
self.default_git_user = git_user self.default_git_user = git_user
@ -523,10 +523,10 @@ class Manage:
"-d", "--depth", default="", help="git仓库克隆深度" "-d", "--depth", default="", help="git仓库克隆深度"
) )
sub_parser_csv.add_argument( sub_parser_csv.add_argument(
"-c1", "--commit_old", default="", help="commit_old" "-c1", "--start_commit_id", default="", help="start_commit_id"
) )
sub_parser_csv.add_argument( sub_parser_csv.add_argument(
"-c2", "--commit_new", default="", help="commit_new" "-c2", "--end_commit_id", default="", help="end_commit_id"
) )
sub_parser_csv.add_argument( sub_parser_csv.add_argument(
"-s", "--startdate", default="", help="统计开始时间" "-s", "--startdate", default="", help="统计开始时间"
@ -542,8 +542,8 @@ class Manage:
Args.password.value: args.password or self.default_git_password, Args.password.value: args.password or self.default_git_password,
Args.branch.value: args.branch or self.default_branch, Args.branch.value: args.branch or self.default_branch,
Args.depth.value: args.depth or self.default_depth, Args.depth.value: args.depth or self.default_depth,
Args.commit1.value: args.commit_old or self.default_commit1, Args.start_commit_id.value: args.start_commit_id or self.default_start_commit_id,
Args.commit2.value: args.commit_new or self.default_commit2, Args.end_commit_id.value: args.end_commit_id or self.default_end_commit_id,
Args.startdate.value: args.startdate or self.default_startdate, Args.startdate.value: args.startdate or self.default_startdate,
Args.enddate.value: args.enddate or self.default_enddate, Args.enddate.value: args.enddate or self.default_enddate,
} }
@ -565,17 +565,24 @@ class Manage:
git_clone(**git_kwargs) git_clone(**git_kwargs)
if all( if all(
[ [
git_kwargs.get(Args.app_name.value), git_kwargs.get(Args.app_name.value),
git_kwargs.get(Args.commit1.value), any(
git_kwargs.get(Args.commit2.value), [
git_kwargs.get(Args.startdate.value), all(
git_kwargs.get(Args.enddate.value), [
] git_kwargs.get(Args.start_commit_id.value),
git_kwargs.get(Args.end_commit_id.value),
]
),
git_kwargs.get(Args.startdate.value),
]
)
]
): ):
from src.git.code_statistics import CodeStatistics from src.git.code_statistics import CodeStatistics
check_git_installed() check_git_installed()
CodeStatistics(**git_kwargs).write_result() CodeStatistics(**git_kwargs).codex()
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -256,4 +256,34 @@ CLASS_NAME_ENDSWITH = Widget
;支持类名包含 xxx 的,自动将函数说明打印为日志,多个参数以逗号隔开 ;支持类名包含 xxx 的,自动将函数说明打印为日志,多个参数以逗号隔开
CLASS_NAME_CONTAIN = ShortCut CLASS_NAME_CONTAIN = ShortCut
# ============================================== # ==============================================
;=============================== PMS CONFIG ===================================
;git子命令用于拉取子项目仓库代码也可统计某两个commit之间或一段时间内用例和方法的修改数据。
[git]
;git仓库的地址
GIT_URL =
;git仓库的用户名
GTI_USER =
;git仓库的密码
GIT_PASSWORD =
;git仓库的分支
BRANCH =
;git clone 时的深度(--depth
DEPTH =
;起始commit id
START_COMMIT_ID =
;结束commit id
END_COMMIT_ID =
;起始日期
START_DATE =
;结束日期
END_DATE =

View File

@ -171,6 +171,18 @@ class _GlobalConfig:
.split(",") .split(",")
) )
# [git]
git_cfg = GetCfg(GLOBAL_CONFIG_FILE_PATH, "git")
GIT_URL = log_cli.get("GIT_URL", default="")
GTI_USER = log_cli.get("GTI_USER", default="")
GIT_PASSWORD = log_cli.get("GIT_PASSWORD", default="")
BRANCH = log_cli.get("BRANCH", default="")
DEPTH = log_cli.get("DEPTH", default="")
START_COMMIT_ID = log_cli.get("START_COMMIT_ID", default="")
END_COMMIT_ID = log_cli.get("END_COMMIT_ID", default="")
START_DATE = log_cli.get("STAR_TDATE", default="")
END_DATE = log_cli.get("END_DATE", default="")
# ====================== 动态获取变量 ====================== # ====================== 动态获取变量 ======================
# username # username
USERNAME = getuser() USERNAME = getuser()
@ -179,7 +191,7 @@ class _GlobalConfig:
HOST_IP = str(popen("hostname -I |awk '{print $1}'").read()).strip("\n").strip() HOST_IP = str(popen("hostname -I |awk '{print $1}'").read()).strip("\n").strip()
PRODUCT_INFO = popen("cat /etc/product-info").read() PRODUCT_INFO = popen("cat /etc/product-info").read()
VERSION = (OS_VERSION.get("EditionName[zh_CN]") or "") + ( VERSION = (OS_VERSION.get("EditionName[zh_CN]") or "") + (
OS_VERSION.get("MinorVersion") or "" OS_VERSION.get("MinorVersion") or ""
) )
# machine type # machine type
# e.g. x86_64 # e.g. x86_64

View File

@ -7,21 +7,34 @@ 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,
): ):
branch = branch or conf.BRANCH
depth = depth or conf.DEPTH
os.system( os.system(
f"cd {conf.ROOT_DIR}/src/utils && " f"cd {conf.ROOT_DIR}/src/utils && "
f"bash sslclone.sh {conf.APPS_PATH} {url} {user} {password} {branch if branch else ''} {depth if depth else ''}" f"bash sslclone.sh {conf.APPS_PATH} "
f"{url or conf.GIT_URL}"
f" {user or conf.GTI_USER} {password or conf.GIT_PASSWORD} "
f"{f'-b {branch}' or ''} {f'--depth {depth}' or ''}"
) )
def clone(url: str = None, branch: str = "", depth: [str, int] = "", **kwargs): def clone(
url: str = None,
branch: str = "",
depth: [str, int] = "",
**kwargs
):
branch = branch or conf.BRANCH
depth = depth or conf.DEPTH
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"{url or conf.GIT_URL}"
f"{f'-b {branch}' or ''} {f'--depth {depth}' or ''}"
) )

View File

@ -19,23 +19,23 @@ class CodeStatistics(Commit):
self, self,
app_name: str, app_name: str,
branch: str, branch: str,
commit_old: str = None, start_commit_id: str = None,
commit_new: str = None, end_commit_id: str = None,
startdate: str = None, startdate: str = None,
enddate: str = None, enddate: str = None,
**kwargs, **kwargs,
): ):
if (commit_old or commit_new) and startdate: if (start_commit_id or end_commit_id) and startdate:
raise ValueError("commit id 和 startdate是两种不同的方式不能同时使用") raise ValueError("commit id 和 startdate是两种不同的方式不能同时使用")
if (commit_old and commit_new is None) or (commit_old is None and commit_new): if (start_commit_id and end_commit_id is None) or (start_commit_id is None and end_commit_id):
raise ValueError("commit_old 和 commit_new 两个参数必须同时传入") raise ValueError("end_commit_id 和 end_commit_id 参数需要同时传入")
self.app_name = transform_app_name(app_name) self.app_name = transform_app_name(app_name or conf.APP_NAME)
self.branch = branch
self.repo_path = f"{conf.APPS_PATH}/{self.app_name}" self.repo_path = f"{conf.APPS_PATH}/{self.app_name}"
self.commit_old = commit_old self.branch = branch or conf.BRANCH
self.commit_new = commit_new self.start_commit_id = start_commit_id or conf.START_COMMIT_ID
self.startdate = startdate self.end_commit_id = end_commit_id or conf.END_COMMIT_ID
self.startdate = startdate or conf.START_DATE
if startdate: if startdate:
super().__init__(app_name, branch=branch, startdate=startdate, enddate=enddate) super().__init__(app_name, branch=branch, startdate=startdate, enddate=enddate)
@ -44,9 +44,9 @@ class CodeStatistics(Commit):
for i in range(100): for i in range(100):
self.ignore_txt.append(" " * i + "#") self.ignore_txt.append(" " * i + "#")
def get_git_files(self, commit_old, commit_new): def get_git_files(self, start_commit_id, end_commit_id):
diffs = ( diffs = (
os.popen(f"cd {self.repo_path};git diff {commit_old} {commit_new}") os.popen(f"cd {self.repo_path};git diff {start_commit_id} {end_commit_id}")
.read() .read()
.split("\n") .split("\n")
) )
@ -66,7 +66,7 @@ class CodeStatistics(Commit):
git_files.append(file_info) git_files.append(file_info)
return git_files return git_files
def compare_files(self, commit_old, commit_new, author): def compare_files(self, start_commit_id, end_commit_id, author):
_fix_debug = [] _fix_debug = []
_new_debug = [] _new_debug = []
new_test_case_num = 0 new_test_case_num = 0
@ -75,7 +75,7 @@ class CodeStatistics(Commit):
new_method_num = 0 new_method_num = 0
del_method_num = 0 del_method_num = 0
fix_method_num = 0 fix_method_num = 0
git_files = self.get_git_files(commit_old, commit_new) git_files = self.get_git_files(start_commit_id, end_commit_id)
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]
@ -86,13 +86,13 @@ class CodeStatistics(Commit):
print("filepath:", filepath, "\n") print("filepath:", filepath, "\n")
new_code = ( new_code = (
os.popen(f"cd {self.repo_path}/;git show {commit_new}:{filepath}") os.popen(f"cd {self.repo_path}/;git show {end_commit_id}:{filepath}")
.read() .read()
.splitlines() .splitlines()
or "" or ""
) )
old_code = ( old_code = (
os.popen(f"cd {self.repo_path}/;git show {commit_old}:{filepath}") os.popen(f"cd {self.repo_path}/;git show {start_commit_id}:{filepath}")
.read() .read()
.splitlines() .splitlines()
or "" or ""
@ -162,9 +162,10 @@ class CodeStatistics(Commit):
break break
res = { res = {
"commit_new": commit_new, "start_commit_id": start_commit_id,
"commit_old": commit_old, "end_commit_id": end_commit_id,
"author": author, "author": author,
"branch": self.branch,
"新增用例": new_test_case_num, "新增用例": new_test_case_num,
"删除用例": del_test_case_num, "删除用例": del_test_case_num,
"修改用例": fix_test_case_num, "修改用例": fix_test_case_num,
@ -179,40 +180,63 @@ class CodeStatistics(Commit):
os.makedirs(conf.REPORT_PATH) os.makedirs(conf.REPORT_PATH)
result_file = os.path.join(conf.REPORT_PATH, f"{self.app_name}_git_compare_result.json") 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: 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=4, default=None))
with open(result_file, "r", encoding="utf-8") as f: with open(result_file, "r", encoding="utf-8") as f:
print(f.read()) print(f.read())
print(f"数据结果报告:{result_file}")
def codex(self): def codex(self):
results = None
if self.startdate: if self.startdate:
commit_id_pairs = self.commit_id() commit_id_pairs = self.commit_id()
results = [] results = {}
for _commit_old, _commit_new, _author in commit_id_pairs: # results_detail = []
res = self.compare_files(_commit_old, _commit_new, _author) for i, (_start_commit_id, _end_commit_id, _author) in enumerate(commit_id_pairs):
results.append(res) res = self.compare_files(_start_commit_id, _end_commit_id, _author)
# results_detail.append(res)
author = res["author"]
new_test_case_num = res["新增用例"]
del_test_case_num = res["删除用例"]
fix_test_case_num = res["修改用例"]
new_method_num = res["新增方法"]
del_method_num = res["删除方法"]
fix_method_num = res["修改方法"]
commit_new = res["end_commit_id"]
if results.get(author) is None:
results[author] = res
else:
results[author]["新增用例"] += new_test_case_num
results[author]["删除用例"] += del_test_case_num
results[author]["修改用例"] += fix_test_case_num
results[author]["新增方法"] += new_method_num
results[author]["删除方法"] += del_method_num
results[author]["修改方法"] += fix_method_num
results[author]["end_commit_id"] = commit_new
elif self.commit_new and self.commit_old: elif self.end_commit_id and self.start_commit_id:
a = os.popen(f"cd {self.repo_path} && git show {self.commit_new}").read() a = os.popen(f"cd {self.repo_path} && git show {self.end_commit_id}").read()
re_author = re.findall(r"Author: (.*?)\n", a) re_author = re.findall(r"Author: (.*?)\n", a)
if re_author: if re_author:
# self.write_result(self.commit_old, self.commit_new, re_author[0])
re_author = re_author[0] re_author = re_author[0]
else: else:
raise ValueError() raise ValueError()
results = self.compare_files(self.commit_old, self.commit_new, re_author) results = self.compare_files(self.start_commit_id, self.end_commit_id, re_author)
if results is None:
raise ValueError()
self.write_result(results) self.write_result(results)
if __name__ == "__main__": if __name__ == "__main__":
app_name = "apps/autotest_deepin_downloader" app_name = "apps/autotest_deepin_downloader"
# commit_new = "059632e9e2dfc7fe580abefe3c51f15d8672d213" end_commit_id = "059632e9e2dfc7fe580abefe3c51f15d8672d213"
# commit_old = "c30572e113a2e90cf340426a8c16c44b7605bfd7" # start_commit_id = "c30572e113a2e90cf340426a8c16c44b7605bfd7"
CodeStatistics( CodeStatistics(
app_name=app_name, app_name=app_name,
branch="master", branch="master",
# commit_old=commit_old, # start_commit_id=start_commit_id,
# commit_new=commit_new, end_commit_id=end_commit_id,
startdate="2024-02-25", # startdate="2024-02-25",
# enddate="2024-02-27", # enddate="2024-02-23",
).codex() ).codex()

View File

@ -27,6 +27,8 @@ class Commit:
else datetime.strptime(enddate, "%Y-%m-%d") + timedelta(days=1) else datetime.strptime(enddate, "%Y-%m-%d") + timedelta(days=1)
) )
self.branch = branch self.branch = branch
if self.branch is None:
raise ValueError("branch 参数必传")
@property @property
def now_dt(self): def now_dt(self):
@ -41,10 +43,6 @@ class Commit:
return git_logs return git_logs
def commit_id(self): def commit_id(self):
start_commit_id = None
start_commit_date = None
end_commit_id = None
end_commit_date = None
commit_ids = deque() commit_ids = deque()
flag = False flag = False
for commit_id, author, _time_str in self.git_logs: for commit_id, author, _time_str in self.git_logs:
@ -59,7 +57,9 @@ class Commit:
flag = True flag = True
if commit_ids and flag: if commit_ids and flag:
commit_id_pairs = [[commit_ids[i][0], commit_ids[i + 1][0], commit_ids[i + 1][1]] for i in range(len(commit_ids) - 1)] commit_id_pairs = [
[commit_ids[i][0], commit_ids[i + 1][0], commit_ids[i + 1][1]] for i in range(len(commit_ids) - 1)
]
return commit_id_pairs return commit_id_pairs
raise ValueError(f"{self.startdate}{self.enddate} 没有获取到有效的 commit id") raise ValueError(f"{self.startdate}{self.enddate} 没有获取到有效的 commit id")

View File

@ -71,8 +71,8 @@ class Args(Enum):
pms_link_csv = "pms_link_csv" pms_link_csv = "pms_link_csv"
send2task = "send2task" send2task = "send2task"
url = "url" url = "url"
commit1 = "commit_old" start_commit_id = "start_commit_id"
commit2 = "commit_new" end_commit_id = "end_commit_id"
startdate = "startdate" startdate = "startdate"
enddate = "enddate" enddate = "enddate"
user = "user" user = "user"