Skip to content

Commit

Permalink
[Python] Update linter rules
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
modocache committed Mar 13, 2016
1 parent 57c869a commit bb16e98
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .pep8
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[flake8]
filename = *.py,bootstrap
ignore = E101,E111,E128,E265,E302,E402,E501,W191
max-line-length = 80
67 changes: 47 additions & 20 deletions Utilities/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@ import subprocess
import sys
import tempfile

###

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


def error(message):
print("%s: error: %s" % (os.path.basename(sys.argv[0]), message))
sys.stdout.flush()
raise SystemExit(1)


def mkdir_p(path):
"""
mkdir_p(path)
Expand All @@ -67,6 +68,7 @@ def mkdir_p(path):
if e.errno != errno.EEXIST:
raise


# FIXME: Consider eliminating this once the build task format supports node
# hashing.
def write_file_if_changed(path, data):
Expand Down Expand Up @@ -94,9 +96,12 @@ g_num_cpus = os.sysconf("SC_NPROCESSORS_ONLN")
g_default_sysroot = None
if platform.system() == 'Darwin':
g_platform_path = subprocess.check_output(
["xcrun", "--sdk", "macosx", "--show-sdk-platform-path"], universal_newlines=True).strip()
["xcrun", "--sdk", "macosx", "--show-sdk-platform-path"],
universal_newlines=True).strip()
g_default_sysroot = subprocess.check_output(
["xcrun", "--sdk", "macosx", "--show-sdk-path"], universal_newlines=True).strip()
["xcrun", "--sdk", "macosx", "--show-sdk-path"],
universal_newlines=True).strip()


class Target(object):
@property
Expand Down Expand Up @@ -168,22 +173,27 @@ class Target(object):
print(" outputs: %s" % json.dumps(
[compile_swift_node, module_path] + swift_objects), file=output)
print(" module-name: %s" % json.dumps(module_name), file=output)
print(" module-output-path: %s" % json.dumps(module_path), file=output)
print(" sources: %s" % json.dumps(
self.swift_sources), file=output)
print(" module-output-path: %s" % json.dumps(module_path),
file=output)
print(" sources: %s" % json.dumps(self.swift_sources), file=output)
print(" objects: %s" % json.dumps(swift_objects), file=output)
print(" import-paths: %s" % json.dumps(
[module_dir]), file=output)
print(" other-args: %s" % json.dumps(' '.join(other_args)), file=output)
print(" other-args: %s" % json.dumps(' '.join(other_args)),
file=output)
print(" temps-path: %s" % json.dumps(target_build_dir), file=output)
print(" is-library: %s" % json.dumps(
str(bool(self.is_library)).lower()), file=output)
print(file=output)


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

def convert(match):
Expand All @@ -198,7 +208,8 @@ def parse_manifest():
try:
return next(a for a in targets if a.name == targetName)
except StopIteration:
# this target is not explicit in the manifest: it is an implicit target
# this target is not explicit in the manifest: it is an
# implicit target
b = Target(targetName)
targets.append(b)
return b
Expand All @@ -207,6 +218,7 @@ def parse_manifest():
# fill dependency graph and set dependencies back to strings
def convert(target):
myset = set()

def recurse(root):
deps = []
for dep in root.dependencies:
Expand All @@ -224,6 +236,7 @@ g_source_root = os.path.join(g_project_root, "Sources")
targets = parse_manifest()
target_map = dict((t.name, t) for t in targets)


def create_bootstrap_files(sandbox_path, args):
# Write out the build file.
output = StringIO()
Expand Down Expand Up @@ -284,7 +297,8 @@ def create_bootstrap_files(sandbox_path, args):
print(" %s:" % json.dumps(predecessor_node), file=output)
print(" tool: phony", file=output)
print(" inputs: %s" % json.dumps(
[target_map[name].virtual_node for name in target.dependencies]), file=output)
[target_map[name].virtual_node for name in target.dependencies]),
file=output)
print(" outputs: %s" % json.dumps([predecessor_node]), file=output)
print(file=output)

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

link_command = [args.swiftc_path, '-o', pipes.quote(link_output_path)]
link_command = [args.swiftc_path,
'-o', pipes.quote(link_output_path)]
if args.sysroot:
link_command.extend(["-sdk", args.sysroot])
if platform.system() == 'Darwin':
Expand All @@ -325,7 +340,10 @@ def create_bootstrap_files(sandbox_path, args):
# Write out the link command.
print(" %s:" % json.dumps(target.linked_virtual_node), file=output)
print(" tool: shell", file=output)
print(" description: Link %s" % (target.name if target.is_library else link_output_path,), file=output)
if target.is_library:
print(" description: Link %s" % target.name, file=output)
else:
print(" description: Link %s" % link_output_path, file=output)
print(" inputs: %s" % json.dumps(
link_input_nodes + objects + linked_libraries), file=output)
print(" outputs: %s" % json.dumps(
Expand All @@ -338,13 +356,15 @@ def create_bootstrap_files(sandbox_path, args):
print(" tool: phony", file=output)
print(" inputs: %s" % json.dumps(
[target.linked_virtual_node]), file=output)
print(" outputs: %s" % json.dumps([target.virtual_node]), file=output)
print(" outputs: %s" % json.dumps([target.virtual_node]),
file=output)
print(file=output)

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


def process_runtime_libraries(build_path, args, bootstrap=False):
if bootstrap:
module_input_path = os.path.join(
Expand Down Expand Up @@ -391,7 +411,8 @@ def process_runtime_libraries(build_path, args, bootstrap=False):
# error.
tf = tempfile.NamedTemporaryFile(suffix=".swift")
cmds = subprocess.check_output(
cmd + [tf.name, "-###"], universal_newlines=True).strip().split("\n")
cmd + [tf.name, "-###"],
universal_newlines=True).strip().split("\n")

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


def get_swift_build_tool_path():
# Search for a 'swift-build-tool' to use.

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

# Next, search for it in PATH.
try:
return subprocess.check_output(["which", "swift-build-tool"], universal_newlines=True).strip()
return subprocess.check_output(["which", "swift-build-tool"],
universal_newlines=True).strip()
except:
pass

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


def main():
parser = argparse.ArgumentParser(
usage="%(prog)s [options] [clean|all|test|install]",
Expand Down Expand Up @@ -517,9 +542,11 @@ def main():
args.sbt_path = os.path.abspath(
args.sbt_path or get_swift_build_tool_path())

# Due to bug in Xcode where SWIFT_EXEC is not set correctly by downloadable toolchain
# Due to bug in Xcode where SWIFT_EXEC is not set correctly by downloadable
# toolchain
if os.getenv("XCODE_DEFAULT_TOOLCHAIN_OVERRIDE"):
args.swiftc = os.path.join(os.getenv("XCODE_DEFAULT_TOOLCHAIN_OVERRIDE"), "usr/bin/swiftc")
args.swiftc = os.path.join(
os.getenv("XCODE_DEFAULT_TOOLCHAIN_OVERRIDE"), "usr/bin/swiftc")

# Create or update the bootstrap files.
create_bootstrap_files(sandbox_path, args)
Expand Down Expand Up @@ -561,7 +588,7 @@ def main():
if args.sysroot:
env_cmd.append("SYSROOT=" + args.sysroot)

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

0 comments on commit bb16e98

Please sign in to comment.