blob: e4826cf945046c962e221a22f5aa39726503feb2 [file] [log] [blame]
Brian Silvermanaf485d62015-10-12 00:37:14 -04001# This file is run by shell scripts generated by the aos_downloader Skylark
2# macro. Everything before the first -- is a hard-coded list of files to
3# download.
4
Brian Silvermand54068e2015-10-14 17:56:05 -04005from __future__ import print_function
6
Brian Silvermanaf485d62015-10-12 00:37:14 -04007import sys
8import subprocess
Brian Silvermand54068e2015-10-14 17:56:05 -04009import re
10import os
Brian Silvermanaf485d62015-10-12 00:37:14 -040011
Austin Schuh71f6fa72019-08-31 18:23:02 -070012
Brian Silvermanbd7860e2020-01-05 17:52:40 -080013def install(ssh_target, pkg, ssh_path, scp_path):
Austin Schuh71f6fa72019-08-31 18:23:02 -070014 """Installs a package from NI on the ssh target."""
15 print("Installing", pkg)
16 PKG_URL = "http://download.ni.com/ni-linux-rt/feeds/2015/arm/ipk/cortexa9-vfpv3/" + pkg
17 subprocess.check_call(["wget", PKG_URL, "-O", pkg])
18 try:
19 subprocess.check_call([
Brian Silvermanbd7860e2020-01-05 17:52:40 -080020 scp_path, "-S", ssh_path, pkg,
Austin Schuh71f6fa72019-08-31 18:23:02 -070021 ssh_target + ":/tmp/" + pkg
22 ])
23 subprocess.check_call([
Brian Silvermanbd7860e2020-01-05 17:52:40 -080024 ssh_path, ssh_target, "opkg", "install",
Austin Schuh71f6fa72019-08-31 18:23:02 -070025 "/tmp/" + pkg
26 ])
27 subprocess.check_call(
Brian Silvermanbd7860e2020-01-05 17:52:40 -080028 [ssh_path, ssh_target, "rm", "/tmp/" + pkg])
Austin Schuh71f6fa72019-08-31 18:23:02 -070029 finally:
30 subprocess.check_call(["rm", pkg])
Austin Schuhb64799c2015-12-19 22:35:51 -080031
32
Brian Silvermanaf485d62015-10-12 00:37:14 -040033def main(argv):
Austin Schuh71f6fa72019-08-31 18:23:02 -070034 args = argv[argv.index("--") + 1:]
Brian Silvermanaf485d62015-10-12 00:37:14 -040035
Austin Schuh71f6fa72019-08-31 18:23:02 -070036 relative_dir = ""
37 recursive = False
Comran Morshed38967332016-04-23 19:26:48 -070038
Austin Schuh71f6fa72019-08-31 18:23:02 -070039 if "--dirs" in argv:
40 dirs_index = argv.index("--dirs")
41 srcs = argv[1:dirs_index]
42 relative_dir = argv[dirs_index + 1]
43 recursive = True
Austin Schuhb64799c2015-12-19 22:35:51 -080044 else:
Austin Schuh71f6fa72019-08-31 18:23:02 -070045 srcs = argv[1:argv.index("--")]
Austin Schuhb64799c2015-12-19 22:35:51 -080046
Austin Schuh71f6fa72019-08-31 18:23:02 -070047 ROBORIO_TARGET_DIR = "/home/admin/robot_code"
48 ROBORIO_USER = "admin"
Brian Silvermanaf485d62015-10-12 00:37:14 -040049
Austin Schuh71f6fa72019-08-31 18:23:02 -070050 target_dir = ROBORIO_TARGET_DIR
51 user = ROBORIO_USER
52 destination = args[-1]
53
54 result = re.match("(?:([^:@]+)@)?([^:@]+)(?::([^:@]+))?", destination)
55 if not result:
56 print(
57 "Not sure how to parse destination \"%s\"!" % destination,
58 file=sys.stderr)
59 return 1
60 if result.group(1):
61 user = result.group(1)
62 hostname = result.group(2)
63 if result.group(3):
64 target_dir = result.group(3)
65
66 ssh_target = "%s@%s" % (user, hostname)
67
Brian Silvermanbd7860e2020-01-05 17:52:40 -080068 ssh_path = "external/ssh/ssh"
69 scp_path = "external/ssh/scp"
70
Austin Schuh71f6fa72019-08-31 18:23:02 -070071 rsync_cmd = ([
Brian Silvermanbd7860e2020-01-05 17:52:40 -080072 "external/rsync/usr/bin/rsync", "-e", ssh_path, "-c",
Austin Schuh71f6fa72019-08-31 18:23:02 -070073 "-v", "-z", "--copy-links"
74 ] + srcs + ["%s:%s/%s" % (ssh_target, target_dir, relative_dir)])
75 try:
76 subprocess.check_call(rsync_cmd)
77 except subprocess.CalledProcessError as e:
Brian Silvermanbd7860e2020-01-05 17:52:40 -080078 if e.returncode == 127 or e.returncode == 12:
Austin Schuh71f6fa72019-08-31 18:23:02 -070079 print("Unconfigured roboRIO, installing rsync.")
Brian Silvermanbd7860e2020-01-05 17:52:40 -080080 install(ssh_target, "libattr1_2.4.47-r0.36_cortexa9-vfpv3.ipk",
81 ssh_path, scp_path)
82 install(ssh_target, "libacl1_2.2.52-r0.36_cortexa9-vfpv3.ipk",
83 ssh_path, scp_path)
84 install(ssh_target, "rsync_3.1.0-r0.7_cortexa9-vfpv3.ipk",
85 ssh_path, scp_path)
Austin Schuh71f6fa72019-08-31 18:23:02 -070086 subprocess.check_call(rsync_cmd)
87 else:
88 raise e
89
90 if not recursive:
91 subprocess.check_call(
Brian Silvermanbd7860e2020-01-05 17:52:40 -080092 (ssh_path, ssh_target, "&&".join([
Austin Schuh71f6fa72019-08-31 18:23:02 -070093 "chmod u+s %s/starter_exe" % target_dir,
94 "echo \'Done moving new executables into place\'",
95 "bash -c \'sync && sync && sync\'",
96 ])))
97
98
99if __name__ == "__main__":
100 main(sys.argv)