def test_popen(): # open a pipe just for reading... pipe_modes = [["ping 127.0.0.1 -n 1", "r"], ["ping 127.0.0.1 -n 1"]] if is_cli: pipe_modes.append(["ping 127.0.0.1 -n 1", ""]) for args in pipe_modes: x = nt.popen(*args) text = x.read() Assert(text.lower().index('pinging') != -1) AreEqual(x.close(), None) # write to a pipe x = nt.popen('sort', 'w') x.write('hello\nabc\n') x.close() # bug 1146 #x = nt.popen('sort', 'w') #x.write('hello\nabc\n') #AreEqual(x.close(), None) # once w/ default mode AssertError(ValueError, nt.popen, "ping 127.0.0.1 -n 1", "a") # popen uses cmd.exe to run stuff -- at least sometimes dir_pipe = nt.popen('dir') dir_pipe.read() dir_pipe.close() # once w/ no mode stdin, stdout = nt.popen2('sort') stdin.write('hello\nabc\n') AreEqual(stdin.close(), None) AreEqual(stdout.read(), 'abc\nhello\n') AreEqual(stdout.close(), None) # bug 1146 # and once w/ each mode #for mode in ['b', 't']: # stdin, stdout = nt.popen2('sort', mode) # stdin.write('hello\nabc\n') # AreEqual(stdin.close(), None) # AreEqual(stdout.read(), 'abc\nhello\n') # AreEqual(stdout.close(), None) # popen3: once w/ no mode stdin, stdout, stderr = nt.popen3('sort') stdin.write('hello\nabc\n') AreEqual(stdin.close(), None) AreEqual(stdout.read(), 'abc\nhello\n') AreEqual(stdout.close(), None) AreEqual(stderr.read(), '') AreEqual(stderr.close(), None) # bug 1146 # popen3: and once w/ each mode #for mode in ['b', 't']: # stdin, stdout, stderr = nt.popen3('sort', mode) # stdin.write('hello\nabc\n') # AreEqual(stdin.close(), None) # AreEqual(stdout.read(), 'abc\nhello\n') # AreEqual(stdout.close(), None) # AreEqual(stderr.read(), '') # AreEqual(stderr.close(), None) tmpfile = 'tmpfile.tmp' f = open(tmpfile, 'w') f.close() nt.unlink(tmpfile) try: nt.chmod('tmpfile.tmp', 256) except Exception: pass #should throw when trying to access file deleted by unlink else: Assert( False, "Error! Trying to access file deleted by unlink should have thrown." ) try: tmpfile = "tmpfile2.tmp" f = open(tmpfile, "w") f.write("testing chmod") f.close() nt.chmod(tmpfile, 256) AssertError(OSError, nt.unlink, tmpfile) nt.chmod(tmpfile, 128) nt.unlink(tmpfile) AssertError(IOError, file, tmpfile) finally: try: nt.chmod(tmpfile, 128) nt.unlink(tmpfile) except Exception as e: print("exc", e) # verify that nt.stat reports times in seconds, not ticks... import time tmpfile = 'tmpfile.tmp' f = open(tmpfile, 'w') f.close() t = time.time() mt = nt.stat(tmpfile).st_mtime nt.unlink(tmpfile) # this deletes the file Assert(abs(t - mt) < 60, "time differs by too much " + str(abs(t - mt))) tmpfile = 'tmpfile.tmp' # need to open it again since we deleted it with 'unlink' f = open(tmpfile, 'w') f.close() nt.chmod('tmpfile.tmp', 256) nt.chmod('tmpfile.tmp', 128) nt.unlink('tmpfile.tmp')
def test_popen(): # open a pipe just for reading... pipe_modes = [["ping 127.0.0.1 -n 1", "r"], ["ping 127.0.0.1 -n 1"]] if is_cli: pipe_modes.append(["ping 127.0.0.1 -n 1", ""]) for args in pipe_modes: x = nt.popen(*args) text = x.read() Assert(text.lower().index('pinging') != -1) AreEqual(x.close(), None) # write to a pipe x = nt.popen('sort', 'w') x.write('hello\nabc\n') x.close() # bug 1146 #x = nt.popen('sort', 'w') #x.write('hello\nabc\n') #AreEqual(x.close(), None) # once w/ default mode AssertError(ValueError, nt.popen, "ping 127.0.0.1 -n 1", "a") # popen uses cmd.exe to run stuff -- at least sometimes dir_pipe = nt.popen('dir') dir_pipe.read() dir_pipe.close() # once w/ no mode stdin, stdout = nt.popen2('sort') stdin.write('hello\nabc\n') AreEqual(stdin.close(), None) AreEqual(stdout.read(), 'abc\nhello\n') AreEqual(stdout.close(), None) # bug 1146 # and once w/ each mode #for mode in ['b', 't']: # stdin, stdout = nt.popen2('sort', mode) # stdin.write('hello\nabc\n') # AreEqual(stdin.close(), None) # AreEqual(stdout.read(), 'abc\nhello\n') # AreEqual(stdout.close(), None) # popen3: once w/ no mode stdin, stdout, stderr = nt.popen3('sort') stdin.write('hello\nabc\n') AreEqual(stdin.close(), None) AreEqual(stdout.read(), 'abc\nhello\n') AreEqual(stdout.close(), None) AreEqual(stderr.read(), '') AreEqual(stderr.close(), None) # bug 1146 # popen3: and once w/ each mode #for mode in ['b', 't']: # stdin, stdout, stderr = nt.popen3('sort', mode) # stdin.write('hello\nabc\n') # AreEqual(stdin.close(), None) # AreEqual(stdout.read(), 'abc\nhello\n') # AreEqual(stdout.close(), None) # AreEqual(stderr.read(), '') # AreEqual(stderr.close(), None) tmpfile = 'tmpfile.tmp' f = open(tmpfile, 'w') f.close() nt.unlink(tmpfile) try: nt.chmod('tmpfile.tmp', 256) except Exception: pass #should throw when trying to access file deleted by unlink else: Assert( False, "Error! Trying to access file deleted by unlink should have thrown." ) try: tmpfile = "tmpfile2.tmp" f = open(tmpfile, "w") f.write("testing chmod") f.close() nt.chmod(tmpfile, 256) AssertError(OSError, nt.unlink, tmpfile) nt.chmod(tmpfile, 128) nt.unlink(tmpfile) AssertError(IOError, file, tmpfile) finally: try: nt.chmod(tmpfile, 128) nt.unlink(tmpfile) except Exception, e: print "exc", e
def number_of_process(arg): return len([ x for x in nt.popen('tasklist.exe').readlines() if x.lower().startswith(arg.lower()) ])
def test_popen(): # open a pipe just for reading... pipe_modes = [["ping 127.0.0.1", "r"], ["ping 127.0.0.1"]] if is_cli: pipe_modes.append(["ping 127.0.0.1", ""]) for args in pipe_modes: x = nt.popen(*args) text = x.read() Assert(text.lower().index('pinging') != -1) AreEqual(x.close(), None) # write to a pipe x = nt.popen('sort', 'w') x.write('hello\nabc\n') x.close() # bug 1146 #x = nt.popen('sort', 'w') #x.write('hello\nabc\n') #AreEqual(x.close(), None) # once w/ default mode AssertError(ValueError, nt.popen, "ping 127.0.0.1", "a") # popen uses cmd.exe to run stuff -- at least sometimes dir_pipe = nt.popen('dir') dir_pipe.read() dir_pipe.close() # once w/ no mode stdin, stdout = nt.popen2('sort') stdin.write('hello\nabc\n') AreEqual(stdin.close(), None) AreEqual(stdout.read(), 'abc\nhello\n') AreEqual(stdout.close(), None) # bug 1146 # and once w/ each mode #for mode in ['b', 't']: # stdin, stdout = nt.popen2('sort', mode) # stdin.write('hello\nabc\n') # AreEqual(stdin.close(), None) # AreEqual(stdout.read(), 'abc\nhello\n') # AreEqual(stdout.close(), None) # popen3: once w/ no mode stdin, stdout, stderr = nt.popen3('sort') stdin.write('hello\nabc\n') AreEqual(stdin.close(), None) AreEqual(stdout.read(), 'abc\nhello\n') AreEqual(stdout.close(), None) AreEqual(stderr.read(), '') AreEqual(stderr.close(), None) # bug 1146 # popen3: and once w/ each mode #for mode in ['b', 't']: # stdin, stdout, stderr = nt.popen3('sort', mode) # stdin.write('hello\nabc\n') # AreEqual(stdin.close(), None) # AreEqual(stdout.read(), 'abc\nhello\n') # AreEqual(stdout.close(), None) # AreEqual(stderr.read(), '') # AreEqual(stderr.close(), None) tmpfile = 'tmpfile.tmp' f = open(tmpfile, 'w') f.close() nt.unlink(tmpfile) try: nt.chmod('tmpfile.tmp', 256) except Exception: pass #should throw when trying to access file deleted by unlink else: Assert(False,"Error! Trying to access file deleted by unlink should have thrown.") try: tmpfile = "tmpfile2.tmp" f = open(tmpfile, "w") f.write("testing chmod") f.close() nt.chmod(tmpfile, 256) AssertError(OSError, nt.unlink, tmpfile) nt.chmod(tmpfile, 128) nt.unlink(tmpfile) AssertError(IOError, file, tmpfile) finally: try: nt.chmod(tmpfile, 128) nt.unlink(tmpfile) except Exception, e: print "exc", e
def number_of_process(arg): return len([x for x in nt.popen('tasklist.exe').readlines() if x.lower().startswith(arg.lower()) ])