blob: 0d15b8779cceba8733850a094ed78e22fa7f8718 [file] [log] [blame]
Austin Schuh085eab92020-11-26 13:54:51 -08001#!/usr/bin/python3
Parker Schuh5dbce222017-03-22 20:52:16 -07002
3# This file is run by shell scripts generated by the aos_vision_downloader Skylark
4# macro. Everything before the first -- is a hard-coded list of files to
5# download.
6
7from __future__ import print_function
8
9import sys
10import subprocess
11import re
12import os
13import os.path
14
Ravago Jones5127ccc2022-07-31 16:32:45 -070015
Parker Schuh5dbce222017-03-22 20:52:16 -070016def RunAndSplitShas(shcmd):
Ravago Jones5127ccc2022-07-31 16:32:45 -070017 out = subprocess.check_output(shcmd)
18 return [
19 line.split(' ')[0] for line in filter(lambda x: x, out.split('\n'))
20 ]
21
Parker Schuh5dbce222017-03-22 20:52:16 -070022
23def GetChecksums(fnames):
Ravago Jones5127ccc2022-07-31 16:32:45 -070024 return RunAndSplitShas(["sha256sum"] + fnames)
25
Parker Schuh5dbce222017-03-22 20:52:16 -070026
27def GetJetsonChecksums(ssh_target, fnames):
Ravago Jones5127ccc2022-07-31 16:32:45 -070028 target_files = ["/root/%s" % fname for fname in fnames]
29 subprocess.check_call(
30 ["ssh", ssh_target, "touch " + " ".join(target_files)])
31 cmds = ["ssh", ssh_target, "sha256sum " + " ".join(target_files)]
32 return RunAndSplitShas(cmds)
33
Parker Schuh5dbce222017-03-22 20:52:16 -070034
35def ToJetsonFname(fname):
Ravago Jones5127ccc2022-07-31 16:32:45 -070036 if (fname[-9:] == ".stripped"):
37 fname = fname[:-9]
38 return os.path.basename(fname)
39
Parker Schuh5dbce222017-03-22 20:52:16 -070040
41def VerifyCheckSumsAndUpload(fnames, ssh_target):
Ravago Jones5127ccc2022-07-31 16:32:45 -070042 jetson_fnames = [ToJetsonFname(fname) for fname in fnames]
43 checksums = GetChecksums(fnames)
44 jetson_checksums = GetJetsonChecksums(ssh_target, jetson_fnames)
45 for i in range(len(fnames)):
46 if (checksums[i] != jetson_checksums[i]):
47 # if empty, unlink
48 subprocess.check_call(
49 ["ssh", ssh_target, "unlink " + jetson_fnames[i]])
50 subprocess.check_call(
51 ["scp", fnames[i], ssh_target + ":" + jetson_fnames[i]])
52
Parker Schuh5dbce222017-03-22 20:52:16 -070053
54def main(argv):
Ravago Jones5127ccc2022-07-31 16:32:45 -070055 args = argv[argv.index('--') + 1:]
56 files = argv[1:argv.index('--')]
Parker Schuh5dbce222017-03-22 20:52:16 -070057
Ravago Jones5127ccc2022-07-31 16:32:45 -070058 VerifyCheckSumsAndUpload(files, args[-1])
59
Parker Schuh5dbce222017-03-22 20:52:16 -070060
61if __name__ == '__main__':
Ravago Jones5127ccc2022-07-31 16:32:45 -070062 main(sys.argv)