|
4 | 4 | Site downloader script for Splash benchmark suite.
|
5 | 5 | """
|
6 | 6 |
|
| 7 | +from contextlib import contextmanager |
7 | 8 | from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
|
8 | 9 | import errno
|
9 | 10 | import json
|
10 | 11 | import os
|
11 | 12 | import re
|
12 | 13 | import subprocess
|
13 | 14 | from urlparse import urlsplit
|
| 15 | +import time |
14 | 16 |
|
| 17 | +import requests |
15 | 18 | from lxml import html
|
16 | 19 |
|
17 | 20 | import w3lib.html
|
18 |
| -from splash.benchmark.file_server import serve_files |
19 | 21 | from splash.tests.stress import lua_runonce
|
20 | 22 |
|
21 | 23 | SCRIPT_HTML = """
|
@@ -72,6 +74,31 @@ def preprocess_main_page(sites_dir, url):
|
72 | 74 | return filename
|
73 | 75 |
|
74 | 76 |
|
| 77 | +@contextmanager |
| 78 | +def serve_files(port, directory, logfile=None): |
| 79 | + """Serve files from specified directory statically in a subprocess.""" |
| 80 | + command = ['twistd', |
| 81 | + '-n', # don't daemonize |
| 82 | + 'web', # start web component |
| 83 | + '--port', str(int(port)), |
| 84 | + '--path', os.path.abspath(directory), ] |
| 85 | + if logfile is not None: |
| 86 | + command += ['--logfile', logfile] |
| 87 | + site_server = subprocess.Popen(command) |
| 88 | + try: |
| 89 | + # It might take some time to bring up the server, wait for up to 10s. |
| 90 | + for i in xrange(100): |
| 91 | + try: |
| 92 | + requests.get('http://localhost:%d' % port) |
| 93 | + except requests.ConnectionError: |
| 94 | + time.sleep(0.1) |
| 95 | + else: |
| 96 | + break |
| 97 | + yield |
| 98 | + finally: |
| 99 | + site_server.terminate() |
| 100 | + |
| 101 | + |
75 | 102 | def download_sites(sites_dir, sites):
|
76 | 103 | local_files = [preprocess_main_page(sites_dir, s) for s in sites]
|
77 | 104 |
|
|
0 commit comments