def main(): pls = [] try: os.chdir("gen") for cmd, grab, bg in commands: with SmartDisplay() as disp: logging.info("======== cmd: %s", cmd) fname_base = cmd.replace(" ", "_") fname = fname_base + ".txt" # logging.info("cmd: %s", cmd) print("file name: %s" % fname) with open(fname, "w") as f: f.write("$ " + cmd) if bg: p = EasyProcess(cmd).start() else: p = EasyProcess(cmd).call() f.write(p.stdout) f.write(p.stderr) pls += [p] if grab: png = fname_base + ".png" sleep(1) img = disp.waitgrab(timeout=9) logging.info("saving %s", png) img.save(png) finally: os.chdir("..") for p in pls: p.stop() embedme = EasyProcess(["npx", "embedme", "../README.md"]) embedme.call() print(embedme.stdout) assert embedme.return_code == 0 assert not "but file does not exist" in embedme.stdout
def main(): pls = [] try: os.chdir("gen") for cmd in commands: logging.info("cmd: %s", cmd) fname_base = cmd.replace(" ", "_") fname = fname_base + ".txt" logging.info("cmd: %s", cmd) print("file name: %s" % fname) with open(fname, "w") as f: f.write("$ " + cmd + "\n") p = EasyProcess(cmd).call() f.write(p.stdout) f.write(p.stderr) pls += [p] finally: os.chdir("..") for p in pls: p.stop() embedme = EasyProcess(["npx", "embedme", "../README.md"]) embedme.call() print(embedme.stdout) assert embedme.return_code == 0 assert not "but file does not exist" in embedme.stdout
def main(): gendir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "gen") logging.info("gendir: %s", gendir) os.makedirs(gendir, exist_ok=True) empty_dir(gendir) pls = [] try: os.chdir("gen") for cmd in commands: logging.info("cmd: %s", cmd) fname_base = cmd.replace(" ", "_") fname = fname_base + ".txt" logging.info("cmd: %s", cmd) print("file name: %s" % fname) with open(fname, "w") as f: f.write("$ " + cmd + "\n") p = EasyProcess(cmd).call() f.write(p.stdout) f.write(p.stderr) pls += [p] finally: os.chdir("..") for p in pls: p.stop() embedme = EasyProcess(["npx", "embedme", "../README.md"]) embedme.call() print(embedme.stdout) assert embedme.return_code == 0 assert not "but file does not exist" in embedme.stdout
def test_lowres(): with Display(): p = EasyProcess([python, "-m", "pyvirtualdisplay.examples.lowres"]).start() sleep(1) assert p.is_alive() p.stop()
def check_N(N, backend): with Display(): ls = [] try: for i in range(N): cmd = [ sys.executable, __file__.rsplit(".", 1)[0] + ".py", str(i), backend, "--debug", ] p = EasyProcess(cmd) p.start() ls += [p] sleep(3) good_count = 0 rc_ls = [] for p in ls: p.wait() if p.return_code == 0: good_count += 1 rc_ls += [p.return_code] finally: for p in ls: p.stop() print(rc_ls) print(good_count) assert good_count == N
def test_slowshot(): disp = SmartDisplay(visible=False).start() py = Path(__file__).parent / ("slowgui.py") proc = EasyProcess([python, py]).start() img = disp.waitgrab() proc.stop() disp.stop() assert img is not None
def test_slowshot(self): disp = SmartDisplay(visible=0).start() py = path(__file__).parent / ('slowgui.py') proc = EasyProcess('python ' + py).start() img = disp.waitgrab() proc.stop() disp.stop() eq_(img is not None, True)
def stop(self): ''' stop display :rtype: self ''' self.redirect_display(False) EasyProcess.stop(self) return self
def testrun(self, command, timeout=0.5): result = {'result': False} pro = EasyProcess(command.split()).call(timeout=timeout) if pro.return_code > 0: result['error'] = pro.stderr else: result['result'] = True pro.stop() return result
def test_deadlock(): d = Display(visible=VISIBLE, size=(600, 400)) d.start() p = EasyProcess([python, '-c', 'import Image;Image.new("RGB",(99, 99)).show()']) p.start() p.sleep(1) # hangs with pipes p.stop() d.stop()
def stop(self): ''' stop display :rtype: self ''' self.redirect_display(False) EasyProcess.stop(self) if self.use_xauth: self._clear_xauth() return self
def test_deadlock(): d = Display(visible=VISIBLE, size=(600, 400)) d.start() p = EasyProcess( [python, '-c', 'import Image;Image.new("RGB",(99, 99)).show()']) p.start() p.sleep(1) # hangs with pipes p.stop() d.stop()
def test_deadlock(): # skip these tests for Windows/Mac if not sys.platform.startswith("linux"): return d = Display(visible=VISIBLE, size=(600, 400)) d.start() p = EasyProcess( [python, "-c", 'import Image;Image.new("RGB",(99, 99)).show()']) p.start() p.sleep(1) # hangs with pipes p.stop() d.stop()
from easyprocess import EasyProcess from pyvirtualdisplay.smartdisplay import SmartDisplay disp = SmartDisplay(visible=0, bgcolor='black').start() xmessage = EasyProcess('xmessage hello').start() img = disp.waitgrab() xmessage.stop() disp.stop() img.show()
class MPPServer: def __init__(self, application, config): self.config = config self.config["room_alias"] = ("#" + self.config["local_room_alias"] + ":" + self.config["hs_name"]) self.config["user_id"] = ("@" + self.config["local_user_id"] + ":" + self.config["hs_name"]) self.disp = disp self.keyboard = Controller() self.api = application.Api() try: # when creating a room with a room alias, only local part of alias is used self.room_id = self.api.create_room( alias=self.config["local_room_alias"], is_public=True) logger.info("new room created: " + self.room_id) except exceptions.MatrixError: # if it already exists just get the room_id self.room_id = self.api.get_room_id(self.config["room_alias"]) logger.info("using existing room: " + self.room_id) self._start_mgba() def _start_mgba(self): self.pkmn = EasyProcess("mgba -s 2 -b " + self.config["bios_location"] + " " + self.config["rom_location"]) self.pkmn.start() logger.info("mgba started") self.ts = time.time() self.save_timers = [[index, time.time(), l, f] for index, (l, f) in enumerate(((50, Key.f1), (1000, Key.f2), (10000, Key.f3), (50000, Key.f4), (100000, Key.f5)))] time.sleep(5) self._load() self.send_screenshot() def __del__(self): self.pkmn.stop() self.disp.stop() def send_screenshot(self): if self.ts + 2 < time.time(): self._send() self.ts = time.time() def _send(self): img = self.disp.grab() f = BytesIO() try: img.save(f, format="JPEG", quality=50, optimize=True) mxc = self.api.media_upload(f.getvalue(), "image/jpeg")["content_uri"] file_name = str(int(self.ts)) + ".jpg" self.api.send_content(self.room_id, mxc, file_name, "m.image") logger.debug("sent screenshot to " + self.room_id) except AttributeError: self.api.send_notice(self.room_id, "Error in capturing screenshot") logger.error("could not capture screenshot from display") def _save(self): for i, t, l, f in self.save_timers: if t + l < time.time(): with self.keyboard.pressed(Key.shift): self._press_key(f) logger.info("saved state to " + str(f) + " after " + str(l) + " seconds") self.save_timers[i][1] = time.time() def _load(self): self._press_key(Key.f1) logger.debug("loaded gamestate from f1") def room_handler(self, room_alias): # Only room created is #matrixplayspokemon logger.info("homeserver asked for " + room_alias) return room_alias == self.config["room_alias"] def user_handler(self, mxid): # Only user is as_user.mxid logger.info("homeserver asked for " + mxid) return mxid == self.config["user_id"] def transaction_handler(self, event_stream): for event in event_stream: if (event.id == self.room_id and event.type == "m.room.message" and event.content["msgtype"] == "m.text"): content = event.content["body"].lower() if content == "a": self._press_key("x") elif content == "b": self._press_key("z") elif content == "l": self._press_key("a") elif content == "r": self._press_key("s") elif content == "up": self._press_key(Key.up) elif content == "down": self._press_key(Key.down) elif content == "left": self._press_key(Key.left) elif content == "right": self._press_key(Key.right) elif content == "start": self._press_key(Key.enter) elif content == "select": self._press_key(Key.backspace) elif content == "dump" and self.config["debug"]: logger.debug("received dump command") self._dump() elif content == "save" and self.config["debug"]: logger.debug("received save command") with self.keyboard.pressed(Key.shift): self._press_key(Key.f1) logger.debug("saved current gamestate to f1") elif content == "load" and self.config["debug"]: logger.debug("received load command") self._load() logger.debug("handled " + event.type + #" from " + event.mxid + " in " + event.id) self.send_screenshot() self._save() return True def _press_key(self, key): self.keyboard.press(key) # make sure the key is pressed long enought to register with mgba time.sleep(0.05) self.keyboard.release(key) logger.debug("pressed key: " + str(key)) def _dump(self): self.pkmn.stop() logger.warning("mgba stdout: " + self.pkmn.stdout) logger.warning("mgba stderr: " + self.pkmn.stderr) self._start_mgba()
def test_stop(self): p = EasyProcess('ls -la').start() time.sleep(0.2) eq_(p.stop().return_code, 0) eq_(p.stop().return_code, 0) eq_(p.stop().return_code, 0)
def test_vncserver(): p = EasyProcess([python, "-m", "pyvirtualdisplay.examples.vncserver"]).start() sleep(1) assert p.is_alive() p.stop()
def test_stop(): p = EasyProcess("ls -la").start() time.sleep(0.2) assert p.stop().return_code == 0 assert p.stop().return_code == 0 assert p.stop().return_code == 0