Parker Schuh | 5dbce22 | 2017-03-22 20:52:16 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 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 | |
| 7 | from __future__ import print_function |
| 8 | |
| 9 | import sys |
| 10 | import subprocess |
| 11 | import re |
| 12 | import os |
| 13 | import os.path |
| 14 | |
| 15 | def RunAndSplitShas(shcmd): |
| 16 | out = subprocess.check_output(shcmd) |
| 17 | return [line.split(' ')[0] for line in filter(lambda x: x, out.split('\n'))] |
| 18 | |
| 19 | def GetChecksums(fnames): |
| 20 | return RunAndSplitShas(["sha256sum"] + fnames) |
| 21 | |
| 22 | def GetJetsonChecksums(ssh_target, fnames): |
| 23 | target_files = ["/root/%s" % fname for fname in fnames] |
| 24 | subprocess.check_call(["ssh", ssh_target, "touch " + " ".join(target_files)]) |
| 25 | cmds = ["ssh", ssh_target, "sha256sum " + " ".join(target_files)] |
| 26 | return RunAndSplitShas(cmds) |
| 27 | |
| 28 | def ToJetsonFname(fname): |
| 29 | if (fname[-9:] == ".stripped"): |
| 30 | fname = fname[:-9] |
| 31 | return os.path.basename(fname) |
| 32 | |
| 33 | def VerifyCheckSumsAndUpload(fnames, ssh_target): |
| 34 | jetson_fnames = [ToJetsonFname(fname) for fname in fnames] |
| 35 | checksums = GetChecksums(fnames) |
| 36 | jetson_checksums = GetJetsonChecksums(ssh_target, jetson_fnames) |
| 37 | for i in xrange(len(fnames)): |
| 38 | if (checksums[i] != jetson_checksums[i]): |
| 39 | # if empty, unlink |
| 40 | subprocess.check_call(["ssh", ssh_target, "unlink " + jetson_fnames[i]]) |
| 41 | subprocess.check_call(["scp", fnames[i], ssh_target + ":" + jetson_fnames[i]]) |
| 42 | |
| 43 | def main(argv): |
| 44 | args = argv[argv.index('--') + 1:] |
| 45 | files = argv[1:argv.index('--')] |
| 46 | |
| 47 | VerifyCheckSumsAndUpload(files, args[-1]) |
| 48 | |
| 49 | if __name__ == '__main__': |
| 50 | main(sys.argv) |