Exemplo n.º 1
0
 def test_package_name(self):
     pkg = package_name(self.nm_path)
     self.assertEqual(
         'fkie_node_manager_daemon', pkg[0],
         "wrong package name, expected: %s, got: %s" %
         ('fkie_node_manager_daemon', pkg[0]))
     pkg_dir = self.nm_path
     self.assertEqual(
         pkg_dir, pkg[1],
         "wrong package path, expected: %s, got: %s" % (pkg_dir, pkg[1]))
     # test cache
     pkg = package_name(self.nm_path)
     self.assertEqual(
         'fkie_node_manager_daemon', pkg[0],
         "wrong package name from cache, expected: %s, got: %s" %
         ('fkie_node_manager_daemon', pkg[0]))
     self.assertEqual(
         pkg_dir, pkg[1],
         "wrong package path from cache, expected: %s, got: %s" %
         (pkg_dir, pkg[1]))
     # test invalid path
     pkg = package_name('INVALID')
     self.assertEqual(
         None, pkg[0],
         "wrong package name, expected: %s, got: %s" % (None, pkg[0]))
     self.assertEqual(
         None, pkg[1],
         "wrong package name, expected: %s, got: %s" % (None, pkg[1]))
    def _bc_resolve_abs_paths(cls, value, host, auto_pw_request, user, pw):
        '''
        Replaces the local absolute path by remote absolute path. Only valid ROS
        package paths are resolved.

        :return: value, is absolute path, remote package found (ignore it on local host or if is not absolute path!), package name (if absolute path and remote package NOT found)
        '''
        if isstring(value) and value.startswith('/') and (os.path.isfile(value) or os.path.isdir(value)):
            if nm.is_local(host):
                return value, True, True, ''
            else:
                path = os.path.dirname(value) if os.path.isfile(value) else value
                package, package_path = package_name(path)
                if package:
                    _, stdout, _, ok = nm.ssh().ssh_exec(host, ['rospack', 'find', package], user, pw, auto_pw_request, close_stdin=True, close_stderr=True)
                    output = stdout.read()
                    stdout.close()
                    if ok:
                        if output:
                            value.replace(package_path, output)
                            return value.replace(package_path, output.strip()), True, True, package
                        else:
                            # package on remote host not found!
                            # TODO add error message
                            #      error = stderr.read()
                            pass
                return value, True, False, ''
        else:
            return value, False, False, ''
 def transfer_files(cls, host, path, auto_pw_request=False, user=None, pw=None):
     '''
     Copies the given file to the remote host. Uses caching of remote paths.
     '''
     # get package of the file
     if nm.is_local(host):
         # it's local -> no copy needed
         return
     (pkg_name, pkg_path) = package_name(os.path.dirname(path))
     if pkg_name is not None:
         # get the subpath of the file
         subfile_path = path.replace(pkg_path, '')
         # get the path of the package on the remote machine
         try:
             output = ''
             error = ''
             ok = True
             if host in CACHED_PKG_PATH and pkg_name in CACHED_PKG_PATH[host]:
                 output = CACHED_PKG_PATH[host][pkg_name]
             else:
                 if host not in CACHED_PKG_PATH:
                     CACHED_PKG_PATH[host] = dict()
                 _, stdout, stderr, ok = nm.ssh().ssh_exec(host, [nm.settings().start_remote_script, '--package', pkg_name], user, pw, auto_pw_request, close_stdin=True)
                 output = stdout.read()
                 error = stderr.read()
                 stdout.close()
                 stderr.close()
             if ok:
                 if error:
                     rospy.logwarn("ERROR while transfer %s to %s: %s", path, host, error)
                     raise StartException(utf8(''.join(['The host "', host, '" reports:\n', error])))
                 if output:
                     CACHED_PKG_PATH[host][pkg_name] = output
                     nm.ssh().transfer(host, path, os.path.join(output.strip(), subfile_path.strip(os.sep)), user)
                 else:
                     raise StartException("Remote host no returned any answer. Is there the new version of node_manager installed?")
             else:
                 raise StartException("Can't get path from remote host. Is there the new version of node_manager installed?")
         except nm.AuthenticationRequest as e:
             raise nm.InteractionNeededError(e, cls.transfer_files, {'host': host, 'path': path, 'auto_pw_request': auto_pw_request, 'user': user, 'pw': pw})