Changeset: 2aa4d22736fc for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=2aa4d22736fc
Modified Files:
        testing/process.py
Branch: default
Log Message:

Do not assume server isn't run as daemon in process.server().


diffs (180 lines):

diff --git a/testing/process.py b/testing/process.py
--- a/testing/process.py
+++ b/testing/process.py
@@ -67,96 +67,30 @@ def _delfiles():
 atexit.register(_delfiles)
 
 class _BufferedPipe:
-    def __init__(self, fd, waitfor=None, skip=None):
+    def __init__(self, fd):
         self._pipe = fd
         self._queue = queue.Queue()
         self._eof = False
         self._empty = ''
-        if waitfor is not None:
-            self._wfq = queue.Queue()
-        else:
-            self._wfq = None
         self._thread = threading.Thread(target=self._readerthread,
-                                        args=(fd, self._queue, waitfor, 
self._wfq, skip))
+                                        args=(fd, self._queue))
         self._thread.setDaemon(True)
         self._thread.start()
 
-    def _readerthread(self, fh, queue, waitfor, wfq, skip):
-        # If `skip' has a value, don't pass it through the first time
-        # we encounter it.
-        # If `waitfor' has a value, put something into the wfq queue
-        # when we've seen it.
+    def _readerthread(self, fh, queue):
         s = 0
         w = 0
-        skipqueue = []
         first = True
         while True:
-            if skipqueue:
-                c = skipqueue[0]
-                del skipqueue[0]
-            else:
-                c = fh.read(1)
-                if first:
-                    if type(c) is type(b''):
-                        self._empty = b''
-                    first = False
-                if skip and c:
-                    if c == skip[s]:
-                        s += 1
-                        if s == len(skip):
-                            skip = None
-                    else:
-                        j = 0
-                        while j < s:
-                            if not skip.startswith(skip[j:s] + c):
-                                skipqueue.append(skip[j])
-                                j += 1
-                            else:
-                                s -= j - 1
-                                break
-                        else:
-                            if c == skip[0]:
-                                s = 1
-                            else:
-                                skipqueue.append(c)
-                                s = 0
-                    continue
-            if waitfor and c:
-                if c == waitfor[w]:
-                    w += 1
-                    if w == len(waitfor):
-                        waitfor = None
-                        wfq.put('ready')
-                        wfq = None
-                else:
-                    j = 0
-                    while j < w:
-                        if not waitfor.startswith(waitfor[j:w] + c):
-                            queue.put(waitfor[j])
-                            j += 1
-                        else:
-                            w = w-j+1
-                            break
-                    else:
-                        if c == waitfor[0]:
-                            w = 1
-                        else:
-                            queue.put(c)
-                            w = 0
-                continue
+            c = fh.read(1)
+            if first:
+                if type(c) is type(b''):
+                    self._empty = b''
+                first = False
             queue.put(c)                # put '' if at EOF
             if not c:
-                if waitfor is not None:
-                    # if at EOF and still waiting for string, signal EOF
-                    wfq.put('eof')
-                    waitfor = None
-                    wfq = None
                 break
 
-    def _waitfor(self):
-        rdy = self._wfq.get()
-        self._wfq = None
-
     def close(self):
         if self._thread:
             self._thread.join()
@@ -397,16 +331,19 @@ def server(args=[], stdin=None, stdout=N
             break
     else:
         dbpath = None
-    if dbname is None and dbfarm is not None:
-        dbname = 'demo'
-    if dbname is not None:
+    if dbpath is not None:
         if dbfarm is None:
-            if _dbfarm is None:
-                raise RuntimeError('no dbfarm known')
-            dbfarm = _dbfarm
-        dbpath = os.path.join(dbfarm, dbname)
-    if dbpath is not None:
-        cmd.append('--dbpath=%s' % dbpath)
+            dbfarm = os.path.dirname(dbpath)
+        if dbname is None:
+            dbname = os.path.basename(dbpath)
+    if dbname is None:
+        dbname = 'demo'
+    if dbfarm is None:
+        if _dbfarm is None:
+            raise RuntimeError('no dbfarm known')
+        dbfarm = _dbfarm
+    dbpath = os.path.join(dbfarm, dbname)
+    cmd.append('--dbpath=%s' % dbpath)
     for i in range(len(cmd)):
         if cmd[i].startswith('--dbextra='):
             dbextra_path = cmd[i][10:]
@@ -450,6 +387,11 @@ def server(args=[], stdin=None, stdout=N
         sys.stderr.write(prompt + '\n')
         sys.stderr.write('\n')
         sys.stderr.flush()
+    started = os.path.join(dbpath, '.started')
+    try:
+        os.unlink(started)
+    except OSError:
+        pass
     p = Popen(cmd + args,
               stdin=stdin,
               stdout=stdout,
@@ -460,20 +402,18 @@ def server(args=[], stdin=None, stdout=N
     if stderr == PIPE:
         p.stderr = _BufferedPipe(p.stderr)
     if stdout == PIPE:
-        if stdin == PIPE:
-            # If both stdin and stdout are pipes, we wait until the
-            # server is ready.  This is done by sending a print
-            # command and waiting for the result to appear.
-            rdy = '\nServer Ready.\n'
-            cmd = 'io.printf("%s");\n' % rdy.replace('\n', '\\n')
-            p.stdout = _BufferedPipe(p.stdout, rdy, cmd)
-            p.stdin.write(cmd)
-            p.stdin.flush()
-            p.stdout._waitfor()
-        else:
-            p.stdout = _BufferedPipe(p.stdout)
+        p.stdout = _BufferedPipe(p.stdout)
     # store database name and port in the returned instance for the
     # client to pick up
     p.dbname = dbname
     p.dbport = mapiport
+    while True:
+        p.poll()
+        if p.returncode is not None:
+            # process exited already
+            break
+        if os.path.exists(started):
+            # server is ready
+            break
+        time.sleep(0.001)
     return p
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to