kmisc 2.1.87__py3-none-any.whl → 2.1.90__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kmisc/__init__.py +1273 -1872
- {kmisc-2.1.87.dist-info → kmisc-2.1.90.dist-info}/METADATA +1 -1
- kmisc-2.1.90.dist-info/RECORD +6 -0
- kmisc-2.1.87.dist-info/RECORD +0 -6
- {kmisc-2.1.87.dist-info → kmisc-2.1.90.dist-info}/LICENSE +0 -0
- {kmisc-2.1.87.dist-info → kmisc-2.1.90.dist-info}/WHEEL +0 -0
- {kmisc-2.1.87.dist-info → kmisc-2.1.90.dist-info}/top_level.txt +0 -0
kmisc/__init__.py
CHANGED
@@ -14,40 +14,38 @@ import zlib
|
|
14
14
|
import copy
|
15
15
|
import json
|
16
16
|
import random
|
17
|
-
import struct
|
18
17
|
import string
|
19
18
|
import pickle
|
20
|
-
import random
|
21
19
|
import base64
|
22
20
|
import hashlib
|
23
21
|
import fnmatch
|
24
22
|
import smtplib
|
25
|
-
import tarfile
|
26
23
|
import zipfile
|
27
24
|
import tarfile
|
28
|
-
import zipfile
|
29
|
-
import inspect
|
30
|
-
import traceback
|
31
|
-
import importlib
|
32
|
-
import subprocess
|
33
25
|
import email.utils
|
34
|
-
import kmport as kp
|
35
|
-
from kmport import *
|
36
26
|
from sys import modules
|
37
27
|
from pprint import pprint
|
38
28
|
import fcntl,socket,struct
|
39
29
|
from email import encoders
|
40
|
-
from sys import version_info
|
41
30
|
from threading import Thread
|
42
31
|
from datetime import datetime
|
43
32
|
from http.cookies import Morsel # This module for requests when you use build by pyinstaller command
|
44
|
-
from sys import path as mod_path
|
45
33
|
import xml.etree.ElementTree as ET
|
46
34
|
from email.mime.text import MIMEText
|
47
35
|
from email.mime.base import MIMEBase
|
48
|
-
from distutils.version import LooseVersion
|
49
36
|
from multiprocessing import Process, Queue
|
50
37
|
from email.mime.multipart import MIMEMultipart
|
38
|
+
try:
|
39
|
+
from kmport import *
|
40
|
+
except:
|
41
|
+
pip_user_install='--user' if os.environ.get('VIRTUAL_ENV') is None else ''
|
42
|
+
os.system('''for i in 1 2 3 4 5; do python3 -m pip install pip --upgrade {} 2>&1 | grep "however version" |grep "is available" >& /dev/null || break; done'''.format(pip_user_install))
|
43
|
+
os.system('python3 -m pip install kmport {}'.format(pip_user_install))
|
44
|
+
try:
|
45
|
+
from kmport import *
|
46
|
+
except:
|
47
|
+
print('Can not install kmport')
|
48
|
+
os._exit(1)
|
51
49
|
|
52
50
|
|
53
51
|
global krc_define
|
@@ -220,52 +218,6 @@ def Delete(*inps,**opts):
|
|
220
218
|
return rt
|
221
219
|
return default
|
222
220
|
|
223
|
-
class COLOR:
|
224
|
-
def __init__(self,**opts):
|
225
|
-
self.color_db=opts.get('color',{'blue': 34, 'grey': 30, 'yellow': 33, 'green': 32, 'cyan': 36, 'magenta': 35, 'white': 37, 'red': 31})
|
226
|
-
self.bg_color_db=opts.get('bg',{'cyan': 46, 'white': 47, 'grey': 40, 'yellow': 43, 'blue': 44, 'magenta': 45, 'red': 41, 'green': 42})
|
227
|
-
self.attr_db=opts.get('attr',{'reverse': 7, 'blink': 5,'concealed': 8, 'underline': 4, 'bold': 1})
|
228
|
-
|
229
|
-
def Color_code(self,name,default=None):
|
230
|
-
return self.color_db.get(name,default)
|
231
|
-
|
232
|
-
def Background_code(self,name,default=None):
|
233
|
-
return self.color_db.get(name,default)
|
234
|
-
|
235
|
-
def Attr_code(self,name,default=None):
|
236
|
-
return self.color_db.get(name,default)
|
237
|
-
|
238
|
-
def Get(self,color,mode='color',default=None):
|
239
|
-
color_code=None
|
240
|
-
if mode == 'color':
|
241
|
-
color_code=self.Color_code(color,default=default)
|
242
|
-
elif mode in ['background','bg']:
|
243
|
-
color_code=self.Background_code(color,default=default)
|
244
|
-
elif mode in ['attr','attribute']:
|
245
|
-
color_code=self.Attr_code(color,default=default)
|
246
|
-
return color_code
|
247
|
-
|
248
|
-
def String(self,msg,color,bg=False,attr=False,mode='shell'):
|
249
|
-
if mode in ['html','HTML']:
|
250
|
-
if bg:
|
251
|
-
return '''<p style="background-color: {}">{}</p>'''.format(format(color,msg))
|
252
|
-
else:
|
253
|
-
return '''<font color={}>{}</font>'''.format(color,msg)
|
254
|
-
else:
|
255
|
-
if bg:
|
256
|
-
color_code=self.Get(color,mode='bg',default=None)
|
257
|
-
elif attr:
|
258
|
-
color_code=self.Get(color,mode='attr',default=None)
|
259
|
-
else:
|
260
|
-
color_code=self.Get(color,default=None)
|
261
|
-
if IsNone(color_code):
|
262
|
-
return msg
|
263
|
-
if IsNone(os.getenv('ANSI_COLORS_DISABLED')):
|
264
|
-
reset='''\033[0m'''
|
265
|
-
fmt_msg='''\033[%dm%s'''
|
266
|
-
msg=fmt_msg % (color_code,msg)
|
267
|
-
return msg+reset
|
268
|
-
|
269
221
|
class DIFF:
|
270
222
|
def __init__(self):
|
271
223
|
pass
|
@@ -300,328 +252,6 @@ class DIFF:
|
|
300
252
|
def File(self):
|
301
253
|
pass
|
302
254
|
|
303
|
-
|
304
|
-
class LIST(list):
|
305
|
-
def __init__(self,*inps):
|
306
|
-
if len(inps) == 1 and isinstance(inps[0],(list,tuple)):
|
307
|
-
self.root=list(inps[0])
|
308
|
-
else:
|
309
|
-
self.root=list(inps)
|
310
|
-
|
311
|
-
# def __new__(cls,*inps):
|
312
|
-
# if len(inps) == 1 and isinstance(inps[0],(list,tuple)):
|
313
|
-
# return list(inps[0])
|
314
|
-
# else:
|
315
|
-
# return list(inps)
|
316
|
-
|
317
|
-
# reply self.root back to the Class's output a=List(['a']), return the data to a
|
318
|
-
def __repr__(self):
|
319
|
-
return repr(self.root)
|
320
|
-
|
321
|
-
def Convert(self,src,path=False,default=False,symbol=':white_space:',**opts):
|
322
|
-
if isinstance(src,str) and src:
|
323
|
-
if path and isinstance(symbol,str):
|
324
|
-
if symbol == ':white_space:':
|
325
|
-
symbol='/'
|
326
|
-
start=0
|
327
|
-
if src[0] == symbol:
|
328
|
-
start=1
|
329
|
-
if src[-1] == symbol:
|
330
|
-
return src.split(symbol)[start:-1]
|
331
|
-
return src.split(symbol)[start:]
|
332
|
-
else:
|
333
|
-
if symbol == ':white_space:':
|
334
|
-
return src.strip().split()
|
335
|
-
elif isinstance(symbol,str):
|
336
|
-
return src.split(symbol)
|
337
|
-
elif isinstance(symbol,(tuple,list)):
|
338
|
-
#regexPattern = '|'.join(map(re.escape,tuple(symbol)))
|
339
|
-
regexPattern = Join(map(re.escape,tuple(symbol)),symbol='|')
|
340
|
-
return re.split(regexPattern,src)
|
341
|
-
return default
|
342
|
-
elif isinstance(src,(list,tuple)):
|
343
|
-
return list(src)
|
344
|
-
else:
|
345
|
-
return [src]
|
346
|
-
|
347
|
-
def Append(self,*inps,**opts):
|
348
|
-
uniq=opts.get('uniq',False)
|
349
|
-
symbol=opts.get('symbol',':white_space:')
|
350
|
-
path=opts.get('path',False)
|
351
|
-
default=opts.get('default',False)
|
352
|
-
for pp in inps:
|
353
|
-
if Type(pp,('bytes','str')):
|
354
|
-
if symbol == ':white_space:':
|
355
|
-
pp=pp.strip()
|
356
|
-
symbol=' '
|
357
|
-
if path: symbol='/'
|
358
|
-
for rp in Split(pp,symbol,default=[]):
|
359
|
-
if rp == default: continue
|
360
|
-
if uniq and rp in self.root: continue
|
361
|
-
if path:
|
362
|
-
if rp == '.': continue
|
363
|
-
if rp == '..' and len(self.root):
|
364
|
-
del self.root[-1]
|
365
|
-
continue
|
366
|
-
self.root.append(rp)
|
367
|
-
else:
|
368
|
-
if uniq and pp in self.root: continue
|
369
|
-
self.root.append(pp)
|
370
|
-
return self.root
|
371
|
-
|
372
|
-
def append(self,inp):
|
373
|
-
self.root.append(inp)
|
374
|
-
|
375
|
-
def Uniq(self,*inps,**opts):
|
376
|
-
symbol=opts.get('symbol',':white_space:')
|
377
|
-
path=opts.get('path',False)
|
378
|
-
default=opts.get('default',False)
|
379
|
-
rt=[]
|
380
|
-
for pp in self.root + list(inps):
|
381
|
-
if Type(pp,('bytes','str')):
|
382
|
-
if symbol == ':white_space:':
|
383
|
-
pp=pp.strip()
|
384
|
-
symbol=' '
|
385
|
-
if path: symbol='/'
|
386
|
-
for rp in Split(pp,symbol,default=[]):
|
387
|
-
if rp not in rt and rp != default:
|
388
|
-
if path:
|
389
|
-
if rp == '.': continue
|
390
|
-
if rp == '..' and len(rt):
|
391
|
-
del rt[-1]
|
392
|
-
continue
|
393
|
-
rt.append(rp)
|
394
|
-
else:
|
395
|
-
if pp not in rt: rt.append(pp)
|
396
|
-
self.root=rt
|
397
|
-
return self.root
|
398
|
-
|
399
|
-
def Delete(self,*inps,**opts):
|
400
|
-
find=opts.get('find','index')
|
401
|
-
default=opts.get('default',False)
|
402
|
-
self.root=rm(self.root,*inps,data=True if find in ['data','element','value'] else False,default=default)
|
403
|
-
|
404
|
-
def Get(self,*inps,**opts):
|
405
|
-
if not inps: return self.root
|
406
|
-
find=opts.get('find','data')
|
407
|
-
default=opts.get('default',None)
|
408
|
-
out=opts.get('out',list)
|
409
|
-
err=opts.get('err',False)
|
410
|
-
if len(self.root) == 0 and err:
|
411
|
-
return default
|
412
|
-
rt=[]
|
413
|
-
if find in ['index','idx']:
|
414
|
-
rt=List(self.root,find=inps,mode='err' if err else 'auto',default=default)
|
415
|
-
else:
|
416
|
-
rt=List(self.root,idx=inps,mode='err' if err else 'auto',default=default)
|
417
|
-
if rt:
|
418
|
-
if out in [tuple,'tuple']:
|
419
|
-
return tuple(rt)
|
420
|
-
elif IsNone(out,chk_val=[None,'','raw']):
|
421
|
-
if len(rt) == 1: return rt[0]
|
422
|
-
return rt
|
423
|
-
return default
|
424
|
-
|
425
|
-
def Index(self,*inps):
|
426
|
-
return self.Get(*inps,find='index')
|
427
|
-
|
428
|
-
def Insert(self,*inps,**opts):
|
429
|
-
start=opts.get('at',0)
|
430
|
-
default=opts.get('default',False)
|
431
|
-
err=opts.get('err',False)
|
432
|
-
if isinstance(at,str):
|
433
|
-
if at in ['start','first']: self.root=list(inps)+self.root
|
434
|
-
if at in ['end','last']: self.root=self.root+list(inps)
|
435
|
-
elif len(self.root) == 0:
|
436
|
-
self.root=list(inps)
|
437
|
-
elif isinstance(start,int) and len(self.root) > start:
|
438
|
-
self.root=self.root[:start]+list(inps)+self.root[start:]
|
439
|
-
else:
|
440
|
-
if err:
|
441
|
-
return default
|
442
|
-
self.root=self.root+list(inps)
|
443
|
-
|
444
|
-
def Update(self,*inps,**opts):
|
445
|
-
at=opts.get('at',0)
|
446
|
-
err=opts.get('err',False)
|
447
|
-
default=opts.get('default',False)
|
448
|
-
n=len(self.root)
|
449
|
-
if n == 0:
|
450
|
-
if err is True:
|
451
|
-
return default
|
452
|
-
else:
|
453
|
-
self.root=list(inps)
|
454
|
-
elif isinstance(at,int) and n > at:
|
455
|
-
for i in range(0,len(inps)):
|
456
|
-
if n > at+i:
|
457
|
-
self.root[at+i]=inps[i]
|
458
|
-
elif err is True:
|
459
|
-
return default
|
460
|
-
else:
|
461
|
-
self.root=self.root+list(inps)[i:]
|
462
|
-
break
|
463
|
-
elif isinstance(at,(tuple,list)):
|
464
|
-
if len(inps) == len(at):
|
465
|
-
for i in range(0,len(at)):
|
466
|
-
if isinstance(at[i],int) and n > at[i]:
|
467
|
-
self.root[at[i]]=inps[i]
|
468
|
-
elif err is True:
|
469
|
-
return default
|
470
|
-
else:
|
471
|
-
self.root.append(inps[i])
|
472
|
-
|
473
|
-
def Find(self,*inps,**opts):
|
474
|
-
find=opts.get('find','index')
|
475
|
-
default=opts.get('default',[])
|
476
|
-
rt=[]
|
477
|
-
for i in range(0,len(self.root)):
|
478
|
-
for j in inps:
|
479
|
-
j=j.replace('*','.+').replace('?','.')
|
480
|
-
mm=re.compile(j)
|
481
|
-
if bool(re.match(mm,self.root[i])):
|
482
|
-
if find in ['index','idx']:
|
483
|
-
rt.append(i)
|
484
|
-
else:
|
485
|
-
rt.append(self.root[i])
|
486
|
-
if len(rt):
|
487
|
-
return rt
|
488
|
-
return default
|
489
|
-
|
490
|
-
def Copy(self):
|
491
|
-
return self.root[:]
|
492
|
-
def copy(self):
|
493
|
-
return self.root[:]
|
494
|
-
|
495
|
-
def Tuple(self):
|
496
|
-
return tuple(self.root)
|
497
|
-
|
498
|
-
def Move2first(self,find):
|
499
|
-
if isinstance(find,(list,tuple)):
|
500
|
-
self.Delete(*find,find='data')
|
501
|
-
self.root=list(find)+self.root
|
502
|
-
else:
|
503
|
-
self.Delete(*(find,),find='data')
|
504
|
-
self.root=[find]+self.root
|
505
|
-
return self.root
|
506
|
-
|
507
|
-
def Move2end(self,find):
|
508
|
-
if isinstance(find,(list,tuple)):
|
509
|
-
self.Delete(*find,find='data')
|
510
|
-
self.root=self.root+list(find)
|
511
|
-
else:
|
512
|
-
self.Delete(*(find,),find='data')
|
513
|
-
self.root=self.root+[find]
|
514
|
-
return self.root
|
515
|
-
|
516
|
-
def Sort(self,reverse=False,func=None,order=None,field=None):
|
517
|
-
self.root=Sort(self.root,revers=revers,func=func,order=order,field=field)
|
518
|
-
|
519
|
-
class STR(str):
|
520
|
-
def __init__(self,src,byte=None):
|
521
|
-
if isinstance(byte,bool):
|
522
|
-
if byte:
|
523
|
-
self.src=Bytes(src)
|
524
|
-
else:
|
525
|
-
self.src=Str(src)
|
526
|
-
else:
|
527
|
-
self.src=src
|
528
|
-
|
529
|
-
def Rand(self,length=8,strs=None,mode='*'):
|
530
|
-
return Random(length=length,strs=strs,mode=mode)
|
531
|
-
|
532
|
-
def Cut(self,head_len=None,body_len=None,new_line='\n',out=str):
|
533
|
-
if not isinstance(self.src,str):
|
534
|
-
self.src='''{}'''.format(self.src)
|
535
|
-
return Cut(self.src,head_len,body_len,new_line,out)
|
536
|
-
|
537
|
-
def Space(num=1,fill=' ',mode='space'):
|
538
|
-
return Space(num,fill,mode)
|
539
|
-
|
540
|
-
def Reduce(self,start=0,end=None,sym=None,default=None):
|
541
|
-
if isinstance(self.src,str):
|
542
|
-
if sym:
|
543
|
-
arr=self.src.split(sym)
|
544
|
-
if isinstance(end,int):
|
545
|
-
return Join(arr[start:end],symbol=sym)
|
546
|
-
else:
|
547
|
-
return Join(arr[start],symbol=sym)
|
548
|
-
else:
|
549
|
-
if isinstance(end,int):
|
550
|
-
return self.src[start:end]
|
551
|
-
else:
|
552
|
-
return self.src[start:]
|
553
|
-
return default
|
554
|
-
|
555
|
-
def Find(self,find,src='_#_',prs=None,sym='\n',pattern=True,default=[],out=None,findall=True,word=False,line_num=False,peel=None):
|
556
|
-
if IsNone(src,chk_val=['_#_'],chk_only=True): src=self.src
|
557
|
-
return FIND(src).Find(find,prs=prs,sym=sym,default=default,out=out,findall=findall,word=word,mode='value',line_num=line_num,peel=peel)
|
558
|
-
|
559
|
-
def Index(self,find,start=None,end=None,sym='\n',default=[],word=False,pattern=False,findall=False,out=None):
|
560
|
-
if not isinstance(self.src,str): return default
|
561
|
-
rt=[]
|
562
|
-
source=self.src.split(sym)
|
563
|
-
for row in range(0,len(source)):
|
564
|
-
for ff in self.Find(find,src=source[row],pattern=pattern,word=word,findall=findall,default=[],out=list):
|
565
|
-
if findall:
|
566
|
-
rt=rt+[(row,[m.start() for m in re.finditer(ff,source[row])])]
|
567
|
-
else:
|
568
|
-
idx=source[row].index(ff,start,end)
|
569
|
-
if idx >= 0:
|
570
|
-
rt.append((row,idx))
|
571
|
-
if rt:
|
572
|
-
if out in ['tuple',tuple]: return tuple(rt)
|
573
|
-
if out not in ['list',list] and len(rt) == 1 and rt[0][0] == 0:
|
574
|
-
if len(rt[0][1]) == 1:return rt[0][1][0]
|
575
|
-
return rt[0][1]
|
576
|
-
return rt
|
577
|
-
return default
|
578
|
-
|
579
|
-
def Replace(self,replace_what,replace_to,default=None):
|
580
|
-
if isinstance(self.src,str):
|
581
|
-
if replace_what[-1] == '$' or replace_what[0] == '^':
|
582
|
-
return re.sub(replace_what, replace_to, self.src)
|
583
|
-
else:
|
584
|
-
head, _sep, tail = self.src.rpartition(replace_what)
|
585
|
-
return head + replace_to + tail
|
586
|
-
return default
|
587
|
-
|
588
|
-
def RemoveNewline(self,src='_#_',mode='edge',newline='\n',byte=None):
|
589
|
-
if IsNone(src,chk_val=['_#_'],chk_only=True): src=self.src
|
590
|
-
if isinstance(byte,bool):
|
591
|
-
if byte:
|
592
|
-
src=Bytes(src)
|
593
|
-
else:
|
594
|
-
src=Str(src)
|
595
|
-
src_a=Split(src,newline,default=False)
|
596
|
-
if src_a is False:
|
597
|
-
return src
|
598
|
-
if mode in ['edge','both']:
|
599
|
-
if not src_a[0].strip() and not src_a[-1].strip():
|
600
|
-
return Join(src_a[1:-1],symbol=newline)
|
601
|
-
elif not src_a[0].strip():
|
602
|
-
return Join(src_a[1:],symbol=newline)
|
603
|
-
elif not src_a[-1].strip():
|
604
|
-
return Join(src_a[:-1],symbol=newline)
|
605
|
-
elif mode in ['first','start',0]:
|
606
|
-
if not src_a[0].strip():
|
607
|
-
return Join(src_a[1:],symbol=newline)
|
608
|
-
elif mode in ['end','last',-1]:
|
609
|
-
if not src_a[-1].strip():
|
610
|
-
return Join(src_a[:-1],symbol=newline)
|
611
|
-
elif mode in ['*','all','everything']:
|
612
|
-
return Join(src_a,symbol='')
|
613
|
-
return src
|
614
|
-
|
615
|
-
def Wrap(self,src='_#_',space='',space_mode='space',sym='\n',default='org',NFLT=False,out=str):
|
616
|
-
if IsNone(src,chk_val=[None,'_#_'],chk_only=True): src=self.src
|
617
|
-
if isinstance(space,str): space=len(space)
|
618
|
-
return WrapString(src,fspace=space,nspace=space,new_line=sym,NFLT=NFLT,mode=space_mode)
|
619
|
-
|
620
|
-
def Tap(self,space='',sym='\n',default='org',NFLT=False,out=str):
|
621
|
-
if isinstance(space,str):
|
622
|
-
space=len(space)
|
623
|
-
return WrapString(fspace=space,nspace=space,new_line=sym,NFLT=NFLT)
|
624
|
-
|
625
255
|
class CONVERT:
|
626
256
|
def __init__(self,src):
|
627
257
|
self.src=src
|
@@ -646,10 +276,10 @@ class CONVERT:
|
|
646
276
|
return default
|
647
277
|
|
648
278
|
def Mac2Str(self,case='lower',default=False):
|
649
|
-
return
|
279
|
+
return MacV4(self.src,case=case,default=default)
|
650
280
|
|
651
281
|
def Str2Mac(self,case='lower',default=False,sym=':',chk=False):
|
652
|
-
return
|
282
|
+
return MacV4(self.src,case=case,default=default,symbol=sym)
|
653
283
|
|
654
284
|
def Size(self,unit='b:g',default=False):
|
655
285
|
return sizeConvert(self.src,unit=unit)
|
@@ -657,1274 +287,1191 @@ class CONVERT:
|
|
657
287
|
def Url(self):
|
658
288
|
return str2url(self.src)
|
659
289
|
|
660
|
-
class
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
290
|
+
class FILE:
|
291
|
+
'''
|
292
|
+
sub_dir : True (Get files in recuring directory)
|
293
|
+
data : True (Get File Data)
|
294
|
+
md5sum : True (Get File's MD5 SUM)
|
295
|
+
link2file: True (Make a real file instead sym-link file)
|
296
|
+
'''
|
297
|
+
def __init__(self,*inp,**opts):
|
298
|
+
self.root_path=opts.get('root_path',None)
|
299
|
+
#if self.root_path is None: self.root_path=os.path.dirname(os.path.abspath(__file__))
|
300
|
+
#if self.root_path is None: self.root_path=self.Path()
|
301
|
+
if IsNone(self.root_path): self.root_path=self.Path()
|
302
|
+
info=opts.get('info',None)
|
303
|
+
if isinstance(info,dict):
|
304
|
+
self.info=info
|
305
|
+
else:
|
306
|
+
self.info={}
|
307
|
+
sub_dir=opts.get('sub_dir',opts.get('include_sub_dir',opts.get('include_dir',False)))#???
|
308
|
+
data=opts.get('data',False)
|
309
|
+
md5sum=opts.get('md5sum',False)
|
310
|
+
link2file=opts.get('link2file',False) # If True then copy file-data of sym-link file, so get it real file instead of sym-link file
|
311
|
+
self.filelist={}
|
312
|
+
for filename in inp:
|
313
|
+
root,flist=self.FileList(filename,sub_dir=sub_dir,dirname=True)
|
314
|
+
if root not in self.filelist: self.filelist[root]=[]
|
315
|
+
self.filelist[root]=self.filelist[root]+flist
|
316
|
+
for ff in self.filelist:
|
317
|
+
self.info.update(self.Get(ff,*self.filelist[ff],data=data,md5sum=md5sum,link2file=link2file))
|
683
318
|
|
684
|
-
def
|
685
|
-
if isinstance(
|
686
|
-
|
687
|
-
|
688
|
-
if
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
319
|
+
def FileList(self,name,sub_dir=False,dirname=False,default=[]):
|
320
|
+
if isinstance(name,str):
|
321
|
+
if name[0] == '/': # Start from root path
|
322
|
+
if os.path.isfile(name) or os.path.islink(name): return os.path.dirname(name),[os.path.basename(name)]
|
323
|
+
if os.path.isdir(name):
|
324
|
+
if sub_dir:
|
325
|
+
rt = []
|
326
|
+
pwd=os.getcwd()
|
327
|
+
os.chdir(name)
|
328
|
+
for base, dirs, files in os.walk('.'):
|
329
|
+
if dirname: rt.extend(os.path.join(base[2:], d) for d in dirs)
|
330
|
+
rt.extend(os.path.join(base[2:], f) for f in files)
|
331
|
+
os.chdir(pwd)
|
332
|
+
return Path(name),rt
|
333
|
+
else:
|
334
|
+
return Path(name),[f for f in os.listdir(name)]
|
335
|
+
elif self.root_path: # start from defined root path
|
336
|
+
#chk_path=os.path.join(self.root_path,name)
|
337
|
+
chk_path=Path(self.root_path,name)
|
338
|
+
if os.path.isfile(chk_path) or os.path.islink(chk_path): return Path(self.root_path),[name]
|
339
|
+
if os.path.isdir(chk_path):
|
340
|
+
if sub_dir:
|
341
|
+
rt = []
|
342
|
+
pwd=os.getcwd()
|
343
|
+
os.chdir(self.root_path) # Going to defined root path
|
344
|
+
# Get recuring file list of the name (when current dir then '.')
|
345
|
+
for base, dirs, files in os.walk(name):
|
346
|
+
if dirname: rt.extend(os.path.join(base[2:], d) for d in dirs)
|
347
|
+
rt.extend(os.path.join(base[2:], f) for f in files)
|
348
|
+
os.chdir(pwd) # recover to the original path
|
349
|
+
return Path(self.root_path),rt
|
350
|
+
else:
|
351
|
+
if name == '.': name=''
|
352
|
+
return Path(self.root_path),[os.path.join(name,f) for f in os.listdir('{}/{}'.format(self.root_path,name))]
|
696
353
|
return default
|
697
354
|
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
return self.ip
|
705
|
-
return ip
|
706
|
-
|
707
|
-
def IsV4(self,ip=None):
|
708
|
-
ip=self.GetIP(ip)
|
709
|
-
return True if IpV4(ip) else False
|
355
|
+
def CdPath(self,base,path):
|
356
|
+
rt=base
|
357
|
+
for ii in path.split('/'):
|
358
|
+
if ii not in rt: return False
|
359
|
+
rt=rt[ii]
|
360
|
+
return rt
|
710
361
|
|
711
|
-
def
|
712
|
-
|
362
|
+
def FileName(self,filename):
|
363
|
+
if isinstance(filename,str):
|
364
|
+
filename_info=os.path.basename(filename).split('.')
|
365
|
+
if 'tar' in filename_info:
|
366
|
+
idx=filename_info.index('tar')
|
367
|
+
else:
|
368
|
+
idx=-1
|
369
|
+
#return '.'.join(filename_info[:idx]),'.'.join(filename_info[idx:])
|
370
|
+
return Join(filename_info[:idx],symbol='.'),Join(filename_info[idx:],symbol='.')
|
371
|
+
return None,None
|
713
372
|
|
714
|
-
def
|
715
|
-
|
716
|
-
|
717
|
-
''
|
718
|
-
|
719
|
-
|
720
|
-
if IpV4(ip,port=port,default=default): return True
|
721
|
-
return False
|
373
|
+
def FileType(self,filename,default=False):
|
374
|
+
if not isinstance(filename,str) or not os.path.isfile(filename): return default
|
375
|
+
Import('import magic')
|
376
|
+
aa=magic.from_buffer(open(filename,'rb').read(2048))
|
377
|
+
if aa: return aa.split()[0].lower()
|
378
|
+
return 'unknown'
|
722
379
|
|
723
|
-
def
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
ip=self.GetIP(ip)
|
738
|
-
return IpV4(ip,out=hex,default=default)
|
739
|
-
|
740
|
-
def InRange(self,start_ip,end_ip,**opts):
|
741
|
-
ip=self.GetIP(opts.get('ip'))
|
742
|
-
default=opts.get('default',False)
|
743
|
-
startip=self.Ip2Num(start_ip)
|
744
|
-
myip=self.Ip2Num(ip)
|
745
|
-
endip=self.Ip2Num(end_ip)
|
746
|
-
if isinstance(startip,int) and isinstance(myip,int) and isinstance(endip,int):
|
747
|
-
if startip <= myip <= endip: return True
|
748
|
-
return False
|
749
|
-
return default
|
380
|
+
def GetInfo(self,path=None,*inps):
|
381
|
+
if isinstance(path,str):
|
382
|
+
if not self.info and os.path.exists(path):
|
383
|
+
data={}
|
384
|
+
self.MkInfo(data,path)
|
385
|
+
else:
|
386
|
+
data=self.CdPath(path)
|
387
|
+
if isinstance(data,dict):
|
388
|
+
if not inps and ' i ' in data: return data[' i ']
|
389
|
+
rt=[]
|
390
|
+
for ii in inps:
|
391
|
+
if ii == 'data' and ii in data: rt.append(data[ii])
|
392
|
+
if ' i ' in data and ii in data[' i ']: rt.append(data[' i '][ii])
|
393
|
+
return rt
|
750
394
|
|
751
|
-
def
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
keep_good=opts.get('keep_good',30)
|
757
|
-
cancel_func=opts.get('cancel_func',None)
|
758
|
-
log=opts.get('log',None)
|
759
|
-
init_time=None
|
760
|
-
if self.IsV4(ip):
|
761
|
-
if not kp.ping(ip,count=5):
|
762
|
-
if not kp.ping(ip,count=0,timeout=timeout_sec,keep_good=keep_good,interval=interval,cancel_func=cancel_func,log=log):
|
763
|
-
return True
|
764
|
-
return False
|
765
|
-
return default
|
395
|
+
def Get(self,root_path,*filenames,**opts):
|
396
|
+
data=opts.get('data',False)
|
397
|
+
md5sum=opts.get('md5sum',False)
|
398
|
+
link2file=opts.get('link2file',False)
|
399
|
+
base={}
|
766
400
|
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
run_time=time.Int()
|
782
|
-
if self.IsV4(ip):
|
783
|
-
if log:
|
784
|
-
log('[',direct=True,log_level=1)
|
785
|
-
while True:
|
786
|
-
if time.Out(timeout_sec):
|
787
|
-
if log:
|
788
|
-
log(']\n',direct=True,log_level=1)
|
789
|
-
return False,'Timeout monitor'
|
790
|
-
if IsBreak(cancel_func):
|
791
|
-
if log:
|
792
|
-
log(']\n',direct=True,log_level=1)
|
793
|
-
return True,'Stopped monitor by Custom'
|
794
|
-
if self.Ping(ip,cancel_func=cancel_func):
|
795
|
-
if (time.Int() - run_time) > keep:
|
796
|
-
if log:
|
797
|
-
log(']\n',direct=True,log_level=1)
|
798
|
-
return True,'OK'
|
799
|
-
if log:
|
800
|
-
log('-',direct=True,log_level=1)
|
801
|
-
else:
|
802
|
-
run_time=time.Int()
|
803
|
-
if log:
|
804
|
-
log('.',direct=True,log_level=1)
|
805
|
-
time.Sleep(interval)
|
806
|
-
if log:
|
807
|
-
log(']\n',direct=True,log_level=1)
|
808
|
-
return False,'Timeout/Unknown issue'
|
809
|
-
return default,'IP format error'
|
401
|
+
def MkInfo(rt,filename=None,**opts):
|
402
|
+
#if not isinstance(rt,dict) or not isinstance(filename,str): return default
|
403
|
+
if ' i ' not in rt: rt[' i ']={}
|
404
|
+
if filename:
|
405
|
+
state=os.stat(filename)
|
406
|
+
rt[' i ']['exist']=True
|
407
|
+
rt[' i ']['size']=state.st_size
|
408
|
+
rt[' i ']['mode']=oct(state.st_mode)[-4:]
|
409
|
+
rt[' i ']['atime']=state.st_atime
|
410
|
+
rt[' i ']['mtime']=state.st_mtime
|
411
|
+
rt[' i ']['ctime']=state.st_ctime
|
412
|
+
rt[' i ']['gid']=state.st_gid
|
413
|
+
rt[' i ']['uid']=state.st_uid
|
414
|
+
if opts: rt[' i '].update(opts)
|
810
415
|
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
416
|
+
def MkPath(base,path,root_path):
|
417
|
+
rt=base
|
418
|
+
chk_dir='{}'.format(root_path)
|
419
|
+
for ii in path.split('/'):
|
420
|
+
if ii:
|
421
|
+
chk_dir=Path(chk_dir,ii)
|
422
|
+
if ii not in rt:
|
423
|
+
rt[ii]={}
|
424
|
+
if os.path.isdir(chk_dir): MkInfo(rt[ii],chk_dir,type='dir')
|
425
|
+
rt=rt[ii]
|
426
|
+
return rt
|
815
427
|
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
428
|
+
for filename in filenames:
|
429
|
+
tfilename=Path(root_path,filename)
|
430
|
+
if os.path.exists(tfilename):
|
431
|
+
rt=MkPath(base,filename,root_path)
|
432
|
+
if os.path.islink(tfilename): # it is a Link File
|
433
|
+
if os.path.isfile(filename): # it is a File
|
434
|
+
if link2file:
|
435
|
+
name,ext=self.FileName(tfilename)
|
436
|
+
_md5=None
|
437
|
+
if data or md5sum: # MD5SUM or Data
|
438
|
+
filedata=self.Rw(tfilename,out='byte')
|
439
|
+
if filedata[0]:
|
440
|
+
if data: rt['data']=filedata[1]
|
441
|
+
if md5sum: _md5=md5(filedata[1])
|
442
|
+
MkInfo(rt,filename=tfilename,type=self.FileType(tfilename),name=name,ext=ext,md5=_md5)
|
443
|
+
else:
|
444
|
+
MkInfo(rt,filename=tfilename,type='link',dest=os.readlink(tfilename))
|
445
|
+
elif os.path.isdir(tfilename): # it is a directory
|
446
|
+
MkInfo(rt,tfilename,type='dir')
|
447
|
+
elif os.path.isfile(tfilename): # it is a File
|
448
|
+
name,ext=self.FileName(tfilename)
|
449
|
+
_md5=None
|
450
|
+
if data or md5sum: # MD5SUM or Data
|
451
|
+
filedata=self.Rw(tfilename,out='byte')
|
452
|
+
if filedata[0]:
|
453
|
+
if data: rt['data']=filedata[1]
|
454
|
+
if md5sum: _md5=md5(filedata[1])
|
455
|
+
MkInfo(rt,filename=tfilename,type=self.FileType(tfilename),name=name,ext=ext,md5=_md5)
|
456
|
+
else:
|
457
|
+
MkInfo(rt,filename,exist=False)
|
458
|
+
if base:
|
459
|
+
return {root_path:base}
|
460
|
+
return {}
|
822
461
|
|
823
|
-
def
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
462
|
+
def GetInfoFile(self,name,roots=None): #get file info dict from Filename path
|
463
|
+
if IsNone(roots): roots=self.FindRP()
|
464
|
+
if isinstance(name,str):
|
465
|
+
for root in roots:
|
466
|
+
rt=self.info.get(root,{})
|
467
|
+
for ii in name.split('/'):
|
468
|
+
if ii not in rt: break
|
469
|
+
rt=rt[ii]
|
470
|
+
fileinfo=rt.get(' i ',{})
|
471
|
+
if fileinfo: return fileinfo
|
472
|
+
return False
|
831
473
|
|
832
|
-
def
|
833
|
-
|
474
|
+
def GetList(self,name=None,roots=None): #get file info dict from Filename path
|
475
|
+
if IsNone(roots): roots=self.FindRP()
|
476
|
+
for root in roots:
|
477
|
+
if isinstance(root,str):
|
478
|
+
rt=self.info.get(root,{})
|
479
|
+
if name != root:
|
480
|
+
rt=self.CdPath(rt,name)
|
481
|
+
if isinstance(rt,dict):
|
482
|
+
for ii in rt:
|
483
|
+
if ii == ' i ': continue
|
484
|
+
if rt[ii].get(' i ',{}).get('type') == 'dir':
|
485
|
+
print(ii+'/')
|
486
|
+
else:
|
487
|
+
print(ii)
|
488
|
+
return False
|
834
489
|
|
835
|
-
def
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
490
|
+
def GetFileList(self,name=None,roots=None): #get file info dict from Filename path
|
491
|
+
if IsNone(roots): roots=self.FindRP()
|
492
|
+
for root in roots:
|
493
|
+
if isinstance(root,str):
|
494
|
+
rt=self.info.get(root,{})
|
495
|
+
if name != root:
|
496
|
+
rt=self.CdPath(rt,name)
|
497
|
+
if isinstance(rt,dict):
|
498
|
+
for ii in rt:
|
499
|
+
if ii == ' i ': continue
|
500
|
+
if rt[ii].get(' i ',{}).get('type') == 'dir': continue
|
501
|
+
print(ii)
|
502
|
+
return False
|
844
503
|
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
self.
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
504
|
+
def ExecFile(self,filename,bin_name=None,default=None,work_path='/tmp'):
|
505
|
+
# check the filename is excutable in the system bin file then return the file name
|
506
|
+
# if compressed file then extract the file and find bin_name file in the extracted directory
|
507
|
+
# and found binary file then return then binary file path
|
508
|
+
# if filename is excutable file then return the file path
|
509
|
+
# if not found then return default value
|
510
|
+
exist=self.GetInfoFile(filename)
|
511
|
+
if exist:
|
512
|
+
if exist['type'] in ['elf'] and exist['mode'] == 33261:return filename
|
513
|
+
if self.Extract(filename,work_path=work_path):
|
514
|
+
if bin_name:
|
515
|
+
rt=[]
|
516
|
+
for ff in self.Find(work_path,filename=bin_name):
|
517
|
+
if self.Info(ff).get('mode') == 33261:
|
518
|
+
rt.append(ff)
|
519
|
+
return rt
|
520
|
+
else:
|
521
|
+
if find_executable(filename): return filename
|
522
|
+
return default
|
856
523
|
|
857
|
-
def
|
858
|
-
|
859
|
-
|
860
|
-
end_new_line=opts.get('end_new_line','')
|
861
|
-
start_new_line=opts.get('start_new_line','\n')
|
862
|
-
if len(msg) > 0:
|
863
|
-
m_str=None
|
864
|
-
intro=''
|
865
|
-
intro_space=''
|
866
|
-
if log_date_format:
|
867
|
-
intro=TIME().Format(tformat=log_date_format)+' '
|
868
|
-
func_name_name=type(func_name).__name__
|
869
|
-
if func_name_name == 'str':
|
870
|
-
intro=intro+'{0} '.format(func_name)
|
871
|
-
elif func_name is True:
|
872
|
-
intro=intro+'{0}() '.format(CallerName())
|
873
|
-
elif func_name_name in ['function','instancemethod']:
|
874
|
-
intro=intro+'{0}() '.format(func_name.__name__)
|
875
|
-
if intro:
|
876
|
-
for i in range(0,len(intro)):
|
877
|
-
intro_space=intro_space+' '
|
878
|
-
for m in list(msg):
|
879
|
-
n=m.split('\n')
|
880
|
-
#if m_str is None:
|
881
|
-
if IsNone(m_str):
|
882
|
-
m_str='{0}{1}{2}{3}'.format(start_new_line,intro,n[0],end_new_line)
|
883
|
-
else:
|
884
|
-
m_str='{0}{1}{2}{3}{4}'.format(m_str,start_new_line,intro_space,n[0],end_new_line)
|
885
|
-
for nn in n[1:]:
|
886
|
-
m_str='{0}{1}{2}{3}{4}'.format(m_str,start_new_line,intro_space,nn,end_new_line)
|
887
|
-
return m_str
|
524
|
+
def Basename(self,filename,default=False):
|
525
|
+
if isinstance(filename,str):return os.path.basename(filename)
|
526
|
+
return default
|
888
527
|
|
889
|
-
def
|
890
|
-
|
891
|
-
if
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
syslog.syslog(syslog.LOG_CRIT,syslog_msg)
|
902
|
-
elif syslogd in ['WARN','warn']:
|
903
|
-
syslog.syslog(syslog.LOG_WARNING,syslog_msg)
|
904
|
-
elif syslogd in ['DBG','DEBUG','dbg','debug']:
|
905
|
-
syslog.syslog(syslog.LOG_DEBUG,syslog_msg)
|
906
|
-
else:
|
907
|
-
syslog.syslog(syslog_msg)
|
528
|
+
def Dirname(self,filename,bin_name=None,default=False):
|
529
|
+
if not isinstance(filename,str): return default
|
530
|
+
if IsNone(bin_name): return os.path.dirname(filename)
|
531
|
+
if not isinstance(bin_name,str): return default
|
532
|
+
bin_info=bin_name.split('/')
|
533
|
+
bin_n=len(bin_info)
|
534
|
+
filename_info=filename.split('/')
|
535
|
+
filename_n=len(filename_info)
|
536
|
+
for ii in range(0,bin_n):
|
537
|
+
if filename_info[filename_n-1-ii] != bin_info[bin_n-1-ii]: return default
|
538
|
+
#return '/'.join(filename_info[:-bin_n])
|
539
|
+
return Join(filename_info[:-bin_n],symbol='/')
|
908
540
|
|
541
|
+
def Find(self,filename,default=[]):
|
542
|
+
if not isinstance(filename,str): return default
|
543
|
+
filename=os.path.basename(filename)
|
544
|
+
if os.path.isdir(self.root_path):
|
545
|
+
rt = []
|
546
|
+
for base, dirs, files in os.walk(self.root_path):
|
547
|
+
found = fnmatch.filter(files, filename)
|
548
|
+
rt.extend(os.path.join(base, f) for f in found)
|
549
|
+
return rt
|
550
|
+
return default
|
909
551
|
|
910
|
-
def
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
f.write(log_str)
|
927
|
-
return log_file
|
928
|
-
|
929
|
-
def Screen(self,log_str,log_level):
|
930
|
-
if log_level in ['error']:
|
931
|
-
sys.stderr.write(log_str)
|
932
|
-
sys.stderr.flush()
|
933
|
-
elif log_level <= self.limit:
|
934
|
-
sys.stdout.write(log_str)
|
935
|
-
sys.stdout.flush()
|
936
|
-
|
937
|
-
|
938
|
-
def Log(self,*msg,**opts):
|
939
|
-
direct=opts.get('direct',False)
|
940
|
-
func_name=opts.get('func_name',None)
|
941
|
-
date_format=opts.get('date_format','[%m/%d/%Y %H:%M:%S]')
|
942
|
-
start_new_line=opts.get('start_new_line','\n')
|
943
|
-
end_new_line=opts.get('end_new_line','')
|
944
|
-
log_level=opts.get('log_level',3)
|
945
|
-
special_file=opts.get('filename',None)
|
946
|
-
screen=opts.get('screen',None)
|
947
|
-
syslogd=opts.get('syslogd',None)
|
948
|
-
if msg:
|
949
|
-
# send log at syslogd
|
950
|
-
self.Syslogd(*msg,syslogd=syslogd)
|
951
|
-
|
952
|
-
#if date_format in [False,None,'','no','ignore']:
|
953
|
-
if IsNone(date_format,chk_val=[False,None,'','no','ignore']):
|
954
|
-
date_format=None
|
955
|
-
if IsNone(func_name,chk_val=[False,None,'','no','ignore']):
|
956
|
-
func_name=None
|
957
|
-
if direct:
|
958
|
-
#log_str=' '.join(msg)
|
959
|
-
log_str=Join(msg,symbol=' ')
|
960
|
-
else:
|
961
|
-
log_str=self.Format(*msg,func_name=func_name,date_format=date_format,end_new_line=end_new_line,start_new_line=start_new_line)
|
962
|
-
|
963
|
-
# Saving log at file
|
964
|
-
log_file=self.File(log_str,log_level,special_file=special_file)
|
965
|
-
|
966
|
-
# print at screen
|
967
|
-
if screen is True or (IsNone(screen) and self.screen is True):
|
968
|
-
self.Screen(log_str,log_level)
|
969
|
-
|
970
|
-
# Send Log Data to logging function (self.log_file)
|
971
|
-
#if log_file is None:
|
972
|
-
if IsNone(log_file):
|
973
|
-
self.Function(log_str)
|
552
|
+
# def Decompress(self,filename,work_path='/tmp',info={},del_org_file=False):
|
553
|
+
# if not info and isinstance(filename,str) and os.path.isfile(filename): info=self.Get(filename)
|
554
|
+
# filetype=info.get('type',None)
|
555
|
+
# fileext=info.get('ext',None)
|
556
|
+
# if filetype and fileext:
|
557
|
+
# # Tar stuff
|
558
|
+
# if fileext in ['tgz','tar','tar.gz','tar.bz2','tar.xz'] and filetype in ['gzip','tar','bzip2','lzma','xz','bz2']:
|
559
|
+
# tf=tarfile.open(filename)
|
560
|
+
# tf.extractall(work_path)
|
561
|
+
# tf.close()
|
562
|
+
# elif fileext in ['zip'] and filetype in ['compress']:
|
563
|
+
# with zipfile.ZipFile(filename,'r') as zf:
|
564
|
+
# zf.extractall(work_path)
|
565
|
+
# if del_org_file: os.unline(filename)
|
566
|
+
# return True
|
567
|
+
# return False
|
974
568
|
|
975
|
-
def
|
976
|
-
if
|
977
|
-
|
978
|
-
if
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1011
|
-
|
569
|
+
def Rw(self,name,data=None,out='byte',append=False,read=None,overwrite=True,finfo={},file_only=True,default={'err'}):
|
570
|
+
if isinstance(name,str):
|
571
|
+
#if data is None: # Read from file
|
572
|
+
if IsNone(data): # Read from file
|
573
|
+
if os.path.isfile(name) or (not file_only and os.path.exists(name)):
|
574
|
+
try:
|
575
|
+
if read in ['firstread','firstline','first_line','head','readline']:
|
576
|
+
with open(name,'rb') as f:
|
577
|
+
data=f.readline()
|
578
|
+
elif not file_only:
|
579
|
+
data=os.open(name,os.O_RDONLY)
|
580
|
+
os.close(data)
|
581
|
+
else:
|
582
|
+
with open(name,'rb') as f:
|
583
|
+
data=f.read()
|
584
|
+
if out in ['string','str']:
|
585
|
+
return True,Str(data)
|
586
|
+
else:
|
587
|
+
return True,data
|
588
|
+
except:
|
589
|
+
pass
|
590
|
+
if default == {'err'}:
|
591
|
+
return False,'File({}) not found'.format(name)
|
592
|
+
return False,default
|
593
|
+
else: # Write to file
|
594
|
+
file_path=os.path.dirname(name)
|
595
|
+
if not file_path or os.path.isdir(file_path): # current dir or correct directory
|
596
|
+
if append:
|
597
|
+
with open(name,'ab') as f:
|
598
|
+
f.write(Bytes(data))
|
599
|
+
elif not file_only:
|
600
|
+
try:
|
601
|
+
f=os.open(name,os.O_RDWR)
|
602
|
+
os.write(f,data)
|
603
|
+
os.close(f)
|
604
|
+
except:
|
605
|
+
return False,None
|
1012
606
|
else:
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1017
|
-
|
1018
|
-
|
1019
|
-
|
1020
|
-
|
1021
|
-
|
1022
|
-
|
607
|
+
with open(name,'wb') as f:
|
608
|
+
f.write(Bytes(data))
|
609
|
+
if isinstance(finfo,dict) and finfo: self.SetIdentity(name,**finfo)
|
610
|
+
#mode=self.Mode(mode)
|
611
|
+
#if mode: os.chmod(name,int(mode,base=8))
|
612
|
+
#if uid and gid: os.chown(name,uid,gid)
|
613
|
+
#if mtime and atime: os.utime(name,(atime,mtime))# Time update must be at last order
|
614
|
+
return True,None
|
615
|
+
if default == {'err'}:
|
616
|
+
return False,'Directory({}) not found'.format(file_path)
|
617
|
+
return False,default
|
618
|
+
if default == {'err'}:
|
619
|
+
return False,'Unknown type({}) filename'.format(name)
|
620
|
+
return False,default
|
1023
621
|
|
1024
|
-
def
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
1029
|
-
|
1030
|
-
|
1031
|
-
|
1032
|
-
|
1033
|
-
|
622
|
+
def Mode(self,val,mode='chmod',default=False):
|
623
|
+
'''
|
624
|
+
convert File Mode to mask
|
625
|
+
mode
|
626
|
+
'chmod' : default, convert to mask (os.chmod(<file>,<mask>))
|
627
|
+
'int' : return to int number of oct( ex: 755 )
|
628
|
+
'oct' : return oct number (string)
|
629
|
+
'str' : return string (-rwxr--r--)
|
630
|
+
default: False
|
631
|
+
'''
|
632
|
+
def _mode_(oct_data,mode='chmod'):
|
633
|
+
#convert to octal to 8bit mask, int, string
|
634
|
+
if mode == 'chmod':
|
635
|
+
return int(oct_data,base=8)
|
636
|
+
elif mode in ['int',int]:
|
637
|
+
return int(oct_data.replace('o',''),base=10)
|
638
|
+
elif mode in ['str',str]:
|
639
|
+
m=[]
|
640
|
+
#for i in list(str(int(oct_data,base=10))):
|
641
|
+
t=False
|
642
|
+
for n,i in enumerate(str(int(oct_data.replace('o',''),base=10))):
|
643
|
+
if n == 0:
|
644
|
+
if i == '1': t=True
|
645
|
+
if n > 0:
|
646
|
+
if i == '7':
|
647
|
+
m.append('rwx')
|
648
|
+
elif i == '6':
|
649
|
+
m.append('rw-')
|
650
|
+
elif i == '5':
|
651
|
+
m.append('r-x')
|
652
|
+
elif i == '4':
|
653
|
+
m.append('r--')
|
654
|
+
elif i == '3':
|
655
|
+
m.append('-wx')
|
656
|
+
elif i == '2':
|
657
|
+
m.append('-w-')
|
658
|
+
elif i == '1':
|
659
|
+
m.append('--x')
|
660
|
+
str_mod=Join(m,'')
|
661
|
+
if t: return str_mod[:-1]+'t'
|
662
|
+
return str_mod
|
663
|
+
return oct_data
|
664
|
+
if isinstance(val,int):
|
665
|
+
#if val > 511: #stat.st_mode (32768 ~ 33279)
|
666
|
+
#stat.st_mode (file: 32768~36863, directory: 16384 ~ 20479)
|
667
|
+
if 32768 <= val <= 36863 or 16384 <= val <= 20479: #stat.st_mode
|
668
|
+
#return _mode_(oct(val)[-4:],mode) # to octal number (oct(val)[-4:])
|
669
|
+
return _mode_(oct(val & 0o777),mode) # to octal number (oct(val)[-4:])
|
670
|
+
elif 511 >= val > 63: #mask
|
671
|
+
return _mode_(oct(val),mode) # to ocal number(oct(val))
|
1034
672
|
else:
|
1035
|
-
return
|
1036
|
-
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1046
|
-
|
1047
|
-
|
1048
|
-
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1052
|
-
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
return default
|
1067
|
-
return socket.gethostbyname(socket.gethostname())
|
1068
|
-
|
1069
|
-
def IpmiIp(self,default=None):
|
1070
|
-
rt=rshell('''ipmitool lan print 2>/dev/null| grep "IP Address" | grep -v Source | awk '{print $4}' ''')
|
1071
|
-
if rt[0]:return rt[1]
|
1072
|
-
return default
|
1073
|
-
|
1074
|
-
def IpmiMac(self,default=None):
|
1075
|
-
rt=rshell(""" ipmitool lan print 2>/dev/null | grep "MAC Address" | awk """ + """ '{print $4}' """)
|
1076
|
-
if rt[0]:return rt[1]
|
1077
|
-
return default
|
1078
|
-
|
1079
|
-
def Mac(self,ip=None,dev=None,default=None,ifname=None):
|
1080
|
-
#if dev is None and ifname: dev=ifname
|
1081
|
-
if IsNone(dev) and ifname: dev=ifname
|
1082
|
-
if IP(ip).IsV4():
|
1083
|
-
dev_info=self.NetDevice()
|
1084
|
-
for dev in dev_info.keys():
|
1085
|
-
if self.Ip(ifname=dev) == ip:
|
1086
|
-
return dev_info[dev]['mac']
|
1087
|
-
#ip or anyother input of device then getting default gw's dev
|
1088
|
-
if IsNone(dev): dev=self.DefaultRouteDev()
|
1089
|
-
if dev:
|
1090
|
-
try:
|
1091
|
-
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
1092
|
-
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', Bytes(dev[:15])))
|
1093
|
-
return Join(['%02x' % ord(char) for char in Str(info[18:24])],symbol=':')
|
1094
|
-
except:
|
1095
|
-
return default
|
1096
|
-
#return ':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff) for ele in range(0,8*6,8)][::-1])
|
1097
|
-
return MAC('%012x' % uuid.getnode()).FromStr()
|
1098
|
-
|
1099
|
-
def DevName(self,mac=None,default=None):
|
1100
|
-
if IsNone(mac):
|
1101
|
-
mac=self.Mac()
|
1102
|
-
net_dir='/sys/class/net'
|
1103
|
-
if isinstance(mac,str) and os.path.isdir(net_dir):
|
1104
|
-
dirpath,dirnames,filenames = list(os.walk(net_dir))[0]
|
1105
|
-
for dev in dirnames:
|
1106
|
-
fmac=cat('{}/{}/address'.format(dirpath,dev),no_edge=True)
|
1107
|
-
if isinstance(fmac,str) and fmac.strip().lower() == mac.lower():
|
1108
|
-
return dev
|
1109
|
-
return default
|
1110
|
-
|
1111
|
-
def Info(self):
|
1112
|
-
return {
|
1113
|
-
'host_name':self.Name(),
|
1114
|
-
'host_ip':self.Ip(),
|
1115
|
-
'host_mac':self.Mac(),
|
1116
|
-
'ipmi_ip':self.IpmiIp(),
|
1117
|
-
'ipmi_mac':self.IpmiMac(),
|
1118
|
-
}
|
673
|
+
return _mode_('%04d'%(val),mode) # to ocal number(oct(val))
|
674
|
+
else:
|
675
|
+
val=Str(val,default=None)
|
676
|
+
if isinstance(val,str):
|
677
|
+
val_len=len(val)
|
678
|
+
num=Int(val,default=None)
|
679
|
+
if isinstance(num,int):
|
680
|
+
if 3 <= len(val) <=4 and 100 <= num <= 777: #string type of permission number(octal number)
|
681
|
+
return _mode_('%04d'%(num),mode)
|
682
|
+
else:
|
683
|
+
val_len=len(val)
|
684
|
+
if 9<= val_len <=10:
|
685
|
+
if val_len == 10 and val[0] in ['-','d','s']:
|
686
|
+
val=val[1:]
|
687
|
+
else:
|
688
|
+
StdErr('Bad permission length')
|
689
|
+
return default
|
690
|
+
if not all(val[k] in 'rw-' for k in [0,1,3,4,6,7]):
|
691
|
+
StdErr('Bad permission format (read-write)')
|
692
|
+
return default
|
693
|
+
if not all(val[k] in 'xs-' for k in [2,5]):
|
694
|
+
StdErr('Bad permission format (execute)')
|
695
|
+
return default
|
696
|
+
if val[8] not in 'xt-':
|
697
|
+
StdErr( 'Bad permission format (execute other)')
|
698
|
+
return default
|
699
|
+
m = 0
|
700
|
+
if val[0] == 'r': m |= stat.S_IRUSR
|
701
|
+
if val[1] == 'w': m |= stat.S_IWUSR
|
702
|
+
if val[2] == 'x': m |= stat.S_IXUSR
|
703
|
+
if val[2] == 's': m |= stat.S_IXUSR | stat.S_ISUID
|
1119
704
|
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
if
|
1124
|
-
|
705
|
+
if val[3] == 'r': m |= stat.S_IRGRP
|
706
|
+
if val[4] == 'w': m |= stat.S_IWGRP
|
707
|
+
if isinstance(val,int):
|
708
|
+
#if val > 511: #stat.st_mode (32768 ~ 33279)
|
709
|
+
#stat.st_mode (file: 32768~36863, directory: 16384 ~ 20479)
|
710
|
+
if 32768 <= val <= 36863 or 16384 <= val <= 20479: #stat.st_mode
|
711
|
+
#return _mode_(oct(val)[-4:],mode) # to octal number (oct(val)[-4:])
|
712
|
+
return _mode_(oct(val & 0o777),mode) # to octal number (oct(val)[-4:])
|
713
|
+
elif 511 >= val > 63: #mask
|
714
|
+
return _mode_(oct(val),mode) # to ocal number(oct(val))
|
1125
715
|
else:
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
716
|
+
return _mode_('%04d'%(val),mode) # to ocal number(oct(val))
|
717
|
+
else:
|
718
|
+
val=Str(val,default=None)
|
719
|
+
if isinstance(val,str):
|
720
|
+
val_len=len(val)
|
721
|
+
num=Int(val,default=None)
|
722
|
+
if isinstance(num,int):
|
723
|
+
if 3 <= len(val) <=4 and 100 <= num <= 777: #string type of permission number(octal number)
|
724
|
+
return _mode_('%04d'%(num),mode)
|
725
|
+
else:
|
726
|
+
val_len=len(val)
|
727
|
+
if 9<= val_len <=10:
|
728
|
+
if val_len == 10 and val[0] in ['-','d','s']:
|
729
|
+
val=val[1:]
|
730
|
+
else:
|
731
|
+
StdErr('Bad permission length')
|
732
|
+
return default
|
733
|
+
if not all(val[k] in 'rw-' for k in [0,1,3,4,6,7]):
|
734
|
+
StdErr('Bad permission format (read-write)')
|
735
|
+
return default
|
736
|
+
if not all(val[k] in 'xs-' for k in [2,5]):
|
737
|
+
StdErr('Bad permission format (execute)')
|
738
|
+
return default
|
739
|
+
if val[8] not in 'xt-':
|
740
|
+
StdErr( 'Bad permission format (execute other)')
|
741
|
+
return default
|
742
|
+
m = 0
|
743
|
+
if val[0] == 'r': m |= stat.S_IRUSR
|
744
|
+
if val[1] == 'w': m |= stat.S_IWUSR
|
745
|
+
if val[2] == 'x': m |= stat.S_IXUSR
|
746
|
+
if val[2] == 's': m |= stat.S_IXUSR | stat.S_ISUID
|
1138
747
|
|
1139
|
-
|
1140
|
-
|
1141
|
-
if
|
1142
|
-
|
1143
|
-
|
1144
|
-
|
1145
|
-
|
748
|
+
if val[3] == 'r': m |= stat.S_IRGRP
|
749
|
+
if val[4] == 'w': m |= stat.S_IWGRP
|
750
|
+
if isinstance(val,int):
|
751
|
+
#if val > 511: #stat.st_mode (32768 ~ 33279)
|
752
|
+
#stat.st_mode (file: 32768~36863, directory: 16384 ~ 20479)
|
753
|
+
if 32768 <= val <= 36863 or 16384 <= val <= 20479: #stat.st_mode
|
754
|
+
#return _mode_(oct(val)[-4:],mode) # to octal number (oct(val)[-4:])
|
755
|
+
return _mode_(oct(val & 0o777),mode) # to octal number (oct(val)[-4:])
|
756
|
+
elif 511 >= val > 63: #mask
|
757
|
+
return _mode_(oct(val),mode) # to ocal number(oct(val))
|
1146
758
|
else:
|
1147
|
-
|
1148
|
-
|
1149
|
-
|
1150
|
-
|
759
|
+
return _mode_('%04d'%(val),mode) # to ocal number(oct(val))
|
760
|
+
else:
|
761
|
+
val=Str(val,default=None)
|
762
|
+
if isinstance(val,str):
|
763
|
+
val_len=len(val)
|
764
|
+
num=Int(val,default=None)
|
765
|
+
if isinstance(num,int):
|
766
|
+
if 3 <= len(val) <=4 and 100 <= num <= 777: #string type of permission number(octal number)
|
767
|
+
return _mode_('%04d'%(num),mode)
|
768
|
+
else:
|
769
|
+
val_len=len(val)
|
770
|
+
if 9<= val_len <=10:
|
771
|
+
if val_len == 10 and val[0] in ['-','d','s']:
|
772
|
+
val=val[1:]
|
773
|
+
else:
|
774
|
+
StdErr('Bad permission length')
|
775
|
+
return default
|
776
|
+
if not all(val[k] in 'rw-' for k in [0,1,3,4,6,7]):
|
777
|
+
StdErr('Bad permission format (read-write)')
|
778
|
+
return default
|
779
|
+
if not all(val[k] in 'xs-' for k in [2,5]):
|
780
|
+
StdErr('Bad permission format (execute)')
|
781
|
+
return default
|
782
|
+
if val[8] not in 'xt-':
|
783
|
+
StdErr( 'Bad permission format (execute other)')
|
784
|
+
return default
|
785
|
+
m = 0
|
786
|
+
if val[0] == 'r': m |= stat.S_IRUSR
|
787
|
+
if val[1] == 'w': m |= stat.S_IWUSR
|
788
|
+
if val[2] == 'x': m |= stat.S_IXUSR
|
789
|
+
if val[2] == 's': m |= stat.S_IXUSR | stat.S_ISUID
|
1151
790
|
|
1152
|
-
|
1153
|
-
|
791
|
+
if val[3] == 'r': m |= stat.S_IRGRP
|
792
|
+
if val[4] == 'w': m |= stat.S_IWGRP
|
793
|
+
if val[5] == 'x': m |= stat.S_IXGRP
|
794
|
+
if val[5] == 's': m |= stat.S_IXGRP | stat.S_ISGID
|
1154
795
|
|
1155
|
-
|
1156
|
-
|
796
|
+
if val[6] == 'r': m |= stat.S_IROTH
|
797
|
+
if val[7] == 'w': m |= stat.S_IWOTH
|
798
|
+
if val[8] == 'x': m |= stat.S_IXOTH
|
799
|
+
if val[8] == 't': m |= stat.S_IXOTH | stat.S_ISVTX
|
800
|
+
return _mode_(oct(m),mode)
|
801
|
+
return default
|
802
|
+
# Find filename's root path and filename according to the db
|
803
|
+
def FindRP(self,filename=None,default=None):
|
804
|
+
if isinstance(filename,str) and self.info:
|
805
|
+
info_keys=list(self.info.keys())
|
806
|
+
info_num=len(info_keys)
|
807
|
+
if filename[0] != '/':
|
808
|
+
if info_num == 1: return info_keys[0]
|
809
|
+
return self.root_path
|
810
|
+
aa='/'
|
811
|
+
filename_a=filename.split('/')
|
812
|
+
for ii in range(1,len(filename_a)):
|
813
|
+
aa=Path(aa,filename_a[ii])
|
814
|
+
if aa in info_keys:
|
815
|
+
#remain_path='/'.join(filename_a[ii+1:])
|
816
|
+
remain_path=Join(filename_a[ii+1:],symbol='/')
|
817
|
+
if info_num == 1: return aa,remain_path
|
818
|
+
# if info has multi root path then check filename in the db of each root_path
|
819
|
+
if self.GetInfoFile(remain_path,aa): return aa,remain_path
|
820
|
+
elif self.info:
|
821
|
+
return list(self.info.keys())
|
822
|
+
return default
|
1157
823
|
|
1158
|
-
|
1159
|
-
|
1160
|
-
|
1161
|
-
|
1162
|
-
|
1163
|
-
|
1164
|
-
|
1165
|
-
|
1166
|
-
|
1167
|
-
|
1168
|
-
|
1169
|
-
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
1176
|
-
data=opts.get('data',False)
|
1177
|
-
md5sum=opts.get('md5sum',False)
|
1178
|
-
link2file=opts.get('link2file',False) # If True then copy file-data of sym-link file, so get it real file instead of sym-link file
|
1179
|
-
self.filelist={}
|
1180
|
-
for filename in inp:
|
1181
|
-
root,flist=self.FileList(filename,sub_dir=sub_dir,dirname=True)
|
1182
|
-
if root not in self.filelist: self.filelist[root]=[]
|
1183
|
-
self.filelist[root]=self.filelist[root]+flist
|
1184
|
-
for ff in self.filelist:
|
1185
|
-
self.info.update(self.Get(ff,*self.filelist[ff],data=data,md5sum=md5sum,link2file=link2file))
|
824
|
+
def ExtractRoot(self,**opts):
|
825
|
+
root_path=opts.get('root_path',[])
|
826
|
+
dirpath=opts.get('dirpath')
|
827
|
+
sub_dir=opts.get('sub_dir',False)
|
828
|
+
if isinstance(root_path,str):
|
829
|
+
root_path=[root_path]
|
830
|
+
#if not os.path.isdir(opts.get('dest')): os.makedirs(opts.get('dest'))
|
831
|
+
if self.Mkdir(opts.get('dest'),force=True) is False: return False
|
832
|
+
for rp in root_path:
|
833
|
+
new_dest=opts.get('dest')
|
834
|
+
if dirpath:
|
835
|
+
rt=self.CdPath(self.info[rp],dirpath)
|
836
|
+
if rt is False:
|
837
|
+
print('{} not found'.format(dirpath))
|
838
|
+
return
|
839
|
+
else:
|
840
|
+
dirpath=''
|
841
|
+
rt=self.info[rp]
|
1186
842
|
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
1194
|
-
|
1195
|
-
|
1196
|
-
|
1197
|
-
|
1198
|
-
|
1199
|
-
|
1200
|
-
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1207
|
-
|
1208
|
-
if
|
1209
|
-
|
1210
|
-
|
1211
|
-
|
1212
|
-
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
os.
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
843
|
+
rinfo=rt.get(' i ',{})
|
844
|
+
rtype=rinfo.get('type')
|
845
|
+
#dir:directory,None:root directory
|
846
|
+
if not IsNone(rtype,chk_val=['dir',None,'']): # File / Link
|
847
|
+
mydest=os.path.dirname(dirpath)
|
848
|
+
myname=os.path.basename(dirpath)
|
849
|
+
if mydest:
|
850
|
+
mydest=os.path.join(new_dest,mydest)
|
851
|
+
else:
|
852
|
+
mydest=new_dest
|
853
|
+
#if not os.path.isdir(mydest): os.makedirs(mydest)
|
854
|
+
if self.Mkdir(mydest,force=True,info=rinfo) is False: return False
|
855
|
+
if rtype == 'link':
|
856
|
+
os.symlink(rinfo['dest'],os.path.join(mydest,myname))
|
857
|
+
self.SetIdentity(os.path.join(mydest,myname),**rinfo)
|
858
|
+
else: # File
|
859
|
+
if 'data' in rt: self.Rw(Path(mydest,myname),data=rt['data'],finfo=rinfo)
|
860
|
+
else: print('{} file have no data'.format(dirpath))
|
861
|
+
# self.SetIdentity(os.path.join(mydest,myname),**rinfo)
|
862
|
+
else: # directory or root DB
|
863
|
+
for ii in rt:
|
864
|
+
if ii == ' i ': continue
|
865
|
+
finfo=rt[ii].get(' i ',{})
|
866
|
+
ftype=finfo.get('type')
|
867
|
+
if ftype == 'dir':
|
868
|
+
mydir=os.path.join(new_dest,ii)
|
869
|
+
self.Mkdir(mydir,force=True,info=finfo)
|
870
|
+
#self.SetIdentity(mydir,**finfo)
|
871
|
+
# Sub directory
|
872
|
+
if sub_dir: self.ExtractRoot(dirpath=os.path.join(dirpath,ii),root_path=rp,dest=os.path.join(new_dest,ii),sub_dir=sub_dir)
|
873
|
+
#if dmtime and datime: os.utime(mydir,(datime,dmtime)) # Time update must be at last order
|
874
|
+
elif ftype == 'link':
|
875
|
+
iimm=os.path.join(new_dest,ii)
|
876
|
+
if not os.path.exists(iimm):
|
877
|
+
os.symlink(finfo['dest'],iimm)
|
878
|
+
self.SetIdentity(iimm,**finfo)
|
879
|
+
else: # File
|
880
|
+
if 'data' in rt[ii]: self.Rw(os.path.join(new_dest,ii),data=rt[ii]['data'],finfo=finfo)
|
881
|
+
else: print('{} file have no data'.format(ii))
|
1222
882
|
|
1223
|
-
def
|
1224
|
-
|
1225
|
-
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1239
|
-
return None,None
|
883
|
+
def Mkdir(self,path,force=False,info={}):
|
884
|
+
if not isinstance(path,str): return None
|
885
|
+
if os.path.exists(path): return None
|
886
|
+
if force:
|
887
|
+
try:
|
888
|
+
os.makedirs(path)
|
889
|
+
if isinstance(info,dict) and info: self.SetIdentity(path,**info)
|
890
|
+
except:
|
891
|
+
return False
|
892
|
+
else:
|
893
|
+
try:
|
894
|
+
os.mkdir(path)
|
895
|
+
if isinstance(info,dict) and info: self.SetIdentity(path,**info)
|
896
|
+
except:
|
897
|
+
return False
|
898
|
+
return True
|
1240
899
|
|
1241
|
-
def
|
1242
|
-
if
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
900
|
+
def MkTemp(self,filename=None,suffix='-XXXXXXXX',opt='dry',base_dir='/tmp',custom=None,force=False):
|
901
|
+
if IsNone(filename):
|
902
|
+
filename=os.path.join(base_dir,Random(length=len(suffix)-1,strs=custom,mode='str'))
|
903
|
+
dir_name=os.path.dirname(filename)
|
904
|
+
file_name=os.path.basename(filename)
|
905
|
+
name, ext = os.path.splitext(file_name)
|
906
|
+
if type(suffix) is not str or force is True:
|
907
|
+
suffix='-XXXXXXXX'
|
908
|
+
num_type='.%0{}d'.format(len(suffix)-1)
|
909
|
+
if dir_name == '.':
|
910
|
+
dir_name=os.path.dirname(os.path.realpath(__file__))
|
911
|
+
elif dir_name == '':
|
912
|
+
dir_name=base_dir
|
913
|
+
def new_name(name,ext=None,ext2=None):
|
914
|
+
if ext:
|
915
|
+
if ext2:
|
916
|
+
return '{}{}{}'.format(name,ext,ext2)
|
917
|
+
return '{}{}'.format(name,ext)
|
918
|
+
if ext2:
|
919
|
+
return '{}{}'.format(name,ext2)
|
920
|
+
return name
|
921
|
+
def new_dest(dest_dir,name,ext=None,force=False):
|
922
|
+
if os.path.isdir(dest_dir) is False:
|
923
|
+
return False
|
924
|
+
i=0
|
925
|
+
new_file=new_name(name,ext)
|
926
|
+
while True:
|
927
|
+
rfile=os.path.join(dest_dir,new_file)
|
928
|
+
if force is False and os.path.exists(rfile) is False:
|
929
|
+
return rfile
|
930
|
+
force=False
|
931
|
+
if suffix:
|
932
|
+
if '0' in suffix or 'n' in suffix or 'N' in suffix:
|
933
|
+
if suffix[-1] not in ['0','n']:
|
934
|
+
new_file=new_name(name,num_type%i,ext)
|
935
|
+
else:
|
936
|
+
new_file=new_name(name,ext,num_type%i)
|
937
|
+
elif 'x' in suffix or 'X' in suffix:
|
938
|
+
rnd_str='.{}'.format(Random(length=len(suffix)-1,mode='str'))
|
939
|
+
if suffix[-1] not in ['X','x']:
|
940
|
+
new_file=new_name(name,rnd_str,ext)
|
941
|
+
else:
|
942
|
+
new_file=new_name(name,ext,rnd_str)
|
943
|
+
else:
|
944
|
+
if i == 0:
|
945
|
+
new_file=new_name(name,ext,'.{}'.format(suffix))
|
946
|
+
else:
|
947
|
+
new_file=new_name(name,ext,'.{}.{}'.format(suffix,i))
|
948
|
+
else:
|
949
|
+
new_file=new_name(name,ext,'.{}'.format(i))
|
950
|
+
i+=1
|
951
|
+
new_dest_file=new_dest(dir_name,name,ext,force=force)
|
952
|
+
if opt in ['file','f']:
|
953
|
+
os.mknode(new_dest_file)
|
954
|
+
elif opt in ['dir','d','directory']:
|
955
|
+
os.mkdir(new_dest_file)
|
956
|
+
else:
|
957
|
+
return new_dest_file
|
1247
958
|
|
1248
|
-
def
|
1249
|
-
if
|
1250
|
-
|
1251
|
-
|
1252
|
-
|
1253
|
-
|
1254
|
-
|
1255
|
-
|
1256
|
-
if
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1261
|
-
return rt
|
959
|
+
def SetIdentity(self,path,**opts):
|
960
|
+
if os.path.exists(path):
|
961
|
+
chmod=self.Mode(opts.get('mode',None))
|
962
|
+
uid=opts.get('uid',None)
|
963
|
+
gid=opts.get('gid',None)
|
964
|
+
atime=opts.get('atime',None)
|
965
|
+
mtime=opts.get('mtime',None)
|
966
|
+
try:
|
967
|
+
if chmod: os.chmod(path,int(chmod,base=8))
|
968
|
+
if uid and gid: os.chown(path,uid,gid)
|
969
|
+
if mtime and atime: os.utime(path,(atime,mtime)) # Time update must be at last order
|
970
|
+
except:
|
971
|
+
pass
|
1262
972
|
|
1263
|
-
def
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
973
|
+
def Extract(self,*path,**opts):
|
974
|
+
dest=opts.get('dest',None)
|
975
|
+
root_path=opts.get('root_path',None)
|
976
|
+
sub_dir=opts.get('sub_dir',False)
|
977
|
+
if IsNone(dest): return False
|
978
|
+
if not path:
|
979
|
+
self.ExtractRoot(root_path=self.FindRP(),dest=dest,sub_dir=sub_dir)
|
980
|
+
else:
|
981
|
+
for filepath in path:
|
982
|
+
fileRF=self.FindRP(filepath)
|
983
|
+
if isinstance(fileRF,tuple):
|
984
|
+
root_path=[fileRF[0]]
|
985
|
+
filename=fileRF[1]
|
986
|
+
self.ExtractRoot(root_path=root_path,dirpath=filename,dest=dest,sub_dir=sub_dir)
|
987
|
+
elif isinstance(fileRF,list):
|
988
|
+
self.ExtractRoot(root_path=fileRF,dest=dest,sub_dir=sub_dir)
|
1268
989
|
|
1269
|
-
|
1270
|
-
|
1271
|
-
|
1272
|
-
|
1273
|
-
|
1274
|
-
rt[' i ']['exist']=True
|
1275
|
-
rt[' i ']['size']=state.st_size
|
1276
|
-
rt[' i ']['mode']=oct(state.st_mode)[-4:]
|
1277
|
-
rt[' i ']['atime']=state.st_atime
|
1278
|
-
rt[' i ']['mtime']=state.st_mtime
|
1279
|
-
rt[' i ']['ctime']=state.st_ctime
|
1280
|
-
rt[' i ']['gid']=state.st_gid
|
1281
|
-
rt[' i ']['uid']=state.st_uid
|
1282
|
-
if opts: rt[' i '].update(opts)
|
990
|
+
def Save(self,filename):
|
991
|
+
pv=b'3'
|
992
|
+
if PyVer(2): pv=b'2'
|
993
|
+
#self.Rw(filename,data=pv+bz2.compress(pickle.dumps(self.info,protocol=2)))
|
994
|
+
self.Rw(filename,data=pv+Compress(pickle.dumps(self.info,protocol=2),mode='lz4'))
|
1283
995
|
|
1284
|
-
|
1285
|
-
|
1286
|
-
|
1287
|
-
|
1288
|
-
|
1289
|
-
|
1290
|
-
|
1291
|
-
|
1292
|
-
|
1293
|
-
|
1294
|
-
|
996
|
+
def Open(self,filename):
|
997
|
+
if not os.path.isfile(filename):
|
998
|
+
print('{} not found'.format(filename))
|
999
|
+
return False
|
1000
|
+
data=self.Rw(filename)
|
1001
|
+
if data[0]:
|
1002
|
+
pv=data[1][0]
|
1003
|
+
if pv == '3' and PyVer(2):
|
1004
|
+
print('The data version is not matched. Please use Python3')
|
1005
|
+
return False
|
1006
|
+
# decompress data
|
1007
|
+
try:
|
1008
|
+
#dcdata=bz2.BZ2Decompressor().decompress(data[1][1:])
|
1009
|
+
dcdata=Decompress(data[1][1:],mode='lz4')
|
1010
|
+
except:
|
1011
|
+
print('This is not KFILE format')
|
1012
|
+
return False
|
1013
|
+
try:
|
1014
|
+
self.info=pickle.loads(dcdata) # Load data
|
1015
|
+
except:
|
1016
|
+
try:
|
1017
|
+
self.info=pickle.loads(dcdata,encoding='latin1') # Convert 2 to 3 format
|
1018
|
+
except:
|
1019
|
+
print('This is not KFILE format')
|
1020
|
+
return False
|
1021
|
+
else:
|
1022
|
+
print('Can not read {}'.format(filename))
|
1023
|
+
return False
|
1295
1024
|
|
1296
|
-
|
1297
|
-
|
1298
|
-
if
|
1299
|
-
|
1300
|
-
|
1301
|
-
|
1302
|
-
|
1303
|
-
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1307
|
-
|
1308
|
-
|
1309
|
-
|
1310
|
-
|
1311
|
-
|
1312
|
-
|
1313
|
-
elif os.path.isdir(tfilename): # it is a directory
|
1314
|
-
MkInfo(rt,tfilename,type='dir')
|
1315
|
-
elif os.path.isfile(tfilename): # it is a File
|
1316
|
-
name,ext=self.FileName(tfilename)
|
1317
|
-
_md5=None
|
1318
|
-
if data or md5sum: # MD5SUM or Data
|
1319
|
-
filedata=self.Rw(tfilename,out='byte')
|
1320
|
-
if filedata[0]:
|
1321
|
-
if data: rt['data']=filedata[1]
|
1322
|
-
if md5sum: _md5=md5(filedata[1])
|
1323
|
-
MkInfo(rt,filename=tfilename,type=self.FileType(tfilename),name=name,ext=ext,md5=_md5)
|
1324
|
-
else:
|
1325
|
-
MkInfo(rt,filename,exist=False)
|
1326
|
-
if base:
|
1327
|
-
return {root_path:base}
|
1328
|
-
return {}
|
1025
|
+
def Cd(self,data,path,sym='/'):
|
1026
|
+
if Type(data,'module') and data == os:
|
1027
|
+
if isinstance(path,str):
|
1028
|
+
data.chdir(path)
|
1029
|
+
return data
|
1030
|
+
else:
|
1031
|
+
if isinstance(path,int): path='{}'.format(path)
|
1032
|
+
for ii in path.split(sym):
|
1033
|
+
if isinstance(data,dict):
|
1034
|
+
if ii in data:
|
1035
|
+
data=data[ii]
|
1036
|
+
elif isinstance(data,(list,tuple)):
|
1037
|
+
if not isinstance(ii,str) or not ii.isdigit(): continue
|
1038
|
+
ii=int(ii)
|
1039
|
+
if len(data) > ii:
|
1040
|
+
data=data[ii]
|
1041
|
+
return data
|
1329
1042
|
|
1330
|
-
def
|
1331
|
-
if
|
1332
|
-
|
1333
|
-
|
1334
|
-
|
1335
|
-
for ii in name.split('/'):
|
1336
|
-
if ii not in rt: break
|
1337
|
-
rt=rt[ii]
|
1338
|
-
fileinfo=rt.get(' i ',{})
|
1339
|
-
if fileinfo: return fileinfo
|
1340
|
-
return False
|
1043
|
+
def Path(self,filename=None):
|
1044
|
+
if filename:
|
1045
|
+
return os.path.dirname(os.path.realpath(filename))
|
1046
|
+
return os.path.dirname(os.path.realpath((inspect.stack()[-1])[1]))
|
1047
|
+
#if '__file__' in globals() : return os.path.dirname(os.path.realpath(__file__))
|
1341
1048
|
|
1342
|
-
def
|
1343
|
-
if
|
1344
|
-
|
1345
|
-
|
1346
|
-
|
1347
|
-
if
|
1348
|
-
|
1349
|
-
|
1350
|
-
|
1351
|
-
if ii == ' i ': continue
|
1352
|
-
if rt[ii].get(' i ',{}).get('type') == 'dir':
|
1353
|
-
print(ii+'/')
|
1354
|
-
else:
|
1355
|
-
print(ii)
|
1356
|
-
return False
|
1049
|
+
def Rm(self,filelist):
|
1050
|
+
if isinstance(filelist,str):
|
1051
|
+
filelist=filelist.split(',')
|
1052
|
+
if isinstance(filelist,(list,tuple)):
|
1053
|
+
for ii in list(filelist):
|
1054
|
+
if os.path.isfile(ii):
|
1055
|
+
os.unlink(ii)
|
1056
|
+
else:
|
1057
|
+
print('not found {0}'.format(ii))
|
1357
1058
|
|
1358
|
-
|
1359
|
-
|
1360
|
-
|
1361
|
-
|
1362
|
-
|
1363
|
-
if name != root:
|
1364
|
-
rt=self.CdPath(rt,name)
|
1365
|
-
if isinstance(rt,dict):
|
1366
|
-
for ii in rt:
|
1367
|
-
if ii == ' i ': continue
|
1368
|
-
if rt[ii].get(' i ',{}).get('type') == 'dir': continue
|
1369
|
-
print(ii)
|
1059
|
+
def IsJson(src):
|
1060
|
+
try:
|
1061
|
+
json.loads(src)
|
1062
|
+
return True
|
1063
|
+
except:
|
1370
1064
|
return False
|
1371
1065
|
|
1372
|
-
|
1373
|
-
|
1374
|
-
|
1375
|
-
|
1376
|
-
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
if exist['type'] in ['elf'] and exist['mode'] == 33261:return filename
|
1381
|
-
if self.Extract(filename,work_path=work_path):
|
1382
|
-
if bin_name:
|
1383
|
-
rt=[]
|
1384
|
-
for ff in self.Find(work_path,filename=bin_name):
|
1385
|
-
if self.Info(ff).get('mode') == 33261:
|
1386
|
-
rt.append(ff)
|
1387
|
-
return rt
|
1388
|
-
else:
|
1389
|
-
if find_executable(filename): return filename
|
1390
|
-
return default
|
1391
|
-
|
1392
|
-
def Basename(self,filename,default=False):
|
1393
|
-
if isinstance(filename,str):return os.path.basename(filename)
|
1394
|
-
return default
|
1395
|
-
|
1396
|
-
def Dirname(self,filename,bin_name=None,default=False):
|
1397
|
-
if not isinstance(filename,str): return default
|
1398
|
-
if IsNone(bin_name): return os.path.dirname(filename)
|
1399
|
-
if not isinstance(bin_name,str): return default
|
1400
|
-
bin_info=bin_name.split('/')
|
1401
|
-
bin_n=len(bin_info)
|
1402
|
-
filename_info=filename.split('/')
|
1403
|
-
filename_n=len(filename_info)
|
1404
|
-
for ii in range(0,bin_n):
|
1405
|
-
if filename_info[filename_n-1-ii] != bin_info[bin_n-1-ii]: return default
|
1406
|
-
#return '/'.join(filename_info[:-bin_n])
|
1407
|
-
return Join(filename_info[:-bin_n],symbol='/')
|
1408
|
-
|
1409
|
-
def Find(self,filename,default=[]):
|
1410
|
-
if not isinstance(filename,str): return default
|
1411
|
-
filename=os.path.basename(filename)
|
1412
|
-
if os.path.isdir(self.root_path):
|
1413
|
-
rt = []
|
1414
|
-
for base, dirs, files in os.walk(self.root_path):
|
1415
|
-
found = fnmatch.filter(files, filename)
|
1416
|
-
rt.extend(os.path.join(base, f) for f in found)
|
1417
|
-
return rt
|
1418
|
-
return default
|
1419
|
-
|
1420
|
-
# def Decompress(self,filename,work_path='/tmp',info={},del_org_file=False):
|
1421
|
-
# if not info and isinstance(filename,str) and os.path.isfile(filename): info=self.Get(filename)
|
1422
|
-
# filetype=info.get('type',None)
|
1423
|
-
# fileext=info.get('ext',None)
|
1424
|
-
# if filetype and fileext:
|
1425
|
-
# # Tar stuff
|
1426
|
-
# if fileext in ['tgz','tar','tar.gz','tar.bz2','tar.xz'] and filetype in ['gzip','tar','bzip2','lzma','xz','bz2']:
|
1427
|
-
# tf=tarfile.open(filename)
|
1428
|
-
# tf.extractall(work_path)
|
1429
|
-
# tf.close()
|
1430
|
-
# elif fileext in ['zip'] and filetype in ['compress']:
|
1431
|
-
# with zipfile.ZipFile(filename,'r') as zf:
|
1432
|
-
# zf.extractall(work_path)
|
1433
|
-
# if del_org_file: os.unline(filename)
|
1434
|
-
# return True
|
1435
|
-
# return False
|
1066
|
+
def IsXml(src):
|
1067
|
+
firstLine=file_rw(src,out='string',read='firstline')
|
1068
|
+
if firstLine is False:
|
1069
|
+
filename_str=Str(src)
|
1070
|
+
if isinstance(filename_str,str):
|
1071
|
+
firstLine=filename_str.split('\n')[0]
|
1072
|
+
if isinstance(firstLine,str) and firstLine.split(' ')[0] == '<?xml': return True
|
1073
|
+
return False
|
1436
1074
|
|
1437
|
-
|
1438
|
-
|
1439
|
-
#if data is None: # Read from file
|
1440
|
-
if IsNone(data): # Read from file
|
1441
|
-
if os.path.isfile(name) or (not file_only and os.path.exists(name)):
|
1442
|
-
try:
|
1443
|
-
if read in ['firstread','firstline','first_line','head','readline']:
|
1444
|
-
with open(name,'rb') as f:
|
1445
|
-
data=f.readline()
|
1446
|
-
elif not file_only:
|
1447
|
-
data=os.open(name,os.O_RDONLY)
|
1448
|
-
os.close(data)
|
1449
|
-
else:
|
1450
|
-
with open(name,'rb') as f:
|
1451
|
-
data=f.read()
|
1452
|
-
if out in ['string','str']:
|
1453
|
-
return True,Str(data)
|
1454
|
-
else:
|
1455
|
-
return True,data
|
1456
|
-
except:
|
1457
|
-
pass
|
1458
|
-
if default == {'err'}:
|
1459
|
-
return False,'File({}) not found'.format(name)
|
1460
|
-
return False,default
|
1461
|
-
else: # Write to file
|
1462
|
-
file_path=os.path.dirname(name)
|
1463
|
-
if not file_path or os.path.isdir(file_path): # current dir or correct directory
|
1464
|
-
if append:
|
1465
|
-
with open(name,'ab') as f:
|
1466
|
-
f.write(Bytes(data))
|
1467
|
-
elif not file_only:
|
1468
|
-
try:
|
1469
|
-
f=os.open(name,os.O_RDWR)
|
1470
|
-
os.write(f,data)
|
1471
|
-
os.close(f)
|
1472
|
-
except:
|
1473
|
-
return False,None
|
1474
|
-
else:
|
1475
|
-
with open(name,'wb') as f:
|
1476
|
-
f.write(Bytes(data))
|
1477
|
-
if isinstance(finfo,dict) and finfo: self.SetIdentity(name,**finfo)
|
1478
|
-
#mode=self.Mode(mode)
|
1479
|
-
#if mode: os.chmod(name,int(mode,base=8))
|
1480
|
-
#if uid and gid: os.chown(name,uid,gid)
|
1481
|
-
#if mtime and atime: os.utime(name,(atime,mtime))# Time update must be at last order
|
1482
|
-
return True,None
|
1483
|
-
if default == {'err'}:
|
1484
|
-
return False,'Directory({}) not found'.format(file_path)
|
1485
|
-
return False,default
|
1486
|
-
if default == {'err'}:
|
1487
|
-
return False,'Unknown type({}) filename'.format(name)
|
1488
|
-
return False,default
|
1075
|
+
def IsBin(src):
|
1076
|
+
return find_executable(src)
|
1489
1077
|
|
1490
|
-
|
1491
|
-
|
1492
|
-
|
1493
|
-
|
1494
|
-
|
1495
|
-
|
1496
|
-
|
1497
|
-
|
1498
|
-
|
1499
|
-
|
1500
|
-
|
1501
|
-
|
1502
|
-
|
1503
|
-
|
1504
|
-
|
1505
|
-
|
1506
|
-
|
1507
|
-
|
1508
|
-
|
1509
|
-
|
1510
|
-
|
1511
|
-
|
1512
|
-
|
1513
|
-
|
1514
|
-
|
1515
|
-
|
1516
|
-
|
1517
|
-
|
1518
|
-
|
1519
|
-
|
1520
|
-
|
1521
|
-
|
1522
|
-
|
1523
|
-
|
1524
|
-
|
1525
|
-
|
1526
|
-
|
1527
|
-
|
1528
|
-
|
1529
|
-
|
1530
|
-
|
1531
|
-
|
1532
|
-
|
1533
|
-
|
1534
|
-
|
1535
|
-
|
1536
|
-
|
1537
|
-
|
1538
|
-
elif 511 >= val > 63: #mask
|
1539
|
-
return _mode_(oct(val),mode) # to ocal number(oct(val))
|
1540
|
-
else:
|
1541
|
-
return _mode_('%04d'%(val),mode) # to ocal number(oct(val))
|
1542
|
-
else:
|
1543
|
-
val=Str(val,default=None)
|
1544
|
-
if isinstance(val,str):
|
1545
|
-
val_len=len(val)
|
1546
|
-
num=Int(val,default=None)
|
1547
|
-
if isinstance(num,int):
|
1548
|
-
if 3 <= len(val) <=4 and 100 <= num <= 777: #string type of permission number(octal number)
|
1549
|
-
return _mode_('%04d'%(num),mode)
|
1078
|
+
def IsPickle(src):
|
1079
|
+
if isinstance(src,str) and os.path.isfile(src):
|
1080
|
+
try:
|
1081
|
+
with open(src,'rb') as f: # Pickle Type
|
1082
|
+
pickle.load(f)
|
1083
|
+
return True
|
1084
|
+
except:
|
1085
|
+
pass
|
1086
|
+
return False
|
1087
|
+
|
1088
|
+
class LOG:
|
1089
|
+
def __init__(self,**opts):
|
1090
|
+
self.limit=opts.get('limit',3)
|
1091
|
+
self.dbg_level=opts.get('dbg_level',None)
|
1092
|
+
self.path=opts.get('path','/tmp')
|
1093
|
+
self.log_file=opts.get('log_file',None)
|
1094
|
+
self.info_file=opts.get('info_file',None)
|
1095
|
+
self.error_file=opts.get('error_file',None)
|
1096
|
+
self.dbg_file=opts.get('dbg_file',None)
|
1097
|
+
self.screen=opts.get('screen',False)
|
1098
|
+
self.date_format=opts.get('date_format','[%m/%d/%Y %H:%M:%S]')
|
1099
|
+
|
1100
|
+
def Format(self,*msg,**opts):
|
1101
|
+
log_date_format=opts.get('date_format',self.date_format)
|
1102
|
+
func_name=opts.get('func_name',None)
|
1103
|
+
end_new_line=opts.get('end_new_line','')
|
1104
|
+
start_new_line=opts.get('start_new_line','\n')
|
1105
|
+
if len(msg) > 0:
|
1106
|
+
m_str=None
|
1107
|
+
intro=''
|
1108
|
+
intro_space=''
|
1109
|
+
if log_date_format:
|
1110
|
+
intro=TIME().Format(tformat=log_date_format)+' '
|
1111
|
+
func_name_name=type(func_name).__name__
|
1112
|
+
if func_name_name == 'str':
|
1113
|
+
intro=intro+'{0} '.format(func_name)
|
1114
|
+
elif func_name is True:
|
1115
|
+
intro=intro+'{0}() '.format(CallerName())
|
1116
|
+
elif func_name_name in ['function','instancemethod']:
|
1117
|
+
intro=intro+'{0}() '.format(func_name.__name__)
|
1118
|
+
if intro:
|
1119
|
+
for i in range(0,len(intro)):
|
1120
|
+
intro_space=intro_space+' '
|
1121
|
+
for m in list(msg):
|
1122
|
+
n=m.split('\n')
|
1123
|
+
#if m_str is None:
|
1124
|
+
if IsNone(m_str):
|
1125
|
+
m_str='{0}{1}{2}{3}'.format(start_new_line,intro,n[0],end_new_line)
|
1550
1126
|
else:
|
1551
|
-
|
1552
|
-
|
1553
|
-
|
1554
|
-
|
1555
|
-
else:
|
1556
|
-
StdErr('Bad permission length')
|
1557
|
-
return default
|
1558
|
-
if not all(val[k] in 'rw-' for k in [0,1,3,4,6,7]):
|
1559
|
-
StdErr('Bad permission format (read-write)')
|
1560
|
-
return default
|
1561
|
-
if not all(val[k] in 'xs-' for k in [2,5]):
|
1562
|
-
StdErr('Bad permission format (execute)')
|
1563
|
-
return default
|
1564
|
-
if val[8] not in 'xt-':
|
1565
|
-
StdErr( 'Bad permission format (execute other)')
|
1566
|
-
return default
|
1567
|
-
m = 0
|
1568
|
-
if val[0] == 'r': m |= stat.S_IRUSR
|
1569
|
-
if val[1] == 'w': m |= stat.S_IWUSR
|
1570
|
-
if val[2] == 'x': m |= stat.S_IXUSR
|
1571
|
-
if val[2] == 's': m |= stat.S_IXUSR | stat.S_ISUID
|
1127
|
+
m_str='{0}{1}{2}{3}{4}'.format(m_str,start_new_line,intro_space,n[0],end_new_line)
|
1128
|
+
for nn in n[1:]:
|
1129
|
+
m_str='{0}{1}{2}{3}{4}'.format(m_str,start_new_line,intro_space,nn,end_new_line)
|
1130
|
+
return m_str
|
1572
1131
|
|
1573
|
-
|
1574
|
-
|
1575
|
-
if
|
1576
|
-
#
|
1577
|
-
|
1578
|
-
if
|
1579
|
-
|
1580
|
-
|
1581
|
-
|
1582
|
-
|
1132
|
+
def Syslogd(self,*msg,**opts):
|
1133
|
+
syslogd=opts.get('syslogd',None)
|
1134
|
+
if syslogd:
|
1135
|
+
#syslog_msg=' '.join(msg)
|
1136
|
+
syslog_msg=Join(msg,symbol=' ')
|
1137
|
+
if syslogd in ['INFO','info']:
|
1138
|
+
syslog.syslog(syslog.LOG_INFO,syslog_msg)
|
1139
|
+
elif syslogd in ['KERN','kern']:
|
1140
|
+
syslog.syslog(syslog.LOG_KERN,syslog_msg)
|
1141
|
+
elif syslogd in ['ERR','err']:
|
1142
|
+
syslog.syslog(syslog.LOG_ERR,syslog_msg)
|
1143
|
+
elif syslogd in ['CRIT','crit']:
|
1144
|
+
syslog.syslog(syslog.LOG_CRIT,syslog_msg)
|
1145
|
+
elif syslogd in ['WARN','warn']:
|
1146
|
+
syslog.syslog(syslog.LOG_WARNING,syslog_msg)
|
1147
|
+
elif syslogd in ['DBG','DEBUG','dbg','debug']:
|
1148
|
+
syslog.syslog(syslog.LOG_DEBUG,syslog_msg)
|
1583
1149
|
else:
|
1584
|
-
|
1585
|
-
else:
|
1586
|
-
val=Str(val,default=None)
|
1587
|
-
if isinstance(val,str):
|
1588
|
-
val_len=len(val)
|
1589
|
-
num=Int(val,default=None)
|
1590
|
-
if isinstance(num,int):
|
1591
|
-
if 3 <= len(val) <=4 and 100 <= num <= 777: #string type of permission number(octal number)
|
1592
|
-
return _mode_('%04d'%(num),mode)
|
1593
|
-
else:
|
1594
|
-
val_len=len(val)
|
1595
|
-
if 9<= val_len <=10:
|
1596
|
-
if val_len == 10 and val[0] in ['-','d','s']:
|
1597
|
-
val=val[1:]
|
1598
|
-
else:
|
1599
|
-
StdErr('Bad permission length')
|
1600
|
-
return default
|
1601
|
-
if not all(val[k] in 'rw-' for k in [0,1,3,4,6,7]):
|
1602
|
-
StdErr('Bad permission format (read-write)')
|
1603
|
-
return default
|
1604
|
-
if not all(val[k] in 'xs-' for k in [2,5]):
|
1605
|
-
StdErr('Bad permission format (execute)')
|
1606
|
-
return default
|
1607
|
-
if val[8] not in 'xt-':
|
1608
|
-
StdErr( 'Bad permission format (execute other)')
|
1609
|
-
return default
|
1610
|
-
m = 0
|
1611
|
-
if val[0] == 'r': m |= stat.S_IRUSR
|
1612
|
-
if val[1] == 'w': m |= stat.S_IWUSR
|
1613
|
-
if val[2] == 'x': m |= stat.S_IXUSR
|
1614
|
-
if val[2] == 's': m |= stat.S_IXUSR | stat.S_ISUID
|
1150
|
+
syslog.syslog(syslog_msg)
|
1615
1151
|
|
1616
|
-
|
1617
|
-
|
1618
|
-
|
1619
|
-
|
1620
|
-
|
1621
|
-
|
1622
|
-
|
1623
|
-
|
1624
|
-
elif
|
1625
|
-
|
1152
|
+
|
1153
|
+
def File(self,log_str,log_level,special_file=None):
|
1154
|
+
log_file=None
|
1155
|
+
if os.path.isdir(self.path):
|
1156
|
+
if (log_level in ['dbg','debug'] or (isinstance(log_level,int) and isinstance(self.dbg_level,int) and self.dbg_level <= log_level <= self.limit)) and isinstance(self.dbg_file,str):
|
1157
|
+
log_file=os.path.join(self.path,self.dbg_file)
|
1158
|
+
elif log_level in ['info'] and isinstance(self.info_file,str):
|
1159
|
+
log_file=os.path.join(self.path,self.info_file)
|
1160
|
+
elif log_level in ['error'] and isinstance(self.error_file,str):
|
1161
|
+
log_file=os.path.join(self.path,self.error_file)
|
1162
|
+
elif isinstance(self.log_file,str) or isinstance(special_file,str):
|
1163
|
+
if special_file:
|
1164
|
+
log_file=os.path.join(self.path,special_file)
|
1165
|
+
elif log_level in ['dbg','debug','info','error'] or (isinstance(log_level,int) and log_level <= self.limit):
|
1166
|
+
log_file=os.path.join(self.path,self.log_file)
|
1167
|
+
if log_file:
|
1168
|
+
with open(log_file,'a+') as f:
|
1169
|
+
f.write(log_str)
|
1170
|
+
return log_file
|
1171
|
+
|
1172
|
+
def Screen(self,log_str,log_level):
|
1173
|
+
if log_level in ['error']:
|
1174
|
+
sys.stderr.write(log_str)
|
1175
|
+
sys.stderr.flush()
|
1176
|
+
elif log_level <= self.limit:
|
1177
|
+
sys.stdout.write(log_str)
|
1178
|
+
sys.stdout.flush()
|
1179
|
+
|
1180
|
+
|
1181
|
+
def Log(self,*msg,**opts):
|
1182
|
+
direct=opts.get('direct',False)
|
1183
|
+
func_name=opts.get('func_name',None)
|
1184
|
+
date_format=opts.get('date_format','[%m/%d/%Y %H:%M:%S]')
|
1185
|
+
start_new_line=opts.get('start_new_line','\n')
|
1186
|
+
end_new_line=opts.get('end_new_line','')
|
1187
|
+
log_level=opts.get('log_level',3)
|
1188
|
+
special_file=opts.get('filename',None)
|
1189
|
+
screen=opts.get('screen',None)
|
1190
|
+
syslogd=opts.get('syslogd',None)
|
1191
|
+
if msg:
|
1192
|
+
# send log at syslogd
|
1193
|
+
self.Syslogd(*msg,syslogd=syslogd)
|
1194
|
+
|
1195
|
+
#if date_format in [False,None,'','no','ignore']:
|
1196
|
+
if IsNone(date_format,chk_val=[False,None,'','no','ignore']):
|
1197
|
+
date_format=None
|
1198
|
+
if IsNone(func_name,chk_val=[False,None,'','no','ignore']):
|
1199
|
+
func_name=None
|
1200
|
+
if direct:
|
1201
|
+
#log_str=' '.join(msg)
|
1202
|
+
log_str=Join(msg,symbol=' ')
|
1626
1203
|
else:
|
1627
|
-
|
1628
|
-
|
1629
|
-
|
1630
|
-
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1636
|
-
|
1637
|
-
|
1638
|
-
|
1639
|
-
|
1640
|
-
|
1641
|
-
|
1642
|
-
|
1643
|
-
|
1644
|
-
|
1645
|
-
|
1646
|
-
|
1647
|
-
|
1648
|
-
|
1649
|
-
|
1650
|
-
|
1651
|
-
|
1652
|
-
|
1653
|
-
|
1654
|
-
|
1655
|
-
if
|
1656
|
-
|
1657
|
-
|
1204
|
+
log_str=self.Format(*msg,func_name=func_name,date_format=date_format,end_new_line=end_new_line,start_new_line=start_new_line)
|
1205
|
+
|
1206
|
+
# Saving log at file
|
1207
|
+
log_file=self.File(log_str,log_level,special_file=special_file)
|
1208
|
+
|
1209
|
+
# print at screen
|
1210
|
+
if screen is True or (IsNone(screen) and self.screen is True):
|
1211
|
+
self.Screen(log_str,log_level)
|
1212
|
+
|
1213
|
+
# Send Log Data to logging function (self.log_file)
|
1214
|
+
#if log_file is None:
|
1215
|
+
if IsNone(log_file):
|
1216
|
+
self.Function(log_str)
|
1217
|
+
|
1218
|
+
def Function(self,*msg,**opts):
|
1219
|
+
if type(self.log_file).__name__ == 'function':
|
1220
|
+
log_func_arg=FunctionArgs(self.log_file,mode='all')
|
1221
|
+
if 'args' in log_func_arg or 'varargs' in log_func_arg:
|
1222
|
+
log_p=True
|
1223
|
+
args=log_func_arg.get('args',[])
|
1224
|
+
if args and len(args) <= 4 and ('direct' in args or 'log_level' in args or 'func_name' in args):
|
1225
|
+
tmp=[]
|
1226
|
+
for i in range(0,len(args)):
|
1227
|
+
tmp.append(i)
|
1228
|
+
if 'direct' in args:
|
1229
|
+
didx=args.index('direct')
|
1230
|
+
del tmp[didx]
|
1231
|
+
args[didx]=direct
|
1232
|
+
if 'log_level' in args:
|
1233
|
+
lidx=args.index('log_level')
|
1234
|
+
del tmp[lidx]
|
1235
|
+
args[lidx]=log_level
|
1236
|
+
if 'func_name' in args:
|
1237
|
+
lidx=args.index('func_name')
|
1238
|
+
del tmp[lidx]
|
1239
|
+
args[lidx]=func_name
|
1240
|
+
if 'date_format' in args:
|
1241
|
+
lidx=args.index('date_format')
|
1242
|
+
del tmp[lidx]
|
1243
|
+
args[lidx]=date_format
|
1244
|
+
args[tmp[0]]=log_str
|
1245
|
+
self.log_file(*args)
|
1246
|
+
elif 'keywards' in log_func_arg:
|
1247
|
+
self.log_file(log_str,direct=direct,log_level=log_level,func_name=func_name,date_format=date_format)
|
1248
|
+
elif 'defaults' in log_func_arg:
|
1249
|
+
if 'direct' in log_func_arg['defaults'] and 'log_level' in log_func_arg['defaults']:
|
1250
|
+
self.log_file(log_str,direct=direct,log_level=log_level)
|
1251
|
+
elif 'log_level' in log_func_arg['defaults']:
|
1252
|
+
self.log_file(log_str,log_level=log_level)
|
1253
|
+
elif 'direct' in log_func_arg['defaults']:
|
1254
|
+
self.log_file(log_str,direct=direct)
|
1255
|
+
else:
|
1256
|
+
self.log_file(log_str)
|
1257
|
+
else:
|
1258
|
+
self.log_file(log_str)
|
1658
1259
|
|
1659
|
-
|
1660
|
-
|
1661
|
-
|
1662
|
-
if val[5] == 's': m |= stat.S_IXGRP | stat.S_ISGID
|
1260
|
+
class HOST:
|
1261
|
+
def __init__(self):
|
1262
|
+
pass
|
1663
1263
|
|
1664
|
-
|
1665
|
-
|
1666
|
-
if val[8] == 'x': m |= stat.S_IXOTH
|
1667
|
-
if val[8] == 't': m |= stat.S_IXOTH | stat.S_ISVTX
|
1668
|
-
return _mode_(oct(m),mode)
|
1669
|
-
return default
|
1264
|
+
def Name(self):
|
1265
|
+
return socket.gethostname()
|
1670
1266
|
|
1267
|
+
def DefaultRouteDev(self,default=None,gw=None):
|
1268
|
+
for ii in Split(cat('/proc/net/route',no_edge=True),'\n',default=[]):
|
1269
|
+
ii_a=ii.split()
|
1270
|
+
#if len(ii_a) > 8 and '00000000' == ii_a[1] and '00000000' == ii_a[7]: return ii_a[0]
|
1271
|
+
if len(ii_a) < 4 or ii_a[1] != '00000000' or not int(ii_a[3], 16) & 2:
|
1272
|
+
#If not default route or not RTF_GATEWAY, skip it
|
1273
|
+
continue
|
1274
|
+
if gw:
|
1275
|
+
if IsSame(socket.inet_ntoa(struct.pack("<L", int(ii_a[2], 16))),gw):
|
1276
|
+
return ii_a[0]
|
1277
|
+
else:
|
1278
|
+
return ii_a[0]
|
1279
|
+
return default
|
1671
1280
|
|
1672
|
-
|
1673
|
-
|
1674
|
-
|
1675
|
-
|
1676
|
-
|
1677
|
-
|
1678
|
-
|
1679
|
-
return self.root_path
|
1680
|
-
aa='/'
|
1681
|
-
filename_a=filename.split('/')
|
1682
|
-
for ii in range(1,len(filename_a)):
|
1683
|
-
aa=Path(aa,filename_a[ii])
|
1684
|
-
if aa in info_keys:
|
1685
|
-
#remain_path='/'.join(filename_a[ii+1:])
|
1686
|
-
remain_path=Join(filename_a[ii+1:],symbol='/')
|
1687
|
-
if info_num == 1: return aa,remain_path
|
1688
|
-
# if info has multi root path then check filename in the db of each root_path
|
1689
|
-
if self.GetInfoFile(remain_path,aa): return aa,remain_path
|
1690
|
-
elif self.info:
|
1691
|
-
return list(self.info.keys())
|
1281
|
+
def DefaultRouteIp(self,default=None):
|
1282
|
+
for ii in Split(cat('/proc/net/route',no_edge=True),'\n'):
|
1283
|
+
ii_a=ii.split()
|
1284
|
+
if len(ii_a) < 4 or ii_a[1] != '00000000' or not int(ii_a[3], 16) & 2:
|
1285
|
+
#If not default route or not RTF_GATEWAY, skip it
|
1286
|
+
continue
|
1287
|
+
return socket.inet_ntoa(struct.pack("<L", int(ii_a[2], 16)))
|
1692
1288
|
return default
|
1693
|
-
|
1694
|
-
def ExtractRoot(self,**opts):
|
1695
|
-
root_path=opts.get('root_path',[])
|
1696
|
-
dirpath=opts.get('dirpath')
|
1697
|
-
sub_dir=opts.get('sub_dir',False)
|
1698
|
-
if isinstance(root_path,str):
|
1699
|
-
root_path=[root_path]
|
1700
|
-
#if not os.path.isdir(opts.get('dest')): os.makedirs(opts.get('dest'))
|
1701
|
-
if self.Mkdir(opts.get('dest'),force=True) is False: return False
|
1702
|
-
for rp in root_path:
|
1703
|
-
new_dest=opts.get('dest')
|
1704
|
-
if dirpath:
|
1705
|
-
rt=self.CdPath(self.info[rp],dirpath)
|
1706
|
-
if rt is False:
|
1707
|
-
print('{} not found'.format(dirpath))
|
1708
|
-
return
|
1709
|
-
else:
|
1710
|
-
dirpath=''
|
1711
|
-
rt=self.info[rp]
|
1712
1289
|
|
1713
|
-
|
1714
|
-
|
1715
|
-
|
1716
|
-
|
1717
|
-
mydest=os.path.dirname(dirpath)
|
1718
|
-
myname=os.path.basename(dirpath)
|
1719
|
-
if mydest:
|
1720
|
-
mydest=os.path.join(new_dest,mydest)
|
1721
|
-
else:
|
1722
|
-
mydest=new_dest
|
1723
|
-
#if not os.path.isdir(mydest): os.makedirs(mydest)
|
1724
|
-
if self.Mkdir(mydest,force=True,info=rinfo) is False: return False
|
1725
|
-
if rtype == 'link':
|
1726
|
-
os.symlink(rinfo['dest'],os.path.join(mydest,myname))
|
1727
|
-
self.SetIdentity(os.path.join(mydest,myname),**rinfo)
|
1728
|
-
else: # File
|
1729
|
-
if 'data' in rt: self.Rw(Path(mydest,myname),data=rt['data'],finfo=rinfo)
|
1730
|
-
else: print('{} file have no data'.format(dirpath))
|
1731
|
-
# self.SetIdentity(os.path.join(mydest,myname),**rinfo)
|
1732
|
-
else: # directory or root DB
|
1733
|
-
for ii in rt:
|
1734
|
-
if ii == ' i ': continue
|
1735
|
-
finfo=rt[ii].get(' i ',{})
|
1736
|
-
ftype=finfo.get('type')
|
1737
|
-
if ftype == 'dir':
|
1738
|
-
mydir=os.path.join(new_dest,ii)
|
1739
|
-
self.Mkdir(mydir,force=True,info=finfo)
|
1740
|
-
#self.SetIdentity(mydir,**finfo)
|
1741
|
-
# Sub directory
|
1742
|
-
if sub_dir: self.ExtractRoot(dirpath=os.path.join(dirpath,ii),root_path=rp,dest=os.path.join(new_dest,ii),sub_dir=sub_dir)
|
1743
|
-
#if dmtime and datime: os.utime(mydir,(datime,dmtime)) # Time update must be at last order
|
1744
|
-
elif ftype == 'link':
|
1745
|
-
iimm=os.path.join(new_dest,ii)
|
1746
|
-
if not os.path.exists(iimm):
|
1747
|
-
os.symlink(finfo['dest'],iimm)
|
1748
|
-
self.SetIdentity(iimm,**finfo)
|
1749
|
-
else: # File
|
1750
|
-
if 'data' in rt[ii]: self.Rw(os.path.join(new_dest,ii),data=rt[ii]['data'],finfo=finfo)
|
1751
|
-
else: print('{} file have no data'.format(ii))
|
1290
|
+
def Ip(self,ifname=None,mac=None,default=None):
|
1291
|
+
if IsNone(ifname):
|
1292
|
+
if IsNone(mac) : mac=self.Mac()
|
1293
|
+
ifname=self.DevName(mac)
|
1752
1294
|
|
1753
|
-
|
1754
|
-
|
1755
|
-
|
1756
|
-
if force:
|
1295
|
+
if ifname:
|
1296
|
+
if not os.path.isdir('/sys/class/net/{}'.format(ifname)):
|
1297
|
+
return default
|
1757
1298
|
try:
|
1758
|
-
|
1759
|
-
|
1299
|
+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
1300
|
+
return socket.inet_ntoa(fcntl.ioctl(
|
1301
|
+
s.fileno(),
|
1302
|
+
0x8915, # SIOCGIFADDR
|
1303
|
+
struct.pack('256s', ifname[:15])
|
1304
|
+
)[20:24])
|
1760
1305
|
except:
|
1761
|
-
|
1762
|
-
|
1306
|
+
try:
|
1307
|
+
return os.popen('ip addr show {}'.format(ifname)).read().split("inet ")[1].split("/")[0]
|
1308
|
+
except:
|
1309
|
+
return default
|
1310
|
+
return socket.gethostbyname(socket.gethostname())
|
1311
|
+
|
1312
|
+
def IpmiIp(self,default=None):
|
1313
|
+
rt=rshell('''ipmitool lan print 2>/dev/null| grep "IP Address" | grep -v Source | awk '{print $4}' ''')
|
1314
|
+
if rt[0]:return rt[1]
|
1315
|
+
return default
|
1316
|
+
|
1317
|
+
def IpmiMac(self,default=None):
|
1318
|
+
rt=rshell(""" ipmitool lan print 2>/dev/null | grep "MAC Address" | awk """ + """ '{print $4}' """)
|
1319
|
+
if rt[0]:return rt[1]
|
1320
|
+
return default
|
1321
|
+
|
1322
|
+
def Mac(self,ip=None,dev=None,default=None,ifname=None):
|
1323
|
+
#if dev is None and ifname: dev=ifname
|
1324
|
+
if IsNone(dev) and ifname: dev=ifname
|
1325
|
+
if IpV4(ip):
|
1326
|
+
dev_info=self.NetDevice()
|
1327
|
+
for dev in dev_info.keys():
|
1328
|
+
if self.Ip(ifname=dev) == ip:
|
1329
|
+
return dev_info[dev]['mac']
|
1330
|
+
#ip or anyother input of device then getting default gw's dev
|
1331
|
+
if IsNone(dev): dev=self.DefaultRouteDev()
|
1332
|
+
if dev:
|
1763
1333
|
try:
|
1764
|
-
|
1765
|
-
|
1334
|
+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
1335
|
+
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', Bytes(dev[:15])))
|
1336
|
+
return Join(['%02x' % ord(char) for char in Str(info[18:24])],symbol=':')
|
1766
1337
|
except:
|
1767
|
-
return
|
1768
|
-
return
|
1338
|
+
return default
|
1339
|
+
#return ':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff) for ele in range(0,8*6,8)][::-1])
|
1340
|
+
return MacV4('%012x' % uuid.getnode())
|
1769
1341
|
|
1770
|
-
def
|
1771
|
-
if IsNone(
|
1772
|
-
|
1773
|
-
|
1774
|
-
|
1775
|
-
|
1776
|
-
|
1777
|
-
|
1778
|
-
|
1779
|
-
|
1780
|
-
|
1781
|
-
|
1782
|
-
|
1783
|
-
|
1784
|
-
|
1785
|
-
|
1786
|
-
|
1787
|
-
|
1788
|
-
|
1789
|
-
|
1790
|
-
|
1791
|
-
|
1792
|
-
|
1793
|
-
|
1794
|
-
|
1795
|
-
|
1342
|
+
def DevName(self,mac=None,default=None):
|
1343
|
+
if IsNone(mac):
|
1344
|
+
mac=self.Mac()
|
1345
|
+
net_dir='/sys/class/net'
|
1346
|
+
if isinstance(mac,str) and os.path.isdir(net_dir):
|
1347
|
+
dirpath,dirnames,filenames = list(os.walk(net_dir))[0]
|
1348
|
+
for dev in dirnames:
|
1349
|
+
fmac=cat('{}/{}/address'.format(dirpath,dev),no_edge=True)
|
1350
|
+
if isinstance(fmac,str) and fmac.strip().lower() == mac.lower():
|
1351
|
+
return dev
|
1352
|
+
return default
|
1353
|
+
|
1354
|
+
def Info(self):
|
1355
|
+
return {
|
1356
|
+
'host_name':self.Name(),
|
1357
|
+
'host_ip':self.Ip(),
|
1358
|
+
'host_mac':self.Mac(),
|
1359
|
+
'ipmi_ip':self.IpmiIp(),
|
1360
|
+
'ipmi_mac':self.IpmiMac(),
|
1361
|
+
}
|
1362
|
+
|
1363
|
+
def NetDevice(self,name=None,default=False):
|
1364
|
+
def _dev_info_(path,name):
|
1365
|
+
drv=ls('{}/{}/device/driver/module/drivers'.format(path,name))
|
1366
|
+
if drv is False:
|
1367
|
+
drv='unknown'
|
1368
|
+
else:
|
1369
|
+
drv=drv[0].split(':')[1]
|
1370
|
+
return {
|
1371
|
+
'mac':cat('{}/{}/address'.format(path,name),no_end_newline=True),
|
1372
|
+
'duplex':cat('{}/{}/duplex'.format(path,name),no_end_newline=True,file_only=False),
|
1373
|
+
'mtu':cat('{}/{}/mtu'.format(path,name),no_end_newline=True),
|
1374
|
+
'state':cat('{}/{}/operstate'.format(path,name),no_end_newline=True),
|
1375
|
+
'speed':cat('{}/{}/speed'.format(path,name),no_end_newline=True,file_only=False),
|
1376
|
+
'id':cat('{}/{}/ifindex'.format(path,name),no_end_newline=True),
|
1377
|
+
'driver':drv,
|
1378
|
+
'drv_ver':cat('{}/{}/device/driver/module/version'.format(path,name),no_end_newline=True,file_only=False,default=''),
|
1379
|
+
}
|
1380
|
+
|
1381
|
+
|
1382
|
+
net_dev={}
|
1383
|
+
net_dir='/sys/class/net'
|
1384
|
+
if os.path.isdir(net_dir):
|
1385
|
+
dirpath,dirnames,filenames = list(os.walk(net_dir))[0]
|
1386
|
+
if name:
|
1387
|
+
if name in dirnames:
|
1388
|
+
net_dev[name]=_dev_info_(dirpath,name)
|
1389
|
+
else:
|
1390
|
+
for dev in dirnames:
|
1391
|
+
net_dev[dev]=_dev_info_(dirpath,dev)
|
1392
|
+
return net_dev
|
1393
|
+
return default
|
1394
|
+
|
1395
|
+
def Alive(self,ip,keep=20,interval=3,timeout=1800,default=False,log=None,cancel_func=None):
|
1396
|
+
time=TIME()
|
1397
|
+
run_time=time.Int()
|
1398
|
+
if IpV4(ip):
|
1399
|
+
if log:
|
1400
|
+
log('[',direct=True,log_level=1)
|
1796
1401
|
while True:
|
1797
|
-
|
1798
|
-
|
1799
|
-
|
1800
|
-
|
1801
|
-
if
|
1802
|
-
if
|
1803
|
-
|
1804
|
-
|
1805
|
-
|
1806
|
-
|
1807
|
-
|
1808
|
-
|
1809
|
-
|
1810
|
-
|
1811
|
-
|
1812
|
-
new_file=new_name(name,ext,rnd_str)
|
1813
|
-
else:
|
1814
|
-
if i == 0:
|
1815
|
-
new_file=new_name(name,ext,'.{}'.format(suffix))
|
1816
|
-
else:
|
1817
|
-
new_file=new_name(name,ext,'.{}.{}'.format(suffix,i))
|
1402
|
+
if time.Out(timeout_sec):
|
1403
|
+
if log:
|
1404
|
+
log(']\n',direct=True,log_level=1)
|
1405
|
+
return False,'Timeout monitor'
|
1406
|
+
if IsBreak(cancel_func):
|
1407
|
+
if log:
|
1408
|
+
log(']\n',direct=True,log_level=1)
|
1409
|
+
return True,'Stopped monitor by Custom'
|
1410
|
+
if ping(ip,cancel_func=cancel_func):
|
1411
|
+
if (time.Int() - run_time) > keep:
|
1412
|
+
if log:
|
1413
|
+
log(']\n',direct=True,log_level=1)
|
1414
|
+
return True,'OK'
|
1415
|
+
if log:
|
1416
|
+
log('-',direct=True,log_level=1)
|
1818
1417
|
else:
|
1819
|
-
|
1820
|
-
|
1821
|
-
|
1822
|
-
|
1823
|
-
|
1824
|
-
|
1825
|
-
|
1826
|
-
|
1827
|
-
return new_dest_file
|
1828
|
-
|
1829
|
-
def SetIdentity(self,path,**opts):
|
1830
|
-
if os.path.exists(path):
|
1831
|
-
chmod=self.Mode(opts.get('mode',None))
|
1832
|
-
uid=opts.get('uid',None)
|
1833
|
-
gid=opts.get('gid',None)
|
1834
|
-
atime=opts.get('atime',None)
|
1835
|
-
mtime=opts.get('mtime',None)
|
1836
|
-
try:
|
1837
|
-
if chmod: os.chmod(path,int(chmod,base=8))
|
1838
|
-
if uid and gid: os.chown(path,uid,gid)
|
1839
|
-
if mtime and atime: os.utime(path,(atime,mtime)) # Time update must be at last order
|
1840
|
-
except:
|
1841
|
-
pass
|
1418
|
+
run_time=time.Int()
|
1419
|
+
if log:
|
1420
|
+
log('.',direct=True,log_level=1)
|
1421
|
+
time.Sleep(interval)
|
1422
|
+
if log:
|
1423
|
+
log(']\n',direct=True,log_level=1)
|
1424
|
+
return False,'Timeout/Unknown issue'
|
1425
|
+
return default,'IP format error'
|
1842
1426
|
|
1843
|
-
def
|
1844
|
-
|
1845
|
-
|
1846
|
-
sub_dir=opts.get('sub_dir',False)
|
1847
|
-
if IsNone(dest): return False
|
1848
|
-
if not path:
|
1849
|
-
self.ExtractRoot(root_path=self.FindRP(),dest=dest,sub_dir=sub_dir)
|
1850
|
-
else:
|
1851
|
-
for filepath in path:
|
1852
|
-
fileRF=self.FindRP(filepath)
|
1853
|
-
if isinstance(fileRF,tuple):
|
1854
|
-
root_path=[fileRF[0]]
|
1855
|
-
filename=fileRF[1]
|
1856
|
-
self.ExtractRoot(root_path=root_path,dirpath=filename,dest=dest,sub_dir=sub_dir)
|
1857
|
-
elif isinstance(fileRF,list):
|
1858
|
-
self.ExtractRoot(root_path=fileRF,dest=dest,sub_dir=sub_dir)
|
1427
|
+
def Ping(self,ip,keep_good=10,timeout=3600):
|
1428
|
+
if IpV4(ip):
|
1429
|
+
return ping(ip,keep_good=keep_good,timeout=timeout)
|
1859
1430
|
|
1860
|
-
|
1861
|
-
|
1862
|
-
|
1863
|
-
|
1864
|
-
|
1431
|
+
class COLOR:
|
1432
|
+
def __init__(self,**opts):
|
1433
|
+
self.color_db=opts.get('color',{'blue': 34, 'grey': 30, 'yellow': 33, 'green': 32, 'cyan': 36, 'magenta': 35, 'white': 37, 'red': 31})
|
1434
|
+
self.bg_color_db=opts.get('bg',{'cyan': 46, 'white': 47, 'grey': 40, 'yellow': 43, 'blue': 44, 'magenta': 45, 'red': 41, 'green': 42})
|
1435
|
+
self.attr_db=opts.get('attr',{'reverse': 7, 'blink': 5,'concealed': 8, 'underline': 4, 'bold': 1})
|
1865
1436
|
|
1866
|
-
def
|
1867
|
-
|
1868
|
-
print('{} not found'.format(filename))
|
1869
|
-
return False
|
1870
|
-
data=self.Rw(filename)
|
1871
|
-
if data[0]:
|
1872
|
-
pv=data[1][0]
|
1873
|
-
if pv == '3' and PyVer(2):
|
1874
|
-
print('The data version is not matched. Please use Python3')
|
1875
|
-
return False
|
1876
|
-
# decompress data
|
1877
|
-
try:
|
1878
|
-
#dcdata=bz2.BZ2Decompressor().decompress(data[1][1:])
|
1879
|
-
dcdata=Decompress(data[1][1:],mode='lz4')
|
1880
|
-
except:
|
1881
|
-
print('This is not KFILE format')
|
1882
|
-
return False
|
1883
|
-
try:
|
1884
|
-
self.info=pickle.loads(dcdata) # Load data
|
1885
|
-
except:
|
1886
|
-
try:
|
1887
|
-
self.info=pickle.loads(dcdata,encoding='latin1') # Convert 2 to 3 format
|
1888
|
-
except:
|
1889
|
-
print('This is not KFILE format')
|
1890
|
-
return False
|
1891
|
-
else:
|
1892
|
-
print('Can not read {}'.format(filename))
|
1893
|
-
return False
|
1437
|
+
def Color_code(self,name,default=None):
|
1438
|
+
return self.color_db.get(name,default)
|
1894
1439
|
|
1895
|
-
def
|
1896
|
-
|
1897
|
-
if isinstance(path,str):
|
1898
|
-
data.chdir(path)
|
1899
|
-
return data
|
1900
|
-
else:
|
1901
|
-
if isinstance(path,int): path='{}'.format(path)
|
1902
|
-
for ii in path.split(sym):
|
1903
|
-
if isinstance(data,dict):
|
1904
|
-
if ii in data:
|
1905
|
-
data=data[ii]
|
1906
|
-
elif isinstance(data,(list,tuple)):
|
1907
|
-
if not isinstance(ii,str) or not ii.isdigit(): continue
|
1908
|
-
ii=int(ii)
|
1909
|
-
if len(data) > ii:
|
1910
|
-
data=data[ii]
|
1911
|
-
return data
|
1440
|
+
def Background_code(self,name,default=None):
|
1441
|
+
return self.color_db.get(name,default)
|
1912
1442
|
|
1913
|
-
def
|
1914
|
-
|
1915
|
-
return os.path.dirname(os.path.realpath(filename))
|
1916
|
-
return os.path.dirname(os.path.realpath((inspect.stack()[-1])[1]))
|
1917
|
-
#if '__file__' in globals() : return os.path.dirname(os.path.realpath(__file__))
|
1443
|
+
def Attr_code(self,name,default=None):
|
1444
|
+
return self.color_db.get(name,default)
|
1918
1445
|
|
1919
|
-
def
|
1920
|
-
|
1921
|
-
|
1922
|
-
|
1923
|
-
|
1924
|
-
|
1925
|
-
|
1926
|
-
|
1927
|
-
|
1446
|
+
def Get(self,color,mode='color',default=None):
|
1447
|
+
color_code=None
|
1448
|
+
if mode == 'color':
|
1449
|
+
color_code=self.Color_code(color,default=default)
|
1450
|
+
elif mode in ['background','bg']:
|
1451
|
+
color_code=self.Background_code(color,default=default)
|
1452
|
+
elif mode in ['attr','attribute']:
|
1453
|
+
color_code=self.Attr_code(color,default=default)
|
1454
|
+
return color_code
|
1455
|
+
def String(self,msg,color,bg=False,attr=False,mode='shell'):
|
1456
|
+
if mode in ['html','HTML']:
|
1457
|
+
if bg:
|
1458
|
+
return '''<p style="background-color: {}">{}</p>'''.format(format(color,msg))
|
1459
|
+
else:
|
1460
|
+
return '''<font color={}>{}</font>'''.format(color,msg)
|
1461
|
+
else:
|
1462
|
+
if bg:
|
1463
|
+
color_code=self.Get(color,mode='bg',default=None)
|
1464
|
+
elif attr:
|
1465
|
+
color_code=self.Get(color,mode='attr',default=None)
|
1466
|
+
else:
|
1467
|
+
color_code=self.Get(color,default=None)
|
1468
|
+
if IsNone(color_code):
|
1469
|
+
return msg
|
1470
|
+
if IsNone(os.getenv('ANSI_COLORS_DISABLED')):
|
1471
|
+
reset='''\033[0m'''
|
1472
|
+
fmt_msg='''\033[%dm%s'''
|
1473
|
+
msg=fmt_msg % (color_code,msg)
|
1474
|
+
return msg+reset
|
1928
1475
|
|
1929
1476
|
class EMAIL:
|
1930
1477
|
############################
|
@@ -2248,58 +1795,10 @@ class SCREEN:
|
|
2248
1795
|
|
2249
1796
|
####################################STRING##################################################
|
2250
1797
|
def cut_string(string,max_len=None,sub_len=None,new_line='\n',front_space=False,out_format=list):
|
2251
|
-
if
|
2252
|
-
|
2253
|
-
if not isinstance(max_len,int):
|
2254
|
-
if out_format in [str,'str','string']: return string
|
2255
|
-
return [string]
|
2256
|
-
|
2257
|
-
tmp=[]
|
2258
|
-
space=Space(max_len-sub_len) if isinstance(sub_len,int) else ''
|
2259
|
-
for ii in string.split(new_line):
|
2260
|
-
ll=Cut(ii,head_len=max_len,body_len=sub_len,out=list)
|
2261
|
-
tmp.append(Join(ll,new_line,append_front=space))
|
2262
|
-
if out_format in [str,'str','string']: return Join(tmp,new_line)
|
2263
|
-
return tmp
|
1798
|
+
front_space=0 if front_space is True else None
|
1799
|
+
return Cut(string,head_len=max_len,body_len=sub_len,new_line=new_line,front_space=front_space,out=out_format,newline_head=True)
|
2264
1800
|
|
2265
1801
|
####################################KEYS##################################################
|
2266
|
-
def Insert(src,*inps,**opts):
|
2267
|
-
start=opts.pop('at',0)
|
2268
|
-
default=opts.pop('default',False)
|
2269
|
-
err=opts.pop('err',False)
|
2270
|
-
force=opts.pop('force',False)
|
2271
|
-
uniq=opts.pop('uniq',False)
|
2272
|
-
if isinstance(src,(list,tuple,str)):
|
2273
|
-
tuple_out=False
|
2274
|
-
if isinstance(src,tuple) and force:
|
2275
|
-
src=list(src)
|
2276
|
-
tuple_out=True
|
2277
|
-
if uniq:
|
2278
|
-
new=[]
|
2279
|
-
for ii in inps:
|
2280
|
-
if ii not in src:
|
2281
|
-
new.append(ii)
|
2282
|
-
inps=tuple(new)
|
2283
|
-
if isinstance(at,str):
|
2284
|
-
if at in ['start','first']: src=list(inps)+src
|
2285
|
-
if at in ['end','last']: src=src+list(inps)
|
2286
|
-
elif len(src) == 0:
|
2287
|
-
src=list(inps)
|
2288
|
-
elif isinstance(start,int) and len(src) > start:
|
2289
|
-
src=src[:start]+list(inps)+src[start:]
|
2290
|
-
else:
|
2291
|
-
if err:
|
2292
|
-
return default
|
2293
|
-
src=src+list(inps)
|
2294
|
-
if tuple_out: return tuple(src)
|
2295
|
-
elif isinstance(src,dict):
|
2296
|
-
for ii in inps:
|
2297
|
-
if isinstance(ii,dict):
|
2298
|
-
src.update(ii)
|
2299
|
-
if opts:
|
2300
|
-
src.update(opts)
|
2301
|
-
return src
|
2302
|
-
|
2303
1802
|
def FirstKey(src,default=None):
|
2304
1803
|
return Next(src,default=default)
|
2305
1804
|
|
@@ -2350,52 +1849,6 @@ def check_value(src,find,idx=None):
|
|
2350
1849
|
return True
|
2351
1850
|
return False
|
2352
1851
|
|
2353
|
-
def Update(src,*inps,**opts):
|
2354
|
-
at=opts.pop('at',0)
|
2355
|
-
err=opts.pop('err',False)
|
2356
|
-
default=opts.pop('default',False)
|
2357
|
-
force=opts.pop('force',False)
|
2358
|
-
sym=opts.pop('sym',None)
|
2359
|
-
if isinstance(src,(list,tuple,str)):
|
2360
|
-
if isinstance(src,str) and sym: src=src.split(sym)
|
2361
|
-
tuple_out=False
|
2362
|
-
if isinstance(src,tuple) and force:
|
2363
|
-
src=list(src)
|
2364
|
-
tuple_out=True
|
2365
|
-
n=len(src)
|
2366
|
-
if n == 0:
|
2367
|
-
if err is True:
|
2368
|
-
return default
|
2369
|
-
else:
|
2370
|
-
src=list(inps)
|
2371
|
-
elif isinstance(at,int) and n > at:
|
2372
|
-
for i in range(0,len(inps)):
|
2373
|
-
if n > at+i:
|
2374
|
-
src[at+i]=inps[i]
|
2375
|
-
elif err is True:
|
2376
|
-
return default
|
2377
|
-
else:
|
2378
|
-
src=src+list(inps)[i:]
|
2379
|
-
break
|
2380
|
-
elif isinstance(at,(tuple,list)):
|
2381
|
-
if len(inps) == len(at):
|
2382
|
-
for i in range(0,len(at)):
|
2383
|
-
if isinstance(at[i],int) and n > at[i]:
|
2384
|
-
src[at[i]]=inps[i]
|
2385
|
-
elif err is True:
|
2386
|
-
return default
|
2387
|
-
else:
|
2388
|
-
src.append(inps[i])
|
2389
|
-
if tuple_out: return tuple(src)
|
2390
|
-
return src
|
2391
|
-
elif isinstance(src,dict):
|
2392
|
-
for ii in inps:
|
2393
|
-
if isinstance(ii,dict):
|
2394
|
-
src.update(ii)
|
2395
|
-
if opts:
|
2396
|
-
src.update(opts)
|
2397
|
-
return src
|
2398
|
-
|
2399
1852
|
def Keys(src,find=None,start=None,end=None,sym='\n',default=[],word=False,pattern=False,findall=False,out=None):
|
2400
1853
|
rt=[]
|
2401
1854
|
if isinstance(src,str,list,tuple) and find:
|
@@ -2546,19 +1999,6 @@ def Decompress(data,mode='lz4',work_path='/tmp',del_org_file=False,file_info={})
|
|
2546
1999
|
if del_org_file: os.unline(data)
|
2547
2000
|
return True
|
2548
2001
|
|
2549
|
-
def cat(filename,no_end_newline=False,no_edge=False,byte=False,newline='\n',no_first_newline=False,no_all_newline=False,file_only=True,default={'err'}):
|
2550
|
-
tmp=FILE().Rw(filename,file_only=file_only,default=default)
|
2551
|
-
tmp=Get(tmp,1)
|
2552
|
-
if no_edge:
|
2553
|
-
return STR(tmp).RemoveNewline(mode='edge',byte=byte,newline=newline)
|
2554
|
-
elif no_end_newline:
|
2555
|
-
return STR(tmp).RemoveNewline(mode='end',byte=byte,newline=newline)
|
2556
|
-
elif no_first_newline:
|
2557
|
-
return STR(tmp).RemoveNewline(mode='first',byte=byte,newline=newline)
|
2558
|
-
elif no_all_newline:
|
2559
|
-
return STR(tmp).RemoveNewline(mode='all',byte=byte,newline=newline)
|
2560
|
-
return tmp
|
2561
|
-
|
2562
2002
|
def ls(dirname,opt=''):
|
2563
2003
|
if not IsNone(dirname) and os.path.isdir(dirname):
|
2564
2004
|
dirlist=[]
|
@@ -2679,6 +2119,8 @@ def get_file(filename,**opts):
|
|
2679
2119
|
rc['inod']=fstat.st_ino
|
2680
2120
|
rc['mode']=oct(fstat.st_mode)[-4:]
|
2681
2121
|
rc['exist']=True
|
2122
|
+
file_path=os.path.dirname(in_filename)
|
2123
|
+
if file_path != '.': rc['path_mode']=oct(os.stat(file_path).st_mode)[-4:]
|
2682
2124
|
if os.path.islink(in_filename):
|
2683
2125
|
rc['link']=True
|
2684
2126
|
else:
|
@@ -2690,12 +2132,16 @@ def get_file(filename,**opts):
|
|
2690
2132
|
else:
|
2691
2133
|
rc['dir']=False
|
2692
2134
|
if md5sum or data:
|
2693
|
-
|
2694
|
-
|
2695
|
-
|
2696
|
-
|
2697
|
-
|
2698
|
-
|
2135
|
+
try:
|
2136
|
+
with open(in_filename,'rb') as f:
|
2137
|
+
fdata=f.read()
|
2138
|
+
if md5sum:
|
2139
|
+
rc['md5']=md5(fdata)
|
2140
|
+
if data:
|
2141
|
+
rc['data']=fdata
|
2142
|
+
except:
|
2143
|
+
print('Permission denied: {}'.format(in_filename))
|
2144
|
+
rc['exist']=False
|
2699
2145
|
return rc
|
2700
2146
|
|
2701
2147
|
rc={'exist':False,'includes':[]}
|
@@ -2704,55 +2150,65 @@ def get_file(filename,**opts):
|
|
2704
2150
|
if rc['dir']:
|
2705
2151
|
root_path=filename
|
2706
2152
|
real_filename=None
|
2707
|
-
|
2708
|
-
|
2709
|
-
|
2710
|
-
|
2711
|
-
|
2712
|
-
|
2713
|
-
|
2714
|
-
|
2715
|
-
|
2716
|
-
|
2717
|
-
|
2718
|
-
if include_sub_dir is False:
|
2719
|
-
break
|
2720
|
-
os.chdir(pwd)
|
2153
|
+
if include_dir:
|
2154
|
+
pwd=os.getcwd()
|
2155
|
+
os.chdir(root_path)
|
2156
|
+
for dirPath, subDirs, fileList in os.walk('.'):
|
2157
|
+
for sfile in fileList:
|
2158
|
+
curFile=os.path.join(dirPath.replace('./',''),sfile)
|
2159
|
+
if curFile != real_filename:
|
2160
|
+
rc['includes'].append(get_file_data(curFile,root_path))
|
2161
|
+
if include_sub_dir is False:
|
2162
|
+
break
|
2163
|
+
os.chdir(pwd)
|
2721
2164
|
return rc
|
2722
2165
|
|
2723
|
-
def save_file(data,dest,filename=None):
|
2166
|
+
def save_file(data,dest=None,filename=None,force=False):
|
2724
2167
|
# return data.Extract(dest=dest,sub_dir=True)
|
2725
|
-
if not isinstance(data,dict)
|
2726
|
-
if
|
2727
|
-
if
|
2728
|
-
|
2729
|
-
|
2730
|
-
|
2168
|
+
if not isinstance(data,dict): return False
|
2169
|
+
if not data.get('exist'): return False
|
2170
|
+
if isinstance(dest,str):
|
2171
|
+
if os.path.exists(dest) and not os.path.isdir(dest):
|
2172
|
+
printf('Already exist {}'.format(dest),dsp='e')
|
2173
|
+
return False
|
2174
|
+
elif os.path.isdir(dest) is False:
|
2175
|
+
os.system('mkdir -p {0}'.format(dest))
|
2731
2176
|
else:
|
2732
|
-
|
2733
|
-
|
2734
|
-
|
2735
|
-
|
2736
|
-
|
2737
|
-
|
2738
|
-
|
2739
|
-
|
2740
|
-
|
2741
|
-
|
2742
|
-
os.chmod(new_file,chmod_mode)
|
2743
|
-
if 'includes' in data and data['includes']: # If include directory or files
|
2744
|
-
for ii in data['includes']:
|
2177
|
+
dest=os.getcwd()
|
2178
|
+
if data.get('dir'):
|
2179
|
+
if os.path.exists(data['path']) and not os.path.isdir(data['path']):
|
2180
|
+
printf('Already exist {}'.format(dest),dsp='e')
|
2181
|
+
return False
|
2182
|
+
elif not os.path.isdir(data['path']):
|
2183
|
+
os.system('mkdir -p {0}'.format(data['path']))
|
2184
|
+
if data.get('mode'): os.chmod(data['path'],file_mode(data.get('mode')))
|
2185
|
+
# If include directory or files
|
2186
|
+
for ii in data.get('includes',[]):
|
2745
2187
|
if ii['path']:
|
2746
2188
|
sub_dir=os.path.join(dest,ii['path'])
|
2747
2189
|
else:
|
2748
2190
|
sub_dir='{}'.format(dest)
|
2749
|
-
if os.path.isdir(sub_dir)
|
2191
|
+
if not os.path.isdir(sub_dir):
|
2192
|
+
os.system('mkdir -p {}'.format(sub_dir))
|
2193
|
+
if ii.get('path_mode'): os.chmod(sub_file,file_mode(ii.get('path_mode')))
|
2750
2194
|
sub_file=os.path.join(sub_dir,ii['name'])
|
2751
2195
|
with open(sub_file,'wb') as f:
|
2752
2196
|
f.write(ii['data'])
|
2753
|
-
|
2754
|
-
|
2755
|
-
|
2197
|
+
if ii.get('mode'): os.chmod(sub_file,file_mode(ii.get('mode')))
|
2198
|
+
else:
|
2199
|
+
# if file then save
|
2200
|
+
if force is False and os.path.exists(dest) and not os.path.isdir(dest):
|
2201
|
+
printf('Already exist {}'.format(dest),dsp='e')
|
2202
|
+
return False
|
2203
|
+
if os.path.isdir(dest):
|
2204
|
+
new_file=os.path.join(dest,data['name'])
|
2205
|
+
else:
|
2206
|
+
new_file=dest
|
2207
|
+
with open(new_file,'wb') as f:
|
2208
|
+
f.write(data.get('data',''))
|
2209
|
+
chmod_mode=file_mode(data.get('mode'))
|
2210
|
+
if chmod_mode: os.chmod(new_file,chmod_mode)
|
2211
|
+
return True
|
2756
2212
|
|
2757
2213
|
#########################################################################
|
2758
2214
|
def error_exit(msg=None):
|
@@ -2962,147 +2418,96 @@ def find_usb_dev(size=None,max_size=None):
|
|
2962
2418
|
#Payload Channel : 1 (0x01)
|
2963
2419
|
#Payload Port : 623
|
2964
2420
|
|
2965
|
-
def net_send_data(sock,data,key='kg',enc=False,timeout=0,
|
2421
|
+
def net_send_data(sock,data,key='kg',enc=False,timeout=0,instant=False,log=None,err_scr=True):
|
2966
2422
|
# if close=True then just send data and close socket
|
2967
2423
|
# if close=False then it need close socket code
|
2968
2424
|
# ex)
|
2969
2425
|
# aa=net_send_data(sock,.....)
|
2970
2426
|
# if aa[0] is True: sock.close()
|
2971
|
-
|
2972
|
-
|
2973
|
-
|
2427
|
+
try:
|
2428
|
+
sock.sendall(packet_enc(data,key=key,enc=enc))
|
2429
|
+
if instant is True:
|
2430
|
+
sock.close()
|
2431
|
+
return True,'OK'
|
2432
|
+
except:
|
2433
|
+
if instant is True:
|
2434
|
+
if sock: sock.close()
|
2974
2435
|
if timeout > 0:
|
2975
|
-
sock.
|
2976
|
-
|
2977
|
-
|
2978
|
-
|
2979
|
-
if enc and key:
|
2980
|
-
# encode code here
|
2981
|
-
#enc_tf=Bytes('t') # Now not code here. So, everything to 'f'
|
2982
|
-
#pdata=encode(key,pdata)
|
2983
|
-
enc_tf=Bytes('f')
|
2984
|
-
else:
|
2985
|
-
enc_tf=Bytes('f')
|
2986
|
-
ndata=struct.pack('>IssI',len(pdata),data_type,enc_tf,nkey)+pdata
|
2987
|
-
try:
|
2988
|
-
sock.sendall(ndata)
|
2989
|
-
if close: sock.close()
|
2990
|
-
return True,'OK'
|
2991
|
-
except:
|
2992
|
-
if close:
|
2993
|
-
if sock: sock.close()
|
2994
|
-
if timeout > 0:
|
2995
|
-
#timeout=sock.gettimeout()
|
2996
|
-
if TIME().Int() - start_time > timeout-1:
|
2997
|
-
#Timeout
|
2998
|
-
return False,'Sending Socket Timeout'
|
2436
|
+
#timeout=sock.gettimeout()
|
2437
|
+
if TIME().Int() - start_time > timeout-1:
|
2438
|
+
#Timeout
|
2439
|
+
return False,'Sending Socket Timeout'
|
2999
2440
|
return False,'Sending Fail'
|
3000
2441
|
|
3001
|
-
def net_receive_data(sock,key='kg',progress=None,retry=0,retry_timeout=30,progress_msg=None):
|
2442
|
+
def net_receive_data(sock,key='kg',progress=None,retry=0,retry_timeout=30,progress_msg=None,log=None,err_scr=True):
|
3002
2443
|
# decode code here
|
3003
|
-
|
3004
|
-
buf = b''
|
3005
|
-
file_size_d=int('{0}'.format(count))
|
3006
|
-
#if progress: print('\n')
|
3007
|
-
tn=0
|
3008
|
-
newbuf=None
|
3009
|
-
while count:
|
3010
|
-
if progress:
|
3011
|
-
if progress_msg:
|
3012
|
-
StdOut('\r{} [ {} % ]'.format(progress_msg,int((file_size_d-count) / file_size_d * 100)))
|
3013
|
-
else:
|
3014
|
-
StdOut('\rDownloading... [ {} % ]'.format(int((file_size_d-count) / file_size_d * 100)))
|
3015
|
-
try:
|
3016
|
-
newbuf = sock.recv(count)
|
3017
|
-
except socket.error as e:
|
3018
|
-
if tn < retry:
|
3019
|
-
print("[ERROR] timeout value:{} retry: {}/{}\n{}".format(sock.gettimeout(),tn,retry,e))
|
3020
|
-
tn+=1
|
3021
|
-
TIME().Sleep(1)
|
3022
|
-
sock.settimeout(retry_timeout)
|
3023
|
-
continue
|
3024
|
-
if e == 'timed out':
|
3025
|
-
return 'timeout',e
|
3026
|
-
if not newbuf: return True,None #maybe something socket issue.
|
3027
|
-
buf += newbuf
|
3028
|
-
count -= len(newbuf)
|
3029
|
-
if progress:
|
3030
|
-
if progress_msg:
|
3031
|
-
StdOut('\r{} [ 100 % ]\n'.format(progress_msg))
|
3032
|
-
else:
|
3033
|
-
StdOut('\rDownloading... [ 100 % ]\n')
|
3034
|
-
return True,buf
|
3035
|
-
ok,head=recvall(sock,10)
|
2444
|
+
ok,size,data_type,enc=packet_head(sock)
|
3036
2445
|
if krc(ok,chk=True):
|
3037
|
-
if
|
3038
|
-
|
3039
|
-
|
3040
|
-
|
3041
|
-
|
3042
|
-
|
3043
|
-
|
3044
|
-
|
3045
|
-
|
3046
|
-
|
3047
|
-
|
3048
|
-
if st_head[2] == 't':
|
3049
|
-
# decode code here
|
3050
|
-
# data=decode(data)
|
3051
|
-
pass
|
3052
|
-
if data: return [st_head[1],pickle.loads(data)]
|
3053
|
-
return [True,None]
|
3054
|
-
else:
|
3055
|
-
return [ok,data]
|
3056
|
-
else:
|
3057
|
-
return [False,'Wrong key']
|
3058
|
-
return ['lost','Connection lost']
|
3059
|
-
return [ok,head]
|
3060
|
-
|
3061
|
-
def net_put_and_get_data(IP,data,PORT=8805,key='kg',timeout=3,try_num=1,try_wait=[0,5],progress=None,enc=False,upacket=None,SSLC=False,log=True,progress_msg=None):
|
2446
|
+
# File not found Error log size is 57. So if 57 then ignore progress
|
2447
|
+
if size == 57: progress=False
|
2448
|
+
data_ok,data=packet_receive_all(sock,size,progress=progress,progress_msg=progress_msg,log=log,retry=retry,err_scr=err_scr)
|
2449
|
+
if krc(data_ok,chk=True):
|
2450
|
+
real_data=packet_dec(data,enc,key=key)
|
2451
|
+
if real_data: return [data_type,real_data]
|
2452
|
+
return [True,None]
|
2453
|
+
return [data_ok,data]
|
2454
|
+
return [ok,size]
|
2455
|
+
|
2456
|
+
def net_put_and_get_data(IP,data,PORT=8805,key='kg',timeout=3,try_num=1,try_wait=[0,5],progress=None,enc=False,upacket=None,SSLC=False,progress_msg=None,instant=True,dbg=6,log=None,wait_time=3,err_scr=True):
|
3062
2457
|
sent=False,'Unknown issue'
|
3063
2458
|
for ii in range(0,try_num):
|
3064
2459
|
if upacket: # Update packet function for number of try information ([#/<total #>])
|
3065
2460
|
data=upacket('ntry',[ii+1,try_num],data)
|
3066
2461
|
start_time=TIME().Int()
|
3067
|
-
sock=net_get_socket(IP,PORT,timeout=timeout,SSLC=SSLC)
|
2462
|
+
ok,sock=net_get_socket(IP,PORT,timeout=timeout,SSLC=SSLC,log=log,err_scr=err_scr)
|
2463
|
+
if ok is False:
|
2464
|
+
if ii >= try_num-1:
|
2465
|
+
return ok,sock,sock
|
2466
|
+
printf('Can not get socket data [{}/{}], wait {}s'.format(ii+1,try_num,wait_time) if dbg < 4 else '.',log=log)
|
2467
|
+
TIME().Sleep(wait_time)
|
2468
|
+
continue
|
3068
2469
|
if try_num > 0:
|
3069
2470
|
rtry_wait=(timeout//try_num)+1
|
3070
2471
|
else:
|
3071
2472
|
rtry_wait=try_wait
|
3072
|
-
sent=False,'Unknown issue'
|
2473
|
+
sent=False,'Unknown issue',sock
|
3073
2474
|
try:
|
3074
|
-
sent=net_send_data(sock,data,key=key,enc=enc)
|
2475
|
+
sent=net_send_data(sock,data,key=key,enc=enc,log=log,err_scr=err_scr)
|
3075
2476
|
except:
|
3076
2477
|
os.system("""[ -f /tmp/.{0}.{1}.crt ] && rm -f /tmp/.{0}.{1}.crt""".format(IP,PORT))
|
2478
|
+
continue
|
3077
2479
|
if sent[0]:
|
3078
|
-
nrcd=net_receive_data(sock,key=key,progress=progress,progress_msg=progress_msg)
|
3079
|
-
return nrcd
|
2480
|
+
nrcd=net_receive_data(sock,key=key,progress=progress,progress_msg=progress_msg,log=log,err_scr=err_scr)
|
2481
|
+
return nrcd+[sock]
|
3080
2482
|
else:
|
3081
2483
|
if timeout >0:
|
3082
2484
|
if TIME().Int() - start_time >= timeout-1:
|
3083
|
-
return [False,'Socket Send Timeout']
|
2485
|
+
return [False,'Socket Send Timeout',sock]
|
3084
2486
|
#return [False,'Data protocol version mismatch']
|
3085
|
-
if sock:
|
2487
|
+
if sock and instant is True:
|
2488
|
+
sock.close()
|
2489
|
+
sock=None
|
3086
2490
|
if try_num > 1:
|
3087
|
-
|
3088
|
-
print('try send data ... [{}/{}]'.format(ii+1,try_num))
|
2491
|
+
printf('try send data ... [{}/{}]'.format(ii+1,try_num),log=log)
|
3089
2492
|
TIME().Sleep(try_wait)
|
3090
|
-
return [False,'Send fail({}) :\n{}'.format(sent[1],data)]
|
2493
|
+
return [False,'Send fail({}) :\n{}'.format(sent[1],data),sock]
|
3091
2494
|
|
3092
|
-
def net_get_socket(host,port,timeout=3,dbg=
|
2495
|
+
def net_get_socket(host,port,timeout=3,dbg=6,SSLC=False,log=None,err_scr=True): # host : Host name or IP
|
3093
2496
|
try:
|
3094
2497
|
af, socktype, proto, canonname, sa = socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM)[0]
|
3095
2498
|
except:
|
3096
|
-
|
3097
|
-
return False
|
2499
|
+
_e_='Can not get network informatin of {}:{}'.format(host,port)
|
2500
|
+
return [False,_e_]
|
3098
2501
|
try:
|
3099
2502
|
soc = socket.socket(af, socktype, proto)
|
3100
2503
|
if timeout > 0:
|
3101
2504
|
soc.settimeout(timeout)
|
3102
2505
|
except socket.error as msg:
|
3103
|
-
|
3104
|
-
|
2506
|
+
_e_='could not open socket of {0}:{1}\n{2}'.format(host,port,msg)
|
2507
|
+
printf(_e_,log=log,dsp='e' if err_scr else 'd')
|
2508
|
+
return [False,_e_]
|
3105
2509
|
###### SSL Wrap ######
|
2510
|
+
_e_=None
|
3106
2511
|
if SSLC:
|
3107
2512
|
for i in range(0,5):
|
3108
2513
|
icertfile='/tmp/.{}.{}.crt'.format(host,port)
|
@@ -3119,22 +2524,21 @@ def net_get_socket(host,port,timeout=3,dbg=0,SSLC=False): # host : Host name or
|
|
3119
2524
|
try:
|
3120
2525
|
soc=ssl.wrap_socket(soc,ca_certs=icertfile,cert_reqs=ssl.CERT_REQUIRED)
|
3121
2526
|
soc.connect((host,port))
|
3122
|
-
return soc
|
2527
|
+
return [True,soc]
|
3123
2528
|
except socket.error as msg:
|
3124
|
-
if
|
3125
|
-
print(msg)
|
2529
|
+
printf(msg,log=log,log_level=dbg,mode='e' if err_scr else 'd')
|
3126
2530
|
TIME().Sleep(1)
|
3127
2531
|
########################
|
3128
2532
|
else:
|
3129
2533
|
try:
|
3130
2534
|
soc.connect(sa)
|
3131
|
-
return soc
|
2535
|
+
return [True,soc]
|
3132
2536
|
except socket.error as msg:
|
3133
|
-
|
3134
|
-
|
3135
|
-
return False
|
2537
|
+
_e_='can not connect at {0}:{1}\n{2}'.format(host,port,msg)
|
2538
|
+
printf(_e_,log=log,log_level=dbg,dsp='e' if err_scr else 'd')
|
2539
|
+
return [False,_e_]
|
3136
2540
|
|
3137
|
-
def net_start_server(server_port,main_func_name,server_ip='',timeout=0,max_connection=10,log_file=None,certfile=None,keyfile=None):
|
2541
|
+
def net_start_server(server_port,main_func_name,server_ip='',timeout=0,max_connection=10,log_file=None,certfile=None,keyfile=None,log=None,err_scr=True):
|
3138
2542
|
ssoc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
3139
2543
|
ssoc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
3140
2544
|
if timeout > 0:
|
@@ -3142,10 +2546,10 @@ def net_start_server(server_port,main_func_name,server_ip='',timeout=0,max_conne
|
|
3142
2546
|
try:
|
3143
2547
|
ssoc.bind((server_ip, server_port))
|
3144
2548
|
except socket.error as msg:
|
3145
|
-
|
2549
|
+
printf('Bind failed. Error : {0}'.format(msg),log=log,mode='e' if err_scr else 'd',logfile=log_file)
|
3146
2550
|
os._exit(1)
|
3147
2551
|
ssoc.listen(max_connection)
|
3148
|
-
|
2552
|
+
printf('Start server for {0}:{1}'.format(server_ip,server_port),log=log,logfile=log_file,dsp='f')
|
3149
2553
|
# for handling task in separate jobs we need threading
|
3150
2554
|
while True:
|
3151
2555
|
conn, addr = ssoc.accept()
|
@@ -3157,10 +2561,10 @@ def net_start_server(server_port,main_func_name,server_ip='',timeout=0,max_conne
|
|
3157
2561
|
else:
|
3158
2562
|
Thread(target=main_func_name, args=(conn, ip, port, log_file)).start()
|
3159
2563
|
except:
|
3160
|
-
|
2564
|
+
printf('No more generate thread for client from {0}:{1}'.format(ip,port),dsp='e' if err_scr else 'd',log=log,logfile=log_file)
|
3161
2565
|
ssoc.close()
|
3162
2566
|
|
3163
|
-
def net_start_single_server(server_port,main_func_name,server_ip='',timeout=0,max_connection=10,log_file=None,certfile=None,keyfile=None):
|
2567
|
+
def net_start_single_server(server_port,main_func_name,server_ip='',timeout=0,max_connection=10,log_file=None,certfile=None,keyfile=None,log=None,err_scr=True):
|
3164
2568
|
ssoc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
3165
2569
|
ssoc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
3166
2570
|
if timeout > 0:
|
@@ -3168,10 +2572,10 @@ def net_start_single_server(server_port,main_func_name,server_ip='',timeout=0,ma
|
|
3168
2572
|
try:
|
3169
2573
|
ssoc.bind((server_ip, server_port))
|
3170
2574
|
except socket.error as msg:
|
3171
|
-
|
2575
|
+
printf('Bind failed. Error : {0}'.format(msg),log=log,dsp='e' if err_scr else 'd',logfile=log_file)
|
3172
2576
|
os._exit(1)
|
3173
2577
|
ssoc.listen(max_connection)
|
3174
|
-
|
2578
|
+
printf('Start server for {0}:{1}'.format(server_ip,server_port),log=log,dsp='e' if err_scr else 'd',logfile=log_file)
|
3175
2579
|
# for handling task in separate jobs we need threading
|
3176
2580
|
conn, addr = ssoc.accept()
|
3177
2581
|
ip, port = str(addr[0]), str(addr[1])
|
@@ -3326,36 +2730,33 @@ def cert_file(keyfile,certfile,C='US',ST='CA',L='San Jose',O='KGC',OU='KG',CN=No
|
|
3326
2730
|
return key_file,crt_file
|
3327
2731
|
return None,None
|
3328
2732
|
|
3329
|
-
def net_put_data(IP,data,PORT=8805,key='kg',timeout=3,try_num=1,try_wait=[1,10],progress=None,enc=False,upacket=None,dbg=
|
3330
|
-
sent=False,'Unknown issue'
|
2733
|
+
def net_put_data(IP,data,PORT=8805,key='kg',timeout=3,try_num=1,try_wait=[1,10],progress=None,enc=False,upacket=None,dbg=6,wait_time=3,SSLC=False,instant=True,log=None,err_scr=True):
|
2734
|
+
sent=False,'Unknown issue',None
|
3331
2735
|
for ii in range(0,try_num):
|
3332
2736
|
if upacket: # Update packet function for number of try information ([#/<total #>])
|
3333
2737
|
data=upacket('ntry',[ii+1,try_num],data)
|
3334
|
-
sock=net_get_socket(IP,PORT,timeout=timeout,dbg=dbg,SSLC=SSLC)
|
3335
|
-
|
3336
|
-
|
3337
|
-
|
3338
|
-
|
3339
|
-
else:
|
3340
|
-
sys.stdout.write('.')
|
3341
|
-
sys.stdout.flush()
|
2738
|
+
ok,sock=net_get_socket(IP,PORT,timeout=timeout,dbg=dbg,SSLC=SSLC,log=log,err_scr=err_scr)
|
2739
|
+
if ok is False:
|
2740
|
+
if ii >= try_num-1:
|
2741
|
+
return [ok,sock,sock]
|
2742
|
+
printf('Can not get socket data [{}/{}], wait {}s'.format(ii+1,try_num,wait_time) if dbg < 4 else '.',log=log)
|
3342
2743
|
TIME().Sleep(wait_time)
|
3343
2744
|
continue
|
3344
|
-
sent=False,'Unknown issue'
|
2745
|
+
sent=[False,'Unknown issue',sock]
|
3345
2746
|
try:
|
3346
|
-
sent=net_send_data(sock,data,key=key,enc=enc)
|
2747
|
+
sent=net_send_data(sock,data,key=key,enc=enc,log=log)
|
3347
2748
|
except:
|
3348
|
-
|
2749
|
+
printf('send fail, try again ... [{}/{}]'.format(ii+1,try_num),log=log)
|
3349
2750
|
if sent[0]:
|
3350
|
-
if sock:
|
2751
|
+
if sock and instant:
|
3351
2752
|
sock.close()
|
3352
|
-
|
2753
|
+
sock=None
|
2754
|
+
return [True,'sent',sock]
|
3353
2755
|
if try_num > 1:
|
3354
2756
|
wait_time=Random(length=0,strs=try_wait,mode='int')
|
3355
|
-
|
3356
|
-
print('try send data ... [{}/{}], wait {}s'.format(ii+1,try_num,wait_time))
|
2757
|
+
printf('try send data ... [{}/{}], wait {}s'.format(ii+1,try_num,wait_time),log=log,log_level=dbg)
|
3357
2758
|
TIME().Sleep(wait_time)
|
3358
|
-
return [False,'Send fail({}) :\n{}'.format(sent[1],data)]
|
2759
|
+
return [False,'Send fail({}) :\n{}'.format(sent[1],data),sock]
|
3359
2760
|
|
3360
2761
|
|
3361
2762
|
def encode(string):
|
@@ -3864,7 +3265,7 @@ def ipv4(ipaddr=None,chk=False):
|
|
3864
3265
|
return IpV4(ipaddr)
|
3865
3266
|
|
3866
3267
|
def ip_in_range(ip,start,end):
|
3867
|
-
return
|
3268
|
+
return IpV4(ip,pool=(start,end))
|
3868
3269
|
|
3869
3270
|
def string2data(src,default='org',want_type=None,spliter=None):
|
3870
3271
|
return TypeData(src,default,want_type,spliter)
|
@@ -3878,9 +3279,9 @@ def str2mac(mac,sym=':',case='lower',chk=False):
|
|
3878
3279
|
def is_mac4(mac=None,symbol=':',convert=True):
|
3879
3280
|
return True if MacV4(mac,symbol=symbol) else False
|
3880
3281
|
|
3881
|
-
def Wrap(src,
|
3282
|
+
def Wrap(src,fspace='',nspace='',space_mode='space',sym='\n',default=None,NFLT=False,out=str):
|
3882
3283
|
if isinstance(space,str): space=len(space)
|
3883
|
-
return
|
3284
|
+
return STR(src).Tap(fspace=fspace,nspace=nspace,new_line=sym,NFLT=NFLT,mode=space_mode,default=default,out=out)
|
3884
3285
|
|
3885
3286
|
def ddict(*inps,**opts):
|
3886
3287
|
return Dict(*inps,**opts)
|