Add the Skylark machinery for downloading code to a robot

The Python script it runs currently does basically nothing.

Change-Id: Ic2d5bdccc7a8bcb6ce5b8c6987ec151ccfbce3d2
diff --git a/aos/prime/downloader/BUILD b/aos/prime/downloader/BUILD
new file mode 100644
index 0000000..536a7ad
--- /dev/null
+++ b/aos/prime/downloader/BUILD
@@ -0,0 +1,7 @@
+py_binary(
+  name = 'downloader',
+  visibility = ['//visibility:public'],
+  srcs = [
+    'downloader.py',
+  ],
+)
diff --git a/aos/prime/downloader/downloader.bzl b/aos/prime/downloader/downloader.bzl
new file mode 100644
index 0000000..9a435fc
--- /dev/null
+++ b/aos/prime/downloader/downloader.bzl
@@ -0,0 +1,48 @@
+def _aos_downloader_impl(ctx):
+  ctx.file_action(
+    output = ctx.outputs.executable,
+    executable = True,
+    content = '\n'.join([
+      '#!/bin/bash',
+      'exec %s %s -- %s "$@"' % (ctx.executable._downloader.short_path,
+                                 ' '.join([src.short_path for src in ctx.files.srcs]),
+                                 ctx.attr.default_target),
+    ]),
+  )
+
+  return struct(
+    runfiles = ctx.runfiles(
+      files = ctx.files.srcs + ctx.files._downloader,
+      collect_data = True,
+      collect_default = True,
+    ),
+    files = set([ctx.outputs.executable]),
+  )
+
+'''Creates a binary which downloads code to a robot.
+
+Running this with `bazel run` will actually download everything.
+
+Attrs:
+  srcs: The files to download. They currently all get shoved into one folder.
+  default_target: The default host to download to. If not specified, defaults to
+                  roboRIO-971.local.
+'''
+aos_downloader = rule(
+  implementation = _aos_downloader_impl,
+  attrs = {
+    '_downloader': attr.label(
+      executable = True,
+      cfg = HOST_CFG,
+      default = Label('//aos/prime/downloader'),
+    ),
+    'srcs': attr.label_list(
+      mandatory = True,
+      allow_files = True,
+    ),
+    'default_target': attr.string(
+      default = 'roboRIO-971.local',
+    ),
+  },
+  executable = True,
+)
diff --git a/aos/prime/downloader/downloader.py b/aos/prime/downloader/downloader.py
new file mode 100644
index 0000000..de3f872
--- /dev/null
+++ b/aos/prime/downloader/downloader.py
@@ -0,0 +1,16 @@
+# This file is run by shell scripts generated by the aos_downloader Skylark
+# macro. Everything before the first -- is a hard-coded list of files to
+# download.
+
+import sys
+import subprocess
+
+def main(argv):
+  srcs = argv[:argv.index('--')]
+  args = argv[argv.index('--') + 1:]
+
+  # TODO(Brian): Actually do something useful...
+  subprocess.check_call(['echo', 'scp'] + srcs + ['%s:/tmp' % args[0]])
+
+if __name__ == '__main__':
+  main(sys.argv)
diff --git a/y2014/prime/BUILD b/y2014/prime/BUILD
index 0b7d88a..0690d43 100644
--- a/y2014/prime/BUILD
+++ b/y2014/prime/BUILD
@@ -1,7 +1,9 @@
 package(default_visibility = ['//visibility:public'])
 
-filegroup(
-  name = 'All',
+load('/aos/prime/downloader/downloader', 'aos_downloader')
+
+aos_downloader(
+  name = 'download',
   srcs = [
     '//aos:prime_binaries',
     '//y2014/control_loops/drivetrain:drivetrain',