|
| 1 | +import os |
| 2 | +import argparse |
| 3 | +import shutil |
| 4 | +import re |
| 5 | +import gc |
| 6 | +import git |
| 7 | +from git import Repo |
| 8 | +from git.util import rmtree |
| 9 | +from git.remote import Remote |
| 10 | +from embarc_tools.utils import cd, getcwd |
| 11 | +import sys |
| 12 | +''' |
| 13 | +# EMBARC_ROOT = "C:/Users/jingru/Documents/gitlab/embarc_osp" ## get from |
| 14 | +when generate patch, use |
| 15 | +git format-patch xxx --add-header="repo_link:https://github.com/ARMmbed/mbedtls.git repo_commit:mbedtls-2.4.1" |
| 16 | +''' |
| 17 | + |
| 18 | +def parse_arguments(): |
| 19 | + parser = argparse.ArgumentParser( |
| 20 | + description=__doc__, |
| 21 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 22 | + parser.add_argument("--embarc-root", required=True, help="Specify the embARC_osp directory") |
| 23 | + patches_required_roots = ["library", "middleware", "os"] |
| 24 | + parser.add_argument("--patch-roots", action='append', default=patches_required_roots, help="Directories to search for patch files." |
| 25 | + "All .patch files in these directories will be processed.") |
| 26 | + return parser.parse_args() |
| 27 | + |
| 28 | + |
| 29 | +class Patch: |
| 30 | + def __init__(self, root, files=None): |
| 31 | + self.required_files = ["library", "include"] |
| 32 | + if isinstance(files, list) and files: |
| 33 | + self.required_files = files.extend(self.required_files) |
| 34 | + self.url, self.commit = self.repo_info(root) |
| 35 | + self.repo = None |
| 36 | + # self.repo.git = self.repo.git |
| 37 | + |
| 38 | + |
| 39 | + def repo_info(self, root): |
| 40 | + patch_file = None |
| 41 | + for file in os.listdir(root): |
| 42 | + if os.path.splitext(file)[-1] == ".patch": |
| 43 | + patch_file = os.path.join(root, file) |
| 44 | + break |
| 45 | + if patch_file: |
| 46 | + pattern = r"^repo_link:(.*) repo_commit:(.*)" |
| 47 | + with open(patch_file) as f: |
| 48 | + lines = f.read().splitlines() |
| 49 | + for line in lines: |
| 50 | + m = re.match(pattern, line, re.M|re.I) |
| 51 | + if m: |
| 52 | + repo_link = m.group(1) |
| 53 | + repo_commit = m.group(2) |
| 54 | + return repo_link, repo_commit |
| 55 | + return False, False |
| 56 | + |
| 57 | + |
| 58 | + def need_patch(self): |
| 59 | + result = False |
| 60 | + if not self.required_files: |
| 61 | + return True |
| 62 | + for file in self.required_files: |
| 63 | + if not os.path.exists(file): |
| 64 | + result = True |
| 65 | + break |
| 66 | + return result |
| 67 | + |
| 68 | + |
| 69 | + def check_remote_branch(self): |
| 70 | + branchs_str = self.repo.git.branch("-a") |
| 71 | + branchs_list = branchs_str.split("\n") |
| 72 | + remote_branch = str() |
| 73 | + pattern = r"^\s*remotes\/" |
| 74 | + for branch in branchs_list: |
| 75 | + remote = re.compile(pattern).findall(branch) |
| 76 | + if remote: |
| 77 | + remote_branch = branch.replace(remote[0] , "") |
| 78 | + if remote_branch: |
| 79 | + break |
| 80 | + return remote_branch |
| 81 | + |
| 82 | + |
| 83 | + def check_commit(self): |
| 84 | + result = self.repo.git.log("--pretty", "-1", self.commit) |
| 85 | + if not result: |
| 86 | + self.repo.git.fetch(depth=3000) |
| 87 | + |
| 88 | + |
| 89 | + def fetch_repo(self, url, branch): |
| 90 | + if os.path.exists(".git"): |
| 91 | + gc.collect() |
| 92 | + repo = git.Repo(getcwd()) |
| 93 | + repo.git.clear_cache() |
| 94 | + rmtree(repo.git_dir) |
| 95 | + self.repo = git.Repo.init(getcwd()) |
| 96 | + r = Remote(self.repo, name="origin") |
| 97 | + if r in Remote.list_items(self.repo): |
| 98 | + self.repo.delete_remote(r) |
| 99 | + self.repo.git.remote("add", "origin", url) |
| 100 | + self.repo.git.fetch("origin", "-t") |
| 101 | + remote_branch = self.check_remote_branch() |
| 102 | + if not remote_branch: |
| 103 | + print("Fetch tag failed, just fetch all the source code") |
| 104 | + self.repo.git.fetch("origin") |
| 105 | + remote_branch = self.check_remote_branch() |
| 106 | + if not remote_branch: |
| 107 | + print("No branch found in the remote repo, patch failed") |
| 108 | + shutil.rmtree(".git") |
| 109 | + return False |
| 110 | + self.repo.git.checkout(remote_branch, "--force", b="master") |
| 111 | + print("Branch 'master' set up to track remote branch '{}' from 'origin'".format(remote_branch)) |
| 112 | + self.check_commit() |
| 113 | + self.repo.git.checkout(self.commit, "--force", b=branch) |
| 114 | + print("Switched to a new branch '{}'".format(branch)) |
| 115 | + |
| 116 | + |
| 117 | + def patch(self, branch="embARC"): |
| 118 | + """Apply patch to a repo |
| 119 | + When get repo url and repo commit from .patch file, it will fetch |
| 120 | + tags from one repositories according to url, and create a new |
| 121 | + branch to apply patch. Or it will apply patch to current branch. |
| 122 | + @param branch A new branch to apply patches. |
| 123 | +
|
| 124 | + """ |
| 125 | + if self.need_patch(): |
| 126 | + if self.url and self.commit: |
| 127 | + self.fetch_repo(self.url, branch) |
| 128 | + for file in os.listdir(getcwd()): |
| 129 | + if os.path.splitext(file)[-1] == ".patch": |
| 130 | + patch_file = os.path.join(getcwd(), file) |
| 131 | + print("Try to apply patch {} for this repo".format(file)) |
| 132 | + self.repo.git.am(patch_file) |
| 133 | + print("-----Patch successfully-----") |
| 134 | + return True |
| 135 | + |
| 136 | + |
| 137 | +def get_patch_root(path=None): |
| 138 | + result = list() |
| 139 | + if not path: |
| 140 | + path = getcwd() |
| 141 | + if os.path.exists(path): |
| 142 | + for root, dirs, files in os.walk(path): |
| 143 | + for file in files: |
| 144 | + if os.path.splitext(file)[-1] == ".patch": |
| 145 | + result.append(root) |
| 146 | + break |
| 147 | + return result |
| 148 | + |
| 149 | + |
| 150 | +def main(): |
| 151 | + options = parse_arguments() |
| 152 | + patches_required_roots = list() |
| 153 | + EMBARC_ROOT = os.path.abspath(options.embarc_root) |
| 154 | + if not os.path.exists(EMBARC_ROOT): |
| 155 | + print("Please Specify the embARC_osp directory with --embarc-root option") |
| 156 | + return |
| 157 | + # sys.path.insert(0, os.path.join(EMBARC_ROOT, "scripts/")) |
| 158 | + for patch_root in options.patch_roots: |
| 159 | + patch_root = os.path.join(EMBARC_ROOT, patch_root) |
| 160 | + patch_root_list = get_patch_root(patch_root) |
| 161 | + if patch_root_list: |
| 162 | + patches_required_roots.extend(patch_root_list) |
| 163 | + for patch_root in patches_required_roots: |
| 164 | + with cd(patch_root): |
| 165 | + patch = Patch(patch_root) |
| 166 | + patch.patch() |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == "__main__": |
| 170 | + main() |
0 commit comments