Skip to content

Commit d6c5abf

Browse files
author
Wayne Ren
authored
Merge pull request #105 from wangnuannuan/feature/scripts
tool: add python script to apply patch
2 parents 2c03471 + ee2ee36 commit d6c5abf

File tree

10 files changed

+214
-7
lines changed

10 files changed

+214
-7
lines changed

.ci/before_install.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fi
2626
pip install recommonmark || die
2727
pip install sphinx_rtd_theme || die
2828
}
29+
pip install GitPython || die
2930
pip install embarc_cli || die
3031
pip install configparser || die
3132
pip install requests || die

.ci/script.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ if [ "${TOOLCHAIN}" != "sphinx" ] ; then
5656
fi
5757

5858
echo "Using ${TOOLCHAIN}-${TOOLCHAIN_VER}" || die
59-
bash apply_embARC_patch.sh || die
59+
# bash apply_embARC_patch.sh || die
60+
python ./scripts/patch.py --embarc-root . || die
6061
cd .ci || die
6162
if [ "${TOOLCHAIN}" == "sphinx" ] ; then
6263
bash deploy_doc.sh || die

doc/documents/conf.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@
181181
# Example configuration for intersphinx: refer to the Python standard library.
182182
intersphinx_mapping = {'https://docs.python.org/': None}
183183

184-
184+
linkcheck_ignore = [
185+
'http://www.solomon-systech.com/en/product/display-ic/oled-driver-controller/ssd1306/',
186+
]
185187

186188
rst_epilog = u"""
187189
.. |arc| replace:: DesignWare® ARC® processors

doc/documents/contribute/CONTRIBUTING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ These guidelines must be followed when submitting contributions to Synopsys proj
3232

3333
### Code of Conduct
3434

35-
This project adheres to the Contributor Covenant [code of conduct](CODE_OF_CONDUCT.html).
35+
This project adheres to the Contributor Covenant [code of conduct](./CODE_OF_CONDUCT.md).
3636
By participating, you are expected to uphold this code.
3737
Please report unacceptable behavior to [[email protected]](mailto:[email protected]).
3838

@@ -98,7 +98,7 @@ Include details about your configuration and environment:
9898
* **Provide name and version of the OS and toolchain you are using**
9999
* **Provide hardware board and arc core you are using**
100100

101-
#### [Template For Submitting Bug Reports](ISSUE_TEMPLATE.html)
101+
#### [Template For Submitting Bug Reports](./ISSUE_TEMPLATE.md)
102102

103103
### Suggesting Enhancements
104104

@@ -126,7 +126,7 @@ Enhancement suggestions are tracked as [GitHub issues](https://guides.github.com
126126
* **List some other text software platform where this enhancement exists.**
127127
* **Specify the name and version of the OS you're using.**
128128

129-
#### [Template For Submitting Enhancement Suggestions](ISSUE_TEMPLATE.html)
129+
#### [Template For Submitting Enhancement Suggestions](./ISSUE_TEMPLATE.md)
130130

131131
### Your First Code Contribution
132132

@@ -217,4 +217,4 @@ This section lists the labels we use to help us track and manage issues and pull
217217
The labels are loosely grouped by their purpose, but it's not required that every issue have a label from every group or that an issue can't have more than one label from the same group.
218218

219219
## Special Thanks to the Atom Project
220-
The contribution guideline takes a lot of reference from the [Atom Contribution Guideline](https://github.com/atom/atom/blob/master/CONTRIBUTING.md).
220+
<p>The contribution guideline takes a lot of reference from the <a class="reference external" href="https://github.com/atom/atom/blob/master/CONTRIBUTING">Atom Contribution Guideline</a>.</p>

example/baremetal/openthread/cli/OT_CLI.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1732,4 +1732,4 @@ Done
17321732
Diagnostics module is enabled only when building OpenThread with --enable-diag option.
17331733
Go [diagnostics module][1] for more information.
17341734

1735-
[1]:./OT_DIAG.html
1735+
[1]:./OT_DIAG.md

middleware/mbedtls/0001-Add-embARC-FreeRTOS-LwIP-support-to-mbedtls.patch

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ From 0d228d14b1fb16f909ab03ff45b4635b90494ca8 Mon Sep 17 00:00:00 2001
22
From: XiangcaiHuang <[email protected]>
33
Date: Thu, 17 Aug 2017 11:21:12 +0800
44
Subject: [PATCH 1/2] Add embARC (FreeRTOS+LwIP) support to mbedtls
5+
repo_link:https://github.com/ARMmbed/mbedtls.git repo_commit:mbedtls-2.16.0
56

67
---
78
library/net_sockets.c => embARC/net_alt.c | 99 ++--------------

middleware/mbedtls/0002-update-the-copyright-and-add-the-support-of-DW-timer.patch

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ From 94684fb7616db867d6a666dd99ef690a6af28a58 Mon Sep 17 00:00:00 2001
22
From: Wayne Ren <[email protected]>
33
Date: Sat, 29 Dec 2018 15:16:37 +0800
44
Subject: [PATCH 2/2] update the copyright and add the support of DW timer
5+
repo_link:https://github.com/ARMmbed/mbedtls.git repo_commit:mbedtls-2.16.0
56

67
* update copyright
78
* There is no TIMER1 in EMSDP, instead, use DW timer 0 channel 0

scripts/patch.py

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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()

scripts/patch.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
.. patch script:
3+
4+
embARC apply patch
5+
###################
6+
7+
This script scans for the set of patches in this git repository and attempts to apply them to the `embARC` branch.
8+
9+
The default options will scan in directories ``<EMBARC_ROOT>/library``, ``<EMBARC_ROOT>/middleware`` and ``<EMBARC_ROOT>/os`` for patches.
10+
11+
To run the script in the local tree, follow the steps below:
12+
::
13+
$ python patch.py --embarc-root < EMBARC_ROOT >
14+
15+
The embARC patch script accepts the following optional arguments:
16+
17+
-h, --help show this help message
18+
19+
--patch-roots Directories to search for patch files.
20+
All .patch files in these directories
21+
will be processed.
22+
--embarc-root Specify the embARC_osp directory
23+
24+
25+
.. note:: When generate patches, use the option ``--add-header`` to add the repo url and commit. For example, ``--add-header="repo_link:https://github.com/ARMmbed/mbedtls.git repo_commit:mbedtls-2.4.1"``

scripts/requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
PyYAML>=3.13
2+
colorama
3+
PrettyTable
4+
Jinja2
5+
beautifulsoup4
6+
GitPython

0 commit comments

Comments
 (0)