Skip to content

Commit c9e7daf

Browse files
committed
Adding python 3 support
1 parent b2c1a31 commit c9e7daf

2 files changed

Lines changed: 36 additions & 20 deletions

File tree

build.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616
ch.setFormatter(formatter)
1717
root.addHandler(ch)
1818

19-
2019
SOURCE_FOLDERS = ['src']
2120

2221

2322
@task()
2423
def flake():
25-
print 'flake8 check'
26-
result = execute_sh('flake8 src')
24+
print('flake8 check')
25+
result = _execute_sh('flake8 src')
2726
if result.exitstatus != 0:
2827
print_result_text('Flake errors detected, see above', ShColor.FAIL)
2928
else:
@@ -44,11 +43,11 @@ def test(test_identifier=None):
4443
src_args.append('-s {0}'.format(folder))
4544
test_str = ' '.join(src_args)
4645
else:
47-
print 'Running with test identifier : ' + test_identifier
46+
print('Running with test identifier : ' + test_identifier)
4847
test_str = test_identifier
4948

50-
result = execute_sh('py.test {0} --junitxml=test_results/junit_results.xml'.format(test_str))
51-
49+
result = _execute_sh('py.test {0} --junitxml=test_results/junit_results.xml'.format(test_str))
50+
5251
# Report to the outside world that the tests have failed
5352
if result.exitstatus != 0:
5453
exit(result.exitstatus)
@@ -72,12 +71,12 @@ def run_tests(self, event):
7271
try:
7372
file_to_remove = './' + event.src_path + 'c'
7473
os.remove(file_to_remove)
75-
print 'Deleted pyc file ' + file_to_remove
74+
print('Deleted pyc file ' + file_to_remove)
7675
except OSError:
7776
# it's ok if the file does not exist
78-
print 'Failed to delete file ' + file_to_remove
77+
print('Failed to delete file ' + file_to_remove)
7978
try:
80-
execute_sh("pynt 'test[{0}]'".format(test_identifier if test_identifier is not None else ''))
79+
_execute_sh("pynt 'test[{0}]'".format(test_identifier if test_identifier is not None else ''))
8180

8281
except:
8382
root.exception('Error running tests')
@@ -103,6 +102,7 @@ def on_created(self, event):
103102
ob.stop()
104103
ob.join()
105104

105+
106106
# Utility stuff ----------------------------
107107

108108

@@ -124,24 +124,36 @@ class ExecuteShellError(Exception):
124124
pass
125125

126126

127-
def execute_sh(cmd,
128-
abort_on_error=False,
129-
print_output=True):
130-
'''Execute a shell command'''
127+
# Something simple to make sure python 2 and python 3 are both handled
128+
class StdOutBytesToFile(object):
129+
def write(self, str_or_bytes_to_write):
130+
if isinstance(str_or_bytes_to_write, str):
131+
return sys.stdout.write(str_or_bytes_to_write)
132+
return sys.stdout.write(str_or_bytes_to_write.decode(sys.stdout.encoding))
133+
134+
def flush(self):
135+
return sys.stdout.flush()
136+
137+
138+
def _execute_sh(cmd, abort_on_error=False):
139+
"""Execute a shell command"""
131140
child = pexpect.spawn(cmd)
132-
child.expect(pexpect.EOF)
141+
142+
# redirect the stdout of child to parent
143+
child.logfile = StdOutBytesToFile()
144+
145+
child.expect(pexpect.EOF, timeout=1200)
133146
if child.isalive():
134147
child.wait()
135-
output = child.before.decode('utf-8')
136-
if print_output:
137-
print output
148+
138149
if abort_on_error:
139150
if child.exitstatus != 0:
140-
raise ExecuteShellError('Error excuting command: {0}'.format(cmd))
141-
return ShellResult(output=output,
151+
raise ExecuteShellError('Error executing command: {0}'.format(cmd))
152+
153+
return ShellResult(output=child.before,
142154
exitstatus=child.exitstatus,
143155
signalstatus=child.signalstatus)
144156

145157

146158
def print_result_text(text, color):
147-
print '{3}{0}============= {1} ============={2}'.format(color, text, ShColor.ENDC, ShColor.BOLD)
159+
print('{3}{0}============= {1} ============={2}'.format(color, text, ShColor.ENDC, ShColor.BOLD))

src/something_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import time
12
import unittest
23

34

45
class SomethingTestCase(unittest.TestCase):
56

67
def test_some_stuff(self):
78
pass
9+
10+
def test_a_long_test(self):
11+
time.sleep(3)

0 commit comments

Comments
 (0)