Enable deploying code for the raspberry pis

Also, sets up a systemd service to start all the code.

Change-Id: If993426b36d0910497579ad86e330699f8a7d2e7
diff --git a/frc971/downloader.bzl b/frc971/downloader.bzl
index aff3420..11423e0 100644
--- a/frc971/downloader.bzl
+++ b/frc971/downloader.bzl
@@ -1,7 +1,15 @@
 load("//frc971/downloader:downloader.bzl", "aos_downloader")
 load("//tools/build_rules:label.bzl", "expand_label")
 
-def robot_downloader(start_binaries, binaries = [], data = [], dirs = None, default_target = None):
+
+def robot_downloader(start_binaries,
+                     name="download",
+                     binaries=[],
+                     data=[],
+                     dirs=None,
+                     default_target=None,
+                     restricted_to=["//tools:roborio"],
+                     target_type="roborio"):
     """Sets up the standard robot download targets.
 
     Attrs:
@@ -11,27 +19,30 @@
     """
 
     aos_downloader(
-        name = "download",
-        start_srcs = [
+        name=name,
+        start_srcs=([
             "//aos:prime_start_binaries",
-        ] + start_binaries,
-        srcs = [
+        ] if target_type == "roborio" else []) + start_binaries,
+        srcs=[
             "//aos:prime_binaries",
         ] + binaries + data,
-        dirs = dirs,
-        default_target = default_target,
-        restricted_to = ["//tools:roborio"],
+        dirs=dirs,
+        target_type=target_type,
+        default_target=default_target,
+        restricted_to=restricted_to,
     )
 
     aos_downloader(
-        name = "download_stripped",
-        start_srcs = [
+        name=name + "_stripped",
+        start_srcs=([
             "//aos:prime_start_binaries_stripped",
-        ] + [expand_label(binary) + ".stripped" for binary in start_binaries],
-        srcs = [
+        ] if target_type == "roborio" else []) +
+        [expand_label(binary) + ".stripped" for binary in start_binaries],
+        srcs=[
             "//aos:prime_binaries_stripped",
         ] + [expand_label(binary) + ".stripped" for binary in binaries] + data,
-        dirs = dirs,
-        default_target = default_target,
-        restricted_to = ["//tools:roborio"],
+        dirs=dirs,
+        target_type=target_type,
+        default_target=default_target,
+        restricted_to=restricted_to,
     )
diff --git a/frc971/downloader/downloader.bzl b/frc971/downloader/downloader.bzl
index a5d1bc1..75a1a9b 100644
--- a/frc971/downloader/downloader.bzl
+++ b/frc971/downloader/downloader.bzl
@@ -7,16 +7,16 @@
             "#!/bin/bash",
             "set -e",
             'cd "${BASH_SOURCE[0]}.runfiles/%s"' % ctx.workspace_name,
-        ] + ['%s %s --dirs %s -- %s "$@"' % (
+        ] + ['%s --dir %s --target "$@" --type %s %s' % (
             ctx.executable._downloader.short_path,
-            " ".join([src.short_path for src in d.downloader_srcs]),
             d.downloader_dir,
-            ctx.attr.default_target,
+            ctx.attr.target_type,
+            " ".join([src.short_path for src in d.downloader_srcs]),
         ) for d in ctx.attr.dirs] + [
-            'exec %s %s -- %s "$@"' % (
+            'exec %s --target "$@" --type %s %s' % (
                 ctx.executable._downloader.short_path,
+                ctx.attr.target_type,
                 " ".join([src.short_path for src in all_files]),
-                ctx.attr.default_target,
             ),
         ]),
     )
@@ -57,8 +57,6 @@
   srcs: The files to download. They currently all get shoved into one folder.
   dirs: A list of aos_downloader_dirs to download too.
   start_srcs: Like srcs, except they also get put into start_list.txt.
-  default_target: The default host to download to. If not specified, defaults to
-                  roboRIO-971.local.
 """
 
 aos_downloader = rule(
@@ -76,6 +74,9 @@
             mandatory = True,
             allow_files = True,
         ),
+        "target_type": attr.string(
+            default = "roborio",
+        ),
         "dirs": attr.label_list(
             mandatory = False,
             providers = [
@@ -83,9 +84,6 @@
                 "downloader_srcs",
             ],
         ),
-        "default_target": attr.string(
-            default = "roboRIO-971-frc.local",
-        ),
     },
     executable = True,
     outputs = {
diff --git a/frc971/downloader/downloader.py b/frc971/downloader/downloader.py
index e4826cf..5ebf417 100644
--- a/frc971/downloader/downloader.py
+++ b/frc971/downloader/downloader.py
@@ -4,6 +4,7 @@
 
 from __future__ import print_function
 
+import argparse
 import sys
 import subprocess
 import re
@@ -16,61 +17,74 @@
     PKG_URL = "http://download.ni.com/ni-linux-rt/feeds/2015/arm/ipk/cortexa9-vfpv3/" + pkg
     subprocess.check_call(["wget", PKG_URL, "-O", pkg])
     try:
-        subprocess.check_call([
-            scp_path, "-S", ssh_path, pkg,
-            ssh_target + ":/tmp/" + pkg
-        ])
-        subprocess.check_call([
-            ssh_path, ssh_target, "opkg", "install",
-            "/tmp/" + pkg
-        ])
         subprocess.check_call(
-            [ssh_path, ssh_target, "rm", "/tmp/" + pkg])
+            [scp_path, "-S", ssh_path, pkg, ssh_target + ":/tmp/" + pkg])
+        subprocess.check_call(
+            [ssh_path, ssh_target, "opkg", "install", "/tmp/" + pkg])
+        subprocess.check_call([ssh_path, ssh_target, "rm", "/tmp/" + pkg])
     finally:
         subprocess.check_call(["rm", pkg])
 
 
 def main(argv):
-    args = argv[argv.index("--") + 1:]
+    parser = argparse.ArgumentParser()
+    parser.add_argument("--target",
+                        type=str,
+                        default="roborio-971-frc.local",
+                        help="Target to deploy code to.")
+    parser.add_argument("--type",
+                        type=str,
+                        choices=["roborio", "pi"],
+                        required=True,
+                        help="Target type for deployment")
+    parser.add_argument(
+        "--dir",
+        type=str,
+        help="Directory within robot_code to copy the files to.")
+    parser.add_argument("srcs",
+                        type=str,
+                        nargs='+',
+                        help="List of files to copy over")
+    args = parser.parse_args(argv[1:])
 
     relative_dir = ""
     recursive = False
 
-    if "--dirs" in argv:
-        dirs_index = argv.index("--dirs")
-        srcs = argv[1:dirs_index]
-        relative_dir = argv[dirs_index + 1]
+    srcs = args.srcs
+    if args.dir is not None:
+        relative_dir = args.dir
         recursive = True
-    else:
-        srcs = argv[1:argv.index("--")]
 
-    ROBORIO_TARGET_DIR = "/home/admin/robot_code"
-    ROBORIO_USER = "admin"
-
-    target_dir = ROBORIO_TARGET_DIR
-    user = ROBORIO_USER
-    destination = args[-1]
+    destination = args.target
 
     result = re.match("(?:([^:@]+)@)?([^:@]+)(?::([^:@]+))?", destination)
     if not result:
-        print(
-            "Not sure how to parse destination \"%s\"!" % destination,
-            file=sys.stderr)
+        print("Not sure how to parse destination \"%s\"!" % destination,
+              file=sys.stderr)
         return 1
+    user = None
     if result.group(1):
         user = result.group(1)
     hostname = result.group(2)
+
     if result.group(3):
         target_dir = result.group(3)
 
+    if user is None:
+        if args.type == "pi":
+          user = "pi"
+        elif args.type == "roborio":
+          user = "admin"
+    target_dir = "/home/" + user + "/robot_code"
+
     ssh_target = "%s@%s" % (user, hostname)
 
     ssh_path = "external/ssh/ssh"
     scp_path = "external/ssh/scp"
 
     rsync_cmd = ([
-        "external/rsync/usr/bin/rsync", "-e", ssh_path, "-c",
-        "-v", "-z", "--copy-links"
+        "external/rsync/usr/bin/rsync", "-e", ssh_path, "-c", "-v", "-z",
+        "--copy-links"
     ] + srcs + ["%s:%s/%s" % (ssh_target, target_dir, relative_dir)])
     try:
         subprocess.check_call(rsync_cmd)
@@ -88,12 +102,11 @@
             raise e
 
     if not recursive:
-        subprocess.check_call(
-            (ssh_path, ssh_target, "&&".join([
-                "chmod u+s %s/starter_exe" % target_dir,
-                "echo \'Done moving new executables into place\'",
-                "bash -c \'sync && sync && sync\'",
-            ])))
+        subprocess.check_call((ssh_path, ssh_target, "&&".join([
+            "chmod u+s %s/starter_exe" % target_dir,
+            "echo \'Done moving new executables into place\'",
+            "bash -c \'sync && sync && sync\'",
+        ])))
 
 
 if __name__ == "__main__":