~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/testing/__init__.py

  • Committer: Stuart Bishop
  • Date: 2011-09-05 15:42:27 UTC
  • mto: (13813.4.4 pgbouncer-fixture)
  • mto: This revision was merged to the branch mainline in revision 13875.
  • Revision ID: stuart.bishop@canonical.com-20110905154227-ke7d9fy9jr9uk87y
Revert reversion in launchpad/devel r13865

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
    'logout',
30
30
    'map_branch_contents',
31
31
    'normalize_whitespace',
 
32
    'nonblocking_readline',
32
33
    'oauth_access_token_for',
33
34
    'person_logged_in',
34
35
    'quote_jquery_expression',
69
70
import os
70
71
from pprint import pformat
71
72
import re
 
73
from select import select
72
74
import shutil
73
75
import subprocess
74
76
import sys
1325
1327
def extract_lp_cache(text):
1326
1328
    match = re.search(r'<script>LP.cache = (\{.*\});</script>', text)
1327
1329
    return simplejson.loads(match.group(1))
 
1330
 
 
1331
 
 
1332
def nonblocking_readline(instream, timeout):
 
1333
    """Non-blocking readline.
 
1334
 
 
1335
    Files must provide a valid fileno() method. This is a test helper
 
1336
    as it is inefficient and unlikely useful for production code.
 
1337
    """
 
1338
    result = StringIO()
 
1339
    start = now = time.time()
 
1340
    deadline = start + timeout
 
1341
    while (now < deadline and not result.getvalue().endswith('\n')):
 
1342
        rlist = select([instream], [], [], deadline - now)
 
1343
        if rlist:
 
1344
            # Reading 1 character at a time is inefficient, but means
 
1345
            # we don't need to implement put-back.
 
1346
            next_char = os.read(instream.fileno(), 1)
 
1347
            if next_char == "":
 
1348
                break  # EOF
 
1349
            result.write(next_char)
 
1350
        now = time.time()
 
1351
    return result.getvalue()