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

191 lines
6.4 KiB
Python
Raw Normal View History

2022-06-11 18:25:10 +02:00
#!/usr/bin/python3
2022-06-11 21:50:01 +02:00
"""Pandora-Box is a USB scaning station based on Pandora."""
2022-06-11 18:25:10 +02:00
import curses
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-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-11 21:06:59 +02:00
NO_SCAN = True
USB_AUTO_MOUNT = True
PANDORA_ROOT_URL = "http://127.0.0.1:6100"
2022-06-11 20:06:30 +02:00
2022-06-11 18:25:10 +02:00
2022-06-11 21:06:59 +02:00
# -----------------------------------------------------------
2022-06-11 18:25:10 +02:00
# Screen
2022-06-11 21:06:59 +02:00
# -----------------------------------------------------------
2022-06-11 21:50:01 +02:00
"""Initialise curses"""
def intit_curses():
2022-06-11 21:06:59 +02:00
global screen
screen = curses.initscr()
screen.keypad(1)
curses.curs_set(0)
curses.mousemask(curses.ALL_MOUSE_EVENTS | curses.REPORT_MOUSE_POSITION)
curses.flushinp()
curses.noecho()
screen.clear()
2022-06-11 18:25:10 +02:00
2022-06-11 21:50:01 +02:00
"""Print status string"""
def print_status(strStatus):
2022-06-11 21:06:59 +02:00
screen.addstr(12, 0, "Status : %-32s" % strStatus)
screen.refresh()
2022-06-11 20:06:30 +02:00
2022-06-11 21:50:01 +02:00
"""Print FS Label"""
def print_fslabel(strLabel):
2022-06-11 21:06:59 +02:00
screen.addstr(13, 0, "Device : %-32s" % strLabel)
screen.refresh()
2022-06-11 20:06:30 +02:00
2022-06-11 21:50:01 +02:00
"""Print current action"""
def print_action(strAction):
2022-06-11 21:06:59 +02:00
screen.addstr(14, 0, "Action : %-64s" % strAction)
screen.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
progress_win = curses.newwin(3, 62, 3, 16)
progress_win.border(0)
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
rangex = (60 / float(100)) * progress
pos = int(rangex)
2022-06-11 21:06:59 +02:00
display = "#"
2022-06-11 20:06:30 +02:00
if pos != 0:
progress_win.addstr(1, pos, "{}".format(display))
progress_win.refresh()
2022-06-11 21:06:59 +02:00
2022-06-11 21:50:01 +02:00
def print_screen():
2022-06-11 21:06:59 +02:00
screen.addstr(1, 0, " ██▓███ ▄▄▄ ███▄ █ ▓█████▄ ▒█████ ██▀███ ▄▄▄ ▄▄▄▄ ▒█████ ▒██ ██▒")
screen.addstr(2, 0, " ▓██░ ██▒▒████▄ ██ ▀█ █ ▒██▀ ██▌▒██▒ ██▒▓██ ▒ ██▒▒████▄ ▓█████▄ ▒██▒ ██▒▒▒ █ █ ▒░")
screen.addstr(3, 0, " ▓██░ ██▓▒▒██ ▀█▄ ▓██ ▀█ ██▒░██ █▌▒██░ ██▒▓██ ░▄█ ▒▒██ ▀█▄ ▒██▒ ▄██▒██░ ██▒░░ █ ░")
screen.addstr(4, 0, " ▒██▄█▓▒ ▒░██▄▄▄▄██ ▓██▒ ▐▌██▒░▓█▄ ▌▒██ ██░▒██▀▀█▄ ░██▄▄▄▄██ ▒██░█▀ ▒██ ██░ ░ █ █ ▒ ")
screen.addstr(5, 0, " ▒██▒ ░ ░ ▓█ ▓██▒▒██░ ▓██░░▒████▓ ░ ████▓▒░░██▓ ▒██▒ ▓█ ▓██▒ ░▓█ ▀█▓░ ████▓▒░▒██▒ ▒██▒")
screen.addstr(6, 0, " ▒▓▒░ ░ ░ ▒▒ ▓▒█░░ ▒░ ▒ ▒ ▒▒▓ ▒ ░ ▒░▒░▒░ ░ ▒▓ ░▒▓░ ▒▒ ▓▒█░ ░▒▓███▀▒░ ▒░▒░▒░ ▒▒ ░ ░▓ ░")
screen.addstr(7, 0, " ░▒ ░ ▒ ▒▒ ░░ ░░ ░ ▒░ ░ ▒ ▒ ░ ▒ ▒░ ░▒ ░ ▒░ ▒ ▒▒ ░ ▒░▒ ░ ░ ▒ ▒░ ░░ ░▒ ░")
screen.addstr(8, 0, " ░░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░ ░ ▒ ░░ ░ ░ ▒ ░ ░ ░ ░ ░ ▒ ░ ░ ")
screen.addstr(9, 0, " ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ")
screen.addstr(10, 0, " ░ ░ ")
2022-06-11 21:50:01 +02:00
print_status("WAITING")
print_fslabel("")
print_action("")
init_bar()
update_bar(1)
2022-06-11 21:06:59 +02:00
2022-06-11 18:25:10 +02:00
2022-06-11 21:50:01 +02:00
def end_curses():
2022-06-11 21:06:59 +02:00
curses.endwin()
curses.flushinp()
2022-06-11 18:25:10 +02:00
2022-06-11 21:06:59 +02:00
# -----------------------------------------------------------
2022-06-11 18:25:10 +02:00
# device
2022-06-11 21:06:59 +02:00
# -----------------------------------------------------------
2022-06-11 18:25:10 +02:00
2022-06-11 21:50:01 +02:00
def mount_device(device):
2022-06-11 20:06:30 +02:00
if USB_AUTO_MOUNT:
2022-06-11 21:06:59 +02:00
found = False
loop = 0
while (not found) and (loop < 10):
# 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:
2022-06-11 21:50:01 +02:00
print_action("Mounted at {}".format(partition.mountpoint))
2022-06-11 21:06:59 +02:00
found = True
loop += 1
2022-06-11 18:25:10 +02:00
else:
2022-06-11 21:50:01 +02:00
print_action("mount device to /media/box")
2022-06-11 21:06:59 +02:00
res = os.system("pmount " + device.device_node + " box")
# print("Return type: ", res)
2022-06-11 18:25:10 +02:00
2022-06-11 21:50:01 +02:00
def umount_device():
2022-06-11 21:06:59 +02:00
if not USB_AUTO_MOUNT:
2022-06-11 21:50:01 +02:00
print_action("unmount device /media/box")
2022-06-11 21:06:59 +02:00
res = os.system("pumount /media/box")
# print("Return type: ", res)
2022-06-11 18:25:10 +02:00
2022-06-11 21:50:01 +02:00
def device_loop():
2022-06-11 21:06:59 +02:00
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by("block")
for device in iter(monitor.poll, None):
2022-06-11 21:50:01 +02:00
if device.get("ID_FS_USAGE") == "filesystem" and device.device_node[5:7] == "sd":
2022-06-11 21:06:59 +02:00
if device.action == "add":
2022-06-11 21:50:01 +02:00
# print("New device {}".format(device.device_node))
mount_device(device)
# display device type
print_status("KEY INSERTED")
print_fslabel(device.get("ID_FS_LABEL"))
2022-06-11 21:06:59 +02:00
if device.action == "remove":
2022-06-11 21:50:01 +02:00
print_status("WAITING")
print_action("Device removed")
print_fslabel("")
umount_device()
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-11 18:25:10 +02:00
def scan(mountPoint):
2022-06-11 21:06:59 +02:00
pp = pypandora.PyPandora(root_url=PANDORA_ROOT_URL)
for arg in sys.argv[1:]:
print(arg, end="", flush=True)
print(":", end="", flush=True)
res = pp.submit_from_disk(arg)
while True:
print(".", end="", flush=True)
time.sleep(1)
res = pp.task_status(res["taskId"])
2022-06-11 18:25:10 +02:00
2022-06-11 21:06:59 +02:00
if res["status"] != "WAITING":
break
2022-06-11 18:25:10 +02:00
2022-06-11 21:06:59 +02:00
print(res["status"])
2022-06-11 18:25:10 +02:00
2022-06-11 21:06:59 +02:00
# --------------------------------------
2022-06-11 18:25:10 +02:00
2022-06-11 21:50:01 +02:00
def pandorabox():
2022-06-11 21:06:59 +02:00
try:
2022-06-11 21:50:01 +02:00
intit_curses()
print_screen()
device_loop()
2022-06-11 18:25:10 +02:00
2022-06-11 21:06:59 +02:00
finally:
2022-06-11 21:50:01 +02:00
end_curses()
2022-06-11 18:25:10 +02:00
2022-06-11 20:06:30 +02:00
2022-06-11 21:06:59 +02:00
# --------------------------------------
2022-06-11 18:25:10 +02:00
2022-06-11 21:06:59 +02:00
if __name__ == "__main__":
2022-06-11 21:50:01 +02:00
pandorabox()