File: utils.rb

package info (click to toggle)
ruby-graphviz 1.2.5-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,220 kB
  • sloc: ruby: 7,685; xml: 26; makefile: 17
file content (67 lines) | stat: -rw-r--r-- 2,514 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require 'rbconfig'

class GraphViz
  module Utils
    # Since this code is an adaptation of Launchy::Application#find_executable
    # (http://copiousfreetime.rubyforge.org/launchy/Launchy/Application.html)
    # it follow is licence :
    #
    # Permission to use, copy, modify, and/or distribute this software for any
    # purpose with or without fee is hereby granted, provided that the above
    # copyright notice and this permission notice appear in all copies.
    #
    # THE SOFTWARE IS PROVIDED AS IS AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
    # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
    # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
    # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    def find_executable(bin, custom_paths) #:nodoc:
      system_path = ENV['PATH']
      user_given_path = Array(custom_paths).join(File::PATH_SEPARATOR)
      search_path = system_path + File::PATH_SEPARATOR + user_given_path

      search_path.split(File::PATH_SEPARATOR).each do |path|
        file_path = File.join(path,bin)
        return file_path if File.executable?(file_path) and File.file?(file_path)

        if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ # WAS: elsif RUBY_PLATFORM =~ /mswin|mingw/
          found_ext = (ENV['PATHEXT'] || '.exe;.bat;.com').split(";").find {|ext| File.executable?(file_path + ext) }
          return file_path + found_ext if found_ext
        end
      end
      return nil
    end

    def output_and_errors_from_command(cmd) #:nodoc:
      unless defined? Open3
        begin
          require 'open3'
          require 'win32/open3'
        rescue LoadError
        end
      end
      begin
        out, err, status = Open3.capture3(*cmd, :binmode => true)
        [out, err, status.exitstatus]
      rescue NotImplementedError, NoMethodError
        IO.popen( *cmd ) do |stdout|
          stdout.binmode
          [stdout.read, nil, nil]
        end
      end
    end

    def output_from_command(cmd) #:nodoc:
      output, errors, status = output_and_errors_from_command(cmd)
      if (status.nil? && (errors.nil? || errors.strip.empty?)) || status.zero?
        output
      else
        raise "Error from #{cmd}:\n#{errors}"
      end
    end

  end
end