Skip to content

Commit a600836

Browse files
committed
Merge pull request #639 from alexwlchan/awlc/py3-print-function
Update Python build scripts to use the print function
2 parents 4a0ad9f + ce7ce98 commit a600836

20 files changed

+135
-93
lines changed

docs/scripts/ns-html2rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#!/usr/bin/env python
2+
from __future__ import print_function
3+
24
import sys, re, subprocess
35

46
def run():
57
if len(sys.argv) > 1:
6-
print """
8+
print("""
79
ns-html2rst - Convert Cocoa HTML documentation into ReST
810
911
usage: nshtml2rst < NSString.html > NSString.rst
10-
"""
12+
""")
1113
exit(0)
1214

1315
html = sys.stdin.read()

test/Driver/Dependencies/Inputs/fake-build-for-bitcode.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Emulates the frontend of an -embed-bitcode job. That means we have to handle
44
# -emit-bc and -c actions.
55

6+
from __future__ import print_function
7+
68
import os
79
import shutil
810
import sys
@@ -18,8 +20,8 @@
1820
os.utime(outputFile, None)
1921

2022
if '-emit-bc' in sys.argv:
21-
print "Handled", os.path.basename(primaryFile)
23+
print("Handled", os.path.basename(primaryFile))
2224
elif '-c' in sys.argv:
23-
print "Produced", os.path.basename(outputFile)
25+
print("Produced", os.path.basename(outputFile))
2426
else:
2527
assert False, "unknown action"

test/Driver/Dependencies/Inputs/fake-build-whole-module.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# Emulates the frontend of a -whole-module-optimization compilation.
44

5+
from __future__ import print_function
6+
57
import os
68
import shutil
79
import sys
@@ -16,4 +18,4 @@
1618
with open(outputFile, 'a'):
1719
os.utime(outputFile, None)
1820

19-
print "Produced", os.path.basename(outputFile)
21+
print("Produced", os.path.basename(outputFile))

test/Driver/Dependencies/Inputs/modify-non-primary-files.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# modify-non-primary-files.py simulates a build where the user is modifying the
44
# source files during compilation.
55

6+
from __future__ import print_function
7+
68
import os
79
import shutil
810
import sys
@@ -32,6 +34,6 @@
3234
os.utime(outputFile, None)
3335

3436
if primaryFile:
35-
print "Handled", os.path.basename(primaryFile)
37+
print("Handled", os.path.basename(primaryFile))
3638
else:
37-
print "Produced", os.path.basename(outputFile)
39+
print("Produced", os.path.basename(outputFile))

test/Driver/Dependencies/Inputs/update-dependencies-bad.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Fails if the input file is named "bad.swift"; otherwise dispatches to
44
# update-dependencies.py.
55

6+
from __future__ import print_function
7+
68
import os
79
import sys
810

@@ -11,7 +13,7 @@
1113
primaryFile = sys.argv[sys.argv.index('-primary-file') + 1]
1214

1315
if os.path.basename(primaryFile) == 'bad.swift':
14-
print "Handled", os.path.basename(primaryFile)
16+
print("Handled", os.path.basename(primaryFile))
1517
exit(1)
1618

1719
dir = os.path.dirname(os.path.abspath(__file__))

test/Driver/Dependencies/Inputs/update-dependencies.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#
1515
# If invoked in non-primary-file mode, it only creates the output file.
1616

17+
from __future__ import print_function
18+
1719
import os
1820
import shutil
1921
import sys
@@ -37,6 +39,6 @@
3739
os.utime(outputFile, None)
3840

3941
if primaryFile:
40-
print "Handled", os.path.basename(primaryFile)
42+
print("Handled", os.path.basename(primaryFile))
4143
else:
42-
print "Produced", os.path.basename(outputFile)
44+
print("Produced", os.path.basename(outputFile))

test/Inputs/getmtime.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env python
22

3+
from __future__ import print_function
4+
35
import os
46
import sys
57

6-
print os.path.getmtime(sys.argv[1])
8+
print(os.path.getmtime(sys.argv[1]))

utils/apply-fixit-edits.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#
1313
#===------------------------------------------------------------------------===#
1414

15+
from __future__ import print_function
1516

1617
import subprocess
1718
import json
@@ -29,7 +30,7 @@ def find_remap_files(path):
2930
def apply_edits(path):
3031
remap_files = find_remap_files(path)
3132
if not remap_files:
32-
print "No remap files found"
33+
print("No remap files found")
3334
return 1;
3435

3536
edits_set = set()
@@ -53,7 +54,7 @@ def apply_edits(path):
5354
edits_per_file[fname].append((ed[1], ed[2], ed[3]))
5455

5556
for fname, edits in edits_per_file.iteritems():
56-
print 'Updating', fname
57+
print('Updating', fname)
5758
edits.sort(reverse=True)
5859
file_data = open(fname).read()
5960
for ed in edits:

utils/cmpcodesize/cmpcodesize/compare.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import print_function
2+
13
import re
24
import os
35
import subprocess
@@ -145,7 +147,7 @@ def compareSizes(oldSizes, newSizes, nameKey, title):
145147
perc = "%.1f%%" % ((1.0 - float(newSize) / float(oldSize)) * 100.0)
146148
else:
147149
perc = "- "
148-
print "%-26s%16s: %8d %8d %6s" % (title, nameKey, oldSize, newSize, perc)
150+
print("%-26s%16s: %8d %8d %6s" % (title, nameKey, oldSize, newSize, perc))
149151

150152

151153
def compareSizesOfFile(oldFiles, newFiles, allSections, listCategories):
@@ -226,21 +228,21 @@ def compareFunctionSizes(oldFiles, newFiles):
226228
onlyInFile2Size += newSize
227229

228230
if onlyInFile1:
229-
print "Only in old file(s)"
230-
print listFunctionSizes(onlyInFile1)
231-
print "Total size of functions only in old file: {}".format(onlyInFile1Size)
232-
print
231+
print("Only in old file(s)")
232+
print(listFunctionSizes(onlyInFile1))
233+
print("Total size of functions only in old file: {}".format(onlyInFile1Size))
234+
print()
233235

234236
if onlyInFile2:
235-
print "Only in new files(s)"
236-
print listFunctionSizes(onlyInFile2)
237-
print "Total size of functions only in new file: {}".format(onlyInFile2Size)
238-
print
237+
print("Only in new files(s)")
238+
print(listFunctionSizes(onlyInFile2))
239+
print("Total size of functions only in new file: {}".format(onlyInFile2Size))
240+
print()
239241

240242
if inBoth:
241243
sizeIncrease = 0
242244
sizeDecrease = 0
243-
print "%8s %8s %8s" % ("old", "new", "diff")
245+
print("%8s %8s %8s" % ("old", "new", "diff"))
244246
for triple in sorted(inBoth, key=lambda tup: (tup[2] - tup[1], tup[1])):
245247
func = triple[0]
246248
oldSize = triple[1]
@@ -252,8 +254,8 @@ def compareFunctionSizes(oldFiles, newFiles):
252254
sizeDecrease -= diff
253255
if diff == 0:
254256
inBothSize += newSize
255-
print "%8d %8d %8d %s" %(oldSize, newSize, newSize - oldSize, func)
256-
print "Total size of functions with the same size in both files: {}".format(inBothSize)
257-
print "Total size of functions that got smaller: {}".format(sizeDecrease)
258-
print "Total size of functions that got bigger: {}".format(sizeIncrease)
259-
print "Total size change of functions present in both files: {}".format(sizeIncrease - sizeDecrease)
257+
print("%8d %8d %8d %s" %(oldSize, newSize, newSize - oldSize, func))
258+
print("Total size of functions with the same size in both files: {}".format(inBothSize))
259+
print("Total size of functions that got smaller: {}".format(sizeDecrease))
260+
print("Total size of functions that got bigger: {}".format(sizeIncrease))
261+
print("Total size change of functions present in both files: {}".format(sizeIncrease - sizeDecrease))

utils/cmpcodesize/cmpcodesize/main.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env python
22

3+
from __future__ import print_function
4+
35
import argparse
46
import sys
57
import os
@@ -132,7 +134,7 @@ def main():
132134
'$SWIFT_NEW_BUILDDIR environment variables set.\n' + \
133135
'$SWIFT_OLD_BUILDDIR = {0}\n$SWIFT_NEW_BUILDDIR = {1}'.format(
134136
oldBuildDir, newBuildDir)
135-
oldFileArgs = SHORTCUTS.keys()
137+
oldFileArgs = list(SHORTCUTS.keys())
136138

137139
oldFiles = []
138140
newFiles = []
@@ -150,7 +152,7 @@ def main():
150152
numExpanded += 1
151153

152154
if numExpanded != 0 and numExpanded != len(oldFileArgs):
153-
sys.exit("mix of expanded/not-expanded arguments")
155+
sys.exit("mix of expanded/not-expanded arguments")
154156
if numExpanded == 0:
155157
if len(oldFileArgs) > 2:
156158
sys.exit("too many arguments")
@@ -166,11 +168,11 @@ def main():
166168
sizes = collections.defaultdict(int)
167169
for file in oldFiles:
168170
readSizes(sizes, file, True, False)
169-
print listFunctionSizes(sizes.items())
171+
print(listFunctionSizes(sizes.items()))
170172
else:
171173
compareFunctionSizes(oldFiles, newFiles)
172174
else:
173-
print "%-26s%16s %8s %8s %s" % ("", "Section", "Old", "New", "Percent")
175+
print("%-26s%16s %8s %8s %s" % ("", "Section", "Old", "New", "Percent"))
174176
if parsed_arguments.sum_sizes:
175177
compareSizesOfFile(oldFiles, newFiles,
176178
parsed_arguments.all_sections,

utils/demo-tool

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env python
22

3+
from __future__ import print_function
4+
35
import json
46
import optparse
57
import subprocess
@@ -115,10 +117,10 @@ for example::
115117
def prompterPrint(string):
116118
if hasColor:
117119
attr = [ '32', '1' ]
118-
print '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)
120+
print('\x1b[%sm%s\x1b[0m' % (';'.join(attr), string))
119121
else:
120-
print string
121122

123+
print(string)
122124
def send(*args, **kwargs):
123125
return send_to_screen(opts.screen_name, *args, **kwargs)
124126
parser.add_option("-S", "--screen-name", dest="screen_name", metavar="NAME",
@@ -162,7 +164,7 @@ for example::
162164

163165
if command:
164166
# Send the command slowly, as if it was typed.
165-
print "sending command: %r" % (command.replace('\n', '<cr>'),)
167+
print("sending command: %r" % (command.replace('\n', '<cr>'),))
166168
for c in command:
167169
send(c)
168170
if c == "\n":
@@ -179,7 +181,7 @@ for example::
179181
raw_input('press enter to continue to next command...')
180182
# Send the command slowly, as if it was typed.
181183
if shouldClear:
182-
print "clearing screen"
184+
print("clearing screen")
183185
send(command="clear")
184186
send('\n')
185187

utils/gyb.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# GYB: Generate Your Boilerplate (improved names welcome; at least
33
# this one's short). See -h output for instructions
44

5+
from __future__ import print_function
6+
57
import re
68
from cStringIO import StringIO
79
import tokenize
@@ -1051,8 +1053,8 @@ def succ(a):
10511053
bindings = dict( x.split('=', 1) for x in args.defines )
10521054
ast = parseTemplate(args.file.name, args.file.read())
10531055
if args.dump:
1054-
print ast
10551056

1057+
print(ast)
10561058
# Allow the template to import .py files from its own directory
10571059
sys.path = [os.path.split(args.file.name)[0] or '.'] + sys.path
10581060

utils/omit-needless-words.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# heuristics that omit 'needless' words from APIs imported from Clang
55
# into Swift.
66

7+
from __future__ import print_function
8+
79
import argparse
810
import subprocess
911

@@ -52,32 +54,32 @@ def main():
5254

5355
swift_ide_test_cmd = [args.swift_ide_test, '-print-module', '-source-filename', source_filename, '-sdk', sdkroot, '-target', args.target, '-module-print-skip-overlay', '-skip-unavailable', '-module-print-submodules', '-skip-imports', '-module-to-print=%s' % (args.module)]
5456
omit_needless_words_args = ['-enable-omit-needless-words', '-enable-infer-default-arguments']
55-
57+
5658
# Determine the output files.
5759
# No good way with argparse to set default value based on depenency of other arg.
5860
if not args.before_file:
5961
args.before_file = '%s.before.txt' % (args.module)
6062
if not args.after_file:
6163
args.after_file = '%s.after.txt' % (args.module)
62-
64+
6365
# Create a .swift file we can feed into swift-ide-test
6466
subprocess.call(['touch', source_filename])
65-
67+
6668
if not args.only_after:
6769
# Print the interface without omitting needless words
6870
if not args.quiet:
6971
print('Writing %s...' % args.before_file)
7072
output_command_result_to_file(swift_ide_test_cmd, args.before_file)
71-
73+
7274
if not args.only_before:
7375
# Print the interface omitting needless words
7476
if not args.quiet:
7577
print('Writing %s...' % args.after_file)
7678
output_command_result_to_file(swift_ide_test_cmd + omit_needless_words_args, args.after_file)
77-
79+
7880
# Remove the .swift file we fed into swift-ide-test
7981
subprocess.call(['rm', '-f', source_filename])
80-
82+
8183
# Diff them
8284
subprocess.call([args.diff_tool, args.before_file, args.after_file])
8385

0 commit comments

Comments
 (0)