# Path: ayrton/expansion.py
# def bash (s, single=False):
#     data= backslash_descape (glob_expand (tilde_expand (brace_expand (s))))
#     if single and len(data)==1:
#         data= data[0]
#     return data
# 
# Path: ayrton/execute.py
# class CommandNotFound (NameError):
#     def __init__ (self, name):
#         self.name= name
# 
#     def __str__ (self):
#         return "CommandNotFound or NameError: command %(name)s not found or name %(name)s is not defined" % self.__dict__
# 
# Path: ayrton/utils.py
# def copy_loop (copy_to, finished=None, buf_len=10240):
#     """copy_to is a dict(in: out). When any in is ready to read, data is read
#     from it and writen in its out. When any in is closed, it's removed from
#     copy_to. finished is a pipe; when data comes from the read end, or when
#     no more ins are present, the loop finishes."""
#     if finished is not None:
#         copy_to[finished]= None
# 
#     # NOTE:
#     # os.sendfile (self.dst, self.src, None, 0)
#     # OSError: [Errno 22] Invalid argument
#     # and splice() is not available
#     # so, copy by hand
#     selector = DefaultSelector ()
#     for src in copy_to.keys ():
#         if (     not isinstance (src, int)
#                 and (   getattr (src, 'fileno', None) is None
#                     or not isinstance (src.fileno(), int)) ):
#             logger.debug ('type mismatch: %s', src)
#         else:
#             logger.debug ("registering %s for read", src)
#             # if finished is also one of the srcs, then register() complains
#             try:
#                 selector.register (src, EVENT_READ)
#             except KeyError:
#                 pass
# 
#     def close_file (f):
#         if f in copy_to:
#             del copy_to[f]
# 
#         try:
#             selector.unregister (i)
#         except KeyError:
#             pass
# 
#         close (f)
# 
#     while len (copy_to)>0:
#         logger.debug (copy_to)
# 
#         events= selector.select ()
#         for key, _ in events:
#             logger.debug ("%s is ready to read", key)
#             i= key.fileobj
# 
#             # for error in e:
#             #     logger.debug ('%s error')
#                 # TODO: what?
# 
#             o= copy_to[i]
# 
#             try:
#                 data= read (i, buf_len)
#                 logger.debug2 ('%s -> %s: %s', i, o, data)
# 
#             # ValueError: read of closed file
#             except (OSError, ValueError) as e:
#                 logger.debug ('stopping copying for %s', i)
#                 logger.debug (traceback.format_exc ())
#                 close_file (i)
#                 break
#             else:
#                 if len (data)==0:
#                     logger.debug ('stopping copying for %s, no more data', i)
#                     close_file (i)
# 
#                     if finished is not None and i==finished:
#                         logger.debug ('finishing')
#                         # quite a hack :)
#                         copy_to= {}
# 
#                         break
#                 else:
#                     write (o, data)
# 
#     selector.close ()
#     logger.debug ('over and out')
# 
# def close (f):
#     logger.debug ('closing %s', f, callers=1)
#     try:
#         if isinstance (f, int):
#             os.close (f)
#         else:
#             if f is not None:
#                 f.close ()
#     except OSError as e:
#         logger.debug ('closing gave %s', e)
#         if e.errno!=errno.EBADF:
#             raise


import unittest
import unittest.case
import sys
import io
import os
import tempfile
import os.path
import time
import signal
import traceback
import paramiko.ssh_exception
import ayrton
import logging
from socket import socket, AF_INET, SOCK_STREAM, SO_REUSEADDR, SOL_SOCKET
from tempfile import mkstemp
from ayrton.expansion import bash
from ayrton.execute import CommandNotFound
from ayrton.utils import copy_loop, close
        pid= os.fork ()
        if pid != 0:
            logger.debug ('main parent')
            # parent
            self.addCleanup (os.waitpid, pid, 0)

            # give nc time to come up
            time.sleep (0.2)
        else:
            try:
                # child
                logger.debug('nc')

                server = socket(AF_INET, SOCK_STREAM)
                logger.debug(server.fileno())
                server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
                server.settimeout(1)
                server.bind(('127.0.0.1', 2233))
                server.listen()

                client, _ = server.accept()
                logger.debug(client.fileno())
                # bash resets the O_ONONBLOCK flag
                # see below
                # client.settimeout (3)
                close (server)

                logger.debug ('bash')
                os.dup2 (client.fileno (), 0)
                os.dup2 (client.fileno (), 1)

# Path: ayrton/expansion.py
# def bash (s, single=False):
#     data= backslash_descape (glob_expand (tilde_expand (brace_expand (s))))
#     if single and len(data)==1:
#         data= data[0]
#     return data
# 
# Path: ayrton/execute.py
# class CommandNotFound (NameError):
#     def __init__ (self, name):
#         self.name= name
# 
#     def __str__ (self):
#         return "CommandNotFound or NameError: command %(name)s not found or name %(name)s is not defined" % self.__dict__
# 
# Path: ayrton/utils.py
# def copy_loop (copy_to, finished=None, buf_len=10240):
#     """copy_to is a dict(in: out). When any in is ready to read, data is read
#     from it and writen in its out. When any in is closed, it's removed from
#     copy_to. finished is a pipe; when data comes from the read end, or when
#     no more ins are present, the loop finishes."""
#     if finished is not None:
#         copy_to[finished]= None
# 
#     # NOTE:
#     # os.sendfile (self.dst, self.src, None, 0)
#     # OSError: [Errno 22] Invalid argument
#     # and splice() is not available
#     # so, copy by hand
#     selector = DefaultSelector ()
#     for src in copy_to.keys ():
#         if (     not isinstance (src, int)
#                 and (   getattr (src, 'fileno', None) is None
#                     or not isinstance (src.fileno(), int)) ):
#             logger.debug ('type mismatch: %s', src)
#         else:
#             logger.debug ("registering %s for read", src)
#             # if finished is also one of the srcs, then register() complains
#             try:
#                 selector.register (src, EVENT_READ)
#             except KeyError:
#                 pass
# 
#     def close_file (f):
#         if f in copy_to:
#             del copy_to[f]
# 
#         try:
#             selector.unregister (i)
#         except KeyError:
#             pass
# 
#         close (f)
# 
#     while len (copy_to)>0:
#         logger.debug (copy_to)
# 
#         events= selector.select ()
#         for key, _ in events:
#             logger.debug ("%s is ready to read", key)
#             i= key.fileobj
# 
#             # for error in e:
#             #     logger.debug ('%s error')
#                 # TODO: what?
# 
#             o= copy_to[i]
# 
#             try:
#                 data= read (i, buf_len)
#                 logger.debug2 ('%s -> %s: %s', i, o, data)
# 
#             # ValueError: read of closed file
#             except (OSError, ValueError) as e:
#                 logger.debug ('stopping copying for %s', i)
#                 logger.debug (traceback.format_exc ())
#                 close_file (i)
#                 break
#             else:
#                 if len (data)==0:
#                     logger.debug ('stopping copying for %s, no more data', i)
#                     close_file (i)
# 
#                     if finished is not None and i==finished:
#                         logger.debug ('finishing')
#                         # quite a hack :)
#                         copy_to= {}
# 
#                         break
#                 else:
#                     write (o, data)
# 
#     selector.close ()
#     logger.debug ('over and out')
# 
# def close (f):
#     logger.debug ('closing %s', f, callers=1)
#     try:
#         if isinstance (f, int):
#             os.close (f)
#         else:
#             if f is not None:
#                 f.close ()
#     except OSError as e:
#         logger.debug ('closing gave %s', e)
#         if e.errno!=errno.EBADF:
#             raise


# Path: ayrton/tests/test_remote.py
import unittest
import unittest.case
import sys
import io
import os
import tempfile
import os.path
import time
import signal
import traceback
import paramiko.ssh_exception
import ayrton
import logging
from socket import socket, AF_INET, SOCK_STREAM, SO_REUSEADDR, SOL_SOCKET
from tempfile import mkstemp
from ayrton.expansion import bash
from ayrton.execute import CommandNotFound
from ayrton.utils import copy_loop, close

        pid= os.fork ()
        if pid != 0:
            logger.debug ('main parent')
            # parent
            self.addCleanup (os.waitpid, pid, 0)

            # give nc time to come up
            time.sleep (0.2)
        else:
            try:
                # child
                logger.debug('nc')

                server = socket(AF_INET, SOCK_STREAM)
                logger.debug(server.fileno())
                server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
                server.settimeout(1)
                server.bind(('127.0.0.1', 2233))
                server.listen()

                client, _ = server.accept()
                logger.debug(client.fileno())
                # bash resets the O_ONONBLOCK flag
                # see below
                # client.settimeout (3)
                close (server)

                logger.debug ('bash')
                os.dup2 (client.fileno (), 0)
                os.dup2 (client.fileno (), 1)

                close (client)