# Path: mdk/command.py
# class Command(object):
#     """Represents a command"""
# 
#     _arguments = [
#             (
#                 ['foo'],
#                 {
#                     'help': 'I\'m an argument'
#                 }
#             ),
#             (
#                 ['-b', '--bar'],
#                 {
#                     'action': 'store_true',
#                     'help': 'I\'m a flag'
#                 }
#             )
#         ]
#     _description = 'Undocumented command'
# 
#     __C = None
#     __Wp = None
# 
#     def __init__(self, config):
#         self.__C = config
# 
#     def argumentError(self, message):
#         raise CommandArgumentError(message)
# 
#     @property
#     def arguments(self):
#         return self._arguments
# 
#     @property
#     def C(self):
#         return self.__C
# 
#     @property
#     def description(self):
#         return self._description
# 
#     def run(self, args):
#         return True
# 
#     @property
#     def Wp(self):
#         if not self.__Wp:
#             from .workplace import Workplace
#             self.__Wp = Workplace()
#         return self.__Wp
# 
# Path: mdk/tools.py
# def process(cmd, cwd=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
#     if type(cmd) != list:
#         cmd = shlex.split(str(cmd))
#     logging.debug(' '.join(cmd))
#     try:
#         proc = subprocess.Popen(cmd, cwd=cwd, stdout=stdout, stderr=stderr, encoding='utf-8')
#         (out, err) = proc.communicate()
#     except KeyboardInterrupt as e:
#         proc.kill()
#         raise e
#     return (proc.returncode, out.decode('utf-8') if type(out) == bytes else out, err)
# 
# class ProcessInThread(threading.Thread):
#     """Executes a process in a separate thread"""
# 
#     cmd = None
#     cwd = None
#     stdout = None
#     stderr = None
#     _kill = False
#     _pid = None
# 
#     def __init__(self, cmd, cwd=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
#         threading.Thread.__init__(self)
#         if type(cmd) != 'list':
#             cmd = shlex.split(str(cmd))
#         self.cmd = cmd
#         self.cwd = cwd
#         self.stdout = stdout
#         self.stderr = stderr
# 
#     def kill(self):
#         os.kill(self._pid, signal.SIGKILL)
# 
#     def run(self):
#         logging.debug(' '.join(self.cmd))
#         proc = subprocess.Popen(self.cmd, cwd=self.cwd, stdout=self.stdout, stderr=self.stderr)
#         self._pid = proc.pid
#         while True:
#             if proc.poll():
#                 break
# 
#             # Reading the output seems to prevent the process to hang.
#             if self.stdout == subprocess.PIPE:
#                 proc.stdout.read(1)
# 
# def downloadProcessHook(count, size, total):
#     """Hook to report the downloading a file using urllib.urlretrieve"""
#     if count <= 0:
#         return
#     downloaded = int((count * size) / (1024))
#     total = int(total / (1024)) if total != 0 else '?'
#     if downloaded > total:
#         downloaded = total
#     sys.stderr.write("\r  %sKB / %sKB" % (downloaded, total))
#     sys.stderr.flush()
# 
# def question(q, default=None, options=None, password=False):
#     """Asks the user a question, and return the answer"""
#     text = q
#     if default != None:
#         text = text + ' [%s]' % str(default)
# 
#     if password:
#         i = getpass.getpass('%s\n   ' % text)
#     else:
#         i = input('%s\n  ' % text)
# 
#     if i.strip() == '':
#         return default
#     else:
#         if options != None and i not in options:
#             return question(q, default, options)
#         return i
# 
# def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
#     """
#     Use as key when naturally sorting elements.
# 
#     Source: https://stackoverflow.com/a/16090640/867720
#     """
#     return [int(text) if text.isdigit() else text.lower() for text in _nsre.split(s)]


import os
import urllib.request, urllib.parse, urllib.error
import re
import logging
import gzip
from tempfile import gettempdir
from time import sleep
from ..command import Command
from ..tools import process, ProcessInThread, downloadProcessHook, question, natural_sort_key
                logging.info('Preparing Behat testing')

                # Preparing PHP Server
                phpServer = None
                if olderThan26 and not M.get('behat_switchcompletely'):
                    logging.info('Starting standalone PHP server')
                    kwargs = {}
                    kwargs['cwd'] = M.get('path')
                    phpServer = ProcessInThread(phpCommand, **kwargs)
                    phpServer.start()

                # Launching Selenium
                seleniumServer = None
                if seleniumPath and not nojavascript:
                    logging.info('Starting Selenium server')
                    kwargs = {}
                    if args.seleniumverbose:
                        kwargs['stdout'] = None
                        kwargs['stderr'] = None
                    else:
                        # Logging Selenium to a temporary file, this can be useful, and also it appears
                        # that Selenium hangs when stderr is not buffered.
                        fileOutPath = os.path.join(gettempdir(), 'selenium_%s_out.log' % (M.get('identifier')))
                        fileErrPath = os.path.join(gettempdir(), 'selenium_%s_err.log' % (M.get('identifier')))
                        tmpfileOut = open(fileOutPath, 'w')
                        tmpfileErr = open(fileErrPath, 'w')
                        logging.debug('Logging Selenium output to: %s' % (fileOutPath))
                        logging.debug('Logging Selenium errors to: %s' % (fileErrPath))
                        kwargs['stdout'] = tmpfileOut
                        kwargs['stderr'] = tmpfileErr

# Path: mdk/command.py
# class Command(object):
#     """Represents a command"""
# 
#     _arguments = [
#             (
#                 ['foo'],
#                 {
#                     'help': 'I\'m an argument'
#                 }
#             ),
#             (
#                 ['-b', '--bar'],
#                 {
#                     'action': 'store_true',
#                     'help': 'I\'m a flag'
#                 }
#             )
#         ]
#     _description = 'Undocumented command'
# 
#     __C = None
#     __Wp = None
# 
#     def __init__(self, config):
#         self.__C = config
# 
#     def argumentError(self, message):
#         raise CommandArgumentError(message)
# 
#     @property
#     def arguments(self):
#         return self._arguments
# 
#     @property
#     def C(self):
#         return self.__C
# 
#     @property
#     def description(self):
#         return self._description
# 
#     def run(self, args):
#         return True
# 
#     @property
#     def Wp(self):
#         if not self.__Wp:
#             from .workplace import Workplace
#             self.__Wp = Workplace()
#         return self.__Wp
# 
# Path: mdk/tools.py
# def process(cmd, cwd=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
#     if type(cmd) != list:
#         cmd = shlex.split(str(cmd))
#     logging.debug(' '.join(cmd))
#     try:
#         proc = subprocess.Popen(cmd, cwd=cwd, stdout=stdout, stderr=stderr, encoding='utf-8')
#         (out, err) = proc.communicate()
#     except KeyboardInterrupt as e:
#         proc.kill()
#         raise e
#     return (proc.returncode, out.decode('utf-8') if type(out) == bytes else out, err)
# 
# class ProcessInThread(threading.Thread):
#     """Executes a process in a separate thread"""
# 
#     cmd = None
#     cwd = None
#     stdout = None
#     stderr = None
#     _kill = False
#     _pid = None
# 
#     def __init__(self, cmd, cwd=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
#         threading.Thread.__init__(self)
#         if type(cmd) != 'list':
#             cmd = shlex.split(str(cmd))
#         self.cmd = cmd
#         self.cwd = cwd
#         self.stdout = stdout
#         self.stderr = stderr
# 
#     def kill(self):
#         os.kill(self._pid, signal.SIGKILL)
# 
#     def run(self):
#         logging.debug(' '.join(self.cmd))
#         proc = subprocess.Popen(self.cmd, cwd=self.cwd, stdout=self.stdout, stderr=self.stderr)
#         self._pid = proc.pid
#         while True:
#             if proc.poll():
#                 break
# 
#             # Reading the output seems to prevent the process to hang.
#             if self.stdout == subprocess.PIPE:
#                 proc.stdout.read(1)
# 
# def downloadProcessHook(count, size, total):
#     """Hook to report the downloading a file using urllib.urlretrieve"""
#     if count <= 0:
#         return
#     downloaded = int((count * size) / (1024))
#     total = int(total / (1024)) if total != 0 else '?'
#     if downloaded > total:
#         downloaded = total
#     sys.stderr.write("\r  %sKB / %sKB" % (downloaded, total))
#     sys.stderr.flush()
# 
# def question(q, default=None, options=None, password=False):
#     """Asks the user a question, and return the answer"""
#     text = q
#     if default != None:
#         text = text + ' [%s]' % str(default)
# 
#     if password:
#         i = getpass.getpass('%s\n   ' % text)
#     else:
#         i = input('%s\n  ' % text)
# 
#     if i.strip() == '':
#         return default
#     else:
#         if options != None and i not in options:
#             return question(q, default, options)
#         return i
# 
# def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
#     """
#     Use as key when naturally sorting elements.
# 
#     Source: https://stackoverflow.com/a/16090640/867720
#     """
#     return [int(text) if text.isdigit() else text.lower() for text in _nsre.split(s)]


# Path: mdk/commands/behat.py
import os
import urllib.request, urllib.parse, urllib.error
import re
import logging
import gzip
from tempfile import gettempdir
from time import sleep
from ..command import Command
from ..tools import process, ProcessInThread, downloadProcessHook, question, natural_sort_key

                logging.info('Preparing Behat testing')

                # Preparing PHP Server
                phpServer = None
                if olderThan26 and not M.get('behat_switchcompletely'):
                    logging.info('Starting standalone PHP server')
                    kwargs = {}
                    kwargs['cwd'] = M.get('path')
                    phpServer = ProcessInThread(phpCommand, **kwargs)
                    phpServer.start()

                # Launching Selenium
                seleniumServer = None
                if seleniumPath and not nojavascript:
                    logging.info('Starting Selenium server')
                    kwargs = {}
                    if args.seleniumverbose:
                        kwargs['stdout'] = None
                        kwargs['stderr'] = None
                    else:
                        # Logging Selenium to a temporary file, this can be useful, and also it appears
                        # that Selenium hangs when stderr is not buffered.
                        fileOutPath = os.path.join(gettempdir(), 'selenium_%s_out.log' % (M.get('identifier')))
                        fileErrPath = os.path.join(gettempdir(), 'selenium_%s_err.log' % (M.get('identifier')))
                        tmpfileOut = open(fileOutPath, 'w')
                        tmpfileErr = open(fileErrPath, 'w')
                        logging.debug('Logging Selenium output to: %s' % (fileOutPath))
                        logging.debug('Logging Selenium errors to: %s' % (fileErrPath))
                        kwargs['stdout'] = tmpfileOut
                        kwargs['stderr'] = tmpfileErr

                    seleniumServer = ProcessInThread(seleniumCommand, **kwargs)