From a6c78674b84a4968b1103d1c81ff41465aa83a8b Mon Sep 17 00:00:00 2001 From: Samuel Sloniker Date: Sat, 19 Jun 2021 13:17:02 -0700 Subject: [PATCH] Replace os.system with subprocess Closes #2 --- server/backends/x11.py | 11 +++++++---- server/imgproc.py | 20 +++++++++++++++----- server/wss.py | 2 +- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/server/backends/x11.py b/server/backends/x11.py index 2961bae..6ee279f 100644 --- a/server/backends/x11.py +++ b/server/backends/x11.py @@ -1,16 +1,19 @@ import os +import subprocess import threading import time def _long_touch(x, y): - os.system(f'xdotool mousemove {x} {y} mousedown 1') + subprocess.run(['xdotool', 'mousemove', str(x), str(y), 'mousedown', '1']) time.sleep(2) - os.system(f'xdotool mouseup 1') + subprocess.run(['xdotool', 'mouseup', '1']) def get_img(): - os.system('xwd -root -silent | convert xwd:- img.bmp') + xwd = subprocess.Popen(['xwd', '-root', '-silent'], stdout=subprocess.PIPE) + convert = subprocess.Popen(['convert', 'xwd:-', 'bmp:img.bmp'], stdin=xwd.stdout) + convert.wait() return 'img.bmp' @@ -18,4 +21,4 @@ def touch(x, y, is_long): if is_long: threading.Thread(target=_long_touch, args=(x, y,)).start() else: - os.system(f'xdotool mousemove {x} {y} click 1') + subprocess.run(['xdotool', 'mousemove', str(x), str(y), 'click', '1']) diff --git a/server/imgproc.py b/server/imgproc.py index fecbe79..9ba9ddf 100644 --- a/server/imgproc.py +++ b/server/imgproc.py @@ -1,6 +1,10 @@ import json import threading import os +import subprocess + +def run(bmp_path, crop): + subprocess.run(['convert', bmp_path, '-crop', crop, f'pieces/{crop.split("+", 1)[1].replace("+", "x")}.jpg',]) def get_split_imgs(): bmp_path = backend.get_img() @@ -10,27 +14,33 @@ def get_split_imgs(): threads = [] for crop in crops: - threads.append(threading.Thread(target=os.system, args=(f'convert {bmp_path} -crop {crop} pieces/{crop.split("+", 1)[1].replace("+", "x")}.jpg',))) + threads.append(threading.Thread(target=run, args=([bmp_path, crop]))) threads[-1].start() for thread in threads: thread.join() os.unlink(bmp_path) - - os.system("md5sum -c oldlist 2>/dev/null | grep FAILED > newlist") + + with open('newlist', 'w+b') as f: + md5sum = subprocess.Popen(['md5sum', '-c', 'oldlist'], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) + grep = subprocess.Popen(['grep', 'FAILED'], stdin=md5sum.stdout, stdout=f) + grep.wait() + changed = [] with open('newlist') as f: lines = f.readlines() for line in lines: changed.append(line.split(": ")[0].split('.')[0].split('/')[1]) - os.system("md5sum pieces/* > oldlist") + + with open('oldlist', 'w+b') as f: + subprocess.Popen(['md5sum'] + [f'pieces/{i}' for i in os.listdir('pieces')], stdout=f) return changed def get_full_img(): bmp_path = backend.get_img() - os.system(f'convert {bmp_path} img.jpg') + subprocess.run(['convert', bmp_path, 'img.jpg',]) os.unlink(bmp_path) diff --git a/server/wss.py b/server/wss.py index 3119426..31f0252 100644 --- a/server/wss.py +++ b/server/wss.py @@ -95,7 +95,7 @@ def new_client(client, server): clients[client['id']] = Client(client, server) try: imgname = hcapi.get_full_img() - except Exception as e: + except Exceptidon as e: server.send_message(client, 'err%noconn%Server failed to capture screenshot') return