def test_path_search_which(self): " which() finds an executable in $PATH and returns its abspath. " fname = 'gcc' bin_dir = tempfile.mkdtemp() bin_path = os.path.join(bin_dir, fname) save_path = os.environ['PATH'] try: # setup os.environ['PATH'] = bin_dir with open(bin_path, 'w') as fp: pass # given non-executable, os.chmod(bin_path, 0o400) # exercise absolute and relative, assert pexpect.which(bin_path) is None assert pexpect.which(fname) is None # given executable, os.chmod(bin_path, 0o700) # exercise absolute and relative, assert pexpect.which(bin_path) == bin_path assert pexpect.which(fname) == bin_path finally: # restore, os.environ['PATH'] = save_path # destroy scratch files and folders, if os.path.exists(bin_path): os.unlink(bin_path) if os.path.exists(bin_dir): os.rmdir(bin_dir)
def test_absolute_which(self): # make up a path and insert first a non-executable, # then, make it executable, and assert we may which() find it. fname = 'gcc' bin_dir = tempfile.mkdtemp() bin_path = os.path.join(bin_dir, fname) save_path = os.environ['PATH'] try: # setup os.environ['PATH'] = bin_dir with open(bin_path, 'w') as fp: fp.write('#!/bin/sh\necho hello, world\n') os.chmod(bin_path, 0o400) # it should not be found because it is not executable assert pexpect.which(fname) is None, fname # but now it should -- because it is executable os.chmod(bin_path, 0o700) assert pexpect.which(fname) == bin_path, (fname, bin_path) finally: # restore, os.environ['PATH'] = save_path # destroy scratch files and folders, if os.path.exists(bin_path): os.unlink(bin_path) if os.path.exists(bin_dir): os.rmdir(bin_dir)
def test_which_should_match_other_group_user(self): " which() returns executables by other, group, and user ownership. " # create an executable and test that it is found using which() for # each of the 'other', 'group', and 'user' permission bits. fname = 'g77' bin_dir = tempfile.mkdtemp() bin_path = os.path.join(bin_dir, fname) save_path = os.environ['PATH'] try: # setup os.environ['PATH'] = bin_dir with open(bin_path, 'w') as fp: fp.write('#!/bin/sh\necho hello, world\n') for should_match, mode in ((False, 0o000), (True, 0o005), (True, 0o050), (True, 0o500), (False, 0o004), (False, 0o040), (False, 0o400)): os.chmod(bin_path, mode) if not should_match: # should not be found because it is not executable assert pexpect.which(fname) is None else: # should match full path assert pexpect.which(fname) == bin_path finally: # restore, os.environ['PATH'] = save_path # destroy scratch files and folders, if os.path.exists(bin_path): os.unlink(bin_path) if os.path.exists(bin_dir): os.rmdir(bin_dir)
def test_which_follows_symlink(self): " which() follows symlinks and returns its path. " fname = 'original' symname = 'extra-crispy' bin_dir = tempfile.mkdtemp() bin_path = os.path.join(bin_dir, fname) sym_path = os.path.join(bin_dir, symname) save_path = os.environ['PATH'] try: # setup os.environ['PATH'] = bin_dir with open(bin_path, 'w') as fp: pass os.chmod(bin_path, 0o400) os.symlink(bin_path, sym_path) # should not be found because symlink points to non-executable assert pexpect.which(symname) is None # but now it should -- because it is executable os.chmod(bin_path, 0o700) assert pexpect.which(symname) == sym_path finally: # restore, os.environ['PATH'] = save_path # destroy scratch files, symlinks, and folders, if os.path.exists(sym_path): os.unlink(sym_path) if os.path.exists(bin_path): os.unlink(bin_path) if os.path.exists(bin_dir): os.rmdir(bin_dir)
def call_editor(filename): editor = os.getenv("EDITOR") if editor is None: editor = pexpect.which("vim") if editor is None: editor = pexpect.which("vi") if editor is None: raise StandardError("Can't find an editor") os.system("%s %s" % (editor, filename))
def test_which (self): p = os.defpath ep = os.environ['PATH'] os.defpath = ":/tmp" os.environ['PATH'] = ":/tmp" wp = pexpect.which ("ticker.py") assert wp == 'ticker.py', "Should return a string. Returned %s" % wp os.defpath = "/tmp" os.environ['PATH'] = "/tmp" wp = pexpect.which ("ticker.py") assert wp == None, "Executable should not be found. Returned %s" % wp os.defpath = p os.environ['PATH'] = ep
def test_which(self): p = os.defpath ep = os.environ['PATH'] os.defpath = ":/tmp" os.environ['PATH'] = ":/tmp" wp = pexpect.which("ticker.py") assert wp == 'ticker.py', "Should return a string. Returned %s" % wp os.defpath = "/tmp" os.environ['PATH'] = "/tmp" wp = pexpect.which("ticker.py") assert wp == None, "Executable should not be found. Returned %s" % wp os.defpath = p os.environ['PATH'] = ep
def __init__(self, gdb='riscv64-unknown-elf-gdb', openocd='openocd', openocd_config_filename='./testing/targets/ssith_gfe.cfg', timeout=60): print_and_log("Starting GDB session...") if not which(gdb): raise GdbError('Executable {} not found'.format(gdb)) if not which(openocd): raise GdbError('Executable {} not found'.format(openocd)) xlen = 32 # Default try: run_and_log(print_and_log("Starting openocd"), run([openocd, '-f', openocd_config_filename], \ timeout=0.5, stdout=PIPE, stderr=PIPE)) except TimeoutExpired as exc: log = str(exc.stderr, encoding='utf-8') match = re.search('XLEN=(32|64)', log) if match: xlen = int(match.group(1)) else: raise GdbError('XLEN not found by OpenOCD') # Create temporary files for gdb and openocd. The `NamedTemporaryFile` # objects are stored in `self` so the files stay around as long as this # `GdbSession` is in use. We open both in text mode, instead of the # default 'w+b'. self.openocd_log = tempfile.NamedTemporaryFile(mode='w', prefix='openocd.', suffix='.log') self.gdb_log = tempfile.NamedTemporaryFile(mode='w', prefix='gdb.', suffix='.log') init_cmds = '\n'.join([ 'set confirm off', 'set pagination off' 'set width 0', 'set height 0', 'set print entry-values no', 'set remotetimeout {}'.format(timeout), 'set arch riscv:rv{}'.format(xlen), 'target remote | {} -c "gdb_port pipe; log_output {}" -f {}'. format(openocd, self.openocd_log.name, openocd_config_filename) ]) self.pty = spawn(gdb, encoding='utf-8', logfile=self.gdb_log, timeout=timeout) self.repl = REPLWrapper(self.pty, '(gdb) ', None, extra_init_cmd=init_cmds)
def test_os_defpath_which(self): " which() finds an executable in $PATH and returns its abspath. " bin_dir = tempfile.mkdtemp() if sys.getfilesystemencoding() in ("ascii", "ANSI_X3.4-1968"): prefix = "ascii-" else: prefix = u"ǝpoɔıun-" temp_obj = tempfile.NamedTemporaryFile(suffix=u".sh", prefix=prefix, dir=bin_dir, delete=False) bin_path = temp_obj.name fname = os.path.basename(temp_obj.name) save_path = os.environ["PATH"] save_defpath = os.defpath try: # setup os.environ["PATH"] = "" os.defpath = bin_dir with open(bin_path, "w") as fp: pass # given non-executable, os.chmod(bin_path, 0o400) # exercise absolute and relative, assert pexpect.which(bin_path) is None assert pexpect.which(fname) is None # given executable, os.chmod(bin_path, 0o700) # exercise absolute and relative, assert pexpect.which(bin_path) == bin_path assert pexpect.which(fname) == bin_path finally: # restore, os.environ["PATH"] = save_path os.defpath = save_defpath # destroy scratch files and folders, if os.path.exists(bin_path): os.unlink(bin_path) if os.path.exists(bin_dir): os.rmdir(bin_dir)
async def test_telnet_client_cmdline(bind_host, unused_tcp_port, event_loop): """Test executing telnetlib3/client.py as client""" # this code may be reduced when pexpect asyncio is bugfixed .. # we especially need pexpect to pass sys.stdin.isatty() test. prog = pexpect.which('telnetlib3-client') args = [prog, bind_host, str(unused_tcp_port), '--loglevel=info', '--connect-minwait=0.05', '--connect-maxwait=0.05'] class HelloServer(asyncio.Protocol): def connection_made(self, transport): super().connection_made(transport) transport.write(b'hello, space cadet.\r\n') # hangup event_loop.call_soon(transport.close) # start vanilla tcp server await event_loop.create_server(HelloServer, bind_host, unused_tcp_port) proc = await asyncio.create_subprocess_exec( *args, loop=event_loop, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE) line = await asyncio.wait_for(proc.stdout.readline(), 1.5) assert line.strip() == b"Escape character is '^]'." line = await asyncio.wait_for(proc.stdout.readline(), 1.5) assert line.strip() == b'hello, space cadet.' # message received, expect the client to gracefully quit. out, err = await asyncio.wait_for(proc.communicate(), 1) assert out == b'\x1b[m\nConnection closed by foreign host.\n'
def req_check(self): # check pexpect # check BlueZ # gatttool path #self.gatttool_path = "/usr/local/bluez5/attrib/gatttool" self.gatttool_path = pexpect.which('gatttool') if not self.gatttool_path: return "Please install the BlueZ stack: 'apt-get install bluez'" # if there is no MAC set, try to search for a drone (requires root) if not self.mac: if os.geteuid() != 0: return "Drone MAC missing. Init with 'mac=' or run as root to scan." lescan = pexpect.spawn("hcitool lescan") index = lescan.expect(P_OUIS, timeout=5) if index != 0: return "Couldn't find any drones nearby." self.mac = lescan.after self.cb(0, "Drone found! MAC: " + self.mac) elif not re.match(P_MAC, self.mac): return "Drone MAC format incorrect, please check it. Format: XX:XX:XX:XX:XX:XX" self.cb(0, "Everything seems to be alright, time to fly!") return None
def test_newtool(tmpdir): """ Run 'pytool newtool testtool' while testtool.py does not exist. Verify that testtool.py is created and has the right contents. """ pytest.debug_func() toolname = toyfile(tmpdir, "testtool.py") toollink = toyfile(tmpdir, "testtool") with U.Chdir(tmpdir.strpath): cmd = pexpect.which("pytool") if cmd is None: pytest.skip("pytool not found") S = pexpect.spawn("{} newtool {} tt".format(cmd, toollink.strpath)) which = S.expect([r'Are you sure\? >', 'Error:', pexpect.EOF]) if which == 0: S.sendline('no') S.expect(pexpect.EOF) pytest.fail('should not have asked about overwriting') elif which == 1: print S.before + S.after pytest.fail('unexpected exception') assert toolname.strpath == toollink.readlink() expected = expected_testtool_py() got = toolname.read().split("\n") assert got == expected
def test_newpy_overwriting_no(tmpdir): """ Run 'pytool newpy xyzzy' when xyzzy already exists. Verify that confirmation is requested. Answer 'no' and verify that the existing file is not overwritten. """ pytest.debug_func() xyzzy = toyfile(tmpdir, "xyzzy", content="original xyzzy\n") xyzzy_py = toyfile(tmpdir, "xyzzy.py", content="original xyzzy.py\n") with U.Chdir(tmpdir.strpath): cmd = pexpect.which("pytool") S = pexpect.spawn("{} newpy xyzzy".format(cmd)) which = S.expect([r'you sure\? >', 'Error:', pexpect.EOF]) if which == 0: S.sendline('no') S.expect(pexpect.EOF) elif which == 1: print S.before + S.after pytest.fail('unexpected exception') else: pytest.fail('should have asked about overwriting xyzzy') expected = ['original xyzzy'] got = U.contents(xyzzy.strpath) assert got == expected expected = ['original xyzzy.py'] got = U.contents(xyzzy_py.strpath) assert got == expected
def test_newpy_overwriting_yes(tmpdir): """ Run 'pytool newpy xyzzy' when xyzzy, xyzzy.py already exist. Verify that confirmation is requested. Answer 'yes' and verify that the existing file IS overwritten. """ pytest.debug_func() xyzzy = toyfile(tmpdir, "xyzzy", content="original xyzzy") xyzzy_py = toyfile(tmpdir, "xyzzy.py", content="original xyzzy.py") with U.Chdir(tmpdir.strpath): cmd = pexpect.which('pytool') S = pexpect.spawn('%s newpy %s' % (cmd, xyzzy.strpath)) which = S.expect([r'Are you sure\? >', 'Error:', pexpect.EOF]) if which == 0: S.sendline('yes') S.expect(pexpect.EOF) elif which == 1: print S.before + S.after pytest.fail('unexpected exception') else: pytest.fail('should have asked about overwriting xyzzy') exp = os.path.abspath(xyzzy_py.strpath) got = os.readlink(xyzzy.strpath) assert got == exp expected = expected_xyzzy_py() got = U.contents(xyzzy_py.strpath) assert got == expected
def check_missing_requirements (): """This list of missing requirements (mencoder, mplayer, lame, and mkvmerge). Returns None if all requirements are in the execution path. """ missing = [] if pexpect.which("mencoder") is None: missing.append("mencoder") if pexpect.which("mplayer") is None: missing.append("mplayer") #if pexpect.which("lame") is None: # missing.append("lame") #if pexpect.which("mkvmerge") is None: # missing.append("mkvmerge") if len(missing)==0: return None return missing
def test_newtool_overwriting_yes(tmpdir): """ Run 'pytool newtool testtool tt' while testtool.py exists. Verify that we are prompted about overwriting. Answer 'yes' and verify that the original file is overwritten. """ pytest.debug_func() tname = toyfile(tmpdir, "testtool.py", content=["original testtool.py"]) tlink = toyfile(tmpdir, "testtool", content=["original testtool"]) with U.Chdir(tmpdir.strpath): cmd = pexpect.which("pytool") if cmd is None: pytest.skip("pytool not found") S = pexpect.spawn("{} newtool {} tt".format(cmd, tlink)) which = S.expect([r'Are you sure\? >', 'Error:', pexpect.EOF]) if which == 0: S.sendline('yes') S.expect(pexpect.EOF) elif which == 1: print S.before + S.after pytest.fail('unexpected exception') else: pytest.fail("should have asked about overwriting {}".format(tname)) exp = tname.strpath actual = tlink.readlink() assert actual == exp expected = expected_testtool_py() got = tname.read().split("\n") assert got == expected
def sh(self): if self._sh is None: self._sh = pexpect.which('sh') if self._sh is None: raise OSError('"sh" shell not found') return self._sh
async def test_telnet_client_cmdline(bind_host, unused_tcp_port): """Test executing asynctelnet/client.py as client""" # this code may be reduced when pexpect asyncio is bugfixed .. # we especially need pexpect to pass sys.stdin.isatty() test. prog = pexpect.which('asynctelnet-client') args = [ prog, bind_host, str(unused_tcp_port), '--loglevel=info', '--connect-minwait=0.05', '--connect-maxwait=0.05' ] class HelloServer(asyncio.Protocol): def connection_made(self, transport): super().connection_made(transport) transport.write(b'hello, space cadet.\r\n') # hangup event_loop.call_soon(transport.close) # start vanilla tcp server await event_loop.create_server(HelloServer, bind_host, unused_tcp_port) proc = await asyncio.create_subprocess_exec(*args, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE) line = await asyncio.wait_for(proc.stdout.readline(), 1.5) assert line.strip() == b"Escape character is '^]'." line = await asyncio.wait_for(proc.stdout.readline(), 1.5) assert line.strip() == b'hello, space cadet.' # message received, expect the client to gracefully quit. out, err = await asyncio.wait_for(proc.communicate(), 1) assert out == b'\x1b[m\nConnection closed by foreign host.\n'
def test_which_should_not_match_folders(self): " Which does not match folders, even though they are executable. " # make up a path and insert a folder that is 'executable', a naive # implementation might match (previously pexpect versions 3.2 and # sh versions 1.0.8, reported by @lcm337.) fname = 'g++' bin_dir = tempfile.mkdtemp() bin_dir2 = os.path.join(bin_dir, fname) save_path = os.environ['PATH'] try: os.environ['PATH'] = bin_dir os.mkdir(bin_dir2, 0o755) # should not be found because it is not executable *file*, # but rather, has the executable bit set, as a good folder # should -- it should not be returned because it fails isdir() exercise = pexpect.which(fname) assert exercise is None finally: # restore, os.environ['PATH'] = save_path # destroy scratch folders, for _dir in ( bin_dir2, bin_dir, ): if os.path.exists(_dir): os.rmdir(_dir)
def test_which_should_not_match_folders(self): " Which does not match folders, even though they are executable. " # make up a path and insert a folder that is 'executable', a naive # implementation might match (previously pexpect versions 3.2 and # sh versions 1.0.8, reported by @lcm337.) fname = 'g++' bin_dir = tempfile.mkdtemp() bin_dir2 = os.path.join(bin_dir, fname) save_path = os.environ['PATH'] try: os.environ['PATH'] = bin_dir os.mkdir(bin_dir2, 0o755) # should not be found because it is not executable *file*, # but rather, has the executable bit set, as a good folder # should -- it should not be returned because it fails isdir() exercise = pexpect.which(fname) assert exercise is None finally: # restore, os.environ['PATH'] = save_path # destroy scratch folders, for _dir in (bin_dir2, bin_dir,): if os.path.exists(_dir): os.rmdir(_dir)
def test_newtool_overwriting_no(tmpdir): """ Run 'pytool newtool testtool tt' while testtool.py does exist. Verify that we are prompted about overwriting. Answer 'no' and verify that the original file is not overwritten. """ pytest.debug_func() tname = toyfile(tmpdir, "testtool.py", content=["original testtool.py"]) tlink = toyfile(tmpdir, "testtool", content=["original testtool"]) with U.Chdir(tmpdir.strpath): cmd = pexpect.which("pytool") if cmd is None: pytest.skip("pytool not found") S = pexpect.spawn("{} newtool {} tt".format(cmd, tlink)) which = S.expect([r'Are you sure\? >', 'Error:', pexpect.EOF]) if which == 0: S.sendline('no') S.expect(pexpect.EOF) elif which == 1: print S.before + S.after pytest.fail('unexpected exception') else: pytest.fail("should have asked about overwriting {}".format(tname)) for fname in [tlink, tname]: exp = "original %s" % fname.basename got = fname.read() assert got == exp
def test_telnet_client_cmdline_stdin_pipe(bind_host, unused_tcp_port, event_loop): """Test sending data through command-line client (by os PIPE).""" # this code may be reduced when pexpect asyncio is bugfixed .. # we especially need pexpect to pass sys.stdin.isatty() test. prog = pexpect.which('telnetlib3-client') fd, logfile = tempfile.mkstemp(prefix='telnetlib3', suffix='.log') os.close(fd) args = [ prog, bind_host, str(unused_tcp_port), '--loglevel=info', '--connect-minwait=0.15', '--connect-maxwait=0.15', '--logfile={0}'.format(logfile) ] @asyncio.coroutine def shell(reader, writer): writer.write('Press Return to continue:') inp = yield from reader.readline() if inp: writer.echo(inp) writer.write('\ngoodbye.\n') yield from writer.drain() writer.close() # start server yield from telnetlib3.create_server(host=bind_host, port=unused_tcp_port, shell=shell, loop=event_loop, connect_maxwait=0.05) # start client by way of pipe proc = yield from asyncio.create_subprocess_exec( *args, loop=event_loop, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE) #line = yield from asyncio.wait_for(proc.stdout.readline(), 1.5) #assert line.strip() == b"Escape character is '^]'." # message received, expect the client to gracefully quit. stdout, stderr = yield from asyncio.wait_for(proc.communicate(b'\r'), 2) # And finally, analyze the contents of the logfile, # - 2016-03-18 20:19:25,227 INFO client_base.py:113 Connected to <Peer 127.0.0.1 51237> # - 2016-03-18 20:19:25,286 INFO client_base.py:67 Connection closed to <Peer 127.0.0.1 51237> logfile_output = open(logfile).read().splitlines() assert stdout == (b"Escape character is '^]'.\n" b"Press Return to continue:\r\ngoodbye.\n" b"\x1b[m\nConnection closed by foreign host.\n") # verify, assert len(logfile_output) == 2, logfile assert 'Connected to <Peer' in logfile_output[0], logfile assert 'Connection closed to <Peer' in logfile_output[1], logfile os.unlink(logfile)
def test_bash_which(self): """ Calling bash_which('emacs') should get back a string with a path to the emacs executable """ self.dbgfunc() self.assertEq(pexpect.which("emacs"), bscr.bash_which("emacs"))
def cmdline(): binpath = pexpect.which('gnuchess') if binpath is None: utils.log.error('cannot find gnuchess on this system') args = ['--xboard'] return [binpath] + args
def test_ascii(fx_ascii): """ Run ascii and see if its output matches what is expected. """ pytest.debug_func() cmd = pexpect.which("ascii") result = pexpect.run(cmd) assert "\r\n".join(fx_ascii.exp) == result
def script_which_command(self, cmdname): """ ScriptBase: Make sure the tcc command exists and is executable """ cmd = pexpect.which(cmdname) if cmd is None: cmd = "bin/" + cmdname self.assertTrue(os.access(cmd, os.X_OK))
def sh(self): if self._sh is None: shell_name = os.environ.get("SHELL", "sh") self._sh = pexpect.which(shell_name) if self._sh is None: raise OSError('"{}" shell not found'.format(shell_name)) return self._sh
def test_final_newline(fx_ascii): """ Run ascii and verify output ends with a newline """ pytest.debug_func() cmd = pexpect.which("ascii") result = pexpect.run(cmd) assert "\r\n".join(fx_ascii.exp) == result
def setup_commands(): """ Bootstrap the PATH to have /usr/local/bin, where the op command should be """ os.environ["PATH"] += os.pathsep + '/usr/local/bin' basecmd = 'op' cmdpath = pexpect.which(basecmd) return cmdpath
def run(cmd, args, log): """ Execute CMD with argument ARGS in a subshell with pty. CMD: The command. ARGS: The command arguments. """ # print(cmd, args, log) if not pexpect.which(cmd): raise click.UsageError(f"ERROR: Command '{cmd}' not found.") logfile = None if log: logfile = open(log, 'a') cols, rows = shutil.get_terminal_size(fallback=(400, 100)) process = pexpect.spawn(f'{cmd} {" ".join(args)}', dimensions=(rows, cols)) patterns = process.compile_pattern_list([ '\r\n', '\r', pexpect.TIMEOUT, pexpect.EOF, ]) while True: index = process.expect(patterns, timeout=60) # Newline if index == 0: text = process.before.decode('utf-8') print(text, flush=True) if log: print(text, file=logfile) # Carriage return elif index == 1: if not process.before: continue text = process.before.decode('utf-8') print(text, flush=True) if log: print(text, file=logfile) # Timeout elif index == 2: print('<pytee: 60 sec elapsed without new input>') continue else: break process.close() # print(process.exitstatus, process.signalstatus) #raise SystemExit(process.exitstatus) return process.exitstatus
def _path_for(self, cmd): if cmd in self._bin_paths: return self._bin_paths[cmd] else: path = pexpect.which(cmd) if path is None: raise CommandNotFoundError("failed to find path of %r" % cmd) self._bin_paths[cmd] = path return path
def test_ascii_help(): """ Run 'ascii --help' and validate the output """ pytest.debug_func() cmd = pexpect.which("ascii") result = pexpect.run("%s --help" % cmd) exp = "Display ASCII collating sequence" assert exp in result
def _path_for(self, cmd): if cmd in self._bin_paths: return self._bin_paths[cmd] else: path = pexpect.which(cmd) if path is None: raise CommandNotFoundError('failed to find path of %r' % cmd) self._bin_paths[cmd] = path return path
def check_dependencies(commands=[]): """Check for the existence of required commands, and sudo access""" # Check for sudo if pexpect.which("sudo") is None: return False # Check for required commands for command in commands: if pexpect.which(command) is None: return False # Check for LANG requirements child = None try: child = env_spawn('sudo', ['-v'], 1) if child.expect([".*ssword.*", "Sorry", pexpect.EOF, pexpect.TIMEOUT]) == 3: global use_env use_env = True child.close() except OSError: if child is not None: child.close() return False # Check for sudo rights child = env_spawn('sudo', ['-v'], 1) try: index = child.expect([".*ssword.*", "Sorry", pexpect.EOF, pexpect.TIMEOUT]) child.close() if index == 0 or index == 2: # User in sudoers, or already admin return True elif index == 1 or index == 3: # User not in sudoers return False except: # Something else went wrong. child.close() return False
def test_os_defpath_which(self): " which() finds an executable in $PATH and returns its abspath. " bin_dir = tempfile.mkdtemp() temp_obj = tempfile.NamedTemporaryFile(suffix=u'.sh', prefix=u'ǝpoɔıun-', dir=bin_dir, delete=False) bin_path = temp_obj.name fname = os.path.basename(temp_obj.name) save_path = os.environ['PATH'] save_defpath = os.defpath try: # setup os.environ['PATH'] = '' os.defpath = bin_dir with open(bin_path, 'w') as fp: pass # given non-executable, os.chmod(bin_path, 0o400) # exercise absolute and relative, assert pexpect.which(bin_path) is None assert pexpect.which(fname) is None # given executable, os.chmod(bin_path, 0o700) # exercise absolute and relative, assert pexpect.which(bin_path) == bin_path assert pexpect.which(fname) == bin_path finally: # restore, os.environ['PATH'] = save_path os.defpath = save_defpath # destroy scratch files and folders, if os.path.exists(bin_path): os.unlink(bin_path) if os.path.exists(bin_dir): os.rmdir(bin_dir)
def CompleteReportAsRoot(run_as,tmp_configfile,gui=False): """Run a new instance of inforevealer with root priviledge to complete tmp_configfile""" if gui: from gui import askPassword if run_as == "substitute": #find the substitute user command and run the script if pexpect.which('su') != None: message=_("Please, enter the root password.") root_instance = str(pexpect.which('su')) + " - -c \'"+ os.path.abspath(sys.argv[0])+" --runfile "+ tmp_configfile+"\'" elif pexpect.which('sudo') != None: #TODO checkme message=_("Please, enter your user password.") root_instance = str(pexpect.which('sudo')) + ' ' + os.path.abspath(sys.argv[0])+' --runfile '+ tmp_configfile else: sys.stderr.write(_("Error: No substitute user command available.\n")) return 1 ret="" count=0 while ret!=[' \r\n'] and count <3: #Get password count+=1 if gui: password=askPassword(question=message) else: print(message) password=getpass.getpass() if password != False: #askPassword could return False #Run the command #TODO exceptions ? child = pexpect.spawn(root_instance) ret=child.expect([".*:",pexpect.EOF]) #Could we do more ? child.sendline(password) ret = child.readlines() if ret ==[' \r\n']: return 0 message=_("Wrong password.\nThe log will be generated without root priviledge.") if gui: import gtk md = gtk.MessageDialog(None, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, message) md.set_title(_("Error")) md.run() md.destroy() else: print(message)
def test_os_defpath_which(self): " which() finds an executable in $PATH and returns its abspath. " bin_dir = tempfile.mkdtemp() temp_obj = tempfile.NamedTemporaryFile( suffix=u'.sh', prefix=u'ǝpoɔıun-', dir=bin_dir, delete=False) bin_path = temp_obj.name fname = os.path.basename(temp_obj.name) save_path = os.environ['PATH'] save_defpath = os.defpath try: # setup os.environ['PATH'] = '' os.defpath = bin_dir with open(bin_path, 'w') as fp: pass # given non-executable, os.chmod(bin_path, 0o400) # exercise absolute and relative, assert pexpect.which(bin_path) is None assert pexpect.which(fname) is None # given executable, os.chmod(bin_path, 0o700) # exercise absolute and relative, assert pexpect.which(bin_path) == bin_path assert pexpect.which(fname) == bin_path finally: # restore, os.environ['PATH'] = save_path os.defpath = save_defpath # destroy scratch files and folders, if os.path.exists(bin_path): os.unlink(bin_path) if os.path.exists(bin_dir): os.rmdir(bin_dir)
def __init__(self, execpath=pexpect.which('M2'), timeout=4, configpath=None): """""" self.conf = M2Config(execpath, configpath) self.proc = None self.proc_command = self.conf.args.execpath self.proc_kwargs = { 'args': ['--silent', '--no-debug', '-e', 'load("init.m2")'], 'cwd': os.path.dirname(__file__) + '/assets/m2-code/', 'timeout': timeout }
async def test_telnet_client_cmdline_stdin_pipe(bind_host, unused_tcp_port, event_loop): """Test sending data through command-line client (by os PIPE).""" # this code may be reduced when pexpect asyncio is bugfixed .. # we especially need pexpect to pass sys.stdin.isatty() test. prog = pexpect.which('telnetlib3-client') fd, logfile = tempfile.mkstemp(prefix='telnetlib3', suffix='.log') os.close(fd) args = [prog, bind_host, str(unused_tcp_port), '--loglevel=info', '--connect-minwait=0.15', '--connect-maxwait=0.15', '--logfile={0}'.format(logfile)] @asyncio.coroutine def shell(reader, writer): writer.write('Press Return to continue:') inp = yield from reader.readline() if inp: writer.echo(inp) writer.write('\ngoodbye.\n') yield from writer.drain() writer.close() # start server await telnetlib3.create_server( host=bind_host, port=unused_tcp_port, shell=shell, loop=event_loop, connect_maxwait=0.05) # start client by way of pipe proc = await asyncio.create_subprocess_exec( *args, loop=event_loop, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE) #line = await asyncio.wait_for(proc.stdout.readline(), 1.5) #assert line.strip() == b"Escape character is '^]'." # message received, expect the client to gracefully quit. stdout, stderr = await asyncio.wait_for(proc.communicate(b'\r'), 2) # And finally, analyze the contents of the logfile, # - 2016-03-18 20:19:25,227 INFO client_base.py:113 Connected to <Peer 127.0.0.1 51237> # - 2016-03-18 20:19:25,286 INFO client_base.py:67 Connection closed to <Peer 127.0.0.1 51237> logfile_output = open(logfile).read().splitlines() assert stdout == (b"Escape character is '^]'.\n" b"Press Return to continue:\r\ngoodbye.\n" b"\x1b[m\nConnection closed by foreign host.\n") # verify, assert len(logfile_output) == 2, logfile assert 'Connected to <Peer' in logfile_output[0], logfile assert 'Connection closed to <Peer' in logfile_output[1], logfile os.unlink(logfile)
def test_bscr_help_help(self): """ Test 'bscr help help' """ self.dbgfunc() cmd = pexpect.which('bscr') result = pexpect.run('%s help help' % cmd) self.assertFalse('Traceback' in result) self.assertTrue('help - show a list' in result) self.assertTrue('With no arguments' in result) self.assertTrue('With a command as argument' in result)
def test_bscr_help(self): """ Test 'bscr help' """ self.dbgfunc() cmd = pexpect.which('bscr') result = pexpect.run('%s help' % cmd) self.assertFalse('Traceback' in result) for f in [x for x in dir(bscr.bscr) if x.startswith('bscr_')]: subc = f.replace('bscr_', '') self.assertTrue('%s - ' % subc in result)
def test_align_help(): """ Verify that 'align --help' does the right thing """ pytest.debug_func() cmd = pexpect.which("align") result = pexpect.run("%s --help" % cmd) nexp = "Traceback" assert nexp not in result exp = "Align columns from input" assert exp in result
def test_calc_help(): """ Verify that 'calc --help' does the right thing """ pytest.debug_func() cmd = pexpect.which("calc") result = pexpect.run("%s --help" % cmd) nexp = "Traceback" assert nexp not in result exp = "Usage: calc [options]" assert exp in result
def test_fl_help_help(self): """ 'fl help help' should get help for the help command """ cmd = pexpect.which('fl') result = pexpect.run('%s help help' % cmd) self.assertFalse('Traceback' in result) for f in ['help - show a list of available commands', 'With no arguments, show a list of commands', 'With a command as argument, show help for that command', ]: self.assertTrue(f in result)
def run(parent_dir=None, logfile=None, verbose=False): """Create TinyTim PSFs using standardized parameters This function will automatically generate ALL PSFs listed in the global filter dictionaries in the module in a tree with a directory for each detector. """ psf_classes = {('ACS', 'WFC'): WFCPSF, ('ACS', 'HRC'): HRCPSF, ('ACS', 'SBC'): SBCPSF, ('WFC3', 'UVIS'): UVISPSF, ('WFC3', 'IR'): IRPSF} if pexpect.which('tiny1') is None: raise FileNotFoundError("TinyTim executables NOT found! Please install first..") if parent_dir is None: parent_dir = os.getcwd() try: for instr, detectors in FILTER_DICT.items(): instr_path = os.path.join(parent_dir, instr.lower()) if not os.path.exists(instr_path): os.makedirs(instr_path) # move into that directory os.chdir(instr_path) # Now, let's start looping over the detectors for det, filter_names in detectors.items(): det_path = os.path.join(instr_path, det.lower()) if not os.path.exists(det_path): os.makedirs(det_path) # move into that directory os.chdir(det_path) # Now, start creating the PSFs listed in the filter_dict for this detector for filter_name in filter_names: # Create class for this PSF psfobj = psf_classes[(instr, det)](filter_name, logfile=logfile, verbose=verbose) psfobj.tiny1() psfobj.tiny2() psfobj.tiny3() # We are finished with creating all the PSFs for this detector... # Return to parent instrument directory os.chdir(instr_path) # We are finished with all the PSFs for this instrument... # return to the parent directory os.chdir(parent_dir) finally: os.chdir(parent_dir)
def check_missing_requirements (): """This list of missing requirements (mencoder, mplayer, lame, and mkvmerge). Returns None if all requirements are in the execution path. """ missing = [] if pexpect.which("mencoder") is None: missing.append("mencoder") if pexpect.which("mplayer") is None: missing.append("mplayer") cmd = "mencoder -oac help" (command_output, exitstatus) = run(cmd) ar = re.findall("(mp3lame)", command_output) if len(ar)==0: missing.append("Mencoder was not compiled with mp3lame support.") #if pexpect.which("lame") is None: # missing.append("lame") #if pexpect.which("mkvmerge") is None: # missing.append("mkvmerge") if len(missing)==0: return None return missing
def check_dependencies(commands=[]): """Check for the existence of required commands, and sudo access""" # Check for required commands for command in commands: if pexpect.which(command) is None: return False # Check for sudo or pkexec if pexpect.which("pkexec") is not None: return True if pexpect.which("sudo") is None: return False # Check for LANG requirements child = env_spawn('sudo -v', 1) if child.expect([".*ssword.*", "Sorry", pexpect.EOF, pexpect.TIMEOUT]) == 3: global use_env use_env = True child.close() # Check for sudo rights child = env_spawn('sudo -v', 1) try: index = child.expect([".*ssword.*", "Sorry", pexpect.EOF, pexpect.TIMEOUT]) child.close() if index in [0, 2]: # User in sudoers, or already admin return True except Exception: # Something else went wrong. child.close() return False
def which(filename: str, env: Optional[Mapping[str, str]] = None): '''This takes a given filename; tries to find it in the environment path; then checks if it is executable. This returns the full path to the filename if found and executable. Otherwise this returns None. If `env` is not specified, use `os.environ` instead. Example: | ${path} | `Which` | java | { 'PATH': '/usr/lib/jvm/java-1.14.0-openjdk-amd64/bin' } | | Log To Console | ${path} | ''' return pexpect.which(filename, env)
def script_help(self, cmdname, helplist): """ ScriptBase: Make sure 'tcc help' generates something reasonable """ cmd = pexpect.which(cmdname) if cmd is None: cmd = "bin/" + cmdname result = pexpect.run("%s help" % cmd) self.assertFalse( "Traceback" in result, "'Traceback' not expected in %s" % U.line_quote(result)) for item in helplist: self.assertTrue(item in result, "Expected '%s' in '%s'" % (item, result))
def _start_gap(self): # Signal handlers are inherited by forked processes, and we can't easily # reset it from the subprocess. Since kernelapp ignores SIGINT except in # message handlers, we need to temporarily reset the SIGINT handler here # so that gap and its children are interruptible. sig = signal.signal(signal.SIGINT, signal.SIG_DFL) try: # setup.g contains functions needed for Jupyter interfacing setupg = path.dirname(path.abspath(__file__)) if which('gap') != None: gap_run_command = which('gap') elif which('gap.sh') != None: gap_run_command = which('gap.sh') else: raise NameError('gap executable not found') self.gapwrapper = replwrap.REPLWrapper( gap_run_command + ' -n -b -T %s/gap/setup.g' % (setupg), u'gap|| ', None, None, continuation_prompt=u'|| ') self.gapwrapper.run_command("\n") finally: signal.signal(signal.SIGINT, sig)
def test_path_from_env(self): " executable found from optional env argument " bin_name = 'pexpect-test-path-from-env' tempdir = tempfile.mkdtemp() try: bin_path = os.path.join(tempdir, bin_name) with open(bin_path, 'w') as f: f.write('# test file not to be run') try: os.chmod(bin_path, 0o700) found_path = pexpect.which(bin_name, env={'PATH': tempdir}) finally: os.remove(bin_path) self.assertEqual(bin_path, found_path) finally: os.rmdir(tempdir)
async def test_telnet_server_cmdline(bind_host, unused_tcp_port, event_loop): """Test executing telnetlib3/server.py as server""" # this code may be reduced when pexpect asyncio is bugfixed .. prog = pexpect.which("telnetlib3-server") args = [ prog, bind_host, str(unused_tcp_port), "--loglevel=info", "--connect-maxwait=0.05", ] proc = await asyncio.create_subprocess_exec(*args, loop=event_loop, stderr=asyncio.subprocess.PIPE) seen = b"" while True: line = await asyncio.wait_for(proc.stderr.readline(), 1.5) if b"Server ready" in line: break seen += line assert line, seen.decode() # EOF reached # client connects, reader, writer = await asyncio.open_connection(host=bind_host, port=unused_tcp_port, loop=event_loop) # and closes, await reader.readexactly(3) # IAC DO TTYPE, we ignore it! writer.close() seen = b"" while True: line = await asyncio.wait_for(proc.stderr.readline(), 1.5) if b"Connection closed" in line: break seen += line assert line, seen.decode() # EOF reached # send SIGTERM proc.terminate() # we would expect the server to gracefully end. await proc.communicate() await proc.wait()
def get_netcat(): """Return IPv6-capable nc(1), if any.""" netcat_paths = ('nc', 'netcat', '/usr/bin/nc', '/usr/local/bin/nc', '/bin/nc.openbsd') for nc_name in netcat_paths: prog = pexpect.which(nc_name) if prog is None: continue stdout, stderr = subprocess.Popen( [prog, '-h'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() # only openbsd netcat supports IPv6. # So that's the only one we'll use! if b'-46' in stdout + stderr: return prog return None
def start(self, process_name, *args): """Starts a process.""" if Process.is_running(process_name): self._error("Process '{}' is already running".format(process_name)) return False if len(args) < 2: self._error("'directory' and 'command' arguments not given but mandatory") return False directory_path = xtask.parse_path(args[0]) if not directory_path.exists(): self._error("Location {} does not exist".format(directory_path)) return False command = args[1] args = list(args[2:]) if command.startswith("."): command_path = directory_path / command if not command_path.exists(): self._error("Cannot find command {}".format(command_path)) else: command = str(command_path) elif pexpect.which(command) is None: self._error("Unknown command '{}'".format(command)) return False try: self._info( "Starting process '{}' by calling '{} {}' in directory {}".format( process_name, command, " ".join(args), directory_path ) ) process = pexpect.spawn(command, args, cwd=directory_path, timeout=5) Process._processes[process_name] = process except Exception: self._exception( "Something went wrong while starting process '{}'".format(process_name) ) return False return True