Skip to content

Commit 21cfa85

Browse files
authored
Merge pull request #684 from ahoppen/pr/stress-tester-python3
Fix Python3 issues and add type hints to run_sk_stress_test
2 parents 134cc9b + f959167 commit 21cfa85

File tree

1 file changed

+46
-45
lines changed

1 file changed

+46
-45
lines changed

run_sk_stress_test

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import os
1818
import json
1919
import argparse
2020
import platform
21-
import codecs
2221

2322
import common
23+
from typing import List
2424

2525
script_dir = os.path.abspath(os.path.dirname(__file__))
2626

2727

28-
def main():
28+
def main() -> int:
2929
if platform.system() != 'Darwin':
3030
raise common.UnsupportedPlatform
3131

@@ -51,7 +51,7 @@ def main():
5151
return 0
5252

5353

54-
def parse_args():
54+
def parse_args() -> argparse.Namespace:
5555
parser = argparse.ArgumentParser()
5656
parser.add_argument('swift_branch')
5757
parser.add_argument('--sandbox', action='store_true')
@@ -97,15 +97,19 @@ def parse_args():
9797
action='store_true')
9898
parser.add_argument('--add-swift-flags',
9999
metavar='FLAGS',
100-
help='add flags to each Swift invocation (note: field '
101-
'names from projects.json enclosed in {} will be '
102-
'replaced with their value)',
100+
help='''
101+
add flags to each Swift invocation (note: field
102+
names from projects.json enclosed in {} will be
103+
replaced with their value)
104+
''',
103105
default='')
104106
parser.add_argument('--add-xcodebuild-flags',
105107
metavar='FLAGS',
106-
help='add flags to each xcodebuild invocation (note: field '
107-
'names from projects.json enclosed in {} will be '
108-
'replaced with their value)',
108+
help='''
109+
add flags to each xcodebuild invocation (note: field
110+
names from projects.json enclosed in {} will be
111+
replaced with their value)
112+
''',
109113
default='')
110114
parser.add_argument('--cmake-c-launcher',
111115
metavar="PATH",
@@ -115,45 +119,45 @@ def parse_args():
115119
help='the absolute path to set CMAKE_CXX_COMPILER_LAUNCHER for build-script')
116120
return parser.parse_args()
117121

118-
def get_swiftc_path(workspace, args):
122+
def get_swiftc_path(workspace: str, args: argparse.Namespace) -> str:
119123
if args.swiftc:
120124
return args.swiftc
121125
else:
122126
return os.path.join(workspace, 'build/compat_macos/install/toolchain/usr/bin/swiftc')
123127

124-
def get_sk_swiftc_wrapper_path(workspace, args):
128+
def get_sk_swiftc_wrapper_path(workspace: str, args: argparse.Namespace) -> str:
125129
if args.sk_swiftc_wrapper:
126130
return args.sk_swiftc_wrapper
127131
else:
128132
# If not explicitly specified, fall back to finding `sk-swiftc-wrapper` next to `swiftc`
129133
swiftc_path = get_swiftc_path(workspace, args)
130134
return os.path.join(os.path.dirname(swiftc_path), 'sk-swiftc-wrapper')
131135

132-
def get_sk_stress_test_path(workspace, args):
136+
def get_sk_stress_test_path(workspace: str, args: argparse.Namespace) -> str:
133137
if args.sk_stress_test:
134138
return args.sk_stress_test
135139
else:
136140
# If not explicitly specified, fall back to finding `sk-stress-test` next to `swiftc`
137141
swiftc_path = get_swiftc_path(workspace, args)
138142
return os.path.join(os.path.dirname(swiftc_path), 'sk-stress-test')
139143

140-
def get_sandbox_profile_flags():
144+
def get_sandbox_profile_flags() -> List[str]:
141145
return [
142146
'--sandbox-profile-xcodebuild',
143147
'../../../workspace-private/swift-source-compat-suite-sandbox/sandbox_xcodebuild.sb',
144148
'--sandbox-profile-package',
145149
'../../../workspace-private/swift-source-compat-suite-sandbox/sandbox_package.sb'
146150
]
147151

148-
def clone_stress_tester(workspace, swift_branch):
152+
def clone_stress_tester(workspace: str, swift_branch: str) -> None:
149153
stress_clone_cmd = [
150154
'git','clone', '-q', '-b', swift_branch, '--recursive',
151155
'https://github.com/apple/swift-stress-tester',
152156
'{}/swift-stress-tester'.format(workspace)
153157
]
154158
common.check_execute(stress_clone_cmd, timeout=-1)
155159

156-
def clone_swift_syntax(workspace, swift_branch):
160+
def clone_swift_syntax(workspace: str, swift_branch: str) -> None:
157161
syntax_clone_cmd = [
158162
'git','clone', '-q', '-b', swift_branch, '--recursive',
159163
'https://github.com/apple/swift-syntax',
@@ -162,7 +166,7 @@ def clone_swift_syntax(workspace, swift_branch):
162166
common.check_execute(syntax_clone_cmd, timeout=-1)
163167

164168

165-
def execute_runner(workspace, args):
169+
def execute_runner(workspace: str, args: argparse.Namespace) -> bool:
166170
swiftc_path = get_swiftc_path(workspace, args)
167171
wrapper_path = get_sk_swiftc_wrapper_path(workspace, args)
168172
stress_tester_path = get_sk_stress_test_path(workspace, args)
@@ -189,7 +193,7 @@ def execute_runner(workspace, args):
189193
return passed
190194

191195

192-
def build_swift_toolchain(workspace, args):
196+
def build_swift_toolchain(workspace: str, args: argparse.Namespace) -> None:
193197
build_command = [
194198
os.path.join(workspace, 'swift/utils/build-script'),
195199
'--debug' if args.debug else '--release',
@@ -209,7 +213,7 @@ def build_swift_toolchain(workspace, args):
209213
'--darwin-install-extract-symbols',
210214
'--darwin-toolchain-alias=swift',
211215
'--darwin-toolchain-bundle-identifier=org.swift.compat-macos',
212-
'--darwin-toolchain-display-name-short=Swift Development Snapshot'
216+
'--darwin-toolchain-display-name-short=Swift Development Snapshot',
213217
'--darwin-toolchain-display-name=Swift Development Snapshot',
214218
'--darwin-toolchain-name=swift-DEVELOPMENT-SNAPSHOT',
215219
'--darwin-toolchain-version=3.999.999',
@@ -235,32 +239,37 @@ def build_swift_toolchain(workspace, args):
235239
common.check_execute(build_command, timeout=9999999)
236240

237241

238-
def processed_files_contain(processed_files, file_path):
239-
"""
240-
Returns `True` if a path in `processed_files` contains `file_path` as a substring, `False` otherwise
241-
"""
242-
for processed_file in processed_files:
243-
if file_path in processed_file:
244-
return True
245-
return False
242+
def processed_files_contain(processed_files: List[str], file_path: str) -> bool:
243+
"""
244+
Returns `True` if a path in `processed_files` contains `file_path` as a substring, `False` otherwise
245+
"""
246+
for processed_file in processed_files:
247+
if file_path in processed_file:
248+
return True
249+
return False
246250

247251

248252
class StressTesterRunner(object):
249253
"""sets up the Swift compatibility suite runner to use the stress tester's swiftc-wrapper, executes it, and processes its output for failures."""
250254

251-
def __init__(self, wrapper, stress_tester, swiftc, projects, branch, xfails):
255+
wrapper: str
256+
stress_tester: str
257+
swiftc: str
258+
branch: str
259+
260+
def __init__(self, wrapper: str, stress_tester: str, swiftc: str, projects_path: str, branch: str, xfails_path: str):
252261
self.wrapper = wrapper
253262
self.stress_tester = stress_tester
254263
self.swiftc = swiftc
255264

256-
self.xfails_path = xfails
257-
self.projects_path = projects
265+
self.xfails_path = xfails_path
266+
self.projects_path = projects_path
258267
self.swift_branch = branch
259268

260269
self.compat_runner_failed = False
261270

262271

263-
def run(self, extra_runner_args=[]):
272+
def run(self, extra_runner_args: List[str] = []) -> bool:
264273
# temporary file paths
265274
filtered_projects = os.path.join(script_dir, 'stress-tester-projects.json')
266275
results = os.path.join(script_dir, 'stress-tester-results.json')
@@ -312,14 +321,14 @@ class StressTesterRunner(object):
312321
return success
313322

314323

315-
def _process_output(self, results_path, xfails_path):
324+
def _process_output(self, results_path: str, xfails_path: str) -> bool:
316325
if not os.path.isfile(results_path):
317326
return not self.compat_runner_failed
318327

319-
with open(results_path, 'rb') as results_file:
328+
with open(results_path, 'r') as results_file:
320329
results = json.load(results_file, encoding='utf-8')
321-
with open(xfails_path, 'rb') as xfails_path:
322-
xfails = json.load(xfails_path, encoding='utf-8')
330+
with open(xfails_path, 'r') as xfails_file:
331+
xfails = json.load(xfails_file, encoding='utf-8')
323332

324333
xfails_not_processed = []
325334
for xfail in xfails:
@@ -390,14 +399,14 @@ class StressTesterRunner(object):
390399
return success
391400

392401
@staticmethod
393-
def _print_issue(index, issue, url = None):
402+
def _print_issue(index: int, issue, url = None):
394403
if url != None:
395404
print(u'\n{}. [{}] {}'.format(index + 1, url, issue))
396405
else:
397406
print(u'\n{}. {}'.format(index + 1, issue))
398407

399408

400-
def _filter_projects(self, output):
409+
def _filter_projects(self, output: str) -> str:
401410
with open(self.projects_path) as json_file:
402411
projects = json.load(json_file)
403412
for project in projects:
@@ -419,21 +428,13 @@ class StressTesterRunner(object):
419428
return output
420429

421430
@staticmethod
422-
def _cleanup(paths):
431+
def _cleanup(paths: List[str]) -> None:
423432
for path in paths:
424433
try:
425434
os.remove(path)
426435
except OSError:
427436
pass
428437

429-
if os.isatty(sys.stdout.fileno()):
430-
encoding = sys.stdout.encoding
431-
errors = 'replace'
432-
else:
433-
encoding = 'utf-8'
434-
errors = None
435-
sys.stdout = codecs.getwriter(encoding)(sys.stdout, errors=errors)
436-
437438
if __name__ == '__main__':
438439
sys.exit(main())
439440

0 commit comments

Comments
 (0)