Make Python scripts Python 2 and 3 compatible. An alternative to #71.

This commit is contained in:
Kyle Marek-Spartz
2015-01-05 20:09:23 -06:00
parent 84b2dbf48e
commit ca9b399d56
5 changed files with 40 additions and 33 deletions

View File

@ -2,4 +2,5 @@ This is the (likely imcomplete) list of people who have made Wren what it is.
If you submit a patch to Wren, please add your name and email address to the If you submit a patch to Wren, please add your name and email address to the
end of this list. end of this list.
Robert Nystrom <robert@stuffwithstuff.com> Robert Nystrom <robert@stuffwithstuff.com>
Kyle Marek-Spartz <kyle.marek.spartz@gmail.com>

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
import glob import glob
import os.path import os.path
import re import re
@ -33,7 +34,7 @@ def copy_builtin(filename):
with open("src/wren_" + name + ".c", "w") as f: with open("src/wren_" + name + ".c", "w") as f:
f.write(c_source) f.write(c_source)
print name print(name)
for f in glob.iglob("builtin/*.wren"): for f in glob.iglob("builtin/*.wren"):

View File

@ -7,6 +7,7 @@ import shutil
import subprocess import subprocess
import sys import sys
import time import time
import re
from datetime import datetime from datetime import datetime
def is_up_to_date(path, out_path): def is_up_to_date(path, out_path):
@ -49,7 +50,7 @@ def format_file(path, skip_up_to_date):
elif command == "category": elif command == "category":
category = args category = args
else: else:
print "UNKNOWN COMMAND:", command, args print(' '.join(["UNKNOWN COMMAND:", command, args]))
elif stripped.startswith('#'): elif stripped.startswith('#'):
# Add anchors to the headers. # Add anchors to the headers.
@ -57,7 +58,7 @@ def format_file(path, skip_up_to_date):
headertype = stripped[:index] headertype = stripped[:index]
header = stripped[index:].strip() header = stripped[index:].strip()
anchor = header.lower().replace(' ', '-') anchor = header.lower().replace(' ', '-')
anchor = anchor.translate(None, '.?!:/') anchor = re.compile('\.|\?|!|:|/').sub('', anchor)
contents += indentation + headertype contents += indentation + headertype
contents += '{1} <a href="#{0}" name="{0}" class="header-anchor">#</a>\n'.format(anchor, header) contents += '{1} <a href="#{0}" name="{0}" class="header-anchor">#</a>\n'.format(anchor, header)
@ -84,7 +85,7 @@ def format_file(path, skip_up_to_date):
with open(out_path, 'w') as out: with open(out_path, 'w') as out:
out.write(template.format(**fields)) out.write(template.format(**fields))
print "converted", basename print("converted " + basename)
def check_sass(): def check_sass():
@ -98,7 +99,7 @@ def check_sass():
return return
subprocess.call(['sass', 'doc/site/style.scss', 'build/docs/style.css']) subprocess.call(['sass', 'doc/site/style.scss', 'build/docs/style.css'])
print "built css" print("built css")
def format_files(skip_up_to_date): def format_files(skip_up_to_date):

View File

@ -1,4 +1,4 @@
#!/usr/bin/python #!/usr/bin/env python
import glob import glob
import fnmatch import fnmatch
@ -64,17 +64,17 @@ for dir_path, dir_names, file_names in os.walk("test"):
num_expects += 1 num_expects += 1
continue continue
print "source:" print("source:")
print " files ", num_files print(" files " + str(num_files))
print " semicolons ", num_semicolons print(" semicolons " + str(num_semicolons))
print " TODOs ", num_todos print(" TODOs " + str(num_todos))
print " comment lines ", num_docs print(" comment lines " + str(num_docs))
print " code lines ", num_code print(" code lines " + str(num_code))
print " empty lines ", num_empty print(" empty lines " + str(num_empty))
print print("\n")
print "test:" print("test:")
print " files ", num_test_files print(" files " + str(num_test_files))
print " TODOs ", num_test_todos print(" TODOs " + str(num_test_todos))
print " expectations ", num_expects print(" expectations " + str(num_expects))
print " non-empty lines", num_test print(" non-empty lines " + str(num_test))
print " empty lines ", num_test_empty print(" empty lines " + str(num_test_empty))

View File

@ -1,4 +1,6 @@
#!/usr/bin/python #!/usr/bin/env python
from __future__ import print_function
from collections import defaultdict from collections import defaultdict
from os import listdir from os import listdir
@ -55,11 +57,11 @@ def walk(dir, callback):
def print_line(line=None): def print_line(line=None):
# Erase the line. # Erase the line.
print '\033[2K', print('\033[2K', end='')
# Move the cursor to the beginning. # Move the cursor to the beginning.
print '\r', print('\r', end='')
if line: if line:
print line, print(line, end='')
sys.stdout.flush() sys.stdout.flush()
@ -134,7 +136,7 @@ def run_test(path):
# Invoke wren and run the test. # Invoke wren and run the test.
proc = Popen([WREN_APP, path], stdout=PIPE, stderr=PIPE) proc = Popen([WREN_APP, path], stdout=PIPE, stderr=PIPE)
(out, err) = proc.communicate() (out, err) = proc.communicate()
(out, err) = out.replace('\r\n', '\n'), err.replace('\r\n', '\n') (out, err) = out.decode("utf-8").replace('\r\n', '\n'), err.decode("utf-8").replace('\r\n', '\n')
fails = [] fails = []
@ -193,6 +195,8 @@ def run_test(path):
del out_lines[-1] del out_lines[-1]
for line in out_lines: for line in out_lines:
if sys.version_info < (3, 0):
line = line.encode('utf-8')
if expect_index >= len(expect_output): if expect_index >= len(expect_output):
fails.append('Got output "{0}" when none was expected.'.format(line)) fails.append('Got output "{0}" when none was expected.'.format(line))
elif expect_output[expect_index][0] != line: elif expect_output[expect_index][0] != line:
@ -214,23 +218,23 @@ def run_test(path):
else: else:
failed += 1 failed += 1
print_line(color.RED + 'FAIL' + color.DEFAULT + ': ' + path) print_line(color.RED + 'FAIL' + color.DEFAULT + ': ' + path)
print print('\n')
for fail in fails: for fail in fails:
print ' ', color.PINK + fail + color.DEFAULT print(' ' + color.PINK + fail + color.DEFAULT)
print print('\n')
walk(TEST_DIR, run_test) walk(TEST_DIR, run_test)
print_line() print_line()
if failed == 0: if failed == 0:
print 'All ' + color.GREEN + str(passed) + color.DEFAULT + ' tests passed.' print('All ' + color.GREEN + str(passed) + color.DEFAULT + ' tests passed.')
else: else:
print (color.GREEN + str(passed) + color.DEFAULT + ' tests passed. ' + print(color.GREEN + str(passed) + color.DEFAULT + ' tests passed. ' +
color.RED + str(failed) + color.DEFAULT + ' tests failed.') color.RED + str(failed) + color.DEFAULT + ' tests failed.')
for key in sorted(skipped.keys()): for key in sorted(skipped.keys()):
print ('Skipped ' + color.YELLOW + str(skipped[key]) + color.DEFAULT + print('Skipped ' + color.YELLOW + str(skipped[key]) + color.DEFAULT +
' tests: ' + key) ' tests: ' + key)
if failed != 0: if failed != 0:
sys.exit(1) sys.exit(1)