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