Skip to content

Commit bb16e98

Browse files
committed
[Python] Update linter rules
In swiftlang/swift@d5326bf, @practicalswift updated the Swift project to no longer ignore individual linter rules globally. Update the SwiftPM project to do the same. In addition, fix all remaining linter rule violations. Most of these involved splitting up lines. One spot involved using `endswith()` with a tuple argument to check against multiple suffixes at once. To lint the Python code in the project: $ flake8 To install flake8: $ pip install flake8 See https://flake8.readthedocs.org/en/latest/ for details.
1 parent 57c869a commit bb16e98

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

.pep8

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[flake8]
22
filename = *.py,bootstrap
3-
ignore = E101,E111,E128,E265,E302,E402,E501,W191
3+
max-line-length = 80

Utilities/bootstrap

+47-20
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,18 @@ import subprocess
4242
import sys
4343
import tempfile
4444

45-
###
4645

4746
def note(message):
4847
print("%s: note: %s" % (os.path.basename(sys.argv[0]), message))
4948
sys.stdout.flush()
5049

50+
5151
def error(message):
5252
print("%s: error: %s" % (os.path.basename(sys.argv[0]), message))
5353
sys.stdout.flush()
5454
raise SystemExit(1)
5555

56+
5657
def mkdir_p(path):
5758
"""
5859
mkdir_p(path)
@@ -67,6 +68,7 @@ def mkdir_p(path):
6768
if e.errno != errno.EEXIST:
6869
raise
6970

71+
7072
# FIXME: Consider eliminating this once the build task format supports node
7173
# hashing.
7274
def write_file_if_changed(path, data):
@@ -94,9 +96,12 @@ g_num_cpus = os.sysconf("SC_NPROCESSORS_ONLN")
9496
g_default_sysroot = None
9597
if platform.system() == 'Darwin':
9698
g_platform_path = subprocess.check_output(
97-
["xcrun", "--sdk", "macosx", "--show-sdk-platform-path"], universal_newlines=True).strip()
99+
["xcrun", "--sdk", "macosx", "--show-sdk-platform-path"],
100+
universal_newlines=True).strip()
98101
g_default_sysroot = subprocess.check_output(
99-
["xcrun", "--sdk", "macosx", "--show-sdk-path"], universal_newlines=True).strip()
102+
["xcrun", "--sdk", "macosx", "--show-sdk-path"],
103+
universal_newlines=True).strip()
104+
100105

101106
class Target(object):
102107
@property
@@ -168,22 +173,27 @@ class Target(object):
168173
print(" outputs: %s" % json.dumps(
169174
[compile_swift_node, module_path] + swift_objects), file=output)
170175
print(" module-name: %s" % json.dumps(module_name), file=output)
171-
print(" module-output-path: %s" % json.dumps(module_path), file=output)
172-
print(" sources: %s" % json.dumps(
173-
self.swift_sources), file=output)
176+
print(" module-output-path: %s" % json.dumps(module_path),
177+
file=output)
178+
print(" sources: %s" % json.dumps(self.swift_sources), file=output)
174179
print(" objects: %s" % json.dumps(swift_objects), file=output)
175180
print(" import-paths: %s" % json.dumps(
176181
[module_dir]), file=output)
177-
print(" other-args: %s" % json.dumps(' '.join(other_args)), file=output)
182+
print(" other-args: %s" % json.dumps(' '.join(other_args)),
183+
file=output)
178184
print(" temps-path: %s" % json.dumps(target_build_dir), file=output)
179185
print(" is-library: %s" % json.dumps(
180186
str(bool(self.is_library)).lower()), file=output)
181187
print(file=output)
182188

189+
183190
# currently only returns the targets parsed from the manifest
184191
def parse_manifest():
185-
# we have a *very* strict format for our manifest to make parsing more robust
186-
pattern = re.compile(r'Target\(.*?name: "(.*?)",\n *dependencies: (\[.*?\])\)', re.DOTALL|re.MULTILINE)
192+
# we have a *very* strict format for our manifest to make parsing more
193+
# robust
194+
pattern = re.compile(
195+
r'Target\(.*?name: "(.*?)",\n *dependencies: (\[.*?\])\)',
196+
re.DOTALL | re.MULTILINE)
187197
manifest_data = open(os.path.join(g_project_root, "Package.swift")).read()
188198

189199
def convert(match):
@@ -198,7 +208,8 @@ def parse_manifest():
198208
try:
199209
return next(a for a in targets if a.name == targetName)
200210
except StopIteration:
201-
# this target is not explicit in the manifest: it is an implicit target
211+
# this target is not explicit in the manifest: it is an
212+
# implicit target
202213
b = Target(targetName)
203214
targets.append(b)
204215
return b
@@ -207,6 +218,7 @@ def parse_manifest():
207218
# fill dependency graph and set dependencies back to strings
208219
def convert(target):
209220
myset = set()
221+
210222
def recurse(root):
211223
deps = []
212224
for dep in root.dependencies:
@@ -224,6 +236,7 @@ g_source_root = os.path.join(g_project_root, "Sources")
224236
targets = parse_manifest()
225237
target_map = dict((t.name, t) for t in targets)
226238

239+
227240
def create_bootstrap_files(sandbox_path, args):
228241
# Write out the build file.
229242
output = StringIO()
@@ -284,7 +297,8 @@ def create_bootstrap_files(sandbox_path, args):
284297
print(" %s:" % json.dumps(predecessor_node), file=output)
285298
print(" tool: phony", file=output)
286299
print(" inputs: %s" % json.dumps(
287-
[target_map[name].virtual_node for name in target.dependencies]), file=output)
300+
[target_map[name].virtual_node for name in target.dependencies]),
301+
file=output)
288302
print(" outputs: %s" % json.dumps([predecessor_node]), file=output)
289303
print(file=output)
290304

@@ -307,7 +321,8 @@ def create_bootstrap_files(sandbox_path, args):
307321
else:
308322
link_output_path = os.path.join(bin_dir, target.name)
309323

310-
link_command = [args.swiftc_path, '-o', pipes.quote(link_output_path)]
324+
link_command = [args.swiftc_path,
325+
'-o', pipes.quote(link_output_path)]
311326
if args.sysroot:
312327
link_command.extend(["-sdk", args.sysroot])
313328
if platform.system() == 'Darwin':
@@ -325,7 +340,10 @@ def create_bootstrap_files(sandbox_path, args):
325340
# Write out the link command.
326341
print(" %s:" % json.dumps(target.linked_virtual_node), file=output)
327342
print(" tool: shell", file=output)
328-
print(" description: Link %s" % (target.name if target.is_library else link_output_path,), file=output)
343+
if target.is_library:
344+
print(" description: Link %s" % target.name, file=output)
345+
else:
346+
print(" description: Link %s" % link_output_path, file=output)
329347
print(" inputs: %s" % json.dumps(
330348
link_input_nodes + objects + linked_libraries), file=output)
331349
print(" outputs: %s" % json.dumps(
@@ -338,13 +356,15 @@ def create_bootstrap_files(sandbox_path, args):
338356
print(" tool: phony", file=output)
339357
print(" inputs: %s" % json.dumps(
340358
[target.linked_virtual_node]), file=output)
341-
print(" outputs: %s" % json.dumps([target.virtual_node]), file=output)
359+
print(" outputs: %s" % json.dumps([target.virtual_node]),
360+
file=output)
342361
print(file=output)
343362

344363
# Write the output file.
345364
write_file_if_changed(os.path.join(sandbox_path, "build.swift-build"),
346365
output.getvalue())
347366

367+
348368
def process_runtime_libraries(build_path, args, bootstrap=False):
349369
if bootstrap:
350370
module_input_path = os.path.join(
@@ -391,7 +411,8 @@ def process_runtime_libraries(build_path, args, bootstrap=False):
391411
# error.
392412
tf = tempfile.NamedTemporaryFile(suffix=".swift")
393413
cmds = subprocess.check_output(
394-
cmd + [tf.name, "-###"], universal_newlines=True).strip().split("\n")
414+
cmd + [tf.name, "-###"],
415+
universal_newlines=True).strip().split("\n")
395416

396417
# Get the link command 'swiftc' used.
397418
link_cmd = shlex.split(cmds[-1])
@@ -408,10 +429,12 @@ def process_runtime_libraries(build_path, args, bootstrap=False):
408429
raise SystemExit("unable to understand 'swiftc' driver commands")
409430
del link_cmd[idx - 1]
410431
cmd = [arg for arg in link_cmd
411-
if arg.endswith("swift_begin.o") or arg.endswith("swift_end.o") or (not arg.endswith(".o") and not arg.endswith(".autolink"))]
432+
if arg.endswith(("swift_begin.o", "swift_end.o")) or
433+
(not arg.endswith((".o", ".autolink")))]
412434
subprocess.check_call(cmd)
413435
return (runtime_module_path, runtime_lib_path)
414436

437+
415438
def get_swift_build_tool_path():
416439
# Search for a 'swift-build-tool' to use.
417440

@@ -428,7 +451,8 @@ def get_swift_build_tool_path():
428451

429452
# Next, search for it in PATH.
430453
try:
431-
return subprocess.check_output(["which", "swift-build-tool"], universal_newlines=True).strip()
454+
return subprocess.check_output(["which", "swift-build-tool"],
455+
universal_newlines=True).strip()
432456
except:
433457
pass
434458

@@ -446,6 +470,7 @@ def get_swift_build_tool_path():
446470
# If all else failed, report an error.
447471
error("unable to find 'swift-build-tool' tool for bootstrap build")
448472

473+
449474
def main():
450475
parser = argparse.ArgumentParser(
451476
usage="%(prog)s [options] [clean|all|test|install]",
@@ -517,9 +542,11 @@ def main():
517542
args.sbt_path = os.path.abspath(
518543
args.sbt_path or get_swift_build_tool_path())
519544

520-
# Due to bug in Xcode where SWIFT_EXEC is not set correctly by downloadable toolchain
545+
# Due to bug in Xcode where SWIFT_EXEC is not set correctly by downloadable
546+
# toolchain
521547
if os.getenv("XCODE_DEFAULT_TOOLCHAIN_OVERRIDE"):
522-
args.swiftc = os.path.join(os.getenv("XCODE_DEFAULT_TOOLCHAIN_OVERRIDE"), "usr/bin/swiftc")
548+
args.swiftc = os.path.join(
549+
os.getenv("XCODE_DEFAULT_TOOLCHAIN_OVERRIDE"), "usr/bin/swiftc")
523550

524551
# Create or update the bootstrap files.
525552
create_bootstrap_files(sandbox_path, args)
@@ -561,7 +588,7 @@ def main():
561588
if args.sysroot:
562589
env_cmd.append("SYSROOT=" + args.sysroot)
563590

564-
# We need to embed an RPATH so swift-{build,test} can find the core
591+
# We need to embed an RPATH so swift-{build,test} can find the core
565592
# libraries.
566593
if platform.system() == 'Linux':
567594
embed_rpath = "$ORIGIN/../lib/swift/linux"

0 commit comments

Comments
 (0)