Class: ROS::Package

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

Overview

This is used for adding RUBYLIB path.

Constant Summary

@@all_packages =

all package path hash

self.read_cache_or_find_all

Class Method Summary (collapse)

Class Method Details

+ (Object) add_path_of_package(package)

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

Parameters:

  • package (String)

    name of package



143
144
145
146
147
148
149
150
151
152
# File 'lib/ros/package.rb', line 143

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

+ (Object) add_path_with_depend_packages(package)

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



158
159
160
161
162
163
# File 'lib/ros/package.rb', line 158

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

+ (Array) depends(package, packages = [])

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ros/package.rb', line 95

def self.depends(package, packages=[])
  begin
    if File.exists?("#{@@all_packages[package]}/manifest.xml")
      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
    else
      file = File.open("#{@@all_packages[package]}/package.xml")
      doc = REXML::Document.new(file)
      doc.elements.each('/package/run_depend') do |element|
        depend_package = element.text
        if not packages.include?(depend_package)
          packages.push(depend_package)
          self.depends(depend_package, packages)
        end
      end
    end
  rescue
#        puts "#{package}'s manifest.xml/package.xml not found"
  end
  packages
end

+ (Array) find_all_packages(packages = {}, roots = [])

search all packages that has manifest.xml

Parameters:

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

    current found packages

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

    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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ros/package.rb', line 51

def self.find_all_packages(packages={}, roots=[])
  if roots.empty?
    if ENV['ROS_PACKAGE_PATH']
      roots = ENV['ROS_PACKAGE_PATH'].split(':')
    else
      puts 'Waring: ROS_PACKAGE_PATH is not set'
    end
    if ENV['ROS_ROOT']
      roots.push(ENV['ROS_ROOT'])
    else
      puts 'Waring: ROS_ROOT is not set'
    end
  end
    
  roots.each do |root|
    if File.exists?("#{root}/manifest.xml") or 
        File.exists?("#{root}/package.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

+ (String) find_this_package

get the current program's package

Returns:

  • (String)

    name of running programs’s package



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

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

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

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