blob: 9799b1fa6b857804903cd3090b80fab140ac780f [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 Schuhb64799c2015-12-19 22:35:51 -080012def install(ssh_target, pkg):
13 """Installs a package from NI on the ssh target."""
14 print('Installing', pkg)
15 PKG_URL = 'http://download.ni.com/ni-linux-rt/feeds/2015/arm/ipk/cortexa9-vfpv3/' + pkg
16 subprocess.check_call(['wget', PKG_URL, '-O', pkg])
17 try:
18 subprocess.check_call(['scp', pkg, ssh_target + ':/tmp/' + pkg])
19 subprocess.check_call(['ssh', ssh_target, 'opkg', 'install', '/tmp/' + pkg])
20 subprocess.check_call(['ssh', ssh_target, 'rm', '/tmp/' + pkg])
21 finally:
22 subprocess.check_call(['rm', pkg])
23
24
Brian Silvermanaf485d62015-10-12 00:37:14 -040025def main(argv):
Brian Silvermanaf485d62015-10-12 00:37:14 -040026 args = argv[argv.index('--') + 1:]
27
Comran Morshed38967332016-04-23 19:26:48 -070028 relative_dir = ''
29 recursive = False
30
31 if '--dirs' in argv:
32 dirs_index = argv.index('--dirs')
33 srcs = argv[1:dirs_index]
34 relative_dir = argv[dirs_index + 1]
35 recursive = True
36 else:
37 srcs = argv[1:argv.index('--')]
38
Brian Silvermand54068e2015-10-14 17:56:05 -040039 ROBORIO_TARGET_DIR = '/home/admin/robot_code'
40 ROBORIO_USER = 'admin'
41
42 target_dir = ROBORIO_TARGET_DIR
43 user = ROBORIO_USER
44 destination = args[-1]
45
46 result = re.match('(?:([^:@]+)@)?([^:@]+)(?::([^:@]+))?', destination)
47 if not result:
48 print('Not sure how to parse destination "%s"!' % destination,
49 file=sys.stderr)
50 return 1
51 if result.group(1):
52 user = result.group(1)
53 hostname = result.group(2)
54 if result.group(3):
55 target_dir = result.group(3)
56
57 ssh_target = '%s@%s' % (user, hostname)
58
Austin Schuhb64799c2015-12-19 22:35:51 -080059 rsync_cmd = (['rsync', '-c', '-v', '-z', '--copy-links'] + srcs +
Comran Morshed38967332016-04-23 19:26:48 -070060 ['%s:%s/%s' % (ssh_target, target_dir, relative_dir)])
Austin Schuhb64799c2015-12-19 22:35:51 -080061 try:
62 subprocess.check_call(rsync_cmd)
63 except subprocess.CalledProcessError as e:
64 if e.returncode == 127:
65 print('Unconfigured roboRIO, installing rsync.')
66 install(ssh_target, 'libattr1_2.4.47-r0.36_cortexa9-vfpv3.ipk')
67 install(ssh_target, 'libacl1_2.2.52-r0.36_cortexa9-vfpv3.ipk')
68 install(ssh_target, 'rsync_3.1.0-r0.7_cortexa9-vfpv3.ipk')
69 subprocess.check_call(rsync_cmd)
70 else:
71 raise e
72
Comran Morshed38967332016-04-23 19:26:48 -070073 if not recursive:
74 subprocess.check_call(
75 ('ssh', ssh_target, '&&'.join([
76 'chmod u+s %s/starter_exe' % target_dir,
77 'echo \'Done moving new executables into place\'',
78 'bash -c \'sync && sync && sync\'',
79 ])))
Brian Silvermanaf485d62015-10-12 00:37:14 -040080
81if __name__ == '__main__':
82 main(sys.argv)