-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkitchen_utils.py
548 lines (440 loc) · 19.6 KB
/
kitchen_utils.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
#!/usr/bin/python
#
#
# FreakTabKitchen www.freaktab.com
#
# Copyright 2013 Brian Mahoney [email protected]
#
# <version>2.0.1</version>
#
############################################################################
#
# FreakTabKitchen is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FreakTabKitchen is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FreakTabKitchen. If not, see <http://www.gnu.org/licenses/>.
#
############################################################################
#
# 13/1/14 extend mvcpfilesworker for wildcards
# 5/4/14 correct mvcpfilesworker for wildcards
############################################################################
import os
import logging
import shutil
import glob
import time
import struct
import zipfile
import zlib
#900supersport imports
import KitchenConfig
import rominfo
from rkcrc import rkcrc as crc
from kitchenUI import header, pprint
from utils import CheckMakeFolders, finalisefilesystemimage, mountfileasfilesystem, CheckMakeFoldersRoot, apply_sed, checkfsimage, logerror
from parameter import parse_parameter
# try:
#
# except Exception as e:
# logerror('kitchen_utils::mountsystem ',e,1)
# raise
def rkcrc(flag, infile, outfile):
try:
kc = KitchenConfig.KitchenConfig
if kc.usepycrc == 0:
r = os.system('rkcrc ' + flag + ' ' + infile + ' ' + outfile)
if r > 256:
kc.usepycrc = 1
crc(flag, infile, outfile)
else:
crc(flag, infile, outfile)
except Exception as e:
logerror('kitchen_utils::mountsystem ',e,1)
raise
def query_add(path, check, addition):
'''if check not found in path, append addition
'''
try:
kRar=os.path.join(KitchenConfig.KitchenConfig.KitchenPath, 'kitchenRunasroot.py')
addition ="'" + addition + "'"
check = "'" + check + "'"
#call out to some python as root :-)
os.system('sudo python ' + kRar + ' query_add ' + path + ' ' + check + ' ' + addition)
except Exception as e:
logerror('kitchen_utils::query_add ',e,1)
def query_add_by_file(filepath,deployfilename):
'''iterate through deployfile adding lines to filepath for any cases where the key is not present
'''
try:
logging.info('kitchen_utils::query_add_by_File filepath ' + filepath)
logging.info('kitchen_utils::query_add_by_File deployfilename ' + deployfilename)
kRar=os.path.join(KitchenConfig.KitchenConfig.KitchenPath, 'kitchenRunasroot.py')
path = os.path.expanduser(deployfilename)
with open(path,'r') as f:
for line in f:
cl = line.strip();
if cl[:1] <> '#' and len(cl) > 0:
args = cl.split(',')
p=cl.find(",")
check = cl[:p]
addition ="'" + cl[p+1:] + "'"
#call out to some python as root :-)
os.system('sudo python ' + kRar + ' query_add ' + filepath + ' "' + check + '" "' + addition + '"')
except Exception as e:
logerror('kitchen_utils::query_add_by_File ',e,1)
def custom_remove(targetpath):
'''Custom remove (ROM specific)'''
try:
pprint('=')
pprint('ROM specific file removal: ' + targetpath)
pprint('=')
rfile = raw_input('Enter the path of your ROM specific removal file').strip().strip("'").strip('"')
removefiles(rfile,targetpath);
except Exception as e:
logerror('kitchen_utils::custom_remove ',e,1)
def custom_deploy(deploypath):
'''Custom deploy (ROM specific)'''
try:
pprint('=')
pprint('ROM specific file deploy: ' + deploypath)
pprint('=')
dfile = raw_input('Enter the path of your ROM specific deploy file: ').strip().strip("'").strip('"')
deployfiles(dfile,deploypath,1)
except Exception as e:
logerror('kitchen_utils::custom_deploy ',e,1)
def copyfilesworker(movefilename,sourceroot,asroot,verbose=0):
mvcpfilesworker(movefilename,sourceroot,'cp',asroot,verbose)
def movefilesworker(movefilename,sourceroot,asroot = 1,verbose=0 ):
mvcpfilesworker(movefilename,sourceroot,'mv',asroot,verbose)
def mvcpfilesworker(movefilename,sourceroot,op,asroot,verbose=0):
try:
serr=''
path = os.path.expanduser(movefilename)
with open(path,'r') as f:
for line in f:
cl = line.strip();
if cl[:1] <> '#' and len(cl) > 0:
args = cl.split(',')
source = os.path.join(sourceroot, args[0].strip())
dest = os.path.join(sourceroot, args[1].strip())
if len(args) > 2:
destpath = args[2].strip()
else:
destpath = ''
pth,fn =os.path.split(source)
fn= os.path.join(destpath,fn)
logging.debug('kitchen_utils::mvcpfilesworker source' + source)
logging.debug('kitchen_utils::mvcpfilesworker dest' + dest)
iszip = 0
lendest = len(dest)
if lendest > 3:
if dest[lendest-3:] == 'zip':
iszip = 1
logging.debug('kitchen_utils::mvcpfilesworker iszip' + str(iszip))
try:
#if os.path.exists(source):
if len(glob.glob(source)) > 0:
if iszip == 1:
logging.debug('kitchen_utils::mvcpfilesworker ' + dest)
zf = zipfile.ZipFile(file=dest,mode='a',compression=zipfile.ZIP_DEFLATED)
for file in glob.glob(source):
#now set fn based on the glob'd filename and the destination
pth,fn =os.path.split(file)
fn= os.path.join(destpath,fn)
logging.debug('kitchen_utils::mvcpfilesworker Zipping {} size {:,} kb'.format(file,os.stat(file).st_size/1024))
#zf.printdir()
for file in glob.glob(source):
if verbose ==1:
pprint( 'Zipping {} size {:,} kb'.format(file,os.stat(file).st_size/1024))
zf.write(file,fn)
zf.close()
else:
if verbose ==1:
pprint( 'Processing {} size {:,} kb'.format(source,os.stat(source).st_size/1024))
logging.info('kitchen_utils::mvcpfilesworker ')
logging.info([dest])
CheckMakeFoldersRoot([dest])
moves = op + ' ' + source + ' ' + dest
if asroot==1:
moves = 'sudo ' + moves
logging.debug('kitchen_utils::mvcpfilesworker moves :' + moves)
os.system(moves)
else:
logging.debug('kitchen_utils::mvcpfilesworker source does not exist Source= ' + source)
except IOError as e:
logerror('kitchen_utils::mvcpfilesworker inner ',e,1)
serr = serr + 'could not move ' + sourceroot.strip() + args[0].strip() + '\n'
if serr <> '':
print serr
choice=raw_input('Press enter to continue')
except Exception as e:
logerror('kitchen_utils::mvcpfilesworker ',e,1)
def movefiles(movefilename):
'''move files
sample
G, Superuser.apk, app/, 644
'''
try:
movefilesworker(movefilename, 'working/mntsystem/')
path = os.path.expanduser(movefilename)
except Exception as e:
logerror('kitchen_utils::movefiles ',e,1)
def deployfiles(deployfilename,deploydest,openforreview):
'''deploy files
sample
G, Superuser.apk, app/, 644
'''
try:
serr=''
deploydest = deploydest.strip()
path = os.path.expanduser(deployfilename)
globalroot = os.path.join(KitchenConfig.KitchenConfig.KitchenPath,'deployfiles')
localroot = 'localdeploy/'
with open(path,'r') as f:
for line in f:
cl = line.strip();
if cl[:1] <> '#' and len(cl) > 0:
args = cl.split(',')
sourceroot = ''
if args[0] == 'G':
sourceroot = globalroot
elif args[0] == 'L':
sourceroot = localroot
try:
tgt = os.path.join(sourceroot, args[1].strip())
logging.info('kitchen_utils::deployfiles attempt deploy ' + tgt)
#if os.path.exists(tgt):
if len(glob.glob(tgt)) > 0:
if tgt.find('*') > 0:
flags = '-r '
else:
flags = ''
dest = os.path.join(deploydest, args[2].strip())
CheckMakeFoldersRoot([dest])
copys = 'sudo cp ' + flags + tgt + ' ' + dest
chmods = 'sudo chmod ' + args[3][3:] + ' ' + os.path.join(deploydest, args[2].strip(), args[1].strip())
logging.debug('copy :' + copys)
logging.debug('chmod :' + chmods)
os.system(copys)
os.system(chmods)
except IOError as e:
logerror(e)
serr = serr + 'could not deploy ' + sourceroot.strip() + args[1].strip() + '\n'
if serr <> '':
print serr
choice=raw_input('Press enter to continue')
if openforreview ==1:
os.system('sudo nautilus ' + deploydest)
except Exception as e:
logerror('kitchen_utils::deployfiles ',e,1)
def customremove(targetpath):
'''Custom remove (ROM specific)'''
try:
pprint('=')
pprint('ROM specific file removal: ' + targetpath)
pprint('=')
rfile = raw_input('Enter the path of your ROM specific removal file').strip().strip("'").strip('"')
removefiles(rfile,targetpath)
except Exception as e:
logerror('kitchen_utils::customremove ',e,1)
def removefiles(removefile,root):
'''remove files from the location
iterate removefile removing files from root
moving them to the location specified in the file
app/cube.0.3.0_v2963.apk, removed/app
'''
logging.debug('remove files start (removefile:root)(' + removefile + ':' + root)
path = os.path.expanduser(removefile)
with open(path,'r') as f:
for line in f:
cl = line.strip();
args = cl.split(',')
if cl[:1] <> '#' and len(cl) > 0:
try:
print 'Attempt Move ' + cl
source = os.path.join(root, args[0].strip())
dest = os.path.join('working/', args[1].strip())
#if os.path.exists(source):
if len(glob.glob(source)) > 0:
logging.debug('copy ' + source + ' ' + dest )
logging.debug('Remove ' + source)
CheckMakeFolders([dest])
os.system('sudo cp ' + source + ' ' + dest)
#shutil.copy(source, dest)
os.system('sudo rm ' + root + args[0].strip())
else:
logging.debug(source + ' does not exist')
except IOError as e:
#print 'error ' + root + args[0].strip()
logerror('kitchen_utils::removefiles ' ,e,0)
logging.debug('remove files END')
logging.debug('================')
def browse(path):
'''launch natilus and point at working'''
try:
kc = KitchenConfig.KitchenConfig
os.system('sudo ' + kc.browser + ' ' + path)
except Exception as e:
logerror('kitchen_utils::browse ',e,1)
def copyImages():
'''copy the unpacked image files to the working folder
'''
try:
pprint('Copy image and parameter files')
#and copy to working, no wilcard support so use glob
for file in glob.glob(os.path.join(KitchenConfig.KitchenConfig.SourceROMUnpackedLoc(),'Image/*.img')):
print file
shutil.copy(file,'working')
shutil.copy(
os.path.join(KitchenConfig.KitchenConfig.SourceROMUnpackedLoc(),'parameter')
,'working')
except Exception as e:
logerror('kitchen_utils::copyImages ',e,1)
def saveromdata():
'''save the rom data to the rominfo
'''
try:
parse_parameter()
except Exception as e:
logerror('kitchen_utils::saveromdata ',e,1)
def finalise_boot_recovery(image):
'''pack up boot.img or recovery'''
try:
loc = image.find('.')
folder = image[:loc]
pprint('=')
pprint('Finalising ' + image)
print 'folder= ' + folder
pprint('=')
#check_make_folder('working/' + folder)
touch_and_zip_boot_recovery(folder,image)
print 'os.rename ' ,
print os.path.join('working', image),os.path.join('working', image + '.old')
os.rename(os.path.join('working', image),os.path.join('working', image + '.old'))
print 'renamed'
#logging.debug('rkcrc -k working/custom' + image + '.gz ' + image)
#os.system('rkcrc -k working/custom' + image + '.gz working/' + image)
rkcrc('-k', 'working/custom' + image + '.gz', 'working/' + image)
pprint('=')
pprint(image + ' finalised')
pprint('=')
except Exception as e:
logerror('kitchen_utils::finalise_boot_recovery ',e,1)
def touch_and_zip_boot_recovery(folder,image):
'''touch all files in the image setting the data to 1-1-1970
'''
try:
os.chdir(os.path.join('working', folder))
os.system('sudo find . -exec touch -d "1970-01-01 01:00" {} \;')
os.system('sudo find . ! -name "." | sort | sudo cpio -oa -H newc | sudo gzip -n >../custom' + image + '.gz')
os.chdir('../..')
except Exception as e:
logerror('kitchen_utils::touch_and_zip_boot_recovery ',e,1)
def unpackboot_recovery(image):
'''unpack a boot.img or recovery.img'''
try:
loc = image.find('.')
folder = image[:loc]
filepath = os.path.join('working/', image)
with open(filepath,'rb') as f:
disc = f.read(4)
if disc == 'KRNL':
logging.info('kitchen_utils::unpack_recovery boot.img signed KRNL')
os.system('dd if=working/' + image + ' of=working/' + image + '-ramdisk.gz'
+ ' skip=8 bs=1 count=20000000')
else:
unpackBootRecoveryfile(filepath)
logging.debug('kitchen_utils::unpack_recovery :')
imagefolder = os.path.join('working', folder)
CheckMakeFolders([imagefolder])
os.chdir(imagefolder)
#protect against this being first sudo request
os.system('sudo ls')
os.system('sudo gunzip < ../' + image + '-ramdisk.gz '
+ '| sudo cpio -i --make-directories')
os.chdir('../..')
except Exception as e:
logerror('kitchen_utils::unpackboot_recovery ',e,1)
def unpackBootRecoveryfile(thefile):
try:
go_on = 0
with open(thefile,'r') as f:
check = f.read(8)
if check == 'ANDROID!':
go_on=1
kernel = makeimage('Kernel',f)
ramdisk = makeimage('Ramdisk',f)
sramdisk = makeimage('Second Ramdisk',f)
baseaddress = getint(f.read(4))
pagesize = getint(f.read(4))
kernel.setpagesize(pagesize)
kernel.Offset = pagesize
ramdisk.Offset = kernel.NextPageStart()
sramdisk.Offset = ramdisk.NextPageStart()
if go_on ==1:
kernel.writeimage(thefile,thefile + '-kernel')
ramdisk.writeimage(thefile,thefile + '-ramdisk.gz')
if sramdisk.Size != 0:
sramdisk.writeimage(thefile,thefile + '-sramdisk.gz')
except Exception as e:
logerror('kitchen_utils::unpackBootRecoveryfile ',e,1)
def makeimage(iname,f):
try:
s = getint(f.read(4))
a = getint(f.read(4))
return part(iname,s,a)
except Exception as e:
logerror('kitchen_utils::makeimage ',e,1)
def getint(data):
return struct.unpack('i',data)[0]
class part:
_pagesize = 0
def __init__(self,name,size,offset):
try:
self.Name = name.strip()
self.Size = size
self.Offset = offset
part._pagesize = 0
except Exception as e:
logerror('kitchen_utils::part::__init__ ',e,1)
def NextPageStart(self):
try:
return self.getpages() * part._pagesize + self.Offset
except Exception as e:
logerror('kitchen_utils::part::NextPageStart ',e,1)
def getpages(self):
try:
return int((self.Size + part._pagesize - 1) / part._pagesize)
except Exception as e:
logerror('kitchen_utils::part::getpages ',e,1)
def setpagesize(self,pgsize):
try:
part._pagesize = pgsize
except Exception as e:
logerror('kitchen_utils::part::setpagesize ',e,1)
def writeimage(self,sourcefile,imagename):
try:
with open(sourcefile,'r') as s:
print 'write file starting at ' + str(self.Offset)
print 'size ' + str(self.Size)
discard = s.read(self.Offset)
buff = s.read(self.Size)
print len(buff)
with open(imagename,'w') as w:
w.write(buff)
except Exception as e:
logerror('kitchen_utils::part::writeimage ',e,1)
def __str__(self):
try:
return self.Name + ': ' + str(self.Size) + ' ' + hex(self.Size) + ' ' + str(self.Offset)
except Exception as e:
logerror('kitchen_utils::part::__str__ ',e,1)