forked from ManoloRey/python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwlancaf-merge.py
executable file
·384 lines (357 loc) · 14.7 KB
/
wlancaf-merge.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/env python
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Copyright (C) 2019 Adek Maulana
'''
A simple script to add/update wlan driver QCACLD or PRIMA into/for your,
Android kernel source. *only qcacld-3.0 and prima were supported.
'''
from __future__ import print_function
import os
import sys
from argparse import ArgumentParser
from os import listdir
from os.path import isdir, exists, join
from subprocess import PIPE, Popen, CalledProcessError
from tempfile import mkstemp
def subprocess_run(cmd):
subproc = Popen(cmd, stdout=PIPE, stderr=PIPE,
shell=True, universal_newlines=True)
talk = subproc.communicate()
exitCode = subproc.returncode
if exitCode != 0:
print('An error was detected while running the subprocess:\n'
'exit code: %d\n'
'stdout: %s\n'
'stderr: %s' % (exitCode, talk[0], talk[1]))
if exists('/tmp/merge-message'):
os.remove('/tmp/merge-message')
if 'CONFLICT' in talk[0]:
print('Merge needs manual intervention!.')
print('Resolve conflict(s) and `git commit` if you are done.')
sys.exit(exitCode)
else:
raise CalledProcessError(exitCode, cmd)
return talk
def git_env():
cmd = 'git --version | cut -d " " -f3 | head -n1 | tr -d "\n"'
talk = subprocess_run(cmd)
version = talk[0].strip().split('.')
git_version = (int(version[0]), int(version[1]))
if git_version >= (2, 9):
extra_cmd = '--allow-unrelated-histories'
else:
extra_cmd = ''
return extra_cmd
def parameters():
global wlan_type, merge_type, tag
param = ArgumentParser(description='WLAN-CAF driver updater/initial '
'merge into android kernel source.', )
param.add_argument('-W', '--wlan', choices=['qcacld', 'prima'],
help='Your wlan driver type, either qcacld or prima.',
required=True)
param.add_argument('-I', '--init', choices=['update', 'initial'],
help='Choose wether to update or initial merge.',
required=True)
param.add_argument('-T', '--tag', help='Your current/target CAF TAG.',
required=True)
params = vars(param.parse_args())
wlan_type = params['wlan']
merge_type = params['init']
tag = params['tag']
def repo():
global repo_url, staging, subdir
staging = 'drivers/staging'
if wlan_type == 'qcacld':
repo_url = {
'fw-api': ('https://source.codeaurora.org/'
'quic/la/platform/vendor/qcom-opensource/wlan/fw-api'),
'qca-wifi-host-cmn': ('https://source.codeaurora.org/'
'quic/la/platform/vendor/qcom-opensource/'
'wlan/qca-wifi-host-cmn'),
'qcacld-3.0': ('https://source.codeaurora.org/'
'quic/la/platform/vendor/qcom-opensource/wlan/'
'qcacld-3.0')
}
subdir = ['fw-api', 'qca-wifi-host-cmn', 'qcacld-3.0']
elif wlan_type == 'prima':
repo_url = {
'prima': ('https://source.codeaurora.org/'
'quic/la/platform/vendor/qcom-opensource/wlan/prima')
}
subdir = 'prima'
def check():
if wlan_type == 'qcacld' and merge_type == 'initial':
for subdirs in subdir:
if isdir(join(staging, subdirs)):
if listdir(join(staging, subdirs)):
print('%s exist and not empty' % subdirs)
continue
else:
if subdirs == 'qcacld-3.0' and not listdir(
join(staging, 'fw-api')
) and not listdir(
join(staging, 'qca-wifi-host-cmn')
) and not listdir(join(staging, 'qcacld-3.0')):
return True
else:
return True
else:
print('\n' + 'You might want to use --init initial, '
'because those three are exists, '
'\nor one of them is exist and not empty.')
raise OSError
elif wlan_type == 'qcacld' and merge_type == 'update':
for subdirs in subdir:
if isdir(join(staging, subdirs)):
if not listdir(join(staging, subdirs)):
print('%s exist and empty' % subdirs)
continue
else:
if subdirs == 'qcacld-3.0' and listdir(
join(staging, 'fw-api')
) and listdir(
join(staging, 'qca-wifi-host-cmn')
) and listdir(join(staging, 'qcacld-3.0')):
return True
else:
continue
else:
print('\n' + 'You might want to use --init update, '
"because those three aren't exists."
'\nor exists but one of them has an empty folder.' + '\n')
raise OSError
elif wlan_type == 'prima' and merge_type == 'initial':
if isdir(join(staging, subdir)):
if listdir(join(staging, subdir)):
print('\n' + 'You might want to use --init update, '
"\nbecause prima is exist and it's not empty." + '\n')
raise OSError
else:
return True
else:
return True
elif wlan_type == 'prima' and merge_type == 'update':
if isdir(join(staging, subdir)):
if listdir(join(staging, subdir)):
return True
else:
print("Folder prima exist, but it's just an empty folder.")
raise OSError
else:
print('You might want to use --init initial, '
"because prima isn't exist.")
raise OSError
def merge():
extra_cmd = git_env()
merge_message = '/tmp/merge-message'
if merge_type == 'initial':
for repos in repo_url:
print("Fetching %s with tag '%s'" % (repos, tag))
cmd = 'git fetch --tags -f %s %s' % (repo_url[repos], tag)
subprocess_run(cmd)
SKIP, merge_message = create_merge_message()
if SKIP is not True:
with open(merge_message, 'r') as commit_file:
commit = commit_file.read()
with open(merge_message, 'w') as commit_file:
commit_file.write(repos + ': ' + commit)
else:
with open(merge_message, 'w+') as commit_file:
commit_file.write("%s: Initial tag '%s' into "
"$(git rev-parse --abbrev-ref HEAD)"
% (repos, tag))
while True:
cmds = [
'git merge -s ours --no-commit %s FETCH_HEAD' % extra_cmd,
('git read-tree --prefix=drivers/staging/%s '
'-u FETCH_HEAD' % repos),
('git commit --file %s --no-edit --quiet'
% merge_message)
]
for cmd in cmds:
subprocess_run(cmd)
if cmd == cmds[0]:
print('Merging %s into kernel source...' % repos)
if cmd == cmds[2]:
print('Committing changes...')
print()
if exists(merge_message):
os.remove(merge_message)
break
elif merge_type == 'update':
for repos in repo_url:
print("Fetching %s with tag '%s'" % (repos, tag))
cmd = 'git fetch --tags -f %s %s' % (repo_url[repos], tag)
subprocess_run(cmd)
SKIP, merge_message = create_merge_message()
if SKIP is not True:
with open(merge_message, 'r') as commit_file:
commit = commit_file.read()
with open(merge_message, 'w') as commit_file:
commit_file.write(repos + ': ' + commit)
else:
with open(merge_message, 'w+') as commit_file:
commit_file.write("%s: Merge tag '%s' into "
"$(git rev-parse --abbrev-ref HEAD)"
% (repos, tag))
while True:
print('Merging %s into kernel source...'
% repos)
cmds = [
('git merge -X subtree=drivers/staging/%s FETCH_HEAD '
'--no-edit' % repos),
('git commit --amend --file %s --no-edit --quiet'
% merge_message)
]
print('Committing changes...')
for cmd in cmds:
subprocess_run(cmd)
if wlan_type == 'qcacld':
# Somehow py2 loop repo from 1 to 3 leaving 2 running last
if sys.version_info[0] < 3:
if repos != 'qca-wifi-host-cmn':
print()
else:
if repos != 'qcacld-3.0':
print()
if exists(merge_message):
os.remove(merge_message)
break
return
def include_to_kconfig():
if merge_type == 'initial':
tempRemove = 'endif # STAGING\n'
KconfigToInclude = None
if wlan_type == 'qcacld':
KconfigToInclude = ('source "drivers/staging/qcacld-3.0/Kconfig"'
'\n\nendif # STAGING\n')
KconfigToCheck = 'source "drivers/staging/qcacld-3.0/Kconfig"'
elif wlan_type == 'prima':
KconfigToInclude = ('source "drivers/staging/prima/Kconfig"'
'\n\nendif # STAGING\n')
KconfigToCheck = 'source "drivers/staging/prima/Kconfig"'
with open(join(staging, 'Kconfig'), 'r') as Kconfig:
ValueKconfig = Kconfig.read()
if KconfigToCheck not in ValueKconfig:
print('Including %s into kernel source...' % wlan_type)
with open(join(staging, 'Kconfig'), 'w') as Kconfig:
NewKconfig = ValueKconfig.replace(tempRemove, KconfigToInclude)
Kconfig.write(NewKconfig)
include_to_makefile()
cmds = ['git add drivers/staging/Kconfig',
'git add drivers/staging/Makefile',
'git commit -m "%s: include it into Source"' % wlan_type]
for cmd in cmds:
try:
subprocess_run(cmd)
except CalledProcessError as err:
break
raise err
return
def include_to_makefile():
if merge_type == 'initial':
with open(join(staging, 'Makefile'), 'r') as Makefile:
MakefileValue = Makefile.read()
if wlan_type == 'qcacld':
ValueToCheck = 'CONFIG_QCA_CLD_WLAN'
ValueToInclude = '\nobj-$(CONFIG_QCA_CLD_WLAN)\t+= qcacld-3.0/'
elif wlan_type == 'prima':
ValueToCheck = 'CONFIG_PRONTO_WLAN'
ValueToInclude = '\nobj-$(CONFIG_PRONTO_WLAN)\t+= prima/'
if ValueToCheck not in MakefileValue:
with open(join(staging, 'Makefile'), 'a') as Makefile:
Makefile.write(ValueToInclude)
return
def get_previous_tag():
revision = tag.split('-')[0]
if merge_type == 'initial':
previous_tag = None
return previous_tag
if wlan_type == 'qcacld':
path = 'drivers/staging/qcacld-3.0'
elif wlan_type == 'prima':
path = 'drivers/staging/prima'
cmd = ("git log --oneline --grep='%s*' %s "
"| head -n 1" % (revision, path))
try:
talk = subprocess_run(cmd)
except CalledProcessError:
previous_tag = None
else:
comm = talk[0].strip('\n').replace("'", '').split()
val = [t for t in comm if revision in t]
try:
previous_tag = val[0]
except IndexError:
previous_tag = None
return previous_tag
def create_merge_message():
merge_message = '/tmp/merge-message'
os.rename(mkstemp()[1], merge_message)
previous_tag = get_previous_tag()
tags = 'None'
cmds = [
'git rev-parse --abbrev-ref HEAD',
'git rev-list --count %s' % tags,
('git log --oneline --pretty=format:" %s" "%s"'
% ('%s', tags))
]
SKIP = False
if previous_tag is not None and merge_type == 'update':
range = 'refs/tags/%s..refs/tags/%s' % (previous_tag, tag)
for cmd, value in enumerate(cmds):
cmds[cmd] = value.replace(tags, range)
elif previous_tag is None and merge_type == 'initial':
for cmd, value in enumerate(cmds):
cmds[cmd] = value.replace(tags, tag)
# don't add all commit changes in initial
command = ('git log --oneline --pretty=oneline -45 --pretty=format'
':" %s" "%s"' % ('%s', tag))
for cmd, value in enumerate(cmds):
cmds[cmd] = value.replace(cmds[2], command)
else:
SKIP = True
for cmd in cmds:
talk = subprocess_run(cmd)
if cmd == cmds[0]:
branch = talk[0].strip('\n')
if cmd == cmds[1]:
total_changes = talk[0].strip('\n')
if cmd == cmds[2]:
commits = talk[0]
with open(merge_message, 'w') as commit_msg:
if merge_type == 'initial':
commit_msg.write("Initial tag '%s' into %s" % (tag, branch))
else:
commit_msg.write("Merge tag '%s' into %s" % (tag, branch))
commit_msg.writelines('\n' + '\n')
if merge_type == 'initial':
commit_msg.write('This is an initial merged, '
"all commit changes will not be written fully.\n")
commit_msg.write("Changes in tag '%s': (%s commits)"
% (tag, total_changes))
commit_msg.writelines('\n')
commit_msg.write(commits)
if merge_type == 'initial':
commit_msg.write('\n' + ' ...')
return SKIP, merge_message
def main():
print()
repo()
if not exists('Makefile'):
print('Run this script inside your root kernel source.')
raise OSError
if not exists(staging):
print("Staging folder can't be found, "
'are you sure running it inside kernel source?')
raise OSError
if check() is True:
merge()
include_to_kconfig()
if exists('/tmp/merge-message'):
os.remove('/tmp/merge-message')
if __name__ == '__main__':
parameters()
main()