Class: ROS::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/ros/package.rb

Overview

This is used for adding RUBYLIB path.

Constant Summary collapse

@@all_packages =

all package path hash

self.read_cache_or_find_all

Class Method Summary collapse

Class Method Details

.add_path_of_package(package) ⇒ Object

add package’s [lib/, msg_gen/ruby, srv_gen/ruby] to ‘$:’. this enables load ruby files easily

Parameters:

  • package (String)

    name of package



114
115
116
117
118
119
120
121
122
123
# File 'lib/ros/package.rb', line 114

def self.add_path_of_package(package)
  path = @@all_packages[package]
  ["#{path}/msg_gen/ruby", "#{path}/srv_gen/ruby", "#{path}/lib"].each do |path|
    if File.exists?(path)
      if not $:.include?(path)
        $:.push(path)
      end
    end
  end
end

.add_path_with_depend_packages(package) ⇒ Object

add [lib/, msg_gen/ruby, srv_gen/ruby] dirs of all depend packages to RUBYLIB, if the directory exists

Parameters:

  • package (String)

    name of package



129
130
131
132
133
134
# File 'lib/ros/package.rb', line 129

def self.add_path_with_depend_packages(package)
  add_path_of_package(package)
  Package::depends(package).each do |pack|
    add_path_of_package(pack)
  end
end

.depends(package, packages = []) ⇒ Array

get the depend packages of the arg

Parameters:

  • package (String)

    find depends packages of this package

  • packages (Array) (defaults to: [])

    current found depends

Returns:

  • (Array)

    packages



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ros/package.rb', line 82

def self.depends(package, packages=[])
  file = File.open("#{@@all_packages[package]}/manifest.xml")
  doc = REXML::Document.new(file)
  doc.elements.each('/package/depend') do |element|
    depend_package = element.attributes['package']
    if not packages.include?(depend_package)
      packages.push(depend_package)
      self.depends(depend_package, packages)
    end
  end
  packages
end

.find_all_packages(packages = {}, roots = ENV['ROS_PACKAGE_PATH'].split(':').push(ENV['ROS_ROOT'])) ⇒ Array

search all packages that has manifest.xml

Parameters:

  • packages (Hash) (defaults to: {})

    current found packages

  • roots (Array) (defaults to: ENV['ROS_PACKAGE_PATH'].split(':').push(ENV['ROS_ROOT']))

    root directories for searching

Returns:

  • (Array)

    fullpath list of all packages



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ros/package.rb', line 51

def self.find_all_packages(packages={}, roots=ENV['ROS_PACKAGE_PATH'].split(':').push(ENV['ROS_ROOT']))
  roots.each do |root|
    if File.exists?("#{root}/manifest.xml")
      packages[File.basename(root)] = root
    else
      if File.exists?(root)
        Dir.foreach(root) do |path|
          if path != "." and path != ".."
            full_path = "#{root}/#{path}"
            if File.directory?(full_path)
              self.find_all_packages(packages, [full_path])
            end
          end
        end
      end
    end
  end
  packages
end

.find_this_packageString

get the current program’s package

Returns:

  • (String)

    name of running programs’s package



99
100
101
102
103
104
105
106
107
108
# File 'lib/ros/package.rb', line 99

def self.find_this_package
  path = File::dirname(File.expand_path($PROGRAM_NAME))
  while path != '/'
    if File.exists?("#{path}/manifest.xml")
      return File::basename(path)
    end
    path = File::dirname(path)
  end
  nil
end

.read_cache_or_find_all(cache_file = "#{ENV['HOME']}/.ros/rospack_cache") ⇒ Array

at first check the rospack’s cache, if found use it. if not found, check all package path.

Parameters:

  • cache_file (String) (defaults to: "#{ENV['HOME']}/.ros/rospack_cache")

    cache file of rospack

Returns:

  • (Array)

    fullpath list of all packages



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ros/package.rb', line 26

def self.read_cache_or_find_all(cache_file="#{ENV['HOME']}/.ros/rospack_cache")
  if File.exists?(cache_file)
    f = File.open(cache_file)
    root_line = f.gets.chop
    package_path_line = f.gets.chop
    if root_line == "#ROS_ROOT=#{ENV['ROS_ROOT']}" and
        package_path_line == "#ROS_PACKAGE_PATH=#{ENV['ROS_PACKAGE_PATH']}"
      packages = {}
      while line = f.gets
        packages[File.basename(line.chop)] = line.chop
      end
      packages
    else
      self.find_all_packages
    end
  else
    self.find_all_packages
  end
end