Skip to content

Commit dcd8a4b

Browse files
committed
Add type annotations to build-script.py
No functionality change
1 parent d9af173 commit dcd8a4b

File tree

1 file changed

+56
-50
lines changed

1 file changed

+56
-50
lines changed

build-script.py

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import subprocess
77
import sys
88
import tempfile
9+
from typing import Dict, List, Optional
910

1011

1112
# -----------------------------------------------------------------------------
@@ -47,7 +48,7 @@
4748
# Xcode Projects Generation
4849

4950

50-
def xcode_gen(config):
51+
def xcode_gen(config: str):
5152
print("** Generate SwiftSyntax as an Xcode project **")
5253
os.chdir(PACKAGE_DIR)
5354
swiftpm_call = ["swift", "package", "generate-xcodeproj"]
@@ -60,28 +61,28 @@ def xcode_gen(config):
6061
# Helpers
6162

6263

63-
def printerr(message):
64+
def printerr(message: str):
6465
print(message, file=sys.stderr)
6566

6667

67-
def note(message):
68+
def note(message: str):
6869
print("--- %s: note: %s" % (os.path.basename(sys.argv[0]), message))
6970
sys.stdout.flush()
7071

7172

72-
def fatal_error(message):
73+
def fatal_error(message: str):
7374
printerr(message)
7475
sys.exit(1)
7576

7677

77-
def escapeCmdArg(arg):
78+
def escapeCmdArg(arg: str) -> str:
7879
if '"' in arg or " " in arg:
7980
return '"%s"' % arg.replace('"', '\\"')
8081
else:
8182
return arg
8283

8384

84-
def call(cmd, env=os.environ, stdout=None, stderr=subprocess.STDOUT, verbose=False):
85+
def call(cmd: List[str], env: Dict[str, str] = dict(os.environ), stdout=None, stderr=subprocess.STDOUT, verbose: bool = False):
8586
if verbose:
8687
print(" ".join([escapeCmdArg(arg) for arg in cmd]))
8788
process = subprocess.Popen(cmd, env=env, stdout=stdout, stderr=stderr)
@@ -90,13 +91,13 @@ def call(cmd, env=os.environ, stdout=None, stderr=subprocess.STDOUT, verbose=Fal
9091
return process.returncode
9192

9293

93-
def check_call(cmd, cwd=None, env=os.environ, verbose=False):
94+
def check_call(cmd: List[str], cwd: Optional[str] = None, env: Dict[str, str] = dict(os.environ), verbose: bool = False):
9495
if verbose:
9596
print(" ".join([escapeCmdArg(arg) for arg in cmd]))
96-
return subprocess.check_call(cmd, cwd=cwd, env=env, stderr=subprocess.STDOUT)
97+
subprocess.check_call(cmd, cwd=cwd, env=env, stderr=subprocess.STDOUT)
9798

9899

99-
def realpath(path):
100+
def realpath(path: Optional[str]) -> Optional[str]:
100101
if path is None:
101102
return None
102103
return os.path.realpath(path)
@@ -106,7 +107,7 @@ def realpath(path):
106107
# Generating gyb Files
107108

108109

109-
def check_gyb_exec(gyb_exec):
110+
def check_gyb_exec(gyb_exec: str):
110111
if not os.path.exists(gyb_exec):
111112
fatal_error(
112113
"""
@@ -128,14 +129,14 @@ def check_rsync():
128129

129130

130131
def generate_single_gyb_file(
131-
gyb_exec,
132-
gyb_file,
133-
output_file_name,
134-
destination,
135-
temp_files_dir,
136-
add_source_locations,
137-
additional_gyb_flags,
138-
verbose,
132+
gyb_exec: str,
133+
gyb_file: str,
134+
output_file_name: str,
135+
destination: str,
136+
temp_files_dir: str,
137+
add_source_locations: bool,
138+
additional_gyb_flags: List[str],
139+
verbose: bool,
139140
):
140141
# Source locations are added by default by gyb, and cleared by passing
141142
# `--line-directive=` (nothing following the `=`) to the generator. Our
@@ -171,11 +172,11 @@ def generate_single_gyb_file(
171172
# `destination_dir`, otherwise they will be written to
172173
# `sources_dir/gyb_generated`.
173174
def generate_gyb_files_helper(
174-
sources_dir,
175-
destination_dir,
176-
gyb_exec,
177-
add_source_locations,
178-
verbose,
175+
sources_dir: str,
176+
destination_dir: Optional[str],
177+
gyb_exec: str,
178+
add_source_locations: bool,
179+
verbose: bool,
179180
):
180181
temp_files_dir = tempfile.gettempdir()
181182
make_dir_if_needed(temp_files_dir)
@@ -217,10 +218,10 @@ def generate_gyb_files_helper(
217218
# `destination_dir/syntax_nodes`, otherwise they will be written to
218219
# `sources_dir/gyb_generated/syntax_nodes`.
219220
def generate_syntax_node_template_gyb_files(
220-
destination_dir,
221-
gyb_exec,
222-
add_source_locations,
223-
verbose
221+
destination_dir: Optional[str],
222+
gyb_exec: str,
223+
add_source_locations: bool,
224+
verbose: bool
224225
):
225226
temp_files_dir = tempfile.gettempdir()
226227
make_dir_if_needed(temp_files_dir)
@@ -260,10 +261,11 @@ def generate_syntax_node_template_gyb_files(
260261

261262

262263
def generate_gyb_files(
263-
gyb_exec, verbose, add_source_locations,
264-
swiftsyntax_destination=None, swiftsyntaxbuilder_destination=None,
265-
swiftsyntaxparser_destination=None,
266-
swiftsyntaxbuildergenerator_destination=None
264+
gyb_exec: str, verbose: bool, add_source_locations: bool,
265+
swiftsyntax_destination: Optional[str] = None,
266+
swiftsyntaxbuilder_destination: Optional[str] = None,
267+
swiftsyntaxparser_destination: Optional[str] = None,
268+
swiftsyntaxbuildergenerator_destination: Optional[str] = None
267269
):
268270
print("** Generating gyb Files **")
269271

@@ -307,7 +309,7 @@ def generate_gyb_files(
307309

308310
print("** Done Generating gyb Files **")
309311

310-
def run_code_generation(toolchain, build_dir, multiroot_data_file, release, verbose, swiftsyntaxbuilder_destination):
312+
def run_code_generation(toolchain: str, build_dir: Optional[str], multiroot_data_file: Optional[str], release: bool, verbose: bool, swiftsyntaxbuilder_destination: str) -> bool:
311313
print("** Running code generation **")
312314
swiftpm_call = get_swiftpm_invocation(
313315
toolchain=toolchain,
@@ -338,7 +340,7 @@ def make_dir_if_needed(path):
338340

339341
# Remove any files in the `gyb_generated` directory that no longer have a
340342
# corresponding `.gyb` file in the `Sources` directory.
341-
def clear_gyb_files_from_previous_run(sources_dir, destination_dir, verbose):
343+
def clear_gyb_files_from_previous_run(sources_dir: str, destination_dir: str, verbose: bool):
342344
for previous_gyb_gen_file in os.listdir(destination_dir):
343345
if previous_gyb_gen_file.endswith(".swift"):
344346
gyb_file = os.path.join(
@@ -356,7 +358,7 @@ def clear_gyb_files_from_previous_run(sources_dir, destination_dir, verbose):
356358
# Building SwiftSyntax
357359

358360

359-
def get_swiftpm_invocation(toolchain, action, build_dir, multiroot_data_file, release):
361+
def get_swiftpm_invocation(toolchain: str, action: str, build_dir: Optional[str], multiroot_data_file: Optional[str], release: bool) -> List[str]:
360362
swift_exec = os.path.join(toolchain, "bin", "swift")
361363

362364
swiftpm_call = [swift_exec, action]
@@ -374,14 +376,18 @@ def get_swiftpm_invocation(toolchain, action, build_dir, multiroot_data_file, re
374376

375377

376378
class Builder(object):
379+
swiftpm_call: List[str]
380+
verbose: bool
381+
toolchain: str
382+
377383
def __init__(
378384
self,
379-
toolchain,
380-
build_dir,
381-
multiroot_data_file,
382-
release,
383-
verbose,
384-
disable_sandbox=False,
385+
toolchain: str,
386+
build_dir: Optional[str],
387+
multiroot_data_file: Optional[str],
388+
release: bool,
389+
verbose: bool,
390+
disable_sandbox: bool = False,
385391
):
386392
self.swiftpm_call = get_swiftpm_invocation(
387393
toolchain=toolchain,
@@ -397,7 +403,7 @@ def __init__(
397403
self.verbose = verbose
398404
self.toolchain = toolchain
399405

400-
def build(self, product_name):
406+
def build(self, product_name: str):
401407
print("** Building " + product_name + " **")
402408
command = list(self.swiftpm_call)
403409
command.extend(["--product", product_name])
@@ -414,7 +420,7 @@ def build(self, product_name):
414420
# Testing
415421

416422

417-
def verify_generated_files(gyb_exec, verbose):
423+
def verify_generated_files(gyb_exec: str, verbose: bool):
418424
user_swiftsyntax_generated_dir = os.path.join(
419425
SWIFTSYNTAX_DIR, "gyb_generated"
420426
)
@@ -453,7 +459,7 @@ def verify_generated_files(gyb_exec, verbose):
453459
user_swiftsyntaxbuildergeneration_generated_dir)
454460

455461

456-
def verify_code_generated_files(toolchain, build_dir, multiroot_data_file, release, verbose):
462+
def verify_code_generated_files(toolchain: str, build_dir: Optional[str], multiroot_data_file: Optional[str], release: bool, verbose: bool):
457463
user_swiftsyntaxbuilder_generated_dir = os.path.join(
458464
SWIFTSYNTAXBUILDER_DIR, "generated"
459465
)
@@ -475,7 +481,7 @@ def verify_code_generated_files(toolchain, build_dir, multiroot_data_file, relea
475481
user_swiftsyntaxbuilder_generated_dir)
476482

477483

478-
def check_generated_files_match(self_generated_dir, user_generated_dir):
484+
def check_generated_files_match(self_generated_dir: str, user_generated_dir: str):
479485
command = [
480486
"diff",
481487
"--recursive",
@@ -505,9 +511,9 @@ def verify_c_syntax_nodes_match():
505511

506512

507513
def run_tests(
508-
toolchain, build_dir, multiroot_data_file, release, filecheck_exec,
509-
skip_lit_tests, verbose
510-
):
514+
toolchain: str, build_dir: Optional[str], multiroot_data_file: Optional[str], release: bool, filecheck_exec: Optional[str],
515+
skip_lit_tests: bool, verbose: bool
516+
) -> bool:
511517
print("** Running SwiftSyntax Tests **")
512518

513519
if skip_lit_tests:
@@ -567,7 +573,7 @@ def check_incr_transfer_roundtrip_exec():
567573
)
568574

569575

570-
def find_lit_test_helper_exec(toolchain, build_dir, release):
576+
def find_lit_test_helper_exec(toolchain: str, build_dir: Optional[str], release: bool):
571577
swiftpm_call = get_swiftpm_invocation(
572578
toolchain=toolchain,
573579
action="build",
@@ -582,7 +588,7 @@ def find_lit_test_helper_exec(toolchain, build_dir, release):
582588
return os.path.join(bin_dir.strip().decode('utf-8'), "lit-test-helper")
583589

584590

585-
def run_lit_tests(toolchain, build_dir, release, filecheck_exec, verbose):
591+
def run_lit_tests(toolchain: str, build_dir: Optional[str], release: bool, filecheck_exec: Optional[str], verbose: bool):
586592
print("** Running lit-based tests **")
587593

588594
check_lit_exec()
@@ -615,7 +621,7 @@ def run_lit_tests(toolchain, build_dir, release, filecheck_exec, verbose):
615621
# XCTest Tests
616622

617623

618-
def run_xctests(toolchain, build_dir, multiroot_data_file, release, verbose):
624+
def run_xctests(toolchain: str, build_dir: Optional[str], multiroot_data_file: Optional[str], release: bool, verbose: bool):
619625
print("** Running XCTests **")
620626
swiftpm_call = get_swiftpm_invocation(
621627
toolchain=toolchain,

0 commit comments

Comments
 (0)