|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | + This source file is part of the Swift.org open source project |
| 5 | +
|
| 6 | + Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 7 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 8 | +
|
| 9 | + See http://swift.org/LICENSE.txt for license information |
| 10 | + See http://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 11 | +
|
| 12 | + ------------------------------------------------------------------------- |
| 13 | + This script contains helper operations to use Docker for SwitPM development |
| 14 | + and testing. |
| 15 | +""" |
| 16 | + |
| 17 | +import argparse |
| 18 | +import urllib2 |
| 19 | +import os |
| 20 | +import subprocess |
| 21 | +import yaml |
| 22 | + |
| 23 | +def call(args): |
| 24 | + """Prints and executes a command.""" |
| 25 | + print " ".join(args) |
| 26 | + return subprocess.call(args) |
| 27 | + |
| 28 | +def docker_run(command_args): |
| 29 | + """Runs a command in the container.""" |
| 30 | + call([ |
| 31 | + "docker", |
| 32 | + "run", |
| 33 | + "-it", |
| 34 | + "--security-opt", "seccomp=unconfined", |
| 35 | + "-v", os.getcwd() + ":/swiftpm", |
| 36 | + "-w", "/swiftpm", |
| 37 | + "--rm", |
| 38 | + "swiftpm-docker-1604" |
| 39 | + ] + command_args) |
| 40 | + |
| 41 | +def build(_): |
| 42 | + """Builds a docker image with the latest snapshot.""" |
| 43 | + |
| 44 | + # Get latest snapshot info. |
| 45 | + latest_build_url = "https://swift.org/builds/development/ubuntu1604/latest-build.yml" |
| 46 | + latest_build_data = yaml.load(urllib2.urlopen(latest_build_url).read()) |
| 47 | + snapshot_filename = latest_build_data["download"] |
| 48 | + # FIXME: We shouldn"t need to do this, it should be available in the API. |
| 49 | + snapshot_name = snapshot_filename.replace("-ubuntu16.04.tar.gz", "") |
| 50 | + base_url = latest_build_url.rsplit("/", 1)[0] |
| 51 | + latest_snapshot_url = base_url + "/" + snapshot_name + "/" + snapshot_filename |
| 52 | + docker_dir = os.path.dirname(os.path.realpath(__file__)) + "/Docker" |
| 53 | + |
| 54 | + # Download latest snapshot (if necessary). |
| 55 | + if snapshot_filename in os.listdir(docker_dir): |
| 56 | + print "Not downloading: " + latest_snapshot_url |
| 57 | + else: |
| 58 | + # FIXME: We should remove old tarballs if we have newer ones. |
| 59 | + result = call([ |
| 60 | + "curl", |
| 61 | + "-o", |
| 62 | + docker_dir + "/" + snapshot_filename, |
| 63 | + latest_snapshot_url |
| 64 | + ]) |
| 65 | + |
| 66 | + if result != 0: |
| 67 | + print "Unable to install package: " + latest_snapshot_url |
| 68 | + exit(1) |
| 69 | + |
| 70 | + # Create docker image. |
| 71 | + call([ |
| 72 | + "docker", |
| 73 | + "build", |
| 74 | + "-t", "swiftpm-docker-1604", |
| 75 | + "--build-arg", "SNAPSHOT=%s" % snapshot_filename, |
| 76 | + docker_dir |
| 77 | + ]) |
| 78 | + |
| 79 | +def run(args): |
| 80 | + """Runs an executable in the container.""" |
| 81 | + docker_run([args.executable] + args.arguments) |
| 82 | + |
| 83 | +def bootstrap(args): |
| 84 | + """Runs the bootstrap script in the container.""" |
| 85 | + docker_run(["Utilities/bootstrap"] + args.arguments) |
| 86 | + |
| 87 | +def build_run(args): |
| 88 | + """Runs a built swift executable in the container.""" |
| 89 | + docker_run([".build/x86_64-unknown-linux/debug/" + args.command] + args.arguments) |
| 90 | + |
| 91 | +def main(): |
| 92 | + """Main script entry-point.""" |
| 93 | + |
| 94 | + parser = argparse.ArgumentParser( |
| 95 | + usage="%(prog)s [build|run|bootstrap|swift-build|swift-test|swift-run]", |
| 96 | + description="This script simplifies all the docker operations to build " |
| 97 | + "and run a container for SwiftPM development and testing " |
| 98 | + "on Linux.") |
| 99 | + subparsers = parser.add_subparsers(dest='command') |
| 100 | + |
| 101 | + # build |
| 102 | + parser_build = subparsers.add_parser( |
| 103 | + "build", |
| 104 | + help="builds a docker image from the latest snapshot.") |
| 105 | + parser_build.set_defaults(func=build) |
| 106 | + |
| 107 | + # run |
| 108 | + parser_run = subparsers.add_parser( |
| 109 | + "run", |
| 110 | + help="runs an executable in a container.") |
| 111 | + parser_run.add_argument("executable", help="the executable to run") |
| 112 | + parser_run.add_argument("arguments", nargs="*") |
| 113 | + parser_run.set_defaults(func=run) |
| 114 | + |
| 115 | + # bootstrap |
| 116 | + parser_bootstrap = subparsers.add_parser( |
| 117 | + "bootstrap", |
| 118 | + help="runs the bootstrap script in a container.") |
| 119 | + parser_bootstrap.add_argument("arguments", nargs="*") |
| 120 | + parser_bootstrap.set_defaults(func=bootstrap) |
| 121 | + |
| 122 | + # swift-build |
| 123 | + parser_swift_build = subparsers.add_parser( |
| 124 | + "swift-build", |
| 125 | + help="runs the swift-build executable in a container.") |
| 126 | + parser_swift_build.add_argument("arguments", nargs="*") |
| 127 | + parser_swift_build.set_defaults(func=build_run) |
| 128 | + |
| 129 | + # swift-test |
| 130 | + parser_swift_test = subparsers.add_parser( |
| 131 | + "swift-test", |
| 132 | + help="runs the swift-test executable in a container.") |
| 133 | + parser_swift_test.add_argument("arguments", nargs="*") |
| 134 | + parser_swift_test.set_defaults(func=build_run) |
| 135 | + |
| 136 | + # swift-run |
| 137 | + parser_swift_run = subparsers.add_parser( |
| 138 | + "swift-run", |
| 139 | + help="runs the swift-run executable in a container.") |
| 140 | + parser_swift_run.add_argument("arguments", nargs="*") |
| 141 | + parser_swift_run.set_defaults(func=build_run) |
| 142 | + |
| 143 | + args = parser.parse_args() |
| 144 | + args.func(args) |
| 145 | + |
| 146 | +if __name__ == "__main__": |
| 147 | + main() |
0 commit comments