-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathomit-needless-words.py
106 lines (86 loc) · 3.14 KB
/
omit-needless-words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/python
# This tool helps assess the impact of automatically applying
# heuristics that omit 'needless' words from APIs imported from Clang
# into Swift.
import getopt
import sys
import subprocess
# Print help
def help():
print('omit-needless-words.py [options] -m <modulename>')
print('')
print('Summary:')
print("\tDetermines the effects of omitting 'needless' words from imported APIs")
print('')
print('Options:')
print('\t-s <sdkname>\t\t\tThe SDK to use (e.g., macosx)')
print("\t--sdk=<sdkname>'\t\tDefaults to 'macosx'")
print('')
print('\t-i <executable>\t\t\tThe swift-ide-test executable')
print("\t--swift-ide-test=<executable>\tDefaults to 'swift-ide-test'")
print('')
print('\t-d <executable>\t\t\tThe tool to use to diff the results')
print("\t--diff_tool=<executable>\tDefaults to 'opendiff'")
print('')
print('Examples:')
print('\tpython omit-needless-words.py -m AppKit')
# Configuration information
sdk = 'macosx'
module = ''
source_filename = 'omit-needless-words.swift'
swift_ide_test = 'swift-ide-test'
diff_tool = 'opendiff'
# Parse command-line arguments.
try:
opts, args = getopt.getopt(sys.argv[1:], 'hs:m:i:d:',
['help', 'sdk=', 'module=','swift-ide-test=',
'diff_tool='])
except getopt.GetoptError:
help()
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
help()
sys.exit()
if opt in ('-s', '--sdk'):
sdk = arg
continue
if opt in ('-m', '--module'):
module = arg
continue
if opt in ('-i', '--swift-ide-test'):
swift_ide_test = arg
continue
if opt in ('-d', '--diff_tool'):
diff_tool = arg
continue
help()
sys.exit(2)
if module == '':
help()
sys.exit(2)
# Figure out the SDK root for the requested SDK
sdkroot = subprocess.check_output(['xcrun', '--show-sdk-path', '--sdk', sdk]).rstrip()
print('SDK Root = %s' % (sdkroot))
swift_ide_test_cmd = [swift_ide_test, '-print-module', '-source-filename', source_filename, '-sdk', sdkroot, '-module-print-skip-overlay', '-skip-unavailable', '-module-print-submodules', '-skip-parameter-names', '-module-to-print=%s' % (module)]
omit_needless_words_args = ['-enable-omit-needless-words']
# Determine the output files.
before_filename = '%s.before.txt' % (module)
after_filename = '%s.after.txt' % (module)
# Create a .swift file we can feed into swift-ide-test
subprocess.call(['touch', source_filename])
# Print the interface without omitting needless words
print('Writing %s...' % before_filename)
before_file = open(before_filename, 'w')
subprocess.call(swift_ide_test_cmd, stdout=before_file)
before_file.close()
# Print the interface omitting needless words
print('Writing %s...' % after_filename)
after_file = open(after_filename, 'w')
subprocess.call(swift_ide_test_cmd + omit_needless_words_args, stdout=after_file)
after_file.close()
# Remove the .swift file we fed into swift-ide-test
subprocess.call(['rm', '-f', source_filename])
# Diff them.
if diff_tool != '':
subprocess.call([diff_tool, before_filename, after_filename])