forked from keysaim/genpatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtooler.py
More file actions
524 lines (447 loc) · 15.3 KB
/
Copy pathtooler.py
File metadata and controls
524 lines (447 loc) · 15.3 KB
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
import os
import shutil
import logging
import traceback
import re
from base import *
from patcher import *
class ToolFile( BaseObject ):
def __init__( self, src ):
super(ToolFile, self).__init__()
self.src = src
class FileIgnore( BaseObject ):
def __init__( self ):
super( FileIgnore, self ).__init__()
self.ignoreList = list()
def check_ignore( self, name ):
for ignore in self.ignoreList:
if ignore.match( name ):
logging.info( 'ignore file:'+name+' with pattern:'+str(ignore) )
return True
return False
def add_ignore( self, ignore ):
try:
ig = re.compile( ignore )
self.ignoreList.append( ig )
logging.info( 'add one ignore:'+ignore+', with re:'+str(ig) )
return True
except Exception, e:
logging.error( 'add ignore:'+ignore+' failed in:'+str(self) )
traceback.print_exc()
logging.error( str(e) )
return False
class ToolDir( BaseObject ):
def __init__( self, srcDir ):
super( ToolDir, self ).__init__()
self.srcDir = srcDir
self.fileList = None
self.ignore = None
def init_config( self, parent ):
if not self.srcDir:
logging.error( 'srcDir none error' )
return False
if not os.path.isdir(self.srcDir):
logging.error( 'is not a directory:'+str(self) )
return False
fileList = self.__get_file_list()
if not fileList:
logging.error( 'no file in srcDir:'+self.srcDir )
return False
self.fileList = fileList
return True
def __get_file_list( self ):
fileList = list()
self.__walk_file_list( fileList, self.srcDir, '' )
return fileList
def __walk_file_list( self, fileList, curDir, relDir ):
for fname in os.listdir(curDir):
relPath = os.path.join( relDir, fname )
fpath = os.path.join( curDir, fname )
if self.ignore:
if self.ignore.check_ignore( fname ):
continue
if not os.path.isdir( fpath ):
logging.debug( 'add src file:'+fpath+', curDir:'+curDir+', relDir:'+relDir+' ' )
fileList.append( relPath )
else:
self.__walk_file_list( fileList, fpath, relPath )
class ToolFileBlock( BaseObject ):
def __init__( self ):
super(ToolFileBlock, self).__init__()
self.fileList = list()
self.ignore = None
def add_file( self, tfile ):
self.fileList.append( tfile )
def init_config( self, parent ):
if not self.fileList:
return False
newList = list()
for tfile in self.fileList:
flist = self.__init_file( tfile )
if flist:
newList += flist
self.fileList = newList
if not newList:
return False
return True
def __init_file( self, tfile ):
src = tfile.src
if os.path.exists(src):
if os.path.isfile(src):
if self.ignore:
if self.ignore.check_ignore(src):
return None
return [tfile]
flist = list()
for fname in os.listdir(src):
if self.ignore:
if self.ignore.check_ignore(fname):
continue
fpath = os.path.join(src, fname)
nfile = ToolFile( fpath )
flist.append( nfile )
return flist
return None
class Tooler( BaseObject ):
def __init__( self ):
self.toolsDir = '/sw/tools'
self.name = ''
self.customer = ''
self.version = ''
self.bugs = ''
self.timeTag = None
self.outDir = './'
self.packageDir = None
self.packageDirName = None
self.deviceList = None
self.script = None
self.fileBlock = None
self.srcDir = None
def init_config( self ):
if not self.name:
logging.error( 'no valid tool name defined' )
return False
if not self.script:
logging.error( 'no valid script parsed in tool:'+str(self) )
return False
if not self.script or not os.path.isfile(self.script.src):
logging.error( 'no valid script defined in tool:'+str(self) )
return False
if self.deviceList:
self.deviceList = format_device_name( self.deviceList )
if not self.deviceList:
logging.error( 'no valid device name defined in tool:'+str(self) )
return False
if self.srcDir:
if not self.srcDir.init_config( self ):
logging.error( 'init srcDir failed:'+str(self.srcDir) )
return False
if self.fileBlock:
logging.warn( '!!!!!!!srcDir configured, fileBlock will be skipped!!!!!!!!' )
if self.fileBlock:
if not self.fileBlock.init_config( self ):
logging.error( 'init fileBlock failed:'+str(self.fileBlock) )
return False
return True
def generate( self ):
logging.info( 'generate tool obj:'+str(self) )
logging.info( '\nbegin to generate tool...\n\n' )
self.__prepare_package()
self.__gen_main_script()
def __prepare_package( self ):
if not self.timeTag:
now = datetime.now()
self.timeTag = str_time( now, '%Y%m%d' )
mkdir( self.outDir )
if not self.packageDir:
self.packageDirName = 'tool_' + self.name + '_' + self.version + '_' + self.timeTag
self.packageDir = os.path.join( self.outDir, self.packageDirName )
logging.debug( 'make directory:'+self.packageDir )
if not mkdir(self.packageDir):
logging.fatal( 'create package directory failed:'+str(self) )
return False
self.__copy_script( self.packageDir )
self.__copy_tool_files( self.packageDir )
def __gen_main_script( self ):
scriptName = 'cdsis_' + self.version + '_' + self.name + '_' + self.timeTag + '.sh.sign'
spath = os.path.join( self.outDir, scriptName )
fout = open( spath, 'w' )
self.__gen_head( fout )
fout.write( '\n' )
gen_check_version_func( fout )
fout.write( '\n' )
gen_check_device_mode_func( fout, '' )
fout.write( '\n' )
self.__gen_uncompress_func( fout )
fout.write( '\n' )
self.__gen_check_installed_func( fout )
fout.write( '\n' )
self.__gen_validate_func( fout )
fout.write( '\n' )
self.__gen_install_func( fout )
fout.write( '\n' )
self.__gen_uninstall_func( fout )
fout.write( '\n' )
self.__gen_run_func( fout )
fout.write( '\n' )
self.__gen_verify_func( fout )
fout.write( '\n' )
script = self.__get_path_in_tool( os.path.basename(self.script.src) )
lines = 'case "$1" in\n'
lines += ' install)\n'
lines += ' tool_install\n'
lines += ' ;;\n'
lines += ' uninstall)\n'
lines += ' tool_uninstall\n'
lines += ' ;;\n'
lines += ' verify)\n'
lines += ' tool_verify\n'
lines += ' ;;\n'
lines += ' run)\n'
lines += ' shift\n'
lines += ' arguments="$@"\n'
lines += ' tool_run\n'
lines += ' cd ' + self.__get_tool_dir() + ';sh ' + script + ' "$@"\n'
lines += ' rc=$?\n'
lines += ' if [ $rc -eq 1 ]; then\n'
lines += ' echo "invalid arguments for run command"\n'
lines += ' fi\n'
lines += ' exit $rc\n'
lines += ' ;;\n'
lines += ' *)\n'
lines += ' echo $"Usage: $0 {install|uninstall|verify|run <argvs>}"\n'
lines += 'esac\n'
fout.write( lines )
fout.close()
cmd = 'cd ' + self.outDir
cmd += ';tar czf ' + self.packageDirName + '.tar.gz ' + self.packageDirName
logging.debug( 'run cmd:'+cmd )
os.system( cmd )
cmd = '/ruby/bin/exec_script emit-checksum ' + spath + ' >> ' + spath
logging.debug( 'run cmd:'+cmd )
os.system( cmd )
def __gen_head( self, fout ):
lines = '#!/bin/sh\n'
lines += '# tool on ' + self.version + ' for ' + self.customer + '\n'
lines += '# CDETS: ' + self.bugs + '\n'
lines += '\n'
lines += 'export customer="' + self.customer + '"\n'
lines += 'export target_version="' + self.version + '" # major(.)minor(.)maintenance(b)build\n'
lines += 'export version_tag="vds-is $target_version(For $customer)"\n'
lines += 'export time_tag="' + self.timeTag + '"\n'
lines += '\n'
lines += 'package_name="' + self.packageDirName + '"\n'
lines += 'package_file="./$package_name.tar.gz"\n\n'
lines += 'arguments="$@"\n'
idx = 1
relSrc = os.path.basename( self.script.src )
lines += self.__get_gen_md5sum_code( self.script.src, relSrc, idx )
if self.srcDir:
for src in self.srcDir.fileList:
idx += 1
srcPath = os.path.join( self.srcDir.srcDir, src )
lines += self.__get_gen_md5sum_code( srcPath, src, idx )
elif self.fileBlock:
for tfile in self.fileBlock.fileList:
idx += 1
relSrc = os.path.basename( tfile.src )
lines += self.__get_gen_md5sum_code( tfile.src, relSrc, idx )
lines += 'total_scripts=' + str(idx) + '\n'
lines += '\n\n'
fout.write( lines )
def __gen_uncompress_func( self, fout ):
lines = 'function package_uncompress() {\n'
lines += ' \n'
lines += ' if [ ! -f $package_file ]; then\n'
lines += ' echo "ERROR: patch package file $package_file does not exist, aborting, please check"\n'
lines += ' exit 11\n'
lines += ' fi\n'
lines += ' \n'
lines += ' tar xzf $package_file\n'
lines += ' if [ $? -ne 0 ]; then\n'
lines += ' echo "ERROR: patch package file $package_file is corrupted, aborting, please check"\n'
lines += ' exit 12\n'
lines += ' fi\n'
lines += '\n'
lines += '}\n'
fout.write( lines )
def __gen_validate_func( self, fout ):
lines = 'function scripts_validate() {\n'
lines += ' \n'
lines += ' for (( n=1; n<=$total_scripts; n++ ))\n'
lines += ' do\n'
lines += ' script_file_name=script_file$n\n'
lines += ' expected_script_md5=script_md5sum$n\n'
lines += ' \n'
lines += ' script_file_name=${!script_file_name}\n'
lines += ' expected_script_md5=${!expected_script_md5}\n'
lines += ' \n'
lines += ' actual_script_md5=$(md5sum $script_file_name | awk \'{ print $1 }\')\n'
lines += ' \n'
lines += ' if [[ "$expected_script_md5" != "$actual_script_md5" ]]; then\n'
lines += ' echo "Patch files do not exist or scripts have been modified, aborting."\n'
lines += ' exit 41\n'
lines += ' fi\n'
lines += ' done\n'
lines += '}\n'
fout.write( lines )
def __gen_check_installed_func( self, fout ):
toolDir = self.__get_tool_dir()
lines = 'function check_tool_installed() {\n'
lines += ' local tool_dir="' + toolDir + '"\n'
lines += ' if [ -d "$tool_dir" ]; then\n'
lines += ' return 1\n'
lines += ' else\n'
lines += ' return 0\n'
lines += ' fi\n'
lines += '}\n'
fout.write( lines )
def __gen_install_func( self, fout ):
lines = 'function tool_install() {\n'
lines += ' echo "############################################################"\n'
lines += ' echo "## tool install for $version_tag"\n'
lines += ' echo "############################################################"\n'
lines += ' \n'
lines += ' check_version $target_version\n'
lines += ' if [ $? -ne 0 ]; then\n'
lines += ' echo "cannot install for this version"\n'
lines += ' exit 23\n'
lines += ' fi\n'
fout.write( lines )
if self.deviceList:
gen_check_device_mode( fout, '\t', self.deviceList )
lines = ' check_tool_installed\n'
lines += ' if [ $? -eq 1 ]; then\n'
lines += ' echo "tool is already installed, please uninstall it before installing again!"\n'
lines += ' exit 22\n'
lines += ' fi\n'
lines += ' echo "Uncompressing package file..."\n'
lines += ' package_uncompress\n'
lines += ' echo "Package file uncompressed."\n'
lines += ' scripts_validate\n'
lines += ' \n'
fout.write( lines )
lines = ' echo "installing tool ..."\n'
fout.write( lines )
gen_mount_sw( fout, '\t', 'rw', 5 )
lines = ' if [ ! -d ' + self.toolsDir + ' ]; then\n'
lines += ' mkdir ' + self.toolsDir + '\n'
lines += ' fi\n'
lines += ' mv -f ' + self.packageDirName + ' ' + self.toolsDir + '/\n'
lines += ' local ret=$?\n'
fout.write( lines )
gen_mount_sw( fout, '\t', 'ro', 5 )
lines = ' echo "" \n'
lines += ' if [[ $ret -eq 0 ]]; then\n'
lines += ' echo "tool install succeeded!"\n'
lines += ' else\n'
lines += ' echo "tool install failed, please try again!"\n'
lines += ' fi \n'
lines += '}\n'
fout.write( lines )
def __get_tool_dir( self ):
tpath = os.path.join( self.toolsDir, self.packageDirName )
return tpath
def __gen_uninstall_func( self, fout ):
script = self.__get_path_in_tool( os.path.basename(self.script.src) )
lines = 'function tool_uninstall() {\n'
lines += ' echo "############################################################"\n'
lines += ' echo "## tool uninstall for $version_tag"\n'
lines += ' echo "############################################################"\n'
lines += ' \n'
lines += ' check_tool_installed\n'
lines += ' if [ $? -ne 1 ]; then\n'
lines += ' echo "the tool is not installed, please install it first!"\n'
lines += ' exit 22\n'
lines += ' fi\n'
fout.write( lines )
lines = ' cd ' + self.__get_tool_dir() + ';sh ' + script + ' uninstall\n'
lines += ' if [ $? -eq 0 ]; then\n'
lines += ' echo ""\n'
lines += ' else\n'
lines += ' echo "canot uninstall the tool!"\n'
lines += ' return\n'
lines += ' fi\n'
fout.write( lines )
lines = ' echo "uninstalling tool ..."\n'
fout.write( lines )
gen_mount_sw( fout, '\t', 'rw', 5 )
lines = ' rm -rf ' + self.__get_tool_dir() + '\n'
lines += ' local ret=$?\n'
fout.write( lines )
gen_mount_sw( fout, '\t', 'ro', 5 )
lines = ' if [[ $ret -eq 0 ]]; then\n'
lines += ' echo "tool uninstall succeeded!"\n'
lines += ' else\n'
lines += ' echo "tool uninstall failed, please try again!"\n'
lines += ' fi \n'
lines += '}\n'
fout.write( lines )
def __gen_run_func( self, fout ):
script = self.__get_path_in_tool( os.path.basename(self.script.src) )
lines = 'function tool_run() {\n'
lines += ' echo "############################################################"\n'
lines += ' echo "## tool run for $version_tag"\n'
lines += ' echo "############################################################"\n'
lines += ' \n'
lines += ' check_tool_installed\n'
lines += ' if [ $? -ne 1 ]; then\n'
lines += ' echo "the tool is not installed, please install it first!"\n'
lines += ' exit 22\n'
lines += ' fi\n'
lines += '}\n'
fout.write( lines )
def __gen_verify_func( self, fout ):
lines = 'function tool_verify() {\n'
lines += ' echo "############################################################"\n'
lines += ' echo "## tool verify for $version_tag"\n'
lines += ' echo "############################################################"\n'
lines += ' \n'
lines += ' check_tool_installed\n'
lines += ' if [ $? -ne 1 ]; then\n'
lines += ' echo "the tool is not installed!"\n'
lines += ' else\n'
lines += ' echo "the tool is installed!"\n'
lines += ' fi\n'
lines += '}\n'
fout.write( lines )
def __get_path_in_tool( self, name ):
opath = os.path.join( self.__get_tool_dir(), name )
return opath
def __get_gen_md5sum_code( self, src, relSrc, idx ):
opath = os.path.join( self.packageDirName, relSrc )
md5sum = gen_md5sum_value( src )
lines = 'script_file' + str(idx) + '="' + opath + '"\n'
lines += 'script_md5sum' + str(idx) + '="' + md5sum + '"\n'
return lines
def __copy_script( self, dstDir ):
try:
shutil.copy( self.script.src, dstDir )
self.script.src = os.path.join( dstDir, os.path.basename(self.script.src) )
return True
except:
traceback.print_exc()
logging.error( 'copy script failed in:'+str(self) )
return False
def __copy_tool_files( self, dstDir ):
try:
fileList = None
if self.srcDir:
fileList = self.srcDir.fileList
for fname in fileList:
fsrc = os.path.join( self.srcDir.srcDir, fname )
fdst = os.path.join( dstDir, fname )
parent = os.path.dirname( fdst )
mkdir( parent )
shutil.copy( fsrc, fdst )
elif self.fileBlock:
fileList = self.fileBlock.fileList
for tfile in fileList:
shutil.copy( tfile.src, dstDir )
tfile.src = os.path.join( dstDir, os.path.basename(tfile.src) )
return True
except Exception, e:
logging.error( 'copy tool files failed in:'+str(self) )
traceback.print_exc()
logging.error( str(e) )
return False