-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDNAnother.py
657 lines (651 loc) · 27.8 KB
/
DNAnother.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#!/usr/bin/env python3
#coding=utf-8
#copyright (c) 2021 awathefox.
#This program was under the LGPLv2.1 license.
import os
import subprocess
import sys
import shutil
import json
import traceback
#dialog begin
def choice(run,default=""): # run=['title','menu',['title','obj'],['title','obj'],...]
cmd="dialog --title "+json.dumps(run[0],ensure_ascii=False)+" --default-item "+json.dumps(default,ensure_ascii=False)+" --menu "+json.dumps(run[1],ensure_ascii=False)+" 0 0 0 "
s=2
while s < len(run):
cmd+=json.dumps(run[s][0],ensure_ascii=False)+" "+json.dumps(run[s][1],ensure_ascii=False)+" "
s=s+1
x=subprocess.run(cmd,stderr=subprocess.PIPE,shell=True)
return [x.returncode,x.stderr.decode()]
def inputbox(title,msg,default=""):
cmd="dialog --title "+json.dumps(title,ensure_ascii=False)+" --inputbox "+json.dumps(msg,ensure_ascii=False)+" 0 0 "+json.dumps(default,ensure_ascii=False)
x=subprocess.run(cmd,stderr=subprocess.PIPE,shell=True)
return [x.returncode,x.stderr.decode()]
def textbox(title,file):
cmd="dialog --title "+json.dumps(title,ensure_ascii=False)+" --textbox "+json.dumps(file,ensure_ascii=False)+" 0 0"
os.system(cmd)
return
def fselect(title,dir=""):
cmd="dialog --title "+json.dumps(title,ensure_ascii=False)+" --fselect "+json.dumps(dir,ensure_ascii=False)+" 20 50"
x=subprocess.run(cmd,stderr=subprocess.PIPE,shell=True)
return [x.returncode,x.stderr.decode()]
def dselect(title,dir=""):
cmd="dialog --title "+json.dumps(title,ensure_ascii=False)+" --dselect "+json.dumps(dir,ensure_ascii=False)+" 20 50"
x=subprocess.run(cmd,stderr=subprocess.PIPE,shell=True)
return [x.returncode,x.stderr.decode()]
def mulchoice(run): #run=['title','menu',['title','obj'],['title','obj'],...]
cmd="dialog --title "+json.dumps(run[0],ensure_ascii=False)+" --checklist "+json.dumps(run[1],ensure_ascii=False)+" 0 0 "+str(len(run[2:]) if len(run[2:]) < 5 else 5)+" "
s=2
while s < len(run):
cmd+=json.dumps(run[s][0],ensure_ascii=False)+" "+json.dumps(run[s][1],ensure_ascii=False)+" "+str(s-1)+" "
s=s+1
x=subprocess.run(cmd,stderr=subprocess.PIPE,shell=True)
return [x.returncode,x.stderr.decode().split(' ')]
def msg(title,msg):
cmd="dialog --title "+json.dumps(title,ensure_ascii=False)+" --msgbox "+json.dumps(msg,ensure_ascii=False)+" 0 0"
os.system(cmd)
return
def infobox(title,msg):
cmd="dialog --title "+json.dumps(title,ensure_ascii=False)+" --infobox "+json.dumps(msg,ensure_ascii=False)+" 20 50"
os.system(cmd)
return
def progress(title,msg,percent):
cmd="echo "+str(percent)+" | dialog --title "+json.dumps(title,ensure_ascii=False)+" --gauge "+json.dumps(msg,ensure_ascii=False)+" 20 50 "+str(percent)
os.system(cmd)
return
def yesno(title,msg,yesbutton="是",nobutton="否"):
cmd="dialog --title "+json.dumps(title,ensure_ascii=False)+" --yes-label "+json.dumps(yesbutton,ensure_ascii=False)+" --no-label "+json.dumps(nobutton,ensure_ascii=False)+" --yesno "+json.dumps(msg,ensure_ascii=False)+" 0 0"
return os.system(cmd)
def exitmenu():
os.system("clear")
exit(0)
CHOICE_OK=0
CHOICE_NO=1
CHOICE_EXIT=255
#dialog end
#convert begin
def gettype(path):
x=subprocess.run("file -b "+path,shell=True,stdout=subprocess.PIPE)
return x.stdout.decode()
#br<->dat begin
def br2dat(source,output):
os.system("brotli -d -o {0} {1}".format(output,source))
def dat2br(source,output,ext_arg):
os.system("brotli {0} -o {1} {2}".format(ext_arg,output,source))
#br<->dat end
#dat<->img begin
def dat2img(list,source,output,sparse=False):
if sparse:
os.system("python3 {0}/bin/sdat2img/sdat2img.py {1} {2} temp/output.tmp.img >/dev/null".format(sys.path[0],list,source))
os.system("img2simg temp/output.tmp.img {0}".format(output))
os.remove("temp/output.tmp.img")
else:
os.system("python3 {0}/bin/sdat2img/sdat2img.py {1} {2} {3} >/dev/null".format(sys.path[0],list,source,output))
def img2dat(source,output,prefix):
if gettype(source).startswith("Android sparse image"):
os.system("python3 {0}/bin/img2sdat/img2sdat.py -v 4 {1} -p {2} -o {3} >/dev/null".format(sys.path[0],source,prefix,output))
else:
os.system("python3 {0}/bin/rimg2sdat/rimg2sdat.py -v 4 {1} -p {2} -o {3} >/dev/null".format(sys.path[0],source,prefix,output))
#dat<->img end
#img<->dir begin
def createFile(name,size,bs=1024*1024*500): #bs is set to 500MiB
with open(name,"wb") as f:
while size>bs:
f.write(b'\0'*bs)
size-=bs
f.write(b'\0'*size)
def unpackimg(source,output):
if gettype(source).startswith("Android bootimg"):
os.chdir("temp")
os.mkdir("abootimg")
os.chdir("..")
shutil.copy(source,"temp/abootimg/abootimg.tmp.img")
os.chdir("temp/abootimg")
os.system("abootimg -x abootimg.tmp.img >/dev/null")
os.remove("abootimg.tmp.img")
os.system("abootimg-unpack-initrd initrd.img 2>/dev/null")
os.chdir("../..")
os.system("mv -f temp/abootimg/. "+output)
else:
if gettype(source).startswith("Android sparse image"):
os.system("simg2img {0} temp/output.tmp.img > /dev/null")
source="temp/output.tmp.img"
os.system("mount -o ro {0} mount".format(source))
os.system("cp -r mount/. {0}".format(output))
os.system("umount mount")
if source=="temp/output.tmp.img":
os.remove(source)
return os.path.getsize(source)
def packimg(source,output,size,sparseimg=False):
if os.path.exists(source+"/bootimg.cfg"):
os.system("cp -r "+source+" temp/abootimg")
os.chdir("temp/abootimg")
if os.path.exists("ramdisk"):
os.system("abootimg-pack-initrd initrd.new.img ramdisk/ >/dev/null")
else:
shutil.copyfile("initrd.img","initrd.new.img")
os.system("abootimg --create abootimg.tmp.img -k zImage -r initrd.new.img >/dev/null".format(output))
os.chdir("../..")
shutil.copyfile("temp/abootimg/abootimg.tmp.img",output)
shutil.rmtree("temp/abootimg")
else:
createFile("temp/output.tmp.img",size)
os.system("mkfs.ext2 -L "+source.split("/")[-1]+" -O large_file,huge_file,extent -d {0} temp/output.tmp.img > /dev/null".format(source))
if sparseimg:
os.system("img2simg temp/output.tmp.img {0}".format(output))
os.remove("temp/output.tmp.img")
else:
shutil.move("temp/output.tmp.img",output)
return os.path.getsize(output) #返回sparse_img的size 用于更新
#img<->dir end
#payload->imgs begin
def payload_partitions(file):
x=subprocess.run("python3 {0}/bin/payload_dumper/partget.py {1}".format(sys.path[0],file),shell=True,stdout=subprocess.PIPE)
r=x.stdout.decode().split("\n")
for i in range(len(r)):
if r[i]=='':
del r[i]
return r
def extract_payload(payload,output,images=None):
if images==None:
os.system("python3 {0}/bin/payload_dumper/payload_dumper.py {1} --out {2} > /dev/null".format(sys.path[0],payload,output))
else:
os.system("python3 {0}/bin/payload_dumper/payload_dumper.py {1} --out {2} --images {3} > /dev/null".format(sys.path[0],payload,output,",".join(images)))
#payload->imgs end
#convert end
#temp begin
def cleartemp():
shutil.rmtree("temp",True)
os.mkdir("temp")
def cleartarget():
shutil.rmtree("target",True)
os.mkdir("target")
def clearconv():
shutil.rmtree("conv/",True)
os.mkdir("conv")
#temp end
zip_ext_arg=""
bro_ext_arg="-1"
def convert(path,op):
global bro_ext_arg
global zip_ext_arg
if op=='img2dat':
img2dat(path,'conv/output.new.dat','output')
if path.startswith('conv/'):
shutil.rmtree(path) if os.path.isdir(path) else os.remove(path)
return 'conv/output.new.dat'
elif op=='dat2img':
while 1:
if not path.startswith('conv/') or (path.endswith(".dat") and (not os.path.exists("conv/output.transfer.list"))):
s=fselect("选择 "+path.split("/")[-1].split(".")[0]+".transfer.list")
else:
s=[CHOICE_OK,'conv/output.transfer.list']
if s[0]!=CHOICE_OK:
if yesno("高级转换","确实要取消转换吗?")==CHOICE_OK:
clearconv()
return ''
elif os.path.exists(s[1]) and os.path.isfile(s[1]) and s[1].endswith(".list"):
infobox("高级转换","正在处理文件,请稍等...")
dat2img(s[1],path,'conv/output.img',True)
if path.startswith('conv/'):
shutil.rmtree(path) if os.path.isdir(path) else os.remove(path)
return 'conv/output.img'
else:
msg("高级转换","无法读取文件。")
elif op=='br2dat':
br2dat(path,'conv/output.new.dat')
if path.startswith('conv/'):
shutil.rmtree(path) if os.path.isdir(path) else os.remove(path)
return 'conv/output.new.dat'
elif op=='dat2br':
dat2br(path,'conv/output.new.dat.br',bro_ext_arg)
if path.startswith('conv/'):
shutil.rmtree(path) if os.path.isdir(path) else os.remove(path)
return 'conv/output.new.dat.br'
elif op=='packimg':
while 1:
s=inputbox("高级转换","您需要手动输入镜像大小。\n您可以参照size.json,或输入打包前img的大小。")
if s[0]!=CHOICE_OK:
if yesno("高级转换","确实要取消转换吗?")==CHOICE_OK:
clearconv()
return ''
elif s[1].isdigit() and int(s[1])>=0:
infobox("高级转换","正在处理文件,请稍等...")
packimg(path,'conv/output.img',int(s[1]),True)
if path.startswith('conv/'):
shutil.rmtree(path) if os.path.isdir(path) else os.remove(path)
return 'conv/output.img'
else:
msg("高级转换","数据不合法。")
elif op=='unpackimg':
unpackimg(path,'conv/')
if path.startswith('conv/'):
shutil.rmtree(path) if os.path.isdir(path) else os.remove(path)
return 'conv/'
elif op=='cp':
shutil.copy(path,'conv/')
if path.startswith('conv/'):
shutil.rmtree(path) if os.path.isdir(path) else os.remove(path)
return 'conv/'
elif op=='extract_payload':
extract_payload(path,"conv/")
return 'conv/'
def highconv():
infobox("请稍等","正在预准备")
cleartemp()
if yesno("高级转换","此功能会清理上一次的转换文件。\n确实要这样做吗?")==CHOICE_OK:
infobox("请稍等","正在清理 conv")
clearconv()
conv={
'img':{
'dat':['img2dat'],
'br':['img2dat','dat2br'],
'dir':['unpackimg'],
'img':['cp'],
},
'br':{
'dat':['br2dat'],
'br':['cp'],
'dir':['br2dat','dat2img','unpackimg'],
'img':['br2dat','dat2img'],
},
'dat':{
'dat':['cp'],
'br':['dat2br'],
'dir':['dat2img','unpackimg'],
'img':['dat2img'],
},
'dir':{
'dat':['packimg','dat2br'],
'br':['packimg','img2dat','dat2br'],
'dir':['cp'],
'img':['packimg'],
},
'bin':{
'img':['extract_payload'],
'bin':['cp']
}
}
x=inputbox("高级转换","输入起始和目标,将自动计算转换路径。\n支持的格式:br,dat,img,dir,bin\n语法:原格式->目标格式")
if x[0]!=CHOICE_OK:
return None
else:
a=x[1].split("->")
if len(a)<2 or conv[a[0]]==None or conv[a[0]][a[1]]==None:
msg("高级转换","原/目标格式错误或无法转换。")
q=fselect("选择原文件")
if q[0]!=CHOICE_OK:
return None
elif os.path.exists(q[1]) and (q[1].endswith("."+a[0]) or (os.path.isdir(q[1]) and a[0]=="dir")):
path=q[1]
for x in range(len(conv[a[0]][a[1]])):
progress("高级转换","正在执行 "+conv[a[0]][a[1]][x]+" ["+str(x)+"/"+str(len(conv[a[0]][a[1]]))+"]",round((x/len(conv[a[0]][a[1]]))*100))
path=convert(path,conv[a[0]][a[1]][x])
if path=="":
msg("高级转换","转换失败。")
break
if path=="":
return None
else:
msg("高级转换","完成。\n目标已保存在 "+path+"。")
else:
msg("高级转换","文件(夹)发生错误。")
return None
def rom():
global bro_ext_arg
global zip_ext_arg
if os.path.exists("base") and os.path.isdir("base"):
while 1:
ch=choice([
"DNAnother v1.0.3-stable",
"选择您要对这个项目进行的操作。",
["1","解压"],
["2","安装"],
["3","打包"],
["4","终端"],
["5","其他"],
["6","退出"],
])[1]
if ch=='' or ch=='6':
exitmenu()
elif ch=='1':
f=[
'确认',
'请选择要解压的文件',
]
infobox("DNAnother","正在列出清单")
for o in os.listdir("base"):
s=list(os.path.splitext(o))[1][1:]
w=gettype("base/"+o)
if not os.path.isfile("base/"+o):
continue
elif o.endswith(".img") and (w.startswith("Android bootimg") or w.startswith("Android sparse image") or w.startswith("Linux rev 1.0 ext2 filesystem data")):
f.append([o,s])
elif o.endswith(".br"):
f.append([o,s])
elif o.endswith(".dat") and not o.endswith(".patch.dat"):
f.append([o,s])
elif o.endswith(".bin"):
f.append([o,s])
if len(f)<3:
msg("DNAnother","没有可以用于解压的项目。")
continue
x=mulchoice(f)
infobox("DNAnother","正在预准备")
cleartemp()
if x[0]==CHOICE_OK and x[1]!=['']:
x=x[1]
fail=False
progress("DNAnother","检查清单中",0)
if os.path.exists("target/size.json") and os.path.isfile("target/size.json"):
with open("target/size.json") as f:
sizetable=json.loads(f.read())
else:
sizetable={}
for i in x:
z=os.path.splitext(i)
if os.path.exists("target/{0}".format(i.split(".")[0])):
if yesno(i,"重新解包将会删除已经解包的内容。\n就算这样也要继续吗?")!=CHOICE_OK:
progress(i,"过程被跳过",100)
continue
else:
progress(i,"正在删除文件...",0)
shutil.rmtree("target/{i}".format(i.split(".")[0]),True)
if z[1]==".br":
progress(i,"正在执行 br2dat [1/3]",round(1/6*100))
br2dat("base/{0}".format(i),"temp/{0}.tmp.dat".format(i))
progress(i,"正在执行 dat2img [2/3]",round(2/6*100))
while 1:
if not os.path.exists("base/{0}.transfer.list".format(i.split(".")[0])):
s=fselect("选择 "+i.split(".")[0]+".transfer.list")
else:
s=[CHOICE_OK,'base/{0}.transfer.list'.format(i.split(".")[0])]
if s[0]!=CHOICE_OK:
if yesno(i,"确实要取消转换吗?")==CHOICE_OK:
cleartemp()
fail=True
break
elif os.path.exists(s[1]) and os.path.isfile(s[1]) and s[1].endswith(".list"):
infobox(i,"正在处理文件,请稍等...")
dat2img(s[1],"temp/{0}.tmp.dat".format(i),"temp/{0}.tmp.img".format(i))
break
else:
msg(i,"无法读取文件。")
progress(i,"正在执行 dat2img [2/3]",round(3/6*100))
os.remove("temp/{0}.tmp.dat".format(i))
progress(i,"正在执行 unpackimg [3/3]".format(i),round(4/6*100))
sizetable[i.split(".")[0]]=[]
sizetable[i.split(".")[0]].append(unpackimg("temp/{0}.tmp.img".format(i),"target/{0}".format(i.split(".")[0])))
sizetable[i.split(".")[0]].append(gettype("temp/{0}.tmp.img".format(i)).split(",")[1][6:])
progress(i,"正在执行 unpackimg [3/3]",round(5/6*100))
os.remove("temp/{0}.tmp.img".format(i))
progress(i,"过程完成",100)
elif z[1]==".dat":
progress(i,"正在执行 dat2img [1/2]",round(1/4*100))
while 1:
if not os.path.exists("base/{0}.transfer.list".format(i.split(".")[0])):
s=fselect("选择 "+i.split(".")[0]+".transfer.list")
else:
s=[CHOICE_OK,'base/{0}.transfer.list'.format(i.split(".")[0])]
if s[0]!=CHOICE_OK:
if yesno(i,"确实要取消转换吗?")==CHOICE_OK:
cleartemp()
fail=True
break
elif os.path.exists(s[1]) and os.path.isfile(s[1]) and s[1].endswith(".list"):
infobox(i,"正在处理文件,请稍等...")
dat2img(s[1],"base/{0}".format(i),"temp/{0}.tmp.img".format(i))
else:
msg(i,"无法读取文件。")
progress(i,"正在执行 unpackimg [2/2]".format(i),round(2/4*100))
sizetable[i.split(".")[0]]=unpackimg("temp/{0}.tmp.img".format(i),"target/{0}".format(i.split(".")[0]))
progress(i,"正在执行 unpackimg [2/2]",round(3/4*100))
os.remove("temp/{0}.tmp.img".format(i))
progress(i,"过程完成",100)
elif z[1]==".img":
progress(i,"正在执行 unpackimg [1/1]".format(i),round(1/2*100))
sizetable[i.split(".")[0]]=unpackimg("base/{0}".format(i),"target/{0}".format(i.split(".")[0]))
progress(i,"过程完成",100)
elif z[1]==".bin":
if yesno("警告 - "+i,"确实要解包payload.bin吗?\n打包操作将无法进行或生成错误。")!=CHOICE_OK:
continue
if yesno("警告 - "+i,"为防止解包payload.bin影响其他分区,所有已解包文件将被删除。\n就算这样也要继续吗?")!=CHOICE_OK:
continue
infobox(i,"正在清空 target")
cleartarget()
infobox(i,"正在重置分区大小")
sizetable={}
infobox(i,"完成")
infobox(i,"正在列出分区...")
a=payload_partitions("base/"+i)
p=[
i,
"请选择要解压的分区",
]
for d in range(len(a)):
p.append([str(d+1),a[d]])
p=mulchoice(p)
if p[0]==CHOICE_OK:
p=p[1]
infobox(i,"正在检查情报...")
fin=[]
for l in p:
fin.append(a[int(l)-1])
progress(i,"正在执行 extract_payload [1/1]",100)
extract_payload("base/"+i,"temp/",fin)
for g in os.listdir("temp/"):
c=gettype("temp/"+g)
if c.startswith("Android sparse image") or c.startswith("Android bootimg") or c.startswith("Linux rev 1.0 ext2 filesystem data"):
progress(i+"->"+g,"正在执行 unpackimg [1/1]",50)
sizetable[i.split(".")[0]]=unpackimg("temp/{0}".format(g),"target/{0}".format(g.split(".")[0]))
progress(i+"->"+g,"正在执行 unpackimg [1/1]",100)
os.remove("temp/{0}".format(g))
progress(i+"->"+g,"过程完成",100)
else:
progress(i+"->"+g,"正在执行 cp [1/1]",50)
shutil.copy("temp/{0}".format(g),"target/")
progress(i+"->"+g,"正在执行 cp [1/1]",100)
os.remove("temp/{0}".format(g))
progress(i+"->"+g,"过程完成",100)
else:
fail=True
break
with open("target/size.json","w") as f:
f.write(json.dumps(sizetable))
if fail:
msg("DNAnother","转换失败。\n已转换成功的将被保留。")
else:
msg("DNAnother","完成。")
elif ch=='2':
infobox("DNAnother","正在预准备")
cleartemp()
file=fselect("选择补丁包[.zip]","/")
progress("安装中","准备中...",0)
if file[0]==CHOICE_OK and os.path.exists(file[1]) and os.path.isfile(file[1]) and file[1].endswith(".zip"):
progress("安装中","正在解压缩补丁...",0)
os.system("unzip -d temp \""+file[1].replace("\"","\\\"")+"\"")
progress("安装中","正在安装补丁...",50)
s_backup=os.getcwd()
os.chdir("target")
os.system("bash ../temp/install.sh")
os.chdir(s_backup)
progress("安装中","正在清理临时文件")
cleartemp()
msg("安装补丁","完成。")
input()
elif file[0]==CHOICE_OK:
msg("错误","文件不正确。")
elif ch=='3':
if yesno("警告","是否开始打包?\n操作可能需要5分钟或更久。")==CHOICE_OK:
infobox("DNAnother","正在预准备")
cleartemp()
progress("DNAnother","准备中",0)
if os.path.exists("target/size.json") and os.path.isfile("target/size.json"):
with open("target/size.json") as f:
sizetable=json.dumps(f.read())
else:
sizetable={}
for i in os.listdir("target"):
progress(i,"正在检查信息".format(i),0)
if os.path.isfile("target/"+i):
progress(i,"正在执行 cp [1/1]",100)
os.system("cp target/"+i+" temp/")
elif os.path.exists("target/"+i+"/bootimg.cfg"):
progress(i,"正在执行 packimg [1/1]".format(i),100)
packimg("target/{0}".format(i),"temp/{0}.img".format(i),0)
progress(i,"完成".format(i),100)
else:
progress(i,"正在执行 packimg [1/3]".format(i),round(1/6*100))
if type(sizetable[i])!=list or len(sizetable[i])!=2 or type(sizetable[i][0])!=int or sizetable[i][0]<0 or type(sizetable[i][1])!=str or len(sizetable[i][1])!=32:
while 1:
s=inputbox(i,"分区信息缺失。\n请手动输入分区大小。")
if s[0]!=CHOICE_OK:
if yesno(i,"确实要取消打包操作吗?")==CHOICE_OK:
cleartemp()
fail=True
elif s[1].isdigit() and int(s[1])>=0:
infobox(i,"正在处理数据...")
packimg("target/{0}".format(i),"temp/{0}.img".format(i),int(s[1]),True)
else:
msg("数据不合法。")
else:
packimg("target/{0}".format(i),"temp/{0}.img".format(i),sizetable[i],True)
progress(i,"正在执行 img2dat [2/3]".format(i),round(2/6*100))
img2dat("temp/{0}.img".format(i),"temp",i)
progress(i,"正在执行 img2dat [2/3]".format(i),round(3/6*100))
os.remove("temp/{0}.img".format(i))
progress(i,"正在执行 dat2br [3/3]".format(i),round(4/6*100))
dat2br("temp/{0}.new.dat".format(i),"temp/{0}.new.dat.br".format(i),bro_ext_arg)
progress(i,"正在执行 dat2br [3/3]".format(i),round(5/6*100))
os.remove("temp/{0}.new.dat".format(i))
progress(i,"完成".format(i),100)
progress("DNAnother","准备中",0)
progress("DNAnother","复制文件...",50)
os.system("cp -rn base/* temp")
progress("DNAnother","复制完成...",50)
progress("DNAnother","准备中",0)
progress("DNAnother","打包中",0)
os.chdir("temp")
os.system("zip ../update.zip -rq * "+zip_ext_arg)
os.chdir("..")
progress("DNAnother","打包完成",100)
infobox("DNAnother","正在清理临时文件...")
cleartemp()
msg("DNAnother","包已经生成为 update.zip。\n请查看项目根目录。")
elif ch=='4':
os.system("clear")
s_backup=os.getcwd()
os.chdir("target")
print("您现在正在 target 文件夹内。")
print("如果您需要操作指定分区,只需输入:cd [分区名]")
print("DNAnother 感谢您的使用。")
print("注意:您需要手动回到普通用户。")
print("以root账户操作ROM包可能导致权限错误。")
os.system("bash")
os.chdir(s_backup)
elif ch=='5':
while 1:
ch=choice([
"其他",
"这里列出了一些高级操作。\n不当使用将导致项目损坏。",
["1","清空 temp"],
["2","清空 target"],
["3","高级转换"],
["4","调整额外参数"],
["5","关于"],
["6","返回"],
])[1]
if ch=='' or ch=='6':
break
elif ch=='1':
infobox("请稍等","正在清空 temp")
cleartemp()
elif ch=='2':
if yesno("警告","此操作会删除最后一次打包后所做的任何更改。\n就算这样也要继续吗?")==CHOICE_OK:
infobox("请稍等","正在清空 target")
cleartarget()
elif ch=='3':
highconv()
elif ch=='4':
ch=choice([
"调整额外参数",
"请选择要设定的程序。",
["1","zip"],
["2","brotli"]
])
if ch[0]==CHOICE_OK:
if ch[1]=='1':
x=inputbox("调整额外参数","请输入zip的额外参数:",zip_ext_arg)
if x[0]==CHOICE_OK:
zip_ext_arg=x[1]
msg("调整额外参数","变更已保存。")
if ch[1]=='2':
x=inputbox("调整额外参数","请输入brotli的额外参数:",bro_ext_arg)
if x[0]==CHOICE_OK:
bro_ext_arg=x[1]
msg("调整额外参数","变更已保存。")
elif ch=='5':
if not os.path.exists("{0}/about.txt".format(sys.path[0])) or not os.path.isfile("{0}/about.txt".format(sys.path[0])):
msg("错误","找不到关于文档。")
else:
textbox("关于","{0}/about.txt".format(sys.path[0]))
else:
msg("错误","发生错误,请检查您的项目。")
exitmenu()
if os.geteuid() != 0:
msg("中止","请尝试用sudo再试一遍。")
exitmenu()
while 1:
project_path=dselect("打开项目","/")
if project_path[0]!=CHOICE_OK:
exitmenu()
elif os.path.exists(project_path[1]) and os.path.isdir(project_path[1]):
s_backup=os.getcwd()
os.chdir(project_path[1])
if os.path.exists("base"):
rom()
if len(os.listdir("."))!=0:
msg("错误","文件夹非空。")
os.chdir(s_backup)
continue
while 1:
target=fselect("打开ROM",os.getenv("HOME"))
if target[0]!=CHOICE_OK:
if yesno("不选择ROM","确定继续吗?大部分功能可能出错。")==CHOICE_OK:
progress("请稍等","建立项目文件...",0)
os.mkdir("temp")
os.mkdir("mount")
os.mkdir("target")
os.mkdir("base")
os.mkdir("conv")
progress("请稍等","处理完成",100)
try:
rom()
except Exception as f:
if yesno("发生错误","以下是错误的详细信息。\n\n"+traceback.format_exc()+"\n请参照说明的指示进行操作。","退出","再次启动")==CHOICE_OK:
exitmenu()
else:
rom()
else:
continue
elif os.path.exists(target[1]) and os.path.isfile(target[1]) and target[1].endswith(".zip"):
progress("请稍等","建立项目文件...",0)
os.mkdir("temp")
os.mkdir("mount")
os.mkdir("target")
os.mkdir("conv")
progress("请稍等","解压ROM文件...",50)
os.mkdir("base")
os.system("unzip -qq -d base \""+target[1].replace("\"","\\\"")+"\"")
progress("请稍等","处理完成",100)
try:
rom()
except Exception as f:
if yesno("发生错误","以下是错误的详细信息。\n\n"+traceback.format_exc()+"\n请参照说明的指示进行操作。","退出","再次启动")==CHOICE_OK:
exitmenu()
else:
rom()
else:
msg("错误","ROM包错误。")
else:
msg("错误","无法访问这个文件夹。")