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 __init__(self): if "Darwin" not in platform.platform(): raise RunProgError("This backend runs only on Darwin") p = EasyProcess([PROGRAM, "-help"]) p.enable_stdout_log = False p.enable_stderr_log = False p.call()
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_time_cli2(self): p = EasyProcess([ python, '-c', "import logging;logging.basicConfig(level=logging.DEBUG);from easyprocess import EasyProcess;EasyProcess('sleep 5').call(timeout=0.5)" ]) p.call() eq_(p.return_code, 0)
def backend_version(self): p = EasyProcess([PROGRAM, "-help"]) p.enable_stdout_log = False p.enable_stderr_log = False p.call() if p.return_code == 0: return UNKNOWN_VERSION
def get_helptext(program): p = EasyProcess([program, "-help"]) p.enable_stdout_log = False p.enable_stderr_log = False p.call() helptext = p.stderr return helptext
def backend_version(self): # grim doesn't have a version flag for some reason p = EasyProcess([PROGRAM, "-help"]) p.enable_stdout_log = False p.enable_stderr_log = False p.call() if p.return_code == 0: return UNKNOWN_VERSION
def test_time_cli2(): p = EasyProcess([ python, "-c", "import logging;logging.basicConfig(level=logging.DEBUG);from easyprocess import EasyProcess;EasyProcess('sleep 5').call(timeout=0.5)", ]) p.call() assert p.return_code == 0
def __init__(self): if sys.platform == "darwin": raise ImagemagickBackendError("osx not supported") # TODO p = EasyProcess([PROGRAM, "-version"]) p.enable_stdout_log = False p.enable_stderr_log = False p.call()
def check_installed(cls): p = EasyProcess([PROGRAM, "-help"]) p.enable_stdout_log = False p.enable_stderr_log = False p.call() helptext = p.stdout cls.has_displayfd = "-displayfd" in helptext cls.has_resizeable = "-resizeable" in helptext
def backend_version(self): p = EasyProcess([PROGRAM, "-help"]) p.enable_stdout_log = False p.enable_stderr_log = False p.call() if p.return_code == 0: # TODO: get real version return "0.0"
def prog_check(cmd): try: p = EasyProcess(cmd) p.enable_stdout_log = False p.enable_stderr_log = False p.call() return p.return_code == 0 except Exception: return False
def test_screenshot(): owd = os.getcwd() with TemporaryDirectory(prefix="pyvirtualdisplay_") as tmpdirname: try: os.chdir(tmpdirname) p = EasyProcess([python, "-m", "pyvirtualdisplay.examples.screenshot"]) p.call() assert p.return_code == 0 finally: os.chdir(owd)
def run_prog(fpng, bbox=None): fxwd = fpng + ".xwd" pxwd = EasyProcess([PROGRAM, "-root", "-out", fxwd]) pxwd.call() if pxwd.return_code != 0: raise RunProgError(pxwd.stderr) pconvert = EasyProcess(["convert", "xwd:" + fxwd, fpng]) pconvert.call() if pconvert.return_code != 0: raise RunProgError(pconvert.stderr)
def is_installed(): ''' Return whether or not xauth is installed. ''' try: p = EasyProcess(['xauth', '-V']) p.enable_stdout_log = False p.enable_stderr_log = False p.call() except Exception: return False else: return True
def is_installed(): """ Return whether or not xauth is installed. """ try: p = EasyProcess(["xauth", "-V"]) p.enable_stdout_log = False p.enable_stderr_log = False p.call() except Exception: return False else: return True
def display_size(): if platform_is_osx(): from Quartz import CGDisplayBounds from Quartz import CGMainDisplayID mainMonitor = CGDisplayBounds(CGMainDisplayID()) return int(mainMonitor.size.width), int(mainMonitor.size.height) if platform_is_win(): from win32api import GetSystemMetrics return int(GetSystemMetrics(0)), int(GetSystemMetrics(1)) if platform_is_linux(): # http://www.cyberciti.biz/faq/how-do-i-find-out-screen-resolution-of-my-linux-desktop/ # xdpyinfo | grep 'dimensions:' screen_width, screen_height = 0, 0 xdpyinfo = EasyProcess("xdpyinfo") xdpyinfo.enable_stdout_log = False if xdpyinfo.call().return_code != 0: raise ValueError("xdpyinfo error: %s" % xdpyinfo) for x in xdpyinfo.stdout.splitlines(): if "dimensions:" in x: screen_width, screen_height = map(int, x.strip().split()[1].split("x")) return screen_width, screen_height
def sub_command(cmd_str, timeout=30, debug=False): """Run a command as child process using a timeout""" n = 3 while n: cmd = EasyProcess(cmd_str) cmd.call(timeout=timeout) if cmd.timeout_happened: info("Command failed due to timeout, retry!", debug) if n == 1: raise TimeoutError(u"Erreur lors de la commande: %s, temps de réponse "\ "trop long." % cmd_str) n -= 1 elif cmd.return_code or cmd.oserror: info("Command failed due unknown error, retry!", debug) if n == 1: raise CmdError(u"%s" % (cmd.stderr)) n -= 1 else: break return cmd.stdout
def sub_command(cmd_str, timeout=30, debug=False): """Run a command as child process using a timeout""" n = 3 while n: cmd = EasyProcess(cmd_str) cmd.call(timeout=timeout) if cmd.timeout_happened: info("Command failed due to timeout, retry!", debug) if n == 1: raise TimeoutError(u"Erreur lors de la commande: %s, temps de réponse "\ "trop long." % cmd_str) n -= 1 elif cmd.return_code or cmd.oserror: info("Command failed due unknown error, retry!", debug) if n == 1: raise CmdError(u"%s" % (cmd.stderr.decode('utf-8', errors='ignore'))) n -= 1 else: break return cmd.stdout
def display_size_x(): # http://www.cyberciti.biz/faq/how-do-i-find-out-screen-resolution-of-my-linux-desktop/ # xdpyinfo | grep 'dimensions:' screen_width, screen_height = 0, 0 xdpyinfo = EasyProcess("xdpyinfo") xdpyinfo.enable_stdout_log = False if xdpyinfo.call().return_code != 0: raise ValueError("xdpyinfo error: %s" % xdpyinfo) for x in xdpyinfo.stdout.splitlines(): if "dimensions:" in x: screen_width, screen_height = map(int, x.strip().split()[1].split("x")) return screen_width, screen_height
def display_size(): if sys.platform == "darwin": from Quartz import CGDisplayBounds from Quartz import CGMainDisplayID mainMonitor = CGDisplayBounds(CGMainDisplayID()) return (mainMonitor.size.width, mainMonitor.size.height) # http://www.cyberciti.biz/faq/how-do-i-find-out-screen-resolution-of-my-linux-desktop/ # xdpyinfo | grep 'dimensions:' screen_width, screen_height = None, None xdpyinfo = EasyProcess("xdpyinfo") xdpyinfo.enable_stdout_log = False for x in xdpyinfo.call().stdout.splitlines(): if "dimensions:" in x: screen_width, screen_height = map(int, x.strip().split()[1].split("x")) return screen_width, screen_height
def __init__(self): p = EasyProcess([PROGRAM, '--version']) p.enable_stdout_log = False p.enable_stderr_log = False p.call()
def check_installed(cls): p = EasyProcess([PROGRAM, '-help']) p.enable_stdout_log = False p.enable_stderr_log = False p.call()
def _start1(self): with _mutex: self.display = _search_for_display() while self.display in _USED_DISPLAY_NR_LIST: self.display += 1 self.new_display_var = ":%s" % int(self.display) _USED_DISPLAY_NR_LIST.append(self.display) self._command = self._cmd() + self._extra_args log.debug("command: %s", self._command) self._popen(use_pass_fds=False) self.new_display_var = ":%s" % int(self.display) if self._use_xauth: self._setup_xauth() # https://github.com/ponty/PyVirtualDisplay/issues/2 # https://github.com/ponty/PyVirtualDisplay/issues/14 self.old_display_var = os.environ.get("DISPLAY", None) # wait until X server is active start_time = time.time() d = self.new_display_var ok = False time.sleep(0.05) # give time for early exit while True: if not self.is_alive(): break try: xdpyinfo = EasyProcess(["xdpyinfo"], env=self._env()) xdpyinfo.enable_stdout_log = False xdpyinfo.enable_stderr_log = False exit_code = xdpyinfo.call().return_code except EasyProcessError: log.warning( "xdpyinfo was not found, X start can not be checked! Please install xdpyinfo!" ) time.sleep(_X_START_WAIT) # old method ok = True break if exit_code != 0: pass else: log.info('Successfully started X with display "%s".', d) ok = True break if time.time() - start_time >= _X_START_TIMEOUT: break time.sleep(_X_START_TIME_STEP) if not self.is_alive(): log.warning("process exited early. stderr:%s", self.stderr) msg = "Failed to start process: %s" raise XStartError(msg % self) if not ok: msg = 'Failed to start X on display "%s" (xdpyinfo check failed, stderr:[%s]).' raise XStartTimeoutError(msg % (d, xdpyinfo.stderr))
def run_prog(filename, bbox=None): p = EasyProcess(cmd + [filename]) p.call() if p.return_code != 0: raise RunProgError(p.stderr)
def backend_version(self): p = EasyProcess([PROGRAM, "--version"]) p.enable_stdout_log = False p.enable_stderr_log = False p.call() return extract_version(p.stdout.replace("-", " "))
def _start1(self): if self.has_displayfd: # stdout doesn't work on osx -> create own pipe rfd, self.pipe_wfd = os.pipe() else: with mutex: self.display = search_for_display(randomizer=self.randomizer) while self.display in USED_DISPLAY_NR_LIST: self.display += 1 self.new_display_var = ":%s" % int(self.display) USED_DISPLAY_NR_LIST.append(self.display) self.command = self._cmd() + self.extra_args log.debug("command: %s", self.command) self._stdout_file = tempfile.TemporaryFile(prefix="stdout_") self._stderr_file = tempfile.TemporaryFile(prefix="stderr_") if py2() or not self.has_displayfd: self.subproc = subprocess.Popen( self.command, stdout=self._stdout_file, stderr=self._stderr_file, shell=False, ) else: if self.has_displayfd: self.subproc = subprocess.Popen( self.command, pass_fds=[self.pipe_wfd], stdout=self._stdout_file, stderr=self._stderr_file, shell=False, ) if self.has_displayfd: # rfd = self.subproc.stdout.fileno() self.display = int(self.wait_for_pipe_text(rfd)) os.close(rfd) os.close(self.pipe_wfd) self.new_display_var = ":%s" % int(self.display) if self.use_xauth: self._setup_xauth() # https://github.com/ponty/PyVirtualDisplay/issues/2 # https://github.com/ponty/PyVirtualDisplay/issues/14 self.old_display_var = os.environ.get("DISPLAY", None) # wait until X server is active start_time = time.time() # if self.check_startup: # rp = self._check_startup_fd # display_check = None # rlist, wlist, xlist = select.select((rp,), (), (), X_START_TIMEOUT) # if rlist: # display_check = os.read(rp, 10).rstrip() # else: # msg = "No display number returned by X server" # raise XStartTimeoutError(msg) # dnbs = str(self.display) # if bytes != str: # dnbs = bytes(dnbs, "ascii") # if display_check != dnbs: # msg = 'Display number "%s" not returned by X server' + str( # display_check # ) # raise XStartTimeoutError(msg % self.display) if not self.has_displayfd: self.redirect_display(True) # for xdpyinfo d = self.new_display_var ok = False time.sleep(0.05) # give time for early exit while True: if not self.is_alive(): break try: xdpyinfo = EasyProcess(["xdpyinfo"]) xdpyinfo.enable_stdout_log = False xdpyinfo.enable_stderr_log = False exit_code = xdpyinfo.call().return_code except EasyProcessError: log.warning( "xdpyinfo was not found, X start can not be checked! Please install xdpyinfo!" ) time.sleep(X_START_WAIT) # old method ok = True break if exit_code != 0: pass else: log.info('Successfully started X with display "%s".', d) ok = True break if time.time() - start_time >= X_START_TIMEOUT: break time.sleep(X_START_TIME_STEP) if not self.is_alive(): log.warning("process exited early. stderr:%s", self.stderr) msg = "Failed to start process: %s" raise XStartError(msg % self) if not ok: msg = 'Failed to start X on display "%s" (xdpyinfo check failed, stderr:[%s]).' raise XStartTimeoutError(msg % (d, xdpyinfo.stderr)) return self
def run(self): """ Runs PDDL planner given the target domain and problem. """ start_time = time.time() self._verbprint('Calling the planner.') # print(self.planner_path) self.result = None if not self.incremental: # Write out the domain and problem files. temp_dir = mkdtemp(prefix='planner_temp') # domain_file = self.domain_file domain_file = os.path.join(self.script_dir, 'model', 'domain.pddl') # with open(domain_file, 'w') as f: # f.write(self.domain_file_contents()) # problem_file = os.path.join(self.script_dir, 'model', 'problem.pddl') problem_file = os.path.join(temp_dir, 'problem.pddl') with open(problem_file, 'w') as f: f.write(self.problem.to_pddl()) # build command command = [] # command: timeout elelements # command.extend(['timeout', str(self.timeout)]) # command: planner command.append(self.planner_path) # command: planner's optional parameters if not self.optimize: command.append('-N') command.extend([domain_file, problem_file]) self._verbprint('Command line: ' + str(command)) # run command # using easyprocess process = EasyProcess(command) out = process.call(timeout=self.timeout).stdout if process.timeout_happened: self._verbprint('TIME OUT!!') self.timeout_happened = True # using subprocess32 # try: # out = check_output(command, timeout=3) # except TimeoutExpired as e: # print e # out = e.output # self.timeout_happened = True else: if 'mimimal_total_reward' not in self.problem.specs['Options']: self.problem.specs['Options'].update( {'mimimal_total_reward': 0}) max_reward_value = self.problem.get_maximun_reward_value() max_reward_reached = False current_total_reward = self.problem.specs['Options'][ 'mimimal_total_reward'] reward_increment = min([ reward for task, reward in self.problem.specs['Tasks'] ['TaskReward'].items() if reward > 0 ]) current_timeout = self.timeout - (time.time() - start_time) iteration_index = 0 while current_timeout > 0 and not max_reward_reached: self._verbprint('##########################') self._verbprint('Iteration: ' + str(iteration_index + 1)) print('Iteration: ' + str(iteration_index + 1)) self._verbprint(max_reward_value) self._verbprint('##########################') # Write out the domain and problem files. temp_dir = mkdtemp(prefix='planner_temp') # domain_file = self.domain_file domain_file = os.path.join(self.script_dir, 'model', 'domain.pddl') # with open(domain_file, 'w') as f: # f.write(self.domain_file_contents()) # problem_file = os.path.join(self.script_dir, 'model', 'problem.pddl') problem_file = os.path.join(temp_dir, 'problem.pddl') with open(problem_file, 'w') as f: f.write(self.problem.to_pddl()) # build command command = [] # command: timeout elelements # command.extend(['timeout', str(self.timeout)]) # command: planner command.append(self.planner_path) # command: planner's optional parameters self._verbprint('##############') command.append('-N') command.extend([domain_file, problem_file]) self._verbprint('Command line: ' + str(command)) # compute time left for the planner (seconds) current_timeout = self.timeout - (time.time() - start_time) self._verbprint('Iteration timeout: ' + str(current_timeout) + ' s') if current_timeout <= 0: break # run command # using easyprocess process = EasyProcess(command) out = process.call(timeout=current_timeout).stdout if process.timeout_happened: self._verbprint('TIME OUT!!') self.timeout_happened = True break plan_start_index = out.find(';;;; Solution Found') if plan_start_index != -1: # print(out[plan_start_index:]) self.result = self.pddl_plan_to_schedule( out[plan_start_index:]) evaluation = evaluate_schedule( PUFFERPDDLTranslator.clean_up_schedule(self.result), self.problem.specs) self._verbprint(evaluation) if evaluation['total_task_reward'] >= max_reward_value: self._verbprint('MAX REWARD REACHED!!! reward = ' + str(max_reward_value)) max_reward_reached = True break # increment reward self.problem.specs['Options'][ 'mimimal_total_reward'] = evaluation[ 'total_task_reward'] + reward_increment iteration_index += 1 # get output plan # print out if self.optimize and not self.incremental: planner_output = out.split('\n\n') self._verbprint(planner_output) for i in range(len(planner_output) - 1, -1, -1): plan_start_index = planner_output[i].find('; Plan found') if plan_start_index == -1: plan_start_index = planner_output[i].find( ';;;; Solution Found') if plan_start_index != -1: self.result = self.pddl_plan_to_schedule( planner_output[i][plan_start_index:]) self._verbprint(planner_output[i][plan_start_index:]) return else: plan_start_index = out.find(';;;; Solution Found') if plan_start_index != -1: # print(out[plan_start_index:]) self.result = self.pddl_plan_to_schedule( out[plan_start_index:]) self._verbprint(out[plan_start_index:]) return
def _grab_to_file(self, filename): command = [PROGRAM, '-f', filename] p = EasyProcess(command) p.call() if p.return_code != 0: raise GnomeScreenshotBackendError(p.stderr)
def start(self): """ start display :rtype: self """ if self.use_xauth: self._setup_xauth() EasyProcess.start(self) time.sleep(0.01) # https://github.com/ponty/PyVirtualDisplay/issues/2 # https://github.com/ponty/PyVirtualDisplay/issues/14 self.old_display_var = os.environ.get("DISPLAY", None) self.redirect_display(True) # wait until X server is active start_time = time.time() if self.check_startup: rp = self._check_startup_fd display_check = None rlist, wlist, xlist = select.select((rp, ), (), (), X_START_TIMEOUT) if rlist: display_check = os.read(rp, 10).rstrip() else: msg = "No display number returned by X server" raise XStartTimeoutError(msg) dnbs = str(self.display) if bytes != str: dnbs = bytes(dnbs, "ascii") if display_check != dnbs: msg = 'Display number "%s" not returned by X server' + str( display_check) raise XStartTimeoutError(msg % self.display) d = self.new_display_var ok = False while True: if not EasyProcess.is_alive(self): break try: xdpyinfo = EasyProcess("xdpyinfo") xdpyinfo.enable_stdout_log = False xdpyinfo.enable_stderr_log = False exit_code = xdpyinfo.call().return_code except EasyProcessError: log.warning( "xdpyinfo was not found, X start can not be checked! Please install xdpyinfo!" ) time.sleep(X_START_WAIT) # old method ok = True break if exit_code != 0: pass else: log.info('Successfully started X with display "%s".', d) ok = True break if time.time() - start_time >= X_START_TIMEOUT: break time.sleep(X_START_TIME_STEP) if not EasyProcess.is_alive(self): log.warning("process exited early", ) msg = "Failed to start process: %s" raise XStartError(msg % self) if not ok: msg = 'Failed to start X on display "%s" (xdpyinfo check failed, stderr:[%s]).' raise XStartTimeoutError(msg % (d, xdpyinfo.stderr)) return self
def test_time_cli2(self): p = EasyProcess([python, '-c', "import logging;logging.basicConfig(level=logging.DEBUG);from easyprocess import EasyProcess;EasyProcess('sleep 5').call(timeout=0.5)"]) p.call() eq_(p.return_code, 0)
template = """#!/usr/bin/expect set timeout 20 eval spawn sudo dokku apps:destroy {theme} expect "password" send "$env(SPASSWD) "; expect " ! WARNING: Potentially Destructive Action" expect " ! This command will destroy {theme} (including all add-ons)." expect " ! To proceed, type \\"{theme}\\"" expect "\r\n" expect "> " send "{theme}\r" interact """ for theme in themes_to_remove: theme = theme.lower() filepath = './destroy/{}-command'.format(theme) expect_command = template.format(theme=theme) with open(filepath, 'w') as fh: fh.write( expect_command.format(app=theme) ) os.chmod(filepath, 0755) command = filepath process = EasyProcess(command) result = process.call() logger.debug(result.stderr) logger.debug(result.stdout)
last_temp = uptime.uptime() if info_interval and (uptime.uptime()-info_interval) >= last_info : info("Try to collect informations") msg = sub_command("%s -i %s" % (script_path, args[0]), timeout=cfg.cmd.timeout, debug=cfg.debug) info("Information done", cfg.debug) last_info = uptime.uptime() except TimeoutError, e: date = datetime.now() err_msg = "%s: %s %s\n Rebooting...\n" % (date, type(e), e) sys.stderr.write(err_msg) sys.stderr.flush() time.sleep(3) REBOOT_CMD.call() except Exception, e: date = datetime.now() err_msg = u"%s\n\n%s\n%s" % (e, date, type(e)) sys.stderr.write(err_msg) sys.stderr.flush() errors.append(err_msg) if (uptime.uptime()-error_interval) >= last_error : info("Send email with %s" % "\n".join(errors), cfg.debug) try: email.send("Erreur de %s" % cfg.host_name, "\n".join(errors)) except: date = datetime.now() err_msg = "%s: %s %s\n" % (date, type(e), str(e))