diff --git a/manage.py b/manage.py index adb872d..e9f1453 100644 --- a/manage.py +++ b/manage.py @@ -140,8 +140,8 @@ class Manage: self.default_pms_link_csv = pms_link_csv self.default_send2task = send2task self.default_url = url - self.default_commit1 = commit1 - self.default_commit2 = commit2 + self.default_start_commit_id = commit1 + self.default_end_commit_id = commit2 self.default_startdate = startdate self.default_enddate = enddate self.default_git_user = git_user @@ -523,10 +523,10 @@ class Manage: "-d", "--depth", default="", help="git仓库克隆深度" ) 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( - "-c2", "--commit_new", default="", help="commit_new" + "-c2", "--end_commit_id", default="", help="end_commit_id" ) sub_parser_csv.add_argument( "-s", "--startdate", default="", help="统计开始时间" @@ -542,8 +542,8 @@ class Manage: Args.password.value: args.password or self.default_git_password, Args.branch.value: args.branch or self.default_branch, Args.depth.value: args.depth or self.default_depth, - Args.commit1.value: args.commit_old or self.default_commit1, - Args.commit2.value: args.commit_new or self.default_commit2, + Args.start_commit_id.value: args.start_commit_id or self.default_start_commit_id, + 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.enddate.value: args.enddate or self.default_enddate, } @@ -565,17 +565,24 @@ class Manage: git_clone(**git_kwargs) if all( - [ - git_kwargs.get(Args.app_name.value), - git_kwargs.get(Args.commit1.value), - git_kwargs.get(Args.commit2.value), - git_kwargs.get(Args.startdate.value), - git_kwargs.get(Args.enddate.value), - ] + [ + git_kwargs.get(Args.app_name.value), + any( + [ + all( + [ + 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 check_git_installed() - CodeStatistics(**git_kwargs).write_result() + CodeStatistics(**git_kwargs).codex() if __name__ == "__main__": diff --git a/setting/globalconfig.ini b/setting/globalconfig.ini index b46ec98..df16d07 100644 --- a/setting/globalconfig.ini +++ b/setting/globalconfig.ini @@ -256,4 +256,34 @@ CLASS_NAME_ENDSWITH = Widget ;支持类名包含 xxx 的,自动将函数说明打印为日志,多个参数以逗号隔开 CLASS_NAME_CONTAIN = ShortCut -# ============================================== \ No newline at end of file +# ============================================== + +;=============================== 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 = \ No newline at end of file diff --git a/setting/globalconfig.py b/setting/globalconfig.py index 24a2a56..1c09888 100644 --- a/setting/globalconfig.py +++ b/setting/globalconfig.py @@ -171,6 +171,18 @@ class _GlobalConfig: .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 = getuser() @@ -179,7 +191,7 @@ class _GlobalConfig: HOST_IP = str(popen("hostname -I |awk '{print $1}'").read()).strip("\n").strip() PRODUCT_INFO = popen("cat /etc/product-info").read() VERSION = (OS_VERSION.get("EditionName[zh_CN]") or "") + ( - OS_VERSION.get("MinorVersion") or "" + OS_VERSION.get("MinorVersion") or "" ) # machine type # e.g. x86_64 diff --git a/src/git/clone.py b/src/git/clone.py index 533fc75..4b0a303 100644 --- a/src/git/clone.py +++ b/src/git/clone.py @@ -7,21 +7,34 @@ from setting import conf def sslclone( - url: str = None, - user: str = None, - password: str = None, - branch: str = None, - depth: [str, int] = None, - **kwargs, + url: str = None, + user: str = None, + password: str = None, + branch: str = None, + depth: [str, int] = None, + **kwargs, ): + branch = branch or conf.BRANCH + depth = depth or conf.DEPTH os.system( 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( 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 ''}" ) diff --git a/src/git/code_statistics.py b/src/git/code_statistics.py index f6400ea..56e6e59 100644 --- a/src/git/code_statistics.py +++ b/src/git/code_statistics.py @@ -19,23 +19,23 @@ class CodeStatistics(Commit): self, app_name: str, branch: str, - commit_old: str = None, - commit_new: str = None, + start_commit_id: str = None, + end_commit_id: str = None, startdate: str = None, enddate: str = None, **kwargs, ): - if (commit_old or commit_new) and startdate: + if (start_commit_id or end_commit_id) and startdate: raise ValueError("commit id 和 startdate是两种不同的方式,不能同时使用") - if (commit_old and commit_new is None) or (commit_old is None and commit_new): - raise ValueError("commit_old 和 commit_new 两个参数必须同时传入") + if (start_commit_id and end_commit_id is None) or (start_commit_id is None and end_commit_id): + raise ValueError("end_commit_id 和 end_commit_id 参数需要同时传入") - self.app_name = transform_app_name(app_name) - self.branch = branch + self.app_name = transform_app_name(app_name or conf.APP_NAME) self.repo_path = f"{conf.APPS_PATH}/{self.app_name}" - self.commit_old = commit_old - self.commit_new = commit_new - self.startdate = startdate + self.branch = branch or conf.BRANCH + self.start_commit_id = start_commit_id or conf.START_COMMIT_ID + self.end_commit_id = end_commit_id or conf.END_COMMIT_ID + self.startdate = startdate or conf.START_DATE if startdate: super().__init__(app_name, branch=branch, startdate=startdate, enddate=enddate) @@ -44,9 +44,9 @@ class CodeStatistics(Commit): for i in range(100): 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 = ( - 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() .split("\n") ) @@ -66,7 +66,7 @@ class CodeStatistics(Commit): git_files.append(file_info) 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 = [] _new_debug = [] new_test_case_num = 0 @@ -75,7 +75,7 @@ class CodeStatistics(Commit): new_method_num = 0 del_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: filepath = git_file.get("file").split(" ")[-1].strip("b/") filename = filepath.split("/")[-1] @@ -86,13 +86,13 @@ class CodeStatistics(Commit): print("filepath:", filepath, "\n") 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() .splitlines() or "" ) 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() .splitlines() or "" @@ -162,9 +162,10 @@ class CodeStatistics(Commit): break res = { - "commit_new": commit_new, - "commit_old": commit_old, + "start_commit_id": start_commit_id, + "end_commit_id": end_commit_id, "author": author, + "branch": self.branch, "新增用例": new_test_case_num, "删除用例": del_test_case_num, "修改用例": fix_test_case_num, @@ -179,40 +180,63 @@ class CodeStatistics(Commit): 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=4, default=None)) with open(result_file, "r", encoding="utf-8") as f: print(f.read()) + print(f"数据结果报告:{result_file}") + def codex(self): + results = None if self.startdate: commit_id_pairs = self.commit_id() - results = [] - for _commit_old, _commit_new, _author in commit_id_pairs: - res = self.compare_files(_commit_old, _commit_new, _author) - results.append(res) + results = {} + # results_detail = [] + for i, (_start_commit_id, _end_commit_id, _author) in enumerate(commit_id_pairs): + 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: - a = os.popen(f"cd {self.repo_path} && git show {self.commit_new}").read() + elif self.end_commit_id and self.start_commit_id: + a = os.popen(f"cd {self.repo_path} && git show {self.end_commit_id}").read() re_author = re.findall(r"Author: (.*?)\n", a) if re_author: - # self.write_result(self.commit_old, self.commit_new, re_author[0]) re_author = re_author[0] else: 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) if __name__ == "__main__": app_name = "apps/autotest_deepin_downloader" - # commit_new = "059632e9e2dfc7fe580abefe3c51f15d8672d213" - # commit_old = "c30572e113a2e90cf340426a8c16c44b7605bfd7" + end_commit_id = "059632e9e2dfc7fe580abefe3c51f15d8672d213" + # start_commit_id = "c30572e113a2e90cf340426a8c16c44b7605bfd7" CodeStatistics( app_name=app_name, branch="master", - # commit_old=commit_old, - # commit_new=commit_new, - startdate="2024-02-25", - # enddate="2024-02-27", + # start_commit_id=start_commit_id, + end_commit_id=end_commit_id, + # startdate="2024-02-25", + # enddate="2024-02-23", ).codex() diff --git a/src/git/commit.py b/src/git/commit.py index 6c073d2..b3900cb 100644 --- a/src/git/commit.py +++ b/src/git/commit.py @@ -27,6 +27,8 @@ class Commit: else datetime.strptime(enddate, "%Y-%m-%d") + timedelta(days=1) ) self.branch = branch + if self.branch is None: + raise ValueError("branch 参数必传") @property def now_dt(self): @@ -41,10 +43,6 @@ class Commit: return git_logs def commit_id(self): - start_commit_id = None - start_commit_date = None - end_commit_id = None - end_commit_date = None commit_ids = deque() flag = False for commit_id, author, _time_str in self.git_logs: @@ -59,7 +57,9 @@ class Commit: flag = True 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 raise ValueError(f"{self.startdate} 到 {self.enddate} 没有获取到有效的 commit id") diff --git a/src/rtk/_base.py b/src/rtk/_base.py index 7d65a15..4bf8d94 100644 --- a/src/rtk/_base.py +++ b/src/rtk/_base.py @@ -71,8 +71,8 @@ class Args(Enum): pms_link_csv = "pms_link_csv" send2task = "send2task" url = "url" - commit1 = "commit_old" - commit2 = "commit_new" + start_commit_id = "start_commit_id" + end_commit_id = "end_commit_id" startdate = "startdate" enddate = "enddate" user = "user"