# Path: pgctl/functions.py
# def exec_(argv, env=None):  # never returns
#     """Wrapper to os.execv which runs any atexit handlers (for coverage's sake).
#     Like os.execv, this function never returns.
#     """
#     if env is None:
#         env = os.environ
# 
#     # in python3, sys.exitfunc has gone away, and atexit._run_exitfuncs seems to be the only pubic-ish interface
#     #   https://hg.python.org/cpython/file/3.4/Modules/atexitmodule.c#l289
#     import atexit
#     atexit._run_exitfuncs()
#     os.execvpe(argv[0], argv, env)
# 
# Path: pgctl/functions.py
# def print_stderr(s):
#     # Sadness: https://bugs.python.org/issue13601
#     print(s, file=sys.stderr)
#     sys.stderr.flush()


import os.path
import select
import time
from sys import argv
from .functions import exec_
from .functions import print_stderr
    from .subprocess import call

def check_ready():
    return call('./ready')


def wait_for_down_signal(down_fifo, seconds):
    """Waits at most "seconds" for down_fifo to have data on it, and returns
    True if the first byte from the FIFO is 'd'. Returns False if there is no
    data on the FIFO or the first byte is not a 'd'.

    :param seconds: time to wait for the fifo to have data on it. Zero specifies
    a poll and never blocks.
    :type seconds: float
    :rtype: bool
    :return: if a 'd' was available on the FIFO before the timeout expired.
    """
    rlist, _, _ = select.select([down_fifo], [], [], seconds)
    if rlist:
        fifo_data = os.read(down_fifo, 1)
        if fifo_data == b'd':
            return True

    return False


def pgctl_poll_ready(down_fifo, notification_fd, timeout, poll_ready, poll_down, check_ready=check_ready):

    is_service_down = wait_for_down_signal(down_fifo, 0)
    while True:  # waiting for the service to come up.
        if is_service_down:

# Path: pgctl/functions.py
# def exec_(argv, env=None):  # never returns
#     """Wrapper to os.execv which runs any atexit handlers (for coverage's sake).
#     Like os.execv, this function never returns.
#     """
#     if env is None:
#         env = os.environ
# 
#     # in python3, sys.exitfunc has gone away, and atexit._run_exitfuncs seems to be the only pubic-ish interface
#     #   https://hg.python.org/cpython/file/3.4/Modules/atexitmodule.c#l289
#     import atexit
#     atexit._run_exitfuncs()
#     os.execvpe(argv[0], argv, env)
# 
# Path: pgctl/functions.py
# def print_stderr(s):
#     # Sadness: https://bugs.python.org/issue13601
#     print(s, file=sys.stderr)
#     sys.stderr.flush()


# Path: pgctl/poll_ready.py
import os.path
import select
import time
from sys import argv
from .functions import exec_
from .functions import print_stderr
    from .subprocess import call


def check_ready():
    return call('./ready')


def wait_for_down_signal(down_fifo, seconds):
    """Waits at most "seconds" for down_fifo to have data on it, and returns
    True if the first byte from the FIFO is 'd'. Returns False if there is no
    data on the FIFO or the first byte is not a 'd'.

    :param seconds: time to wait for the fifo to have data on it. Zero specifies
    a poll and never blocks.
    :type seconds: float
    :rtype: bool
    :return: if a 'd' was available on the FIFO before the timeout expired.
    """
    rlist, _, _ = select.select([down_fifo], [], [], seconds)
    if rlist:
        fifo_data = os.read(down_fifo, 1)
        if fifo_data == b'd':
            return True

    return False


def pgctl_poll_ready(down_fifo, notification_fd, timeout, poll_ready, poll_down, check_ready=check_ready):

    is_service_down = wait_for_down_signal(down_fifo, 0)
    while True:  # waiting for the service to come up.
        if is_service_down:

            print_stderr('pgctl-poll-ready: service is stopping -- quitting the poll')