Commit 2446ce69 authored by Łukasz Nowak's avatar Łukasz Nowak

Implement networkcache simulator.

parent f9da19cb
...@@ -17,6 +17,7 @@ $Id$ ...@@ -17,6 +17,7 @@ $Id$
""" """
import BaseHTTPServer import BaseHTTPServer
import SimpleHTTPServer
import errno import errno
import logging import logging
import os import os
...@@ -32,6 +33,7 @@ import textwrap ...@@ -32,6 +33,7 @@ import textwrap
import threading import threading
import time import time
import urllib2 import urllib2
import json
import zc.buildout.buildout import zc.buildout.buildout
import zc.buildout.easy_install import zc.buildout.easy_install
...@@ -347,6 +349,12 @@ def buildoutSetUp(test): ...@@ -347,6 +349,12 @@ def buildoutSetUp(test):
os.chdir(sample) os.chdir(sample)
make_buildout() make_buildout()
def start_nc(path):
port, thread = _start_nc(path, name=path)
url = 'http://localhost:%s/' % port
register_teardown(lambda: stop_server(url, thread))
return url
def start_server(path): def start_server(path):
port, thread = _start_server(path, name=path) port, thread = _start_server(path, name=path)
url = 'http://localhost:%s/' % port url = 'http://localhost:%s/' % port
...@@ -416,6 +424,7 @@ def buildoutSetUp(test): ...@@ -416,6 +424,7 @@ def buildoutSetUp(test):
sdist = sdist, sdist = sdist,
bdist_egg = bdist_egg, bdist_egg = bdist_egg,
start_server = start_server, start_server = start_server,
start_nc = start_nc,
buildout = os.path.join(sample, 'bin', 'buildout'), buildout = os.path.join(sample, 'bin', 'buildout'),
wait_until = wait_until, wait_until = wait_until,
make_py = make_py make_py = make_py
...@@ -511,11 +520,37 @@ class Handler(BaseHTTPServer.BaseHTTPRequestHandler): ...@@ -511,11 +520,37 @@ class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
if self.__server.__log: if self.__server.__log:
print '%s %s %s' % (self.command, code, self.path) print '%s %s %s' % (self.command, code, self.path)
class NCHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
if '__stop__' in self.path:
raise SystemExit
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def do_PUT(self):
path = self.translate_path(self.path)
d = os.path.dirname(path)
if not os.path.exists(d):
os.makedirs(d)
data = self.rfile.read(int(self.headers.getheader('content-length')))
try:
d = json.loads(data)
except ValueError:
pass
else:
data = json.dumps([d])
open(path, 'wb').write(data)
self.send_response(201)
def _run(tree, port): def _run(tree, port):
server_address = ('localhost', port) server_address = ('localhost', port)
httpd = Server(tree, server_address, Handler) httpd = Server(tree, server_address, Handler)
httpd.serve_forever() httpd.serve_forever()
def _run_nc(tree, port):
server_address = ('localhost', port)
httpd = Server(tree, server_address, NCHandler)
httpd.serve_forever()
def get_port(): def get_port():
for i in range(10): for i in range(10):
port = random.randrange(20000, 30000) port = random.randrange(20000, 30000)
...@@ -529,6 +564,17 @@ def get_port(): ...@@ -529,6 +564,17 @@ def get_port():
s.close() s.close()
raise RuntimeError, "Can't find port" raise RuntimeError, "Can't find port"
def _start_nc(tree, name=''):
port = get_port()
thread = threading.Thread(target=_run_nc, args=(tree, port), name=name)
thread.setDaemon(True)
thread.start()
wait(port, up=True)
return port, thread
def start_nc(tree):
return _start_nc(tree)[0]
def _start_server(tree, name=''): def _start_server(tree, name=''):
port = get_port() port = get_port()
thread = threading.Thread(target=_run, args=(tree, port), name=name) thread = threading.Thread(target=_run, args=(tree, port), name=name)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment