blob: 7508c4ca39ed77593b654f4ee4b8a2de23ad0b2a [file] [log] [blame]
#!/usr/bin/env python
This source file is part of the open source project
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See for license information
See for Swift project authors
This script contains helper operations to use Docker for SwitPM development
and testing.
import argparse
import urllib2
import os
import subprocess
import yaml
def call(args):
"""Prints and executes a command."""
print " ".join(args)
def docker_run(command_args):
"""Runs a command in the container."""
"--security-opt", "seccomp=unconfined",
"-v", os.getcwd() + ":/swiftpm",
"-w", "/swiftpm",
] + command_args)
def build(_):
"""Builds a docker image with the latest snapshot."""
# Get latest snapshot info.
latest_build_url = ""
latest_build_data = yaml.load(urllib2.urlopen(latest_build_url).read())
snapshot_filename = latest_build_data["download"]
# FIXME: We shouldn"t need to do this, it should be available in the API.
snapshot_name = snapshot_filename.replace("-ubuntu16.04.tar.gz", "")
base_url = latest_build_url.rsplit("/", 1)[0]
latest_snapshot_url = base_url + "/" + snapshot_name + "/" + snapshot_filename
docker_dir = os.path.dirname(os.path.realpath(__file__)) + "/Docker"
# Download latest snapshot (if necessary).
if snapshot_filename in os.listdir(docker_dir):
print "Not downloading: " + latest_snapshot_url
# FIXME: We should remove old tarballs if we have newer ones.
result = call([
docker_dir + "/" + snapshot_filename,
if result != 0:
print "Unable to install package: " + latest_snapshot_url
# Create docker image.
"-t", "swiftpm-docker-1604",
"--build-arg", "SNAPSHOT=%s" % snapshot_filename,
def run(args):
"""Runs an executable in the container."""
docker_run([args.executable] + args.arguments)
def bootstrap(args):
"""Runs the bootstrap script in the container."""
docker_run(["Utilities/bootstrap"] + args.arguments)
def build_run(args):
"""Runs a built swift executable in the container."""
docker_run([".build/x86_64-unknown-linux/debug/" + args.command] + args.arguments)
def main():
"""Main script entry-point."""
parser = argparse.ArgumentParser(
usage="%(prog)s [build|run|bootstrap|swift-build|swift-test|swift-run]",
description="This script simplifies all the docker operations to build "
"and run a container for SwiftPM development and testing "
"on Linux.")
subparsers = parser.add_subparsers(dest='command')
# build
parser_build = subparsers.add_parser(
help="builds a docker image from the latest snapshot.")
# run
parser_run = subparsers.add_parser(
help="runs an executable in a container.")
parser_run.add_argument("executable", help="the executable to run")
parser_run.add_argument("arguments", nargs="*")
# bootstrap
parser_bootstrap = subparsers.add_parser(
help="runs the bootstrap script in a container.")
parser_bootstrap.add_argument("arguments", nargs="*")
# swift-build
parser_swift_build = subparsers.add_parser(
help="runs the swift-build executable in a container.")
parser_swift_build.add_argument("arguments", nargs="*")
# swift-test
parser_swift_test = subparsers.add_parser(
help="runs the swift-test executable in a container.")
parser_swift_test.add_argument("arguments", nargs="*")
# swift-run
parser_swift_run = subparsers.add_parser(
help="runs the swift-run executable in a container.")
parser_swift_run.add_argument("arguments", nargs="*")
args = parser.parse_args()
if __name__ == "__main__":