Go to the documentation of this file.00001 """
00002 This file contains utilities for other commands to use.
00003
00004 Copyright 2015 Fetch Robotics Inc.
00005 Author: Alex Henning
00006 """
00007
00008 from argcomplete.completers import ChoicesCompleter
00009 import subprocess
00010
00011
00012 def ssh(user, host, command, password=None, fname=None):
00013 "Run the command on the remote host as the given user."
00014
00015 userhost = user + "@" + host
00016 ssh_command = ["ssh", "-t", userhost, command]
00017
00018 e_vars = None
00019 if password:
00020 ssh_command = ["sshpass", "-e"] + ssh_command
00021 e_vars = {"SSHPASS": password}
00022
00023 pipe = open(fname + ".txt", 'w') if fname else None
00024
00025 proc = subprocess.Popen(ssh_command, env=e_vars, stdout=pipe, stderr=pipe)
00026 proc.wait()
00027 if fname:
00028 pipe.close()
00029 return proc.returncode
00030
00031
00032 def run(command):
00033 proc = subprocess.Popen(["bash", "-c", command])
00034 proc.wait()
00035 return proc.returncode
00036
00037
00038
00039 def RobotCompleter(prefix, **kwargs):
00040 options = []
00041 if "fetch".startswith(prefix) and prefix != "fetch":
00042 options.extend("fetch" + str(i) for i in range(10))
00043 if "freight".startswith(prefix) and prefix != "freight":
00044 options.extend("freight" + str(i) for i in range(10))
00045 if options:
00046 return options
00047 return (prefix + str(i) for i in range(10))
00048
00049 users = subprocess.check_output(["awk", "-F:", "{ print $1}", "/etc/passwd"]) \
00050 .split()
00051
00052
00053 def add_user(parser):
00054 arg = parser.add_argument("--user", action="store",
00055 help="User account to use on robot")
00056 arg.completer = ChoicesCompleter(users)
00057
00058
00059 def add_robot(parser):
00060 arg = parser.add_argument("--robot", action="store", help="Robot to use")
00061 arg.completer = RobotCompleter
00062
00063
00064 def add_workspace(parser):
00065 parser.add_argument("--workspace", action="store",
00066 help="Catkin workspace to use")
00067 parser.add_argument("--remote-workspace", action="store",
00068 help="Catkin workspace to use on the robot")