-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpatch_updater.py
141 lines (122 loc) · 4.59 KB
/
patch_updater.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python
import sys
import os
import glob
import subprocess
import argparse
src_root = 'src/'
class Applier():
def __init__(self):
self.files = []
self.dryrun = True
self.dryrun_counter = 0
def start(self, dryrun):
self.dryrun = dryrun
res = glob.glob("patches/err/*.patch", recursive=True)
#res = glob.glob("patches/err/grd_patches/*.patch", recursive=True)
res = sorted(res, key=str.lower)
for path in res:
self.files.append(path)
self.handle_file(self.files)
def cmd_run(self, cmds, cwd):
PIPE = subprocess.PIPE
process = subprocess.Popen(cmds, cwd=cwd, stdout=PIPE, stderr=PIPE)
return process.communicate()
def get_target_path(self, patch):
return src_root + patch.replace('patches/err/grd_patches/', '').replace('patches/err/', '').replace('.patch', '').replace('-', "/")
def copy_run(self, source, dest):
if os.path.exists(dest):
print(dest, "trying")
print("replace-from" , source)
else:
print("copy-from" , source)
patch_dir = os.path.dirname(dest)
os.makedirs( patch_dir, exist_ok = True)
stdoutput, stderroutput = self.cmd_run(['cp', source, dest], patch_dir)
if len(stderroutput) > 0:
exit()
def dry_run(self, patch):
self.dryrun_counter += 1
print('dry_run: #', self.dryrun_counter, patch)
file = self.get_target_path(patch)
if os.path.exists(file):
print(file, "trying")
stdoutput, stderroutput = self.cmd_run(['patch', '--dry-run', '--reject-file=/dev/stdout',file, patch], '.')
output = stdoutput.decode('utf-8')
erroutput = stderroutput.decode('utf-8')
print(output)
print(erroutput)
if self.has_error(output + erroutput):
# with open(patch, 'r') as f:
# print(f.read())
print(patch, "has_error")
exit()
else:
print(patch, "not-appliable")
def has_error(self, output):
return 'FAILED' in output or 'ignoring' in output or 'failed' in output or 'malformed' in output or 'unexpected' in output
def run(self, patch):
print('run:', patch)
file = self.get_target_path(patch)
if os.path.exists(file):
print(file, "trying")
stdoutput, stderroutput = self.cmd_run(['patch', '--reject-file=/dev/stdout',file, patch], '.')
output = stdoutput.decode('utf-8')
erroutput = stderroutput.decode('utf-8')
print(output)
print(erroutput)
if self.has_error(output + erroutput):
with open(patch, 'r') as f:
print(f.read())
exit()
self.cmd_run(['mv', patch, patch.replace('patches/err/', 'patches/fixed/')], '.')
else:
print(patch, "not-appliable")
def dry_run_resource(self, patch):
patch_rel = patch
file = patch_rel.replace('.patch', '')
if os.path.exists(legacy_root + file):
if os.path.exists(src_root + file):
print(patch, "trying")
print("replace-from" , legacy_root + file)
else:
print("copy-from" , legacy_root + file)
else:
print(patch, "not-appliable")
def run_resource(self, patch):
patch_rel = patch.replace(patch_root, '')
file = patch_rel.replace('.patch', '')
if os.path.exists(legacy_root + file):
self.copy_run(legacy_root + file, src_root + file)
else:
print(patch, "not-appliable")
def handle_res_file(self, files):
for file in files:
if self.dryrun:
self.dry_run_resource(file)
else:
self.run_resource(file)
def handle_file(self, files):
for file in files:
if self.dryrun:
self.dry_run(file)
else:
self.run(file)
def print_file(self, files):
file_names = []
for file in files:
file_names.append(os.path.basename(file).split('/')[-1])
file_names.sort()
for file in file_names:
print(file)
def main():
argument_parser = argparse.ArgumentParser()
argument_parser.add_argument('--apply', action='store_true')
args = argument_parser.parse_args()
applier = Applier()
if args.apply:
applier.start(False)
else:
applier.start(True)
if __name__ == '__main__':
main()