1
0
Fork 0
mirror of https://github.com/dbarzin/pandora-box.git synced 2025-07-19 05:19:40 +02:00
pandora-box/pandora-box.py

593 lines
20 KiB
Python
Raw Normal View History

2022-06-11 18:25:10 +02:00
#!/usr/bin/python3
2022-07-13 15:26:00 +02:00
#
2023-02-12 18:10:04 +01:00
# This file is part of the Pandora-box distribution.
# https://github.com/dbarzin/pandora-box
2022-07-13 15:26:00 +02:00
# Copyright (c) 2022 Didier Barzin.
2023-02-12 18:10:04 +01:00
#
2022-07-13 15:26:00 +02:00
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
2022-06-11 21:50:01 +02:00
2022-06-11 18:25:10 +02:00
import curses
2022-06-12 00:26:34 +02:00
from curses import wrapper
2022-06-11 18:25:10 +02:00
import pypandora
2022-06-11 21:06:59 +02:00
import time
2022-06-11 18:25:10 +02:00
import sys
2022-06-11 20:06:30 +02:00
import pyudev
import psutil
2022-06-11 21:06:59 +02:00
import os
2022-06-12 11:14:38 +02:00
import logging
2022-06-16 13:24:36 +02:00
import configparser
2022-06-17 22:09:21 +02:00
import shutil
from datetime import datetime
2022-06-11 20:06:30 +02:00
2022-06-11 21:06:59 +02:00
# -----------------------------------------------------------
# Config variables
# -----------------------------------------------------------
2022-06-11 20:06:30 +02:00
2022-06-16 13:24:36 +02:00
USB_AUTO_MOUNT = False
2022-06-11 21:06:59 +02:00
PANDORA_ROOT_URL = "http://127.0.0.1:6100"
2022-06-16 13:24:36 +02:00
FAKE_SCAN = False
2022-06-17 22:09:21 +02:00
QUARANTINE = False
2022-06-28 17:52:32 +02:00
CURSES = True
2022-06-16 13:24:36 +02:00
""" read configuration file """
def config():
2022-06-17 23:30:37 +02:00
global USB_AUTO_MOUNT, PANDORA_ROOT_URL
2022-06-17 21:18:30 +02:00
global FAKE_SCAN, QUARANTINE, QUARANTINE_FOLDER
2022-07-12 16:42:00 +00:00
global CURSES
2022-06-16 13:24:36 +02:00
# intantiate a ConfirParser
2022-09-25 10:30:14 +02:00
config = configparser.ConfigParser()
# read the config file
config.read('pandora-box.ini')
# set values
FAKE_SCAN=config['DEFAULT']['FAKE_SCAN'].lower()=="true"
USB_AUTO_MOUNT=config['DEFAULT']['USB_AUTO_MOUNT'].lower()=="true"
PANDORA_ROOT_URL=config['DEFAULT']['PANDORA_ROOT_URL']
# Quarantine
QUARANTINE = config['DEFAULT']['QUARANTINE'].lower()=="true"
QUARANTINE_FOLDER = config['DEFAULT']['QUARANTINE_FOLDER']
# Curses
CURSES = config['DEFAULT']['CURSES'].lower()=="true"
2022-06-28 14:29:46 +02:00
2022-06-12 12:50:04 +02:00
# ----------------------------------------------------------
""" Convert size to human readble string """
def human_readable_size(size, decimal_places=1):
for unit in ['B','KB','MB','GB','TB']:
if size < 1024.0:
break
size /= 1024.0
return f"{size:.{decimal_places}f}{unit}"
2022-06-11 20:06:30 +02:00
2022-06-28 17:52:32 +02:00
# -----------------------------------------------------------
# Image Screen
# -----------------------------------------------------------
def display_image(status):
2022-07-25 01:40:02 +02:00
if not CURSES:
if status=="WAIT":
image = "images/key*.png"
elif status=="WORK":
image = "images/wait*.png"
elif status=="OK":
image = "images/ok.png"
elif status=="BAD":
image = "images/bad.png"
elif status=="ERROR":
image = "images/error.png"
else:
return
# hide old image
os.system("killall -s 9 fim 2>/dev/null")
# display image
if "*" in image:
# slide show
2022-09-25 10:30:14 +02:00
os.system("fim -qa -c 'while(1){display;sleep 1;next;}' %s </dev/null 2>/dev/null >/dev/null &" % image)
2022-07-25 01:40:02 +02:00
else :
# only one image
2022-09-25 10:30:14 +02:00
os.system("fim -qa %s </dev/null 2>/dev/null >/dev/null &" % image)
2022-07-25 01:40:02 +02:00
2022-06-28 18:58:29 +02:00
2022-06-30 10:44:21 +02:00
# -----------------------------------------------------------
def waitMouseClick():
mouse = open( "/dev/input/mice", "rb" )
2022-07-25 01:00:07 +02:00
down = False;
2022-06-30 10:44:21 +02:00
while True:
buf = mouse.read(3)
if ((buf[0] & 0x1)==1):
2022-07-25 01:00:07 +02:00
down = True
if (((buf[0] & 0x1)==0) and down):
2022-06-30 10:44:21 +02:00
break;
mouse.close()
2022-06-11 21:06:59 +02:00
# -----------------------------------------------------------
2022-06-28 17:52:32 +02:00
# CURSES Screen
2022-06-11 21:06:59 +02:00
# -----------------------------------------------------------
2022-06-11 21:50:01 +02:00
"""Initialise curses"""
2022-06-28 19:42:16 +00:00
def init_curses():
2022-06-11 21:06:59 +02:00
global screen
2022-06-28 18:58:29 +02:00
if CURSES:
2022-06-30 10:44:21 +02:00
screen = curses.initscr()
screen.keypad(1)
curses.mousemask(curses.ALL_MOUSE_EVENTS | curses.REPORT_MOUSE_POSITION)
curses.flushinp()
curses.noecho()
2022-06-28 18:58:29 +02:00
curses.curs_set(0)
2022-06-28 19:42:16 +00:00
else:
2022-06-28 18:58:29 +02:00
display_image("WAIT")
2022-06-11 21:06:59 +02:00
2022-06-11 21:50:01 +02:00
"""Print FS Label"""
2022-06-12 00:26:34 +02:00
def print_fslabel(label):
global status_win
2022-06-28 18:58:29 +02:00
if CURSES:
status_win.addstr(1, 1, "Partition : %-32s" % label, curses.color_pair(2))
status_win.refresh()
2022-06-12 00:26:34 +02:00
"""Print FS Size"""
def print_size(label):
global status_win
2022-06-28 18:58:29 +02:00
if CURSES:
if label == None:
status_win.addstr(2, 1, "Size : ",curses.color_pair(2))
else:
status_win.addstr(2, 1, "Size : %s " % label,curses.color_pair(2))
2023-02-13 15:45:28 +01:00
logging.info("size=%s" % label)
2022-06-28 18:58:29 +02:00
status_win.refresh()
2022-06-12 00:26:34 +02:00
"""Print FS Used Size"""
def print_used(label):
global status_win
2022-06-28 18:58:29 +02:00
if CURSES:
if label == None:
status_win.addstr(3, 1, "Used : ",curses.color_pair(2))
else:
status_win.addstr(3, 1, "Used : %s " % label,curses.color_pair(2))
2023-02-13 15:45:28 +01:00
logging.info("used=%s" % label)
2022-06-28 18:58:29 +02:00
status_win.refresh()
2022-06-11 21:06:59 +02:00
2022-06-12 00:26:34 +02:00
def print_fstype(label):
global status_win
2022-06-28 18:58:29 +02:00
if CURSES:
status_win.addstr(1, 50, "Part / Type : %-32s" % label, curses.color_pair(2))
status_win.refresh()
2022-06-11 20:06:30 +02:00
2022-06-12 00:26:34 +02:00
def print_model(label):
global status_win
2022-06-28 18:58:29 +02:00
if CURSES:
status_win.addstr(2, 50, "Model : %-32s" % label, curses.color_pair(2))
status_win.refresh()
2022-06-11 21:06:59 +02:00
2022-06-12 00:26:34 +02:00
def print_serial(label):
global status_win
2022-06-28 18:58:29 +02:00
if CURSES:
status_win.addstr(3, 50, "Serial : %-32s" % label, curses.color_pair(2))
status_win.refresh()
2022-06-11 20:06:30 +02:00
2022-06-11 21:50:01 +02:00
"""Initialise progress bar"""
def init_bar():
2022-06-11 20:06:30 +02:00
global progress_win
2022-06-28 18:58:29 +02:00
if CURSES:
progress_win = curses.newwin(3, curses.COLS-12, 17, 5)
progress_win.border(0)
progress_win.refresh()
2022-06-11 21:06:59 +02:00
2022-06-11 21:50:01 +02:00
"""Update progress bar"""
def update_bar(progress):
2022-06-11 20:06:30 +02:00
global progress_win
2022-06-28 18:58:29 +02:00
if CURSES:
if progress == 0:
progress_win.clear()
progress_win.border(0)
time.sleep(0)
progress_win.addstr(0, 1, "Progress:")
else:
pos = ((curses.COLS-14) * progress) // 100
progress_win.addstr(1, 1, "#"*pos)
progress_win.addstr(0, 1, "Progress: %d%%" % progress)
progress_win.refresh()
2022-06-12 00:26:34 +02:00
2022-06-12 11:14:38 +02:00
"""Splash screen"""
s = [None] * 10;
s[0] = " ██▓███ ▄▄▄ ███▄ █ ▓█████▄ ▒█████ ██▀███ ▄▄▄ ▄▄▄▄ ▒█████ ▒██ ██▒"
s[1] = " ▓██░ ██▒▒████▄ ██ ▀█ █ ▒██▀ ██▌▒██▒ ██▒▓██ ▒ ██▒▒████▄ ▓█████▄ ▒██▒ ██▒▒▒ █ █ ▒░"
s[2] = " ▓██░ ██▓▒▒██ ▀█▄ ▓██ ▀█ ██▒░██ █▌▒██░ ██▒▓██ ░▄█ ▒▒██ ▀█▄ ▒██▒ ▄██▒██░ ██▒░░ █ ░"
s[3] = " ▒██▄█▓▒ ▒░██▄▄▄▄██ ▓██▒ ▐▌██▒░▓█▄ ▌▒██ ██░▒██▀▀█▄ ░██▄▄▄▄██ ▒██░█▀ ▒██ ██░ ░ █ █ ▒ "
s[4] = " ▒██▒ ░ ░ ▓█ ▓██▒▒██░ ▓██░░▒████▓ ░ ████▓▒░░██▓ ▒██▒ ▓█ ▓██▒ ░▓█ ▀█▓░ ████▓▒░▒██▒ ▒██▒"
s[5] = " ▒▓▒░ ░ ░ ▒▒ ▓▒█░░ ▒░ ▒ ▒ ▒▒▓ ▒ ░ ▒░▒░▒░ ░ ▒▓ ░▒▓░ ▒▒ ▓▒█░ ░▒▓███▀▒░ ▒░▒░▒░ ▒▒ ░ ░▓ ░"
s[6] = " ░▒ ░ ▒ ▒▒ ░░ ░░ ░ ▒░ ░ ▒ ▒ ░ ▒ ▒░ ░▒ ░ ▒░ ▒ ▒▒ ░ ▒░▒ ░ ░ ▒ ▒░ ░░ ░▒ ░"
s[7] = " ░░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░ ░ ▒ ░░ ░ ░ ▒ ░ ░ ░ ░ ░ ▒ ░ ░ "
s[8] = " ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ "
s[9] = " ░ ░ "
2022-06-11 21:06:59 +02:00
2022-06-16 13:58:07 +02:00
#curses.LINES, curses.COLS
2022-06-16 13:24:36 +02:00
2022-06-12 00:26:34 +02:00
"""Print main screen"""
2022-06-11 21:50:01 +02:00
def print_screen():
2022-06-12 00:26:34 +02:00
global status_win
2022-06-28 18:58:29 +02:00
if CURSES:
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_GREEN, curses.COLOR_BLACK)
title_win = curses.newwin(12, curses.COLS, 0, 0)
# title_win.border(0)
title_col = (curses.COLS - len(s[0]))//2
title_win.addstr(1, title_col, s[0], curses.color_pair(1))
title_win.addstr(2, title_col, s[1], curses.color_pair(1))
title_win.addstr(3, title_col, s[2], curses.color_pair(1))
title_win.addstr(4, title_col, s[3], curses.color_pair(1))
title_win.addstr(5, title_col, s[4], curses.color_pair(1))
title_win.addstr(6, title_col, s[5], curses.color_pair(1))
title_win.addstr(7, title_col, s[6], curses.color_pair(1))
title_win.addstr(8, title_col, s[7], curses.color_pair(1))
title_win.addstr(9, title_col, s[8], curses.color_pair(1))
title_win.addstr(10, title_col, s[9], curses.color_pair(1))
title_win.refresh()
status_win = curses.newwin(5, curses.COLS, 12, 0)
status_win.border(0)
status_win.addstr(0, 1, "USB Key Information")
print_fslabel("")
print_size(None)
print_used(None)
print_fstype("")
print_model("")
print_serial("")
init_bar()
update_bar(0)
2022-06-12 00:26:34 +02:00
log('Ready.')
2022-06-11 18:25:10 +02:00
2022-06-12 00:26:34 +02:00
"""Closes curses"""
2022-06-11 21:50:01 +02:00
def end_curses():
2022-07-25 01:40:02 +02:00
if CURSES:
curses.endwin()
curses.flushinp()
else:
# hide old image
os.system("killall -s 9 fim 2>/dev/null")
2022-06-11 21:06:59 +02:00
2022-06-28 14:29:46 +02:00
# -----------------------------------------------------------
# Logging windows
# -----------------------------------------------------------
def init_log():
global log_win, logging
2022-06-28 18:58:29 +02:00
if CURSES:
log_win = curses.newwin(curses.LINES-20, curses.COLS, 20, 0)
log_win.border(0)
2022-06-28 14:29:46 +02:00
logging.basicConfig(
2022-06-28 17:52:32 +02:00
filename='pandora-box.log',
2022-06-28 14:29:46 +02:00
level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%m/%d/%y %H:%M'
)
logs = []
def log(str):
global log_win, logging
logging.info(str)
2022-06-28 18:58:29 +02:00
if CURSES:
# display log on screen
logs.append(str)
if len(logs)>(curses.LINES-22):
logs.pop(0)
log_win.clear()
log_win.border(0)
for i in range(min(curses.LINES-22,len(logs))):
log_win.addstr(i+1,1,logs[i][:curses.COLS-2],curses.color_pair(3))
log_win.refresh()
2022-09-25 10:30:14 +02:00
# else:
# print(str,end="\n\r")
2022-06-11 18:25:10 +02:00
2022-06-11 21:06:59 +02:00
# -----------------------------------------------------------
2022-06-12 00:26:34 +02:00
# Device
2022-06-11 21:06:59 +02:00
# -----------------------------------------------------------
2022-06-12 00:26:34 +02:00
"""Mount USB device"""
2023-02-13 15:45:28 +01:00
def mount_device():
global device
2022-07-25 01:00:07 +02:00
log('Try to mount partition')
2022-06-11 20:06:30 +02:00
if USB_AUTO_MOUNT:
2022-06-11 21:06:59 +02:00
found = False
loop = 0
2022-07-25 01:00:07 +02:00
while (not found) and (loop < 15):
2022-06-11 21:06:59 +02:00
# need to sleep before devide is mounted
2022-06-11 18:25:10 +02:00
time.sleep(1)
for partition in psutil.disk_partitions():
2022-06-11 21:06:59 +02:00
if partition.device == device.device_node:
found = True
loop += 1
2022-06-12 00:26:34 +02:00
if loop < 10:
return partition.mountpoint
else:
2022-07-25 01:00:07 +02:00
log('No partition mounted')
2022-06-16 17:32:11 +02:00
return None
2022-06-11 18:25:10 +02:00
else:
2022-07-25 01:00:07 +02:00
if not os.path.exists("/media/box"):
log("folder /media/box does not exists")
return None
2022-09-25 10:30:14 +02:00
res = os.system("pmount " + device.device_node + " /media/box >/dev/null 2>/dev/null")
2022-06-13 20:10:33 +00:00
found = False
loop = 0
while (not found) and (loop < 10):
time.sleep(1)
try:
statvfs=os.statvfs(mount_point)
except Exception as e :
loop +=1
continue
break;
return "/media/box"
2022-06-11 21:06:59 +02:00
2022-06-12 00:26:34 +02:00
"""Unmount USB device"""
2022-09-25 10:30:14 +02:00
def umount_device():
if USB_AUTO_MOUNT:
log("Sync partitions")
res = os.system("sync")
2022-08-24 00:12:55 +02:00
else:
2022-09-25 10:30:14 +02:00
log("Unmount partitions")
res = os.system("pumount /media/box 2>/dev/null >/dev/null")
2022-06-11 18:25:10 +02:00
2022-06-12 11:14:38 +02:00
def log_device_info(dev):
2023-02-13 15:45:28 +01:00
logging.info(
"device_name=%s, " % dev.get("DEVNAME") +
"path_id=%s, " % dev.get("ID_PATH") +
"bus system=%s, " % dev.get("ID_BUS") +
"USB_driver=%s, " % dev.get("ID_USB_DRIVER") +
"device_type=%s, " % dev.get("DEVTYPE") +
"device_usage=%s, " % dev.get("ID_FS_USAGE") +
"partition type=%s, " % dev.get("ID_PART_TABLE_TYPE") +
"fs_type=%s, " % dev.get("ID_FS_TYPE") +
"partition_label: %s, " % dev.get("ID_FS_LABEL") +
"device_model=%s, " % dev.get("ID_MODEL") +
'model_id=%s, ' % dev.get("ID_MODEL_ID") +
'serial_short=%s, ' % dev.get("ID_SERIAL_SHORT") +
'serial=%s' % dev.get("ID_SERIAL"))
2022-06-12 11:14:38 +02:00
2022-06-11 21:06:59 +02:00
# -----------------------------------------------------------
2022-06-11 18:25:10 +02:00
# pandora
2022-06-11 21:06:59 +02:00
# -----------------------------------------------------------
2022-06-12 00:26:34 +02:00
"""Scan a mount point with Pandora"""
2022-06-12 12:50:04 +02:00
def scan(mount_point, used):
2023-02-13 15:45:28 +01:00
global device, infected_filed
2022-06-13 20:10:33 +00:00
infected_files = []
2022-06-12 12:50:04 +02:00
scanned = 0
2022-06-12 20:37:49 +02:00
file_count = 0
scan_start_time = time.time()
2022-06-17 22:09:21 +02:00
if QUARANTINE:
quanrantine_folder = os.path.join(QUARANTINE_FOLDER,datetime.now().strftime("%y%m%d-%H%M"))
2022-06-13 20:10:33 +00:00
if not FAKE_SCAN:
pandora = pypandora.PyPandora(root_url=PANDORA_ROOT_URL)
2022-06-30 12:36:46 +00:00
try:
for root, dirs, files in os.walk(mount_point):
for file in files:
2022-06-17 23:30:37 +02:00
status = None
2022-06-13 20:10:33 +00:00
full_path = os.path.join(root,file)
file_size = os.path.getsize(full_path)
2022-06-13 21:00:07 +00:00
# log("Check %s [%s]" % (file, human_readable_size(file_size)))
2022-06-13 20:10:33 +00:00
file_scan_start_time = time.time()
2022-09-25 10:30:14 +02:00
if FAKE_SCAN :
2022-06-12 20:37:49 +02:00
time.sleep(0.1)
2022-06-13 20:10:33 +00:00
status = "SKIPPED"
else:
if file_size > (1024*1024*1024):
status = "TOO BIG"
else:
2022-09-25 10:30:14 +02:00
log("ppypandora : [%s] " % full_path)
2022-06-13 20:10:33 +00:00
res = pandora.submit_from_disk(full_path)
time.sleep(0.1)
2022-06-17 22:09:21 +02:00
loop = 0
2022-07-05 20:14:07 +02:00
while True and (loop < 960):
2022-06-13 20:10:33 +00:00
res = pandora.task_status(res["taskId"])
status = res["status"]
if status != "WAITING":
break
time.sleep(0.5)
2022-06-17 22:09:21 +02:00
loop += 1
2022-06-13 20:10:33 +00:00
file_scan_end_time = time.time()
2023-02-13 15:45:28 +01:00
log("file=%s, size=%s, status=%s, duration=%ds" % (
2022-06-13 21:00:07 +00:00
file,
human_readable_size(file_size),
status,
2022-07-05 20:14:07 +02:00
(file_scan_end_time - file_scan_start_time)))
2022-06-13 20:10:33 +00:00
scanned += os.path.getsize(full_path)
file_count += 1
update_bar(scanned * 100 // used)
2022-06-17 23:30:37 +02:00
if status == "ALERT":
infected_files.append(full_path)
if QUARANTINE:
if not os.path.isdir(quanrantine_folder) :
os.mkdir(quanrantine_folder)
shutil.copyfile(full_path, os.path.join(quanrantine_folder,file))
2022-06-30 12:36:46 +00:00
except Exception as e :
log("Unexpected error: %s" % e)
2022-06-30 12:45:55 +00:00
log("Scan failed !")
2022-06-30 12:36:46 +00:00
if not CURSES:
display_image("ERROR")
raise
2022-06-13 20:10:33 +00:00
update_bar(100)
2023-02-13 15:45:28 +01:00
log("duration=%ds, files_scanned=%d, files_infected=%d" %
2022-06-17 22:09:21 +02:00
((time.time() - scan_start_time),file_count,len(infected_files)))
2022-06-17 23:30:37 +02:00
return infected_files
2022-06-11 18:25:10 +02:00
2022-06-11 21:06:59 +02:00
# --------------------------------------
2022-06-11 18:25:10 +02:00
2023-02-13 15:45:28 +01:00
def wait_device():
global device
# Loop
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by("block")
try:
for device in iter(monitor.poll, None):
if device.get("ID_FS_USAGE") == "filesystem" and device.device_node[5:7] == "sd":
if device.action == "add":
log("Device inserted")
log_device_info(device)
if not CURSES:
display_image("WORK")
else:
# display device type
print_fslabel(device.get("ID_FS_LABEL"))
print_fstype(device.get("ID_PART_TABLE_TYPE") + " " + device.get("ID_FS_TYPE"))
print_model(device.get("ID_MODEL"))
print_serial(device.get("ID_SERIAL_SHORT"))
return "INSERTED"
if device.action == "remove":
log("Device removed")
if not CURSES:
display_image("WAIT")
else:
print_fslabel("")
print_size(None)
print_used(None)
print_fstype("")
print_model("")
print_serial("")
update_bar(0)
return "WAIT"
except Exception as e:
log("Unexpected error: %s" % str(e) )
logging.info("An exception was thrown!", exc_info=True)
finally:
log("Done.")
return "STOP"
# --------------------------------------
def mount():
global device, mount_point
# Mount device
mount_point = mount_device()
log('Partition mounted at %s' % mount_point)
if mount_point == None:
# no partition
if not CURSES:
display_image("WAIT")
return "WAIT"
try:
statvfs=os.statvfs(mount_point)
except Exception as e :
log("error=%s" % e)
logging.info("An exception was thrown!", exc_info=True)
if not CURSES:
display_image("WAIT")
return "WAIT"
return "SCAN"
# --------------------------------------
def scan_device():
global mount_point, infected_files
try:
statvfs=os.statvfs(mount_point)
except Exception as e :
log("error=%s" % e)
logging.info("An exception was thrown!", exc_info=True)
if not CURSES:
display_image("WAIT")
return "WAIT"
print_size(human_readable_size(statvfs.f_frsize * statvfs.f_blocks))
print_used(human_readable_size(statvfs.f_frsize * (statvfs.f_blocks - statvfs.f_bfree)))
infected_files = scan(mount_point, statvfs.f_frsize * (statvfs.f_blocks - statvfs.f_bfree))
return "CLEAN"
# --------------------------------------
def clean():
global infected_files
# Clean files
if len(infected_files) > 0:
log('%d infected files found !' % len(infected_files))
if not CURSES:
display_image("BAD")
waitMouseClick()
else:
log('PRESS KEY TO CLEAN')
screen.getch()
# Remove infected files
for file in infected_files:
try :
os.remove(file)
log('%s removed' % file)
except Exception as e :
log("Unexpected error: %s" % str(e))
logging.info("An exception was thrown!", exc_info=True)
os.system("sync")
log("Clean done.")
if not CURSES:
display_image("OK")
else:
if not CURSES:
display_image("OK")
umount_device()
return "WAIT"
# --------------------------------------
2022-06-28 20:44:04 +00:00
def moveToScriptFolder():
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
2023-02-12 18:10:04 +01:00
# --------------------------------------
2023-02-13 15:45:28 +01:00
def startup():
moveToScriptFolder()
init_log()
config()
init_curses()
print_screen()
# First unmount remaining device
umount_device()
return "WAIT"
# --------------------------------------
def loop(state):
match state:
case "START":
return startup()
case "WAIT":
return wait_device()
case "INSERTED":
return mount()
case "SCAN":
return scan_device()
case "CLEAN":
return clean()
case _:
print("Unknwn state "+state)
return "STOP"
# --------------------------------------
2023-02-12 18:10:04 +01:00
2022-06-12 00:26:34 +02:00
"""Main entry point"""
def main(stdscr):
2022-06-12 20:37:49 +02:00
try :
2023-02-13 15:45:28 +01:00
state="START"
while (state!="STOP"):
state = loop(state)
2022-06-12 20:37:49 +02:00
except Exception as e :
2023-02-13 15:45:28 +01:00
log("error=%s" % e)
2022-09-25 10:30:14 +02:00
logging.info("An exception was thrown!", exc_info=True)
2022-06-11 21:06:59 +02:00
finally:
2022-06-28 19:42:16 +00:00
end_curses()
2022-06-11 18:25:10 +02:00
2023-02-12 18:10:04 +01:00
2022-06-11 21:06:59 +02:00
if __name__ == "__main__":
2022-06-12 00:26:34 +02:00
wrapper(main)