Replace os.system with subprocess

Closes #2
This commit is contained in:
Samuel Sloniker 2021-06-19 13:17:02 -07:00
parent be30709a3f
commit a6c78674b8
3 changed files with 23 additions and 10 deletions

View File

@ -1,16 +1,19 @@
import os import os
import subprocess
import threading import threading
import time import time
def _long_touch(x, y): 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) time.sleep(2)
os.system(f'xdotool mouseup 1') subprocess.run(['xdotool', 'mouseup', '1'])
def get_img(): 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' return 'img.bmp'
@ -18,4 +21,4 @@ def touch(x, y, is_long):
if is_long: if is_long:
threading.Thread(target=_long_touch, args=(x, y,)).start() threading.Thread(target=_long_touch, args=(x, y,)).start()
else: else:
os.system(f'xdotool mousemove {x} {y} click 1') subprocess.run(['xdotool', 'mousemove', str(x), str(y), 'click', '1'])

View File

@ -1,6 +1,10 @@
import json import json
import threading import threading
import os 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(): def get_split_imgs():
bmp_path = backend.get_img() bmp_path = backend.get_img()
@ -10,27 +14,33 @@ def get_split_imgs():
threads = [] threads = []
for crop in crops: 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() threads[-1].start()
for thread in threads: for thread in threads:
thread.join() thread.join()
os.unlink(bmp_path) 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 = [] changed = []
with open('newlist') as f: with open('newlist') as f:
lines = f.readlines() lines = f.readlines()
for line in lines: for line in lines:
changed.append(line.split(": ")[0].split('.')[0].split('/')[1]) 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 return changed
def get_full_img(): def get_full_img():
bmp_path = backend.get_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) os.unlink(bmp_path)

View File

@ -95,7 +95,7 @@ def new_client(client, server):
clients[client['id']] = Client(client, server) clients[client['id']] = Client(client, server)
try: try:
imgname = hcapi.get_full_img() imgname = hcapi.get_full_img()
except Exception as e: except Exceptidon as e:
server.send_message(client, 'err%noconn%Server failed to capture screenshot') server.send_message(client, 'err%noconn%Server failed to capture screenshot')
return return