Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Add Python3 support #128

Merged
merged 2 commits into from
Mar 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
language: java
jdk:
- oraclejdk8
language: python
addons:
apt:
packages:
- oracle-java8-set-default
python:
- "2.7"
- "3.5"
- "3.6"

script:
- scripts/travis_run.sh
64 changes: 39 additions & 25 deletions pynailgun/ng.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,29 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function
import ctypes
import platform
import optparse
import os
import os.path
import Queue
import select
import socket
import struct
import sys
from threading import Condition, Event, Thread, RLock

is_py2 = sys.version[0] == '2'
if is_py2:
import Queue as Queue
import __builtin__ as builtin
def to_bytes(s): return s
else:
import queue as Queue
import builtins as builtin
def to_bytes(s): return bytes(s, "utf-8")
from io import UnsupportedOperation

# @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
# @author Pete Kirkham (Win32 port)
# @author Sergey Balabanov, Ben Hamilton (Python port)
Expand All @@ -39,26 +50,25 @@
THREAD_TERMINATION_TIMEOUT_SEC = 0.5
STDIN_BUFFER_LINE_SIZE = 10

CHUNKTYPE_STDIN = '0'
CHUNKTYPE_STDOUT = '1'
CHUNKTYPE_STDERR = '2'
CHUNKTYPE_STDIN_EOF = '.'
CHUNKTYPE_ARG = 'A'
CHUNKTYPE_LONGARG = 'L'
CHUNKTYPE_ENV = 'E'
CHUNKTYPE_DIR = 'D'
CHUNKTYPE_CMD = 'C'
CHUNKTYPE_EXIT = 'X'
CHUNKTYPE_SENDINPUT = 'S'
CHUNKTYPE_HEARTBEAT = 'H'
CHUNKTYPE_STDIN = b'0'
CHUNKTYPE_STDOUT = b'1'
CHUNKTYPE_STDERR = b'2'
CHUNKTYPE_STDIN_EOF = b'.'
CHUNKTYPE_ARG = b'A'
CHUNKTYPE_LONGARG = b'L'
CHUNKTYPE_ENV = b'E'
CHUNKTYPE_DIR = b'D'
CHUNKTYPE_CMD = b'C'
CHUNKTYPE_EXIT = b'X'
CHUNKTYPE_SENDINPUT = b'S'
CHUNKTYPE_HEARTBEAT = b'H'

NSEC_PER_SEC = 1000000000
DEFAULT_HEARTBEAT_INTERVAL_SEC = 0.5
SELECT_MAX_BLOCK_TIME_SEC = 1.0

# We need to support Python 2.6 hosts which lack memoryview().
import __builtin__
HAS_MEMORYVIEW = 'memoryview' in dir(__builtin__)
HAS_MEMORYVIEW = 'memoryview' in dir(builtin)

EVENT_STDIN_CHUNK = 0
EVENT_STDIN_CLOSED = 1
Expand Down Expand Up @@ -242,7 +252,7 @@ class WindowsNamedPipeTransport(Transport):
""" connect to a named pipe """

def __init__(self, sockpath):
self.sockpath = ur'\\.\pipe\{0}'.format(sockpath)
self.sockpath = u'\\\\.\\pipe\\{0}'.format(sockpath)

while True:
self.pipe = CreateFile(self.sockpath,
Expand Down Expand Up @@ -452,7 +462,7 @@ def _send_command_and_read_response(self, cmd, cmd_args, filearg, env, cwd):
self._send_tty_format(self.stdin)
self._send_tty_format(self.stdout)
self._send_tty_format(self.stderr)
for k, v in env.iteritems():
for k, v in env.items():
self._send_env_var(k, v)
self._send_chunk(cwd, CHUNKTYPE_DIR)
self._send_chunk(cmd, CHUNKTYPE_CMD)
Expand Down Expand Up @@ -523,9 +533,12 @@ def _send_tty_format(self, f):
"""
if not f or not hasattr(f, 'fileno'):
return
fileno = f.fileno()
isatty = os.isatty(fileno)
self._send_env_var('NAILGUN_TTY_' + str(fileno), str(int(isatty)))
try:
fileno = f.fileno()
isatty = os.isatty(fileno)
self._send_env_var('NAILGUN_TTY_' + str(fileno), str(int(isatty)))
except UnsupportedOperation:
return


def _send_file_arg(self, filename):
Expand Down Expand Up @@ -553,7 +566,8 @@ def _recv_to_fd(self, dest_file, num_bytes):
self.buf,
bytes_to_read)
if dest_file:
dest_file.write(self.buf[:bytes_received])
data = self.buf[:bytes_received].decode("utf-8")
dest_file.write(data)
bytes_read += bytes_received


Expand Down Expand Up @@ -611,7 +625,7 @@ def _process_exit(self, exit_len):
"""
num_bytes = min(len(self.buf), exit_len)
self._recv_to_buffer(num_bytes, self.buf)
self.exit_code = int(''.join(self.buf.raw[:num_bytes]))
self.exit_code = int(self.buf.raw[:num_bytes])


def _send_heartbeat(self):
Expand Down Expand Up @@ -730,7 +744,7 @@ def send_thread_main(conn):
(chunk_type, buf) = conn.send_queue.get()
struct.pack_into('>ic', header_buf, 0, len(buf), chunk_type)
conn.transport.sendall(header_buf.raw)
conn.transport.sendall(buf)
conn.transport.sendall(to_bytes(buf))

with conn.send_condition:
if conn.shutdown_event.is_set():
Expand Down Expand Up @@ -872,7 +886,7 @@ def main():
(options, args) = parser.parse_args()

if options.nailgun_showversion:
print 'NailGun client version ' + NAILGUN_VERSION
print('NailGun client version ' + NAILGUN_VERSION)

if len(args):
cmd = args.pop(0)
Expand All @@ -889,7 +903,7 @@ def main():
exit_code = c.send_command(cmd, cmd_args, options.nailgun_filearg)
sys.exit(exit_code)
except NailgunException as e:
print >>sys.stderr, str(e)
sys.stderr.write(str(e))
sys.exit(e.code)
except KeyboardInterrupt as e:
pass
Expand Down
24 changes: 14 additions & 10 deletions pynailgun/test_ng.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import subprocess
import os
import time
import StringIO
import unittest
import tempfile
import shutil
Expand All @@ -10,6 +9,11 @@

from pynailgun import NailgunException, NailgunConnection

is_py2 = sys.version[0] == '2'
if is_py2:
from StringIO import StringIO
else:
from io import StringIO

if os.name == 'posix':
def transport_exists(transport_file):
Expand Down Expand Up @@ -50,7 +54,7 @@ def setUpTransport(self):
else:
pipe_name = u'nailgun-test-{0}'.format(uuid.uuid4().hex)
self.transport_address = u'local:{0}'.format(pipe_name)
self.transport_file = ur'\\.\pipe\{0}'.format(pipe_name)
self.transport_file = u'\\\\.\\pipe\{0}'.format(pipe_name)

def getClassPath(self):
cp = [
Expand Down Expand Up @@ -111,7 +115,7 @@ def preexec_fn():
if os.name == 'posix':
# on *nix we have to wait for server to be ready to accept connections
while True:
the_first_line = self.ng_server_process.stdout.readline().strip()
the_first_line = str(self.ng_server_process.stdout.readline().strip())
if "NGServer" in the_first_line and "started" in the_first_line:
break
if the_first_line is None or the_first_line == '':
Expand Down Expand Up @@ -167,7 +171,7 @@ def __init__(self, *args, **kwargs):
super(TestNailgunConnectionMain, self).__init__(*args, **kwargs)

def test_nailgun_stats(self):
output = StringIO.StringIO()
output = StringIO()
with NailgunConnection(
self.transport_address,
stderr=None,
Expand All @@ -180,7 +184,7 @@ def test_nailgun_stats(self):
self.assertEqual(actual_out, expected_out)

def test_nailgun_exit_code(self):
output = StringIO.StringIO()
output = StringIO()
expected_exit_code = 10
with NailgunConnection(
self.transport_address,
Expand All @@ -193,8 +197,8 @@ def test_nailgun_exit_code(self):
def test_nailgun_stdin(self):
lines = [str(i) for i in range(100)]
echo = '\n'.join(lines)
output = StringIO.StringIO()
input = StringIO.StringIO(echo)
output = StringIO()
input = StringIO(echo)
with NailgunConnection(
self.transport_address,
stderr=None,
Expand All @@ -211,7 +215,7 @@ def test_nailgun_default_streams(self):
self.assertEqual(exit_code, 0)

def test_nailgun_heartbeats(self):
output = StringIO.StringIO()
output = StringIO()
with NailgunConnection(
self.transport_address,
stderr=None,
Expand All @@ -224,7 +228,7 @@ def test_nailgun_heartbeats(self):
self.assertTrue(output.getvalue().count('H') > 10)

def test_nailgun_no_heartbeat(self):
output = StringIO.StringIO()
output = StringIO()
with NailgunConnection(
self.transport_address,
stderr=None,
Expand Down Expand Up @@ -252,7 +256,7 @@ def test_nailgun_disconnect(self):
Server runs for 30 sec given we still have heartbeats, so it should output about 6 'H'
We assert that number of 'H' is smaller
"""
output = StringIO.StringIO()
output = StringIO()
with NailgunConnection(
self.transport_address,
stderr=None,
Expand Down
4 changes: 3 additions & 1 deletion scripts/travis_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ set -eux

mvn package

python -m pynailgun.test_ng
export PYTHONPATH=.
python --version
python pynailgun/test_ng.py