Skip to content

Commit

Permalink
CDE executing in the shell depending on the user's choice
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrj committed Nov 26, 2023
1 parent a004cfc commit 6054884
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 30 deletions.
24 changes: 14 additions & 10 deletions src/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def __init__(self,label='',path=''):
self.files_cde_size_extracted = 0
self.files_cde_errors_quant = 0

cde_list = []
self.cde_list = []

self.creation_os,self.creation_host = f'{platform_system()} {platform_release()}',platform_node()

Expand Down Expand Up @@ -366,6 +366,7 @@ def scan(self,cde_list,check_dev=True):
self.customdata_pool_index = 0

if cde_list:
self.log.info('estimating CD pool')
self.info_line = f'estimating files pool for custom data extraction'
self.prepare_customdata_pool_rec(self.scan_data,[])

Expand Down Expand Up @@ -402,7 +403,7 @@ def prepare_customdata_pool_rec(self,scan_like_data,parent_path):
matched = False

rule_nr=-1
for expressions,use_smin,smin_int,use_smax,smax_int,executable,timeout,crc in cde_list:
for expressions,use_smin,smin_int,use_smax,smax_int,executable,shell,timeout,crc in cde_list:
if self.abort_action:
break
if matched:
Expand Down Expand Up @@ -461,7 +462,7 @@ def extract_customdata(self):
if self.abort_action:
break

expressions,use_smin,smin_int,use_smax,smax_int,executable,timeout,crc = cde_list[rule_nr]
expressions,use_smin,smin_int,use_smax,smax_int,executable,shell,timeout,crc = cde_list[rule_nr]

full_file_path = normpath(abspath(sep.join([scan_path,subpath]))).replace('/',sep)

Expand All @@ -475,7 +476,7 @@ def extract_customdata(self):

self.info_line_current = f'{subpath} ({bytes_to_str(size)})'

cd_ok,output = exe_run(cde_run_list,timeout)
cd_ok,output = exe_run(cde_run_list,shell,timeout)

if not cd_ok:
self_header.files_cde_errors_quant +=1
Expand Down Expand Up @@ -848,10 +849,13 @@ def prepare_info(self):
info_list.append(f'quant_folders:{fnumber(self_header.quant_folders)}')
info_list.append(f'sum_size:{bytes_to_str(self_header.sum_size)}')

if self_header.cde_list:
info_list.append('\nCDE rules (draft):')
for nr,single_cde in enumerate(self_header.cde_list):
info_list.append(str(nr) + ':' + str(single_cde))
try:
if self_header.cde_list:
info_list.append('\nCDE rules (draft):')
for nr,single_cde in enumerate(self_header.cde_list):
info_list.append(str(nr) + ':' + str(single_cde))
except:
pass

self.txtinfo = '\n'.join(info_list)

Expand Down Expand Up @@ -969,11 +973,11 @@ class LibrerCore:
records = set()
db_dir=''

def test_cde(self,executable,timeout,file_to_test):
def test_cde(self,executable,shell,timeout,file_to_test):
exe = Executor()
cde_run_list = executable + [file_to_test]

cd_ok,output = exe.run(cde_run_list,timeout)
cd_ok,output = exe.run(cde_run_list,shell,timeout)
exe.end()

return cd_ok,output
Expand Down
9 changes: 5 additions & 4 deletions src/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self):
self.run_thread=Thread(target=self.run_in_thread,daemon=True)
self.run_thread.start()

def run(self,command_list_to_execute,timeout=None):
def run(self,command_list_to_execute,shell,timeout=None):
#print('run',command_list_to_execute,timeout)

self.output = ''
Expand All @@ -51,10 +51,11 @@ def run(self,command_list_to_execute,timeout=None):
self.running = True
self.res_ok = True
self.killed = False
start = time()
error_message = ''

start = time()
self.shell = shell
self.command_list_to_execute = command_list_to_execute
error_message = ''

while self.running:
if timeout:
Expand Down Expand Up @@ -93,7 +94,7 @@ def run_in_thread(self):
output_list_append = output_list.append

try:
proc = Popen(self.command_list_to_execute, stdout=PIPE, stderr=STDOUT)
proc = Popen(self.command_list_to_execute, stdout=PIPE, stderr=STDOUT,shell=self.shell)
self.pid = proc.pid

proc_stdout_readline = proc.stdout.readline
Expand Down
65 changes: 49 additions & 16 deletions src/librer.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,13 @@ def use_checkbutton_mod(self,e):

if self.CDE_use_var_list[e].get():
self.executable_entry[e].configure(state='normal')
self.shell_checkbutton[e].configure(state='normal')
self.open_button[e].configure(state='normal')
self.timeout_entry[e].configure(state='normal')
self.test_button[e].configure(state='normal')
else:
self.executable_entry[e].configure(state='disabled')
self.shell_checkbutton[e].configure(state='disabled')
self.open_button[e].configure(state='disabled')
self.timeout_entry[e].configure(state='disabled')
self.test_button[e].configure(state='disabled')
Expand Down Expand Up @@ -592,17 +594,19 @@ def get_scan_dialog(self):
(lab_min := Label(cde_frame,text='Min\nSize',bg=self.bg_color,anchor='n',relief='groove',bd=2,width=3)).grid(row=0, column=1,sticky='news')
(lab_max := Label(cde_frame,text='Max\nSize',bg=self.bg_color,anchor='n',relief='groove',bd=2,width=3)).grid(row=0, column=2,sticky='news')
(lab_exec := Label(cde_frame,text='CD Extractor Executable',bg=self.bg_color,anchor='n',relief='groove',bd=2)).grid(row=0, column=3,sticky='news')
(lab_open := Label(cde_frame,text='',bg=self.bg_color,anchor='n')).grid(row=0, column=4,sticky='news')
(lab_timeout := Label(cde_frame,text='TO',bg=self.bg_color,anchor='n',relief='groove',bd=2,width=3)).grid(row=0, column=5,sticky='news')
(lab_test := Label(cde_frame,text='CD\nTest',bg=self.bg_color,anchor='n',relief='groove',bd=2)).grid(row=0, column=6,sticky='news')
(lab_use := Label(cde_frame,text='CD',bg=self.bg_color,anchor='n',relief='groove',bd=2)).grid(row=0, column=7,sticky='news')
(lab_crc := Label(cde_frame,text='CRC',bg=self.bg_color,anchor='n',relief='groove',bd=2)).grid(row=0, column=8,sticky='news')
(lab_shell := Label(cde_frame,text='Shell',bg=self.bg_color,anchor='n',relief='groove',bd=2)).grid(row=0, column=4,sticky='news')
(lab_open := Label(cde_frame,text='',bg=self.bg_color,anchor='n')).grid(row=0, column=5,sticky='news')
(lab_timeout := Label(cde_frame,text='TO',bg=self.bg_color,anchor='n',relief='groove',bd=2,width=3)).grid(row=0, column=6,sticky='news')
(lab_test := Label(cde_frame,text='CD\nTest',bg=self.bg_color,anchor='n',relief='groove',bd=2)).grid(row=0, column=7,sticky='news')
(lab_use := Label(cde_frame,text='CD',bg=self.bg_color,anchor='n',relief='groove',bd=2)).grid(row=0, column=8,sticky='news')
(lab_crc := Label(cde_frame,text='CRC',bg=self.bg_color,anchor='n',relief='groove',bd=2)).grid(row=0, column=9,sticky='news')

use_tooltip = "Mark to use CD Extractor"
mask_tooltip = "glob expresions separated by comma ','\ne.g. '*.7z, *.zip, *.gz'"
min_tooltip = "Minimum size of file\nto aplly CD extraction or crc\nmay be empty e.g. 10k"
max_tooltip = "Maximum size of file\nto aplly CD extraction or crc\nmay be empty e.g. '100MB'"
exec_tooltip = "Executable or batch script that will be run\nwith the file for extraction as last parameter.\nMay have other fixed parameters\nWill be executed with the full path of the scanned file\ne.g. '7z l', 'cat', 'my_extractor.sh', 'my_extractor.bat'"
shell_tooltip = "Execute in system shell"
open_tooltip = "Set executable file as Custom Data Extractor..."
timeout_tooltip = "Timeout limit in seconds for single CD extraction.\nAfter timeout executed process will be terminated\n\n'0' or no value means no timeout"
test_tooltip = "Test Custom Data Extractor\non single manually selected file ..."
Expand All @@ -615,17 +619,20 @@ def get_scan_dialog(self):
self_widget_tooltip(lab_min,min_tooltip)
self_widget_tooltip(lab_max,max_tooltip)
self_widget_tooltip(lab_exec,exec_tooltip)
self_widget_tooltip(lab_shell,shell_tooltip)
self_widget_tooltip(lab_open,open_tooltip)
self_widget_tooltip(lab_timeout,timeout_tooltip)
self_widget_tooltip(lab_test,test_tooltip)
self_widget_tooltip(lab_crc,crc_tooltip)

self.CDE_ENTRIES_MAX = 16

self.CDE_use_var_list = []
self.CDE_mask_var_list=[]
self.CDE_size_min_var_list=[]
self.CDE_size_max_var_list=[]
self.CDE_executable_var_list=[]
self.CDE_shell_var_list=[]
self.CDE_timeout_var_list=[]
self.CDE_crc_var_list=[]

Expand All @@ -634,6 +641,7 @@ def get_scan_dialog(self):
self.size_max_entry={}
self.use_checkbutton={}
self.executable_entry={}
self.shell_checkbutton={}
self.open_button={}
self.timeout_entry={}
self.test_button={}
Expand All @@ -645,6 +653,7 @@ def get_scan_dialog(self):
self.CDE_size_min_var_list.append(StringVar())
self.CDE_size_max_var_list.append(StringVar())
self.CDE_executable_var_list.append(StringVar())
self.CDE_shell_var_list.append(BooleanVar())
self.CDE_timeout_var_list.append(StringVar())
self.CDE_crc_var_list.append(BooleanVar())

Expand All @@ -662,26 +671,30 @@ def get_scan_dialog(self):
self.executable_entry[e] = Entry(cde_frame,textvariable=self.CDE_executable_var_list[e],style='TEntry')
self.executable_entry[e].grid(row=row, column=3,sticky='news')

self.shell_checkbutton[e] = Checkbutton(cde_frame,variable=self.CDE_shell_var_list[e])
self.shell_checkbutton[e].grid(row=row, column=4,sticky='news')

self.open_button[e] = Button(cde_frame,image=self.ico_folder,command = lambda x=e : self.cde_entry_open(x) )
self.open_button[e].grid(row=row,column=4,sticky='news')
self.open_button[e].grid(row=row,column=5,sticky='news')

self.timeout_entry[e] = Entry(cde_frame,textvariable=self.CDE_timeout_var_list[e],width=3,style='TEntry')
self.timeout_entry[e].grid(row=row, column=5,sticky='news')
self.timeout_entry[e].grid(row=row, column=6,sticky='news')

self.test_button[e] = Button(cde_frame,image=self.ico_test,command = lambda x=e : self.cde_test(x) )
self.test_button[e].grid(row=row,column=6,sticky='news')
self.test_button[e].grid(row=row,column=7,sticky='news')

self.use_checkbutton[e] = Checkbutton(cde_frame,variable=self.CDE_use_var_list[e],command = lambda x=e : self.use_checkbutton_mod(x))
self.use_checkbutton[e].grid(row=row,column=7,sticky='news')
self.use_checkbutton[e].grid(row=row,column=8,sticky='news')

self.crc_entry[e] = Checkbutton(cde_frame,variable=self.CDE_crc_var_list[e],command = lambda x=e : self.use_checkbutton_mod(x))
self.crc_entry[e].grid(row=row, column=8,sticky='news')
self.crc_entry[e].grid(row=row, column=9,sticky='news')

self.widget_tooltip(self.mask_entry[e],mask_tooltip)
self.widget_tooltip(self.size_min_entry[e],min_tooltip)
self.widget_tooltip(self.size_max_entry[e],max_tooltip)
self.widget_tooltip(self.use_checkbutton[e],use_tooltip)
self.widget_tooltip(self.executable_entry[e],exec_tooltip)
self.widget_tooltip(self.shell_checkbutton[e],shell_tooltip)
self.widget_tooltip(self.open_button[e],open_tooltip)
self.widget_tooltip(self.timeout_entry[e],timeout_tooltip)
self.widget_tooltip(self.test_button[e],test_tooltip)
Expand Down Expand Up @@ -2759,8 +2772,9 @@ def scan_wrapper(self):
if self.scan(compression_level):
self.scan_dialog_hide_wrapper()
except Exception as e:
l_error(e)
self.status(str(e))
l_error(f'scan_wraper: {e}')
self.status(f'scan_wraper {e}')
self.scan_dialog_hide_wrapper()

self.scanning_in_progress=False

Expand Down Expand Up @@ -2872,6 +2886,7 @@ def scan(self,compression_level):
smin = self.CDE_size_min_var_list[e].get()
smax = self.CDE_size_max_var_list[e].get()
exe = self.CDE_executable_var_list[e].get()
shell = self.CDE_shell_var_list[e].get()
timeout = self.CDE_timeout_var_list[e].get()
crc = self.CDE_crc_var_list[e].get()

Expand All @@ -2889,20 +2904,23 @@ def scan(self,compression_level):
smin,
smax,
exe,
'1' if shell else '0',
timeout,
'1' if self.CDE_crc_var_list[e].get() else '0' ]

cde_sklejka_list.append('|'.join(line_list))

if self.CDE_use_var_list[e].get():
any_cde_enabled=True

cde_list.append( (
tuple([elem.strip() for elem in mask.split(',')]),
True if smin_int>=0 else False,
smin_int,
True if smax_int>=0 else False,
smax_int,
tuple(exe.split()),
shell,
timeout_int,
crc ) )

Expand Down Expand Up @@ -3109,17 +3127,31 @@ def scan_dialog_show(self):
e=0
for e_section in self.cfg.get(CFG_KEY_CDE_SETTINGS).split('\n'):
try:
v1,v2,v3,v4,v5,v6,v7 = e_section.split('|')
v1,v2,v3,v4,v5,v6,v7,v8 = e_section.split('|')

self.CDE_use_var_list[e].set(True if v1=='1' else False)
self.CDE_mask_var_list[e].set(v2)
self.CDE_size_min_var_list[e].set(v3)
self.CDE_size_max_var_list[e].set(v4)
self.CDE_executable_var_list[e].set(v5)
self.CDE_timeout_var_list[e].set(v6)
self.CDE_crc_var_list[e].set(True if v7=='1' else False)
self.CDE_shell_var_list[e].set(True if v6=='1' else False)
self.CDE_timeout_var_list[e].set(v7)
self.CDE_crc_var_list[e].set(True if v8=='1' else False)
e+=1
except Exception as e:
print(e,e_section)
print('clearing settings')
for e in range(self.CDE_ENTRIES_MAX):
self.CDE_use_var_list[e].set(False)
self.CDE_mask_var_list[e].set('')
self.CDE_size_min_var_list[e].set('')
self.CDE_size_max_var_list[e].set('')
self.CDE_executable_var_list[e].set('')
self.CDE_shell_var_list[e].set(False)
self.CDE_timeout_var_list[e].set('')
self.CDE_crc_var_list[e].set(False)
self.cfg.set(CFG_KEY_CDE_SETTINGS,'')
self.cfg.write()
break

for e in range(self.CDE_ENTRIES_MAX):
Expand Down Expand Up @@ -3174,6 +3206,7 @@ def cde_test(self,e):
expr = normpath(abspath(res))

executable = self.CDE_executable_var_list[e].get().split()
shell = self.CDE_shell_var_list[e].get()
timeout = self.CDE_timeout_var_list[e].get()

try:
Expand All @@ -3190,7 +3223,7 @@ def cde_test(self,e):
ask_dialog.show('Test selected Custom Data Extractor on selected file ?',info)

if ask_dialog.res_bool:
cd_ok,output = librer_core.test_cde(executable,timeout_int,file_to_test)
cd_ok,output = librer_core.test_cde(executable,shell,timeout_int,file_to_test)

self.get_text_dialog_on_scan().show('CDE Test',output)

Expand Down

0 comments on commit 6054884

Please sign in to comment.