-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch_repo.py
103 lines (91 loc) · 3.35 KB
/
patch_repo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python
#
# Author: trkzmn (DevOtag)
#
# Require library(ies)
from os import system, path, chdir, getcwd, popen
from json import loads
# Define stuff
class PatchRepo:
def __init__(self):
target_file="device/GM/GM9PRO_sprout/targets.json"
# First parse targets
print("Parsing targets...")
self.parse_targets(target_file)
print("Done.")
# Make sure that all trees cloned which are needed
print("Checking device trees...")
for tree in self.target_trees:
self.clone_tree(tree)
print("Done.")
# Time to cherry-pick
print("Cherry-pick's started...")
if not self.target_commits=="":
keys=self.target_commits.keys()
for key in keys:
self.cherry_pick(key)
print("Done.")
else:
print("There is no patch")
print("All jobs finished successfully. You're ready to building :)")
def __help__(self):
print ("""This script should be used in top of rom folder.
Example: $python device/<manufacturer>/<codename>/patch_repo.py""")
def parse_targets(self, target):
with open(target) as f:
lines=f.read()
lines=loads(lines)
self.target_github=lines["target_github"]
self.target_branch=lines["target_branch"]
self.target_trees=lines["target_trees"]
self.target_commits=lines["target_commits"]
def clone_tree(self, target):
tree_name, tree_path=target.split("|")
if not path.exists(tree_path):
cmd="git clone {} -b {} {} > /dev/null 2>&1".format(self.target_github + tree_name, self.target_branch, tree_path)
response=system(cmd)
if response==32768:
print("Error: Couldn't clone {}".format(tree_name))
self.__help__()
quit()
def cherry_pick(self, target):
main_dir=getcwd()
value=self.target_commits[target]
value_type=type(value)
patch_list=list()
if str(value_type).split("'")[-2]=="list":
for val in value:
patch_list.append(val)
else:
patch_list.append(value)
target_dir=target[8:].replace("_", "/")
if not path.exists(target_dir):
print("Error: Couldn't find target path {}".format(target_dir))
self.__help__()
quit()
chdir(target_dir)
cmd="git fetch {} {} > /dev/null 2>&1".format(self.target_github + target, self.target_branch)
response=system(cmd)
if response==32768:
print("Error: Couldn't fetch {}".format(target))
self.__help__()
quit()
for patch in patch_list:
cmd="git cherry-pick {} > /dev/null 2>&1".format(patch)
response=system(cmd)
if response==0:
pass
elif response==256:
response=popen("git status").read()
system("git reset --hard > /dev/null 2>&1")
if "modified" in response:
print("Error: Please fix conflicts manually !!\n{}:\t{}".format(target, patch))
quit()
else:
print("Error: Unknown Error !!")
self.__help__()
quit()
chdir(main_dir)
# Main
if __name__=="__main__":
PatchRepo()