71
71
72
72
SCAN_DAT_FILE = 'scaninfo'
73
73
SEARCH_DAT_FILE = 'searchinfo'
74
- SIGINT_FILE = 'signal'
74
+ SIGNAL_FILE = 'signal'
75
75
76
76
def get_dev_labes_dict ():
77
77
lsblk = subprocess_run (['lsblk' ,'-fJ' ],capture_output = True ,text = True )
@@ -211,8 +211,7 @@ def popen_lin(command,shell,stdin=DEVNULL):
211
211
212
212
def send_signal (subproc ,temp_dir ,kind = 0 ):
213
213
try :
214
- signal_file = sep .join ([temp_dir ,SIGINT_FILE ])
215
- #print(f'sending signal in file {signal_file}')
214
+ signal_file = sep .join ([temp_dir ,SIGNAL_FILE ])
216
215
217
216
temp_signal_file = signal_file + '_temp'
218
217
with open (temp_signal_file ,'w' ) as tsf :
@@ -304,19 +303,15 @@ def __init__(self,label='',scan_path=''):
304
303
305
304
#######################################################################
306
305
class LibrerRecord :
307
- def __init__ (self ,log , label = None ,scan_path = None ,file_path = None ):
306
+ def __init__ (self ,label = None ,scan_path = None ,file_path = None ):
308
307
self .header = Header (label ,scan_path )
309
308
310
309
self .filestructure = ()
311
310
self .customdata = []
312
311
self .filenames = []
313
312
314
- self .log = log
315
313
self .find_results = []
316
314
317
- self .info_line = ''
318
- #self.info_line_current = ''
319
-
320
315
self .abort_action = False
321
316
322
317
self .file_name = ''
@@ -335,9 +330,6 @@ def load(self,file_path):
335
330
self .file_path = file_path
336
331
self .file_name = basename (normpath (file_path ))
337
332
338
- #self.log.info('loading %s' % file_name)
339
- #TODO - problem w podprocesie
340
-
341
333
try :
342
334
with ZipFile (file_path , "r" ) as zip_file :
343
335
header_ser_compr = zip_file .read ('header' )
@@ -346,16 +338,12 @@ def load(self,file_path):
346
338
self .header .zipinfo ["header" ]= (asizeof (header_ser_compr ),asizeof (header_ser ),asizeof (self .header ))
347
339
348
340
if self .header .data_format_version != DATA_FORMAT_VERSION :
349
- message = f'loading "{ file_path } " error: incompatible data format version: { self .header .data_format_version } vs { DATA_FORMAT_VERSION } '
350
- self .log .error (message )
351
- return message
341
+ return f'loading "{ file_path } " error: incompatible data format version: { self .header .data_format_version } vs { DATA_FORMAT_VERSION } '
352
342
353
343
self .prepare_info ()
354
344
355
345
except Exception as e :
356
- message = f'loading "{ file_path } " error: "{ e } "'
357
- #self.log.error(message)
358
- return message
346
+ return f'loading "{ file_path } " error: "{ e } "'
359
347
360
348
return False
361
349
@@ -368,18 +356,12 @@ def save(self,print_func,file_path=None,compression_level=9):
368
356
369
357
self .file_path = file_path
370
358
371
- #self.info_line = f'saving {filename}'
372
-
373
- #self.log.info('saving %s' % file_path)
374
- #print_func(['save',f'saving {file_path}'])
375
-
376
359
self_header = self .header
377
360
378
361
self .header .compression_level = compression_level
379
362
380
363
with ZipFile (file_path , "w" ) as zip_file :
381
364
def compress_with_header_update_wrapp (data ,datalabel ):
382
- #self.info_line = f'compressing {datalabel}'
383
365
print_func (['save' ,f'compressing { datalabel } ' ],True )
384
366
compress_with_header_update (self .header ,data ,compression_level ,datalabel ,zip_file )
385
367
@@ -434,16 +416,11 @@ def scan_rec(self,print_func,abort_list,path, scan_like_data,filenames_set,check
434
416
if is_file :
435
417
self_header_ext_stats [ext ]+= 1
436
418
437
- #self.info_line_current = entry_name
438
-
439
- #print_func(('scan-line',entry_name))
440
-
441
419
try :
442
420
stat_res = stat (entry )
443
421
mtime = int (stat_res .st_mtime )
444
422
dev = stat_res .st_dev
445
423
except Exception as e :
446
- #self.log.error('stat error:%s', e )
447
424
print_func ( ('error' ,f'stat { entry_name } error:{ e } ' ) )
448
425
#size -1 <=> error, dev,in ==0
449
426
is_bind = False
@@ -501,16 +478,10 @@ def scan_rec(self,print_func,abort_list,path, scan_like_data,filenames_set,check
501
478
self_header .quant_folders += local_folder_folders_count
502
479
503
480
print_func ( ('scan' ,self_header .sum_size ,self_header .quant_files ,self_header .quant_folders ,path ) )
504
- #t_now = perf_counter()
505
- #if t_now>self.progress_update_time+1.0:
506
- #self.progress_update_time = t_now
507
481
508
482
except Exception as e :
509
- #self.log.error('scandir error:%s',e )
510
483
print_func ( ('error' , f'scandir { path } error:{ e } ' ) )
511
484
512
- #self.info_line_current = ''
513
-
514
485
return (local_folder_size_with_subtree + local_folder_size ,subitems )
515
486
516
487
def scan (self ,print_func ,abort_list ,cde_list ,check_dev = True ):
@@ -608,18 +579,19 @@ def extract_customdata(self,print_func,abort_list):
608
579
self_header = self .header
609
580
scan_path = self_header .scan_path
610
581
611
- #self.info_line = 'custom data extraction ...'
612
582
print_func ( ('info' ,'custom data extraction ...' ),True )
613
583
614
584
self_header .files_cde_quant = 0
615
585
self_header .files_cde_size = 0
616
586
self_header .files_cde_size_extracted = 0
617
587
self_header .files_cde_errors_quant = defaultdict (int )
618
588
self_header .files_cde_errors_quant_all = 0
619
- self_header .files_cde_quant_sum = len (self .customdata_pool )
620
-
589
+ files_cde_quant_sum = self_header .files_cde_quant_sum = len (self .customdata_pool )
590
+ files_cde_size_sum = self_header . files_cde_size_sum
621
591
cde_list = self .header .cde_list
622
592
593
+ print_func ( ('cdeinit' ,files_cde_quant_sum ,files_cde_size_sum ),True )
594
+
623
595
customdata_helper = {}
624
596
625
597
customdata_stats_size = defaultdict (int )
@@ -643,10 +615,10 @@ def threaded_cde(timeout_semi_list):
643
615
files_cde_size = 0
644
616
files_cde_size_extracted = 0
645
617
618
+ files_cde_errors_quant_all = 0
646
619
for (scan_like_list ,subpath ,rule_nr ,size ) in self .customdata_pool .values ():
647
620
648
621
self .killed = False
649
- #self.abort_action_single=False
650
622
651
623
time_start = perf_counter ()
652
624
if abort_list [0 ] : #wszystko
@@ -661,8 +633,8 @@ def threaded_cde(timeout_semi_list):
661
633
full_file_path = normpath (abspath (sep .join ([scan_path ,subpath ]))).replace ('/' ,sep )
662
634
command ,command_info = get_command (executable ,parameters ,full_file_path ,shell )
663
635
664
- info_line = f'{ full_file_path } ({ bytes_to_str (size )} )'
665
- print_func ( ('cde' ,info_line ,size , files_cde_size_extracted ,self_header . files_cde_errors_quant_all ,files_cde_quant ,self_header . files_cde_quant_sum , files_cde_size , self_header . files_cde_size_sum ) )
636
+ #print_func( ('cde', f'{full_file_path} ({bytes_to_str(size)})',size,files_cde_size_extracted,files_cde_errors_quant_all,files_cde_quant,files_cde_quant_sum,files_cde_size,files_cde_size_sum) )
637
+ print_func ( ('cde' ,f' { full_file_path } ( { bytes_to_str ( size ) } )' ,size ,files_cde_size_extracted ,files_cde_errors_quant_all ,files_cde_quant ,files_cde_size ) )
666
638
667
639
timeout_val = time ()+ timeout if timeout else None
668
640
#####################################
@@ -706,6 +678,7 @@ def threaded_cde(timeout_semi_list):
706
678
707
679
if returncode or self .killed or aborted :
708
680
files_cde_errors_quant [rule_nr ]+= 1
681
+ files_cde_errors_quant_all += 1
709
682
710
683
if not aborted :
711
684
files_cde_quant += 1
@@ -737,7 +710,7 @@ def threaded_cde(timeout_semi_list):
737
710
time_end_all = perf_counter ()
738
711
739
712
self_header .files_cde_errors_quant = files_cde_errors_quant
740
- self_header .files_cde_errors_quant_all = sum ( files_cde_errors_quant . values ())
713
+ self_header .files_cde_errors_quant_all = files_cde_errors_quant_all
741
714
742
715
self_header .files_cde_quant = files_cde_quant
743
716
self_header .files_cde_size = files_cde_size
@@ -910,8 +883,6 @@ def find_items(self,
910
883
filestructure = self .filestructure
911
884
912
885
search_progress = 0
913
- #search_progress_update_quant = 0
914
- #progress_update_time = perf_counter()
915
886
916
887
if cd_search_kind != 'dont' :
917
888
self .decompress_customdata ()
@@ -937,8 +908,6 @@ def find_items(self,
937
908
938
909
self_customdata = self .customdata
939
910
940
- #results_queue_put = results_queue.append
941
-
942
911
while search_list :
943
912
filestructure ,parent_path_components = search_list_pop ()
944
913
@@ -979,8 +948,6 @@ def find_items(self,
979
948
if name_func_to_call :
980
949
if name_func_to_call (name ):
981
950
print_func ( (search_progress ,size ,mtime ,* next_level ) )
982
- #search_progress_update_quant=0
983
- #progress_update_time = perf_counter()
984
951
985
952
if sub_data :
986
953
search_list_append ( (sub_data ,next_level ) )
@@ -1047,22 +1014,8 @@ def find_items(self,
1047
1014
continue
1048
1015
1049
1016
print_func ( (search_progress ,size ,mtime ,* next_level ) )
1050
- #search_progress_update_quant=0
1051
- #progress_update_time = perf_counter()
1052
-
1053
- #print_func((search_progress))
1054
-
1055
- #t_now = perf_counter()
1056
- #if t_now>progress_update_time+1.0:
1057
- # progress_update_time = t_now
1058
-
1059
- #if search_progress_update_quant>1024:
1060
- #search_progress_update_quant=0
1061
- #else:
1062
- # search_progress_update_quant+=1
1063
1017
1064
1018
print_func ( [search_progress ] )
1065
- #print_func(True)
1066
1019
1067
1020
def find_items_sort (self ,what ,reverse ):
1068
1021
if what == 'data' :
@@ -1288,7 +1241,6 @@ def __init__(self,db_dir,log):
1288
1241
self .db_dir = db_dir
1289
1242
self .log = log
1290
1243
self .info_line = 'init'
1291
- #self.info_line_current = ''
1292
1244
1293
1245
self .records_to_show = []
1294
1246
self .abort_action = False
@@ -1304,7 +1256,7 @@ def update_sorted(self):
1304
1256
self .records_sorted = sorted (self .records ,key = lambda x : x .header .creation_time )
1305
1257
1306
1258
def create (self ,label = '' ,scan_path = '' ):
1307
- new_record = LibrerRecord (self . log , label = label ,scan_path = scan_path )
1259
+ new_record = LibrerRecord (label = label ,scan_path = scan_path )
1308
1260
new_record .db_dir = self .db_dir
1309
1261
1310
1262
self .records .add (new_record )
@@ -1573,14 +1525,14 @@ def find_results_clean(self):
1573
1525
########################################################################################################################
1574
1526
def create_new_record (self ,temp_dir ,update_callback ):
1575
1527
self .log .info (f'create_new_record' )
1528
+ self_log_info = self .log .info
1529
+
1576
1530
1577
1531
new_file_path = sep .join ([self .db_dir ,f'rep.{ int (time ())} .dat' ])
1578
1532
1579
- #new_record_filename = str(int(time()) + .dat
1580
1533
command = self .record_exe ()
1581
1534
command .append ('create' )
1582
1535
command .append (new_file_path )
1583
- #command.append(settings_file)
1584
1536
command .append (temp_dir )
1585
1537
1586
1538
self .abort_action = False
@@ -1602,8 +1554,6 @@ def create_new_record(self,temp_dir,update_callback):
1602
1554
self .stdout_files_cde_size_sum = 0
1603
1555
1604
1556
def threaded_run (command ,results_semi_list ,info_semi_list ,processes_semi_list ):
1605
- #results_list_append = results_semi_list[0].find_results.append
1606
-
1607
1557
try :
1608
1558
subprocess = uni_popen (command ,stdin = PIPE )
1609
1559
except Exception as re :
@@ -1617,12 +1567,12 @@ def threaded_run(command,results_semi_list,info_semi_list,processes_semi_list):
1617
1567
1618
1568
while True :
1619
1569
if line := subprocess_stdout_readline ():
1570
+ line_strip = line .strip ()
1571
+ self_log_info (f'rec:{ line_strip } ' )
1620
1572
try :
1621
- #print(line)
1622
1573
if line [0 ]!= '#' :
1623
- val = json_loads (line . strip () )
1574
+ val = json_loads (line_strip )
1624
1575
1625
- self .info_line = val
1626
1576
kind = val [0 ]
1627
1577
if kind == 'stage' :
1628
1578
self .stage = val [1 ]
@@ -1637,25 +1587,20 @@ def threaded_run(command,results_semi_list,info_semi_list,processes_semi_list):
1637
1587
self .stdout_sum_size ,self .stdout_quant_files ,self .stdout_quant_folders ,self .stdout_info_line_current = val [1 :5 ]
1638
1588
1639
1589
elif self .stage == 1 : #cde
1640
- self .stdout_info_line_current = val [1 ]
1641
- self .stdout_cde_size = val [2 ]
1642
-
1643
- self .stdout_files_cde_size_extracted = val [3 ]
1644
- self .stdout_files_cde_errors_quant_all = val [4 ]
1645
- self .stdout_files_cde_quant = val [5 ]
1646
- self .stdout_files_cde_quant_sum = val [6 ]
1647
- self .stdout_files_cde_size = val [7 ]
1648
- self .stdout_files_cde_size_sum = val [8 ]
1649
-
1650
- self .stdout_info_line_current
1651
- self .stdout_cde_size
1652
-
1653
- #print(type(self.stdout_files_cde_size_extracted))
1654
- #print(type(self.stdout_files_cde_errors_quant_all))
1655
- #print(type(self.stdout_files_cde_quant))
1656
- #print(type(self.stdout_files_cde_quant_sum))
1657
- #print(type(self.stdout_files_cde_size))
1658
- #print(type(self.stdout_files_cde_size_sum))
1590
+ if val [0 ]== 'cdeinit' :
1591
+ #files_cde_quant_sum,files_cde_size_sum
1592
+ self .stdout_files_cde_quant_sum = val [1 ]
1593
+ self .stdout_files_cde_size_sum = val [2 ]
1594
+ elif val [0 ]== 'cde' :
1595
+ self .stdout_info_line_current = val [1 ]
1596
+ self .stdout_cde_size = val [2 ]
1597
+
1598
+ self .stdout_files_cde_size_extracted = val [3 ]
1599
+ self .stdout_files_cde_errors_quant_all = val [4 ]
1600
+ self .stdout_files_cde_quant = val [5 ]
1601
+ self .stdout_files_cde_size = val [6 ]
1602
+ else :
1603
+ self_log_info ('ERROR UNRECOGNIZED LINE' )
1659
1604
1660
1605
elif self .stage == 2 : #pack
1661
1606
self .stdout_info_line_current = val [1 ]
@@ -1666,10 +1611,11 @@ def threaded_run(command,results_semi_list,info_semi_list,processes_semi_list):
1666
1611
elif self .stage == 4 : #end
1667
1612
pass
1668
1613
else :
1669
- info_semi_list [0 ]= line . strip ()
1614
+ info_semi_list [0 ]= line_strip
1670
1615
except Exception as e :
1671
1616
print (f'threaded_run work error:{ e } line:{ line } ' )
1672
1617
info_semi_list [0 ]= f'threaded_run work error:{ e } line:{ line } '
1618
+ self_log_info (f'threaded_run work error:{ e } line:{ line } ' )
1673
1619
else :
1674
1620
if subprocess_poll () is not None :
1675
1621
break
@@ -1683,32 +1629,24 @@ def threaded_run(command,results_semi_list,info_semi_list,processes_semi_list):
1683
1629
job .start ()
1684
1630
job_is_alive = job .is_alive
1685
1631
1632
+ aborted = False
1686
1633
###########################################
1687
1634
while job_is_alive ():
1688
1635
subprocess = processes_semi_list [0 ]
1689
1636
if subprocess :
1690
1637
if self .abort_action :
1691
- self .info_line = 'Aborting ...'
1692
1638
send_signal (subprocess ,temp_dir ,0 )
1693
1639
self .abort_action = False
1640
+ aborted = True
1694
1641
if self .abort_action_single :
1695
- self .info_line = 'Aborting single ...'
1696
1642
send_signal (subprocess ,temp_dir ,1 )
1697
1643
self .abort_action_single = False
1644
+ sleep (0.1 )
1698
1645
1699
- #try:
1700
- # subprocess.kill()
1701
- #except Exception as ke:
1702
- # print('killing error:',ke)
1703
-
1704
- #break
1705
- sleep (0.01 )
1706
-
1707
- self .info_line = f'scanning czy costam'
1708
1646
job .join ()
1709
1647
###########################################
1710
1648
1711
- if not self . abort_action :
1649
+ if not aborted :
1712
1650
new_record = self .create ()
1713
1651
1714
1652
if res := new_record .load (new_file_path ) :
@@ -1717,7 +1655,6 @@ def threaded_run(command,results_semi_list,info_semi_list,processes_semi_list):
1717
1655
print (res )
1718
1656
else :
1719
1657
update_callback (new_record )
1720
- #self.records_to_show.append( (new_record,info_curr_quant,info_curr_size) )
1721
1658
1722
1659
return True
1723
1660
0 commit comments