|
1 |
| -import asyncio |
2 |
| -import errno |
3 |
| -import signal |
4 |
| - |
5 |
| -from pipenv.vendor.pexpect import EOF |
6 |
| - |
7 |
| -@asyncio.coroutine |
8 |
| -def expect_async(expecter, timeout=None): |
9 |
| - # First process data that was previously read - if it maches, we don't need |
10 |
| - # async stuff. |
11 |
| - idx = expecter.existing_data() |
12 |
| - if idx is not None: |
13 |
| - return idx |
14 |
| - if not expecter.spawn.async_pw_transport: |
15 |
| - pw = PatternWaiter() |
16 |
| - pw.set_expecter(expecter) |
17 |
| - transport, pw = yield from asyncio.get_event_loop()\ |
18 |
| - .connect_read_pipe(lambda: pw, expecter.spawn) |
19 |
| - expecter.spawn.async_pw_transport = pw, transport |
20 |
| - else: |
21 |
| - pw, transport = expecter.spawn.async_pw_transport |
22 |
| - pw.set_expecter(expecter) |
23 |
| - transport.resume_reading() |
24 |
| - try: |
25 |
| - return (yield from asyncio.wait_for(pw.fut, timeout)) |
26 |
| - except asyncio.TimeoutError as e: |
27 |
| - transport.pause_reading() |
28 |
| - return expecter.timeout(e) |
29 |
| - |
30 |
| -@asyncio.coroutine |
31 |
| -def repl_run_command_async(repl, cmdlines, timeout=-1): |
32 |
| - res = [] |
33 |
| - repl.child.sendline(cmdlines[0]) |
34 |
| - for line in cmdlines[1:]: |
35 |
| - yield from repl._expect_prompt(timeout=timeout, async_=True) |
36 |
| - res.append(repl.child.before) |
37 |
| - repl.child.sendline(line) |
38 |
| - |
39 |
| - # Command was fully submitted, now wait for the next prompt |
40 |
| - prompt_idx = yield from repl._expect_prompt(timeout=timeout, async_=True) |
41 |
| - if prompt_idx == 1: |
42 |
| - # We got the continuation prompt - command was incomplete |
43 |
| - repl.child.kill(signal.SIGINT) |
44 |
| - yield from repl._expect_prompt(timeout=1, async_=True) |
45 |
| - raise ValueError("Continuation prompt found - input was incomplete:") |
46 |
| - return u''.join(res + [repl.child.before]) |
47 |
| - |
48 |
| -class PatternWaiter(asyncio.Protocol): |
49 |
| - transport = None |
50 |
| - |
51 |
| - def set_expecter(self, expecter): |
52 |
| - self.expecter = expecter |
53 |
| - self.fut = asyncio.Future() |
54 |
| - |
55 |
| - def found(self, result): |
56 |
| - if not self.fut.done(): |
57 |
| - self.fut.set_result(result) |
58 |
| - self.transport.pause_reading() |
59 |
| - |
60 |
| - def error(self, exc): |
61 |
| - if not self.fut.done(): |
62 |
| - self.fut.set_exception(exc) |
63 |
| - self.transport.pause_reading() |
64 |
| - |
65 |
| - def connection_made(self, transport): |
66 |
| - self.transport = transport |
67 |
| - |
68 |
| - def data_received(self, data): |
69 |
| - spawn = self.expecter.spawn |
70 |
| - s = spawn._decoder.decode(data) |
71 |
| - spawn._log(s, 'read') |
72 |
| - |
73 |
| - if self.fut.done(): |
74 |
| - spawn._before.write(s) |
75 |
| - spawn._buffer.write(s) |
76 |
| - return |
77 |
| - |
78 |
| - try: |
79 |
| - index = self.expecter.new_data(s) |
80 |
| - if index is not None: |
81 |
| - # Found a match |
82 |
| - self.found(index) |
83 |
| - except Exception as e: |
84 |
| - self.expecter.errored() |
85 |
| - self.error(e) |
86 |
| - |
87 |
| - def eof_received(self): |
88 |
| - # N.B. If this gets called, async will close the pipe (the spawn object) |
89 |
| - # for us |
90 |
| - try: |
91 |
| - self.expecter.spawn.flag_eof = True |
92 |
| - index = self.expecter.eof() |
93 |
| - except EOF as e: |
94 |
| - self.error(e) |
95 |
| - else: |
96 |
| - self.found(index) |
97 |
| - |
98 |
| - def connection_lost(self, exc): |
99 |
| - if isinstance(exc, OSError) and exc.errno == errno.EIO: |
100 |
| - # We may get here without eof_received being called, e.g on Linux |
101 |
| - self.eof_received() |
102 |
| - elif exc is not None: |
103 |
| - self.error(exc) |
| 1 | +"""Facade that provides coroutines implementation pertinent to running Py version. |
| 2 | +
|
| 3 | +Python 3.5 introduced the async def/await syntax keyword. |
| 4 | +With later versions coroutines and methods to get the running asyncio loop are |
| 5 | +being deprecated, not supported anymore. |
| 6 | +
|
| 7 | +For Python versions later than 3.6, coroutines and objects that are defined via |
| 8 | +``async def``/``await`` keywords are imported. |
| 9 | +
|
| 10 | +Here the code is just imported, to provide the same interface to older code. |
| 11 | +""" |
| 12 | +# pylint: disable=unused-import |
| 13 | +# flake8: noqa: F401 |
| 14 | +from sys import version_info as py_version_info |
| 15 | + |
| 16 | +# this assumes async def/await are more stable |
| 17 | +if py_version_info >= (3, 6): |
| 18 | + from pipenv.vendor.pexpect._async_w_await import ( |
| 19 | + PatternWaiter, |
| 20 | + expect_async, |
| 21 | + repl_run_command_async, |
| 22 | + ) |
| 23 | +else: |
| 24 | + from pipenv.vendor.pexpect._async_pre_await import ( |
| 25 | + PatternWaiter, |
| 26 | + expect_async, |
| 27 | + repl_run_command_async, |
| 28 | + ) |
0 commit comments