From 9c30fdab6e08d7f770442cbc3cd16c443001a272 Mon Sep 17 00:00:00 2001 From: Leo Fang Date: Tue, 13 Oct 2020 15:28:02 +0800 Subject: [PATCH] Optimization and fix for patch matching 1.use yaml2url replace own realized code 2.fix process in patch matching 3.fix gitee url in yaml2url Signed-off-by: Leo Fang --- advisors/match_patches.py | 80 +++++++-------------------------- advisors/simple_update_robot.py | 4 +- advisors/test/test_yaml2url.py | 2 +- advisors/yaml2url.py | 2 +- 4 files changed, 20 insertions(+), 68 deletions(-) diff --git a/advisors/match_patches.py b/advisors/match_patches.py index 1bb05abe..f2f2ae6d 100755 --- a/advisors/match_patches.py +++ b/advisors/match_patches.py @@ -23,72 +23,24 @@ import subprocess import yaml import gitee +import yaml2url import oa_upgradable import simple_update_robot -def _get_github_url(pkg_info): - """Get github repo url of package""" - url = "https://github.com/" + pkg_info.get("src_repo") + ".git" - return url - - -def _get_git_url(pkg_info): - """Get git repo url of package""" - url = pkg_info.get("src_repo") - return url - - -def _get_gitee_url(pkg_info): - """Get gitee repo url of package""" - url = "https://github.com/" + pkg_info.get("src_repo") + ".git" - return url - - -def _get_gitlab_url(pkg_info): - """Get gitlab repo url of package""" - url = "https://gitlab.gnome.org/GNOME/" + pkg_info.get("src_repo") + ".git" - return url - - -def _get_svn_url(pkg_info): - """Get svn repo url of package""" - url = pkg_info.get("src_repo") - return url - - -def _clone_repo(pkg, pkg_info): +def _clone_repo(pkg_info): """ Clone repo to local """ - vc_type = pkg_info.get("version_control", None) - if vc_type is None: - print("Missing version_control in {}.yaml".format(pkg)) - return None - - switcher = { - "github":_get_github_url, - "git":_get_git_url, - "gitee":_get_gitee_url, - "gitlab.gnome":_get_gitlab_url, - "svn":_get_svn_url - } - get_repo_url = switcher.get(vc_type, None) - - if get_repo_url: - repo_url = get_repo_url(pkg_info) - else: - print("WARNING: Current can't support {vc} for {repo}".format(vc=vc_type, repo=pkg)) + repo_url = yaml2url.yaml2url(pkg_info) + if not (repo_url and repo_url.endswith(".git")): + print("WARNING: Patch matching only support for git repo.") return None dir_pkg = os.path.basename(repo_url).split(".")[0] if os.path.exists(dir_pkg): shutil.rmtree(dir_pkg, ignore_errors=True) - if vc_type == "svn": - print("WARNING: Now can't support patch match in svn type.") - return None - print("git clone {url}".format(url=repo_url)) subprocess.call(["git clone {url}".format(url=repo_url)], shell=True) if os.path.exists(dir_pkg): @@ -129,7 +81,7 @@ def _get_commits_info(gt_api, pkg, c_ver, u_ver): print("WARNING: current version tag or upgrade version tag can't be found in repository.") return None - dir_pkg = _clone_repo(pkg, pkg_yaml) + dir_pkg = _clone_repo(pkg_yaml) if dir_pkg: os.chdir(dir_pkg) print("git log -p {t1}...{t2}".format(t1=u_tag, t2=c_tag)) @@ -145,25 +97,21 @@ def _get_commits_info(gt_api, pkg, c_ver, u_ver): return commit_str -def patches_match(gt_api, pkg, branch, c_ver, u_ver): +def patches_match(gt_api, pkg, c_ver, u_ver): """ Patches match function for pkg on src-openeuler - when patch all matched, then return 'patch_name': None + when patch all matched, then return 'patch_name': all when patch part matched, then return 'patch_name': matched index number """ commit_str = _get_commits_info(gt_api, pkg, c_ver, u_ver) if not commit_str: return None - if os.path.exists(pkg): - shutil.rmtree(pkg, ignore_errors=True) - - simple_update_robot.fork_clone_repo(gt_api, pkg, branch) patch_match = {} - for file_name in os.listdir(pkg): + for file_name in os.listdir("./"): if not file_name.endswith(".patch"): continue - patch_file = open("{path}/{fn}".format(path=pkg, fn=file_name), "r") + patch_file = open("./{fn}".format(fn=file_name), "r") index_match = [] part_matched = False for line in patch_file.readlines(): @@ -179,8 +127,7 @@ def patches_match(gt_api, pkg, branch, c_ver, u_ver): if part_matched: patch_match[file_name] = index_match else: - patch_match[file_name] = None - shutil.rmtree(pkg, ignore_errors=True) + patch_match[file_name] = "all" print(patch_match) return patch_match @@ -194,4 +141,7 @@ if __name__ == "__main__": args = pars.parse_args() user_gitee = gitee.Gitee() - patches_match(user_gitee, args.pkg, args.branch, args.cur_ver, args.up_ver) + simple_update_robot.fork_clone_repo(user_gitee, args.pkg, args.branch) + os.chdir(args.pkg) + patches_match(user_gitee, args.pkg, args.cur_ver, args.up_ver) + os.chdir(os.pardir) diff --git a/advisors/simple_update_robot.py b/advisors/simple_update_robot.py index 80d240b8..f73b1b23 100755 --- a/advisors/simple_update_robot.py +++ b/advisors/simple_update_robot.py @@ -317,7 +317,9 @@ def auto_update_pkg(gt_api, u_pkg, u_branch, u_ver=None): create_spec(u_pkg, spec_str, pkg_ver, u_ver) if len(pkg_spec.patches) >= 1: - patch_match = match_patches.patches_match(gt_api, u_pkg, u_branch, pkg_ver, u_ver) + os.chdir(u_pkg) + patch_match = match_patches.patches_match(gt_api, u_pkg, pkg_ver, u_ver) + os.chdir(os.pardir) if patch_match is not None: modify_patch(u_pkg, patch_match) diff --git a/advisors/test/test_yaml2url.py b/advisors/test/test_yaml2url.py index f27ce1e2..930a4b6d 100644 --- a/advisors/test/test_yaml2url.py +++ b/advisors/test/test_yaml2url.py @@ -129,7 +129,7 @@ def test_get_gitee_url(): pkg_info = yaml.load(doc, Loader=yaml.Loader) url = yaml2url.yaml2url(pkg_info) - assert url == "https://gitee.com/openEuler/lcr" + assert url == "https://gitee.com/openEuler/lcr.git" def test_get_gnu_ftp_url(): diff --git a/advisors/yaml2url.py b/advisors/yaml2url.py index 27ca68b4..aab97f5d 100755 --- a/advisors/yaml2url.py +++ b/advisors/yaml2url.py @@ -83,7 +83,7 @@ def __get_gitee_url(pkg_info): """ Get gitee repo url of package """ - url = "https://gitee.com/" + pkg_info["src_repo"] + url = "https://gitee.com/" + pkg_info["src_repo"] + ".git" return url -- Gitee