Skip to content

Commit

Permalink
Python3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
rmohr committed Feb 14, 2015
1 parent 5e58cc4 commit 04986b5
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 101 deletions.
11 changes: 9 additions & 2 deletions .travis.yml
Expand Up @@ -15,7 +15,14 @@
# limitations under the License.

language: python
python: [ "2.7" ]
matrix:
include:
- python: "2.7"
env: REQUIREMENTS=requirements-dev.txt VENV=python2.7
- python: "pypy"
env: REQUIREMENTS=requirements-dev.txt VENV=pypy
- python: "3.3"
env: REQUIREMENTS=requirements3-dev.txt VENV=python3.3

before_install: |
# needed for the UML container
Expand All @@ -37,5 +44,5 @@ before_install: |
chmod +x tests.sh
chmod +x run.sh
install: pip install -r requirements-dev.txt
install: pip install -r $REQUIREMENTS
script: ./run.sh
28 changes: 8 additions & 20 deletions dockerpty/io.py
Expand Up @@ -19,6 +19,7 @@
import errno
import struct
import select as builtin_select
import six


def set_blocking(fd, blocking=True):
Expand All @@ -31,7 +32,7 @@ def set_blocking(fd, blocking=True):
old_flag = fcntl.fcntl(fd, fcntl.F_GETFL)

if blocking:
new_flag = old_flag &~ os.O_NONBLOCK
new_flag = old_flag & ~ os.O_NONBLOCK
else:
new_flag = old_flag | os.O_NONBLOCK

Expand Down Expand Up @@ -59,7 +60,8 @@ def select(read_streams, timeout=0):
)[0]
except builtin_select.error as e:
# POSIX signals interrupt select()
if e[0] == errno.EINTR:
no = e.errno if six.PY3 else e[0]
if no == errno.EINTR:
return []
else:
raise e
Expand All @@ -73,7 +75,6 @@ class Stream(object):
add consistency to the reading of sockets and files alike.
"""


"""
Recoverable IO/OS Errors.
"""
Expand All @@ -83,7 +84,6 @@ class Stream(object):
errno.EWOULDBLOCK,
]


def __init__(self, fd):
"""
Initialize the Stream for the file descriptor `fd`.
Expand All @@ -92,23 +92,20 @@ def __init__(self, fd):
"""
self.fd = fd


def fileno(self):
"""
Return the fileno() of the file descriptor.
"""

return self.fd.fileno()


def set_blocking(self, value):
if hasattr(self.fd, 'setblocking'):
self.fd.setblocking(value)
return True
else:
return set_blocking(self.fd, value)


def read(self, n=4096):
"""
Return `n` bytes of data from the Stream, or None at end of stream.
Expand All @@ -122,7 +119,6 @@ def read(self, n=4096):
if e.errno not in Stream.ERRNO_RECOVERABLE:
raise e


def write(self, data):
"""
Write `data` to the Stream.
Expand Down Expand Up @@ -170,7 +166,6 @@ def __init__(self, stream):
self.stream = stream
self.remain = 0


def fileno(self):
"""
Returns the fileno() of the underlying Stream.
Expand All @@ -180,11 +175,9 @@ def fileno(self):

return self.stream.fileno()


def set_blocking(self, value):
return self.stream.set_blocking(value)


def read(self, n=4096):
"""
Read up to `n` bytes of data from the Stream, after demuxing.
Expand All @@ -201,38 +194,36 @@ def read(self, n=4096):
if size <= 0:
return
else:
data = ''
data = six.binary_type()
while len(data) < size:
nxt = self.stream.read(size - len(data))
if not nxt:
# the stream has closed, return what data we got
return data
data = "{0}{1}".format(data, nxt)
data = data + nxt
return data


def write(self, data):
"""
Delegates the the underlying Stream.
"""

return self.stream.write(data)


def _next_packet_size(self, n=0):
size = 0

if self.remain > 0:
size = min(n, self.remain)
self.remain -= size
else:
data = ''
data = six.binary_type()
while len(data) < 8:
nxt = self.stream.read(8 - len(data))
if not nxt:
# The stream has closed, there's nothing more to read
return 0
data = "{0}{1}".format(data, nxt)
data = data + nxt

if data is None:
return 0
Expand Down Expand Up @@ -270,7 +261,6 @@ def __init__(self, from_stream, to_stream):
self.from_stream = from_stream
self.to_stream = to_stream


def fileno(self):
"""
Returns the `fileno()` of the reader end of the Pump.
Expand All @@ -280,11 +270,9 @@ def fileno(self):

return self.from_stream.fileno()


def set_blocking(self, value):
return self.from_stream.set_blocking(value)


def flush(self, n=4096):
"""
Flush `n` bytes of data from the reader Stream to the writer Stream.
Expand Down
28 changes: 14 additions & 14 deletions features/steps/step_definitions.py
Expand Up @@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from behave import *
from behave import then, given, when
from expects import expect, equal, be_true, be_false
import tests.util as util

Expand All @@ -23,8 +23,8 @@
import sys
import os
import signal
import errno
import time
import six


@given('I am using a TTY')
Expand Down Expand Up @@ -97,6 +97,7 @@ def step_impl(ctx):
)
ctx.pid = pid
util.wait(ctx.pty, timeout=5)
time.sleep(1) # give the terminal some time to print prompt


@when('I resize the terminal to {rows} x {cols}')
Expand All @@ -113,22 +114,21 @@ def step_impl(ctx, rows, cols):

@when('I type "{text}"')
def step_impl(ctx, text):
util.write(ctx.pty, text)

util.write(ctx.pty, text.encode())

@when('I press {key}')
def step_impl(ctx, key):
mappings = {
"enter": "\x0a",
"up": "\x1b[A",
"down": "\x1b[B",
"right": "\x1b[C",
"left": "\x1b[D",
"esc": "\x1b",
"c-c": "\x03",
"c-d": "\x04",
"c-p": "\x10",
"c-q": "\x11",
"enter": b"\x0a",
"up": b"\x1b[A",
"down": b"\x1b[B",
"right": b"\x1b[C",
"left": b"\x1b[D",
"esc": b"\x1b",
"c-c": b"\x03",
"c-d": b"\x04",
"c-p": b"\x10",
"c-q": b"\x11",
}
util.write(ctx.pty, mappings[key.lower()])

Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Expand Up @@ -2,3 +2,4 @@ docker-py>=0.3.2
pytest>=2.5.2
behave>=1.2.4
expects>=0.4
six>=1.3.0
1 change: 1 addition & 0 deletions requirements.txt
@@ -0,0 +1 @@
six>=1.3.0
5 changes: 5 additions & 0 deletions requirements3-dev.txt
@@ -0,0 +1,5 @@
docker-py>=0.7.1
pytest>=2.5.2
behave>=1.2.4
expects>=0.4
six>=1.3.0
2 changes: 1 addition & 1 deletion run.sh
Expand Up @@ -6,7 +6,7 @@ echo 1 > /tmp/build.status
# run the build inside UML kernel
./linux quiet mem=2G rootfstype=hostfs rw \
eth0=slirp,,/usr/bin/slirp-fullbolt \
init=$(pwd)/tests.sh WORKDIR=$(pwd) HOME=$HOME
init=$(pwd)/tests.sh WORKDIR=$(pwd) VENV=$VENV HOME=$HOME

# grab the build result and use it
exit $(cat /tmp/build.status)
3 changes: 3 additions & 0 deletions setup.py
Expand Up @@ -17,9 +17,11 @@
from setuptools import setup
import os


def fopen(filename):
return open(os.path.join(os.path.dirname(__file__), filename))


def read(filename):
return fopen(filename).read()

Expand All @@ -31,6 +33,7 @@ def read(filename):
url='https://github.com/d11wtq/dockerpty',
author='Chris Corbyn',
author_email='chris@w3style.co.uk',
install_requires=['six >= 1.3.0'],
license='Apache 2.0',
keywords='docker, tty, pty, terminal',
packages=['dockerpty'],
Expand Down
2 changes: 1 addition & 1 deletion tests.sh
Expand Up @@ -60,7 +60,7 @@ mount --bind /run/resolvconf/resolv.conf /etc/resolv.conf
sleep 5
# activate virtualenv
source $HOME/virtualenv/python2.7/bin/activate
source $HOME/virtualenv/$VENV/bin/activate
# run the build
py.test -q tests && behave -c

0 comments on commit 04986b5

Please sign in to comment.