def exec_command(self, cmd, tmp_path, become_user=None, sudoable=False, executable='/bin/sh', in_data=None): if in_data: raise errors.AnsibleError('Internal Error: this modules does not support optimized module pipelining') if executable: local_cmd = executable.split() + ['-c', cmd] else: local_cmd = cmd executable = executable.split()[0] if executable else None pipe_to_server = os.pipe() pipe_from_server = os.pipe() # os.environ is special, so we copy it into a dictionary and modify the dictionary instead env = dict() for key in os.environ.keys(): env[key] = os.environ[key] env['SNMP_PIPE_IN'] = str(pipe_from_server[0]) env['SNMP_PIPE_OUT'] = str(pipe_to_server[1]) if 'PYTHONPATH' in env: env['PYTHONPATH'] = os.path.dirname(__file__) + ':' + env['PYTHONPATH'] else: env['PYTHONPATH'] = os.path.dirname(__file__) vvv('EXEC %s' % (local_cmd), host=self.host) p = subprocess.Popen(local_cmd, shell=isinstance(local_cmd, basestring), cwd=self.runner.basedir, executable=executable, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) # pysnmp insist on using its own socket map, so we use that instead conn = self._get_snmp_connection() sock_map = conn.dispatcher.getSocketMap() stdout = _BufferedDispatcher(asyncore.file_wrapper(p.stdout.fileno()), map=sock_map) stderr = _BufferedDispatcher(asyncore.file_wrapper(p.stderr.fileno()), map=sock_map) server = _Server(conn, pipe_to_server[0], pipe_from_server[1], map=sock_map) while stdout.readable() or stderr.readable(): asyncore.poll(0.5, map=sock_map) conn.dispatcher.handleTimerTick(time.time()) p.wait() os.close(pipe_to_server[0]) os.close(pipe_to_server[1]) os.close(pipe_from_server[0]) os.close(pipe_from_server[1]) return (p.returncode, '', stdout.data, stderr.data)
def test_close_twice(self): fd = os.open(support.TESTFN, os.O_RDONLY) f = asyncore.file_wrapper(fd) os.close(fd) f.close() self.assertEqual(f.fd, -1) f.close()
def test_resource_warning(self): # Issue #11453 fd = os.open(support.TESTFN, os.O_RDONLY) f = asyncore.file_wrapper(fd) with support.check_warnings(('', ResourceWarning)): f = None support.gc_collect()
def test_close_twice(self): fd = os.open(support.TESTFN, os.O_RDONLY) f = asyncore.file_wrapper(fd) f.close() self.assertEqual(f.fd, -1) # calling close twice should not fail f.close()
def test_recv(self): fd = os.open(support.TESTFN, os.O_RDONLY) w = asyncore.file_wrapper(fd) os.close(fd) self.assertNotEqual(w.fd, fd) self.assertNotEqual(w.fileno(), fd) self.assertEqual(w.recv(13), b"It's not dead") self.assertEqual(w.read(6), b", it's") w.close() self.assertRaises(OSError, w.read, 1)
def test_send(self): d1 = b'Come again?' d2 = b'I want to buy some cheese.' fd = os.open(support.TESTFN, os.O_WRONLY | os.O_APPEND) w = asyncore.file_wrapper(fd) os.close(fd) w.write(d1) w.send(d2) w.close() with open(support.TESTFN, 'rb') as file: self.assertEqual(file.read(), self.d + d1 + d2)
def test_send(self): d1 = "Come again?" d2 = "I want to buy some cheese." fd = os.open(TESTFN, os.O_WRONLY | os.O_APPEND) w = asyncore.file_wrapper(fd) os.close(fd) w.write(d1) w.send(d2) w.close() self.assertEqual(file(TESTFN).read(), self.d + d1 + d2)
def test_recv(self): fd = os.open(TESTFN, os.O_RDONLY) w = asyncore.file_wrapper(fd) os.close(fd) self.assertNotEqual(w.fd, fd) self.assertNotEqual(w.fileno(), fd) self.assertEqual(w.recv(13), "It's not dead") self.assertEqual(w.read(6), ", it's") w.close() self.assertRaises(OSError, w.read, 1)
def test_close_twice(self): fd = os.open(support.TESTFN, os.O_RDONLY) f = asyncore.file_wrapper(fd) os.close(fd) os.close(f.fd) # file_wrapper dupped fd with self.assertRaises(OSError): f.close() self.assertEqual(f.fd, -1) # calling close twice should not fail f.close()
def test_send(self): d1 = b"Come again?" d2 = b"I want to buy some cheese." fd = os.open(support.TESTFN, os.O_WRONLY | os.O_APPEND) w = asyncore.file_wrapper(fd) os.close(fd) w.write(d1) w.send(d2) w.close() with open(support.TESTFN, 'rb') as file: self.assertEqual(file.read(), self.d + d1 + d2)
def test_close_twice(self): fd = os.open(TESTFN, os.O_RDONLY) f = asyncore.file_wrapper(fd) os.close(fd) os.close(f.fd) # file_wrapper dupped fd with self.assertRaises(OSError): f.close() self.assertEqual(f.fd, -1) # calling close twice should not fail f.close()
def init_file_dispatcher(self, fd): """ Kludge to plug asyncore.file_dispatcher into asynchat. Call from subclass's __init__() method, after calling PDUChannel.__init__(), and don't read this on a full stomach. """ self.connected = True self._fileno = fd self.socket = asyncore.file_wrapper(fd) self.add_channel() flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0) flags = flags | os.O_NONBLOCK fcntl.fcntl(fd, fcntl.F_SETFL, flags)
def set_file(self, fd): try: # fd may be a file object fd = fd.fileno() except AttributeError: pass self.socket = asyncore.file_wrapper(fd) self._fileno = self.socket.fileno() self.add_channel() flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0) flags = flags | os.O_NONBLOCK fcntl.fcntl(fd, fcntl.F_SETFL, flags)
def __init__(self, event_to_raise): self._eventToRaise = event_to_raise self._in_fd, self._out_fd = os.pipe() self._pipe = asyncore.file_wrapper(self._in_fd) super().__init__(self._pipe)
def exec_command(self, cmd, tmp_path, become_user=None, sudoable=False, executable='/bin/sh', in_data=None): if in_data: raise errors.AnsibleError( 'Internal Error: this modules does not support optimized module pipelining' ) if executable: local_cmd = executable.split() + ['-c', cmd] else: local_cmd = cmd executable = executable.split()[0] if executable else None pipe_to_server = os.pipe() pipe_from_server = os.pipe() # os.environ is special, so we copy it into a dictionary and modify the dictionary instead env = dict() for key in os.environ.keys(): env[key] = os.environ[key] env['SNMP_PIPE_IN'] = str(pipe_from_server[0]) env['SNMP_PIPE_OUT'] = str(pipe_to_server[1]) if 'PYTHONPATH' in env: env['PYTHONPATH'] = os.path.dirname( __file__) + ':' + env['PYTHONPATH'] else: env['PYTHONPATH'] = os.path.dirname(__file__) vvv('EXEC %s' % (local_cmd), host=self.host) p = subprocess.Popen(local_cmd, shell=isinstance(local_cmd, basestring), cwd=self.runner.basedir, executable=executable, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) # pysnmp insist on using its own socket map, so we use that instead conn = self._get_snmp_connection() sock_map = conn.dispatcher.getSocketMap() stdout = _BufferedDispatcher(asyncore.file_wrapper(p.stdout.fileno()), map=sock_map) stderr = _BufferedDispatcher(asyncore.file_wrapper(p.stderr.fileno()), map=sock_map) server = _Server(conn, pipe_to_server[0], pipe_from_server[1], map=sock_map) while stdout.readable() or stderr.readable(): asyncore.poll(0.5, map=sock_map) conn.dispatcher.handleTimerTick(time.time()) p.wait() os.close(pipe_to_server[0]) os.close(pipe_to_server[1]) os.close(pipe_from_server[0]) os.close(pipe_from_server[1]) return (p.returncode, '', stdout.data, stderr.data)
def set_file(self, fd): # Like asyncore.file_dispatcher.set_file() but doesn't call # add_channel(). We'll call add_channel() in start() when the # download shall begin. self.socket = asyncore.file_wrapper(fd) self._fileno = self.socket.fileno()
def set_file(self, fd): self.socket = asyncore.file_wrapper(fd) self._fileno = self.socket.fileno()
def set_file(self, fd): self._fileno = fd self.socket = asyncore.file_wrapper(fd) if fd == 0: self.add_channel()