~ubuntu-branches/ubuntu/quantal/ruby-vmc/quantal-201206142105

« back to all changes in this revision

Viewing changes to lib/vmc/micro.rb

  • Committer: Michael Terry
  • Date: 2012-06-14 17:21:18 UTC
  • mfrom: (5.1.1 ruby-vmc)
  • Revision ID: michael.terry@canonical.com-20120614172118-8m3f2k7ugyki6sir
New upstream release (LP: #998111).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'find'
 
2
 
 
3
module VMC::Micro
 
4
  def config_file(file)
 
5
    File.join(File.dirname(__FILE__), '..', '..', 'config', 'micro', file)
 
6
  end
 
7
 
 
8
  def escape_path(path)
 
9
    path = File.expand_path(path)
 
10
    if RUBY_PLATFORM =~ /mingw|mswin32|cygwin/
 
11
      if path.include?(' ')
 
12
        return '"' + path + '"'
 
13
      else
 
14
        return path
 
15
      end
 
16
    else
 
17
      return path.gsub(' ', '\ ')
 
18
    end
 
19
  end
 
20
 
 
21
  def locate_file(file, directory, search_paths)
 
22
    search_paths.each do |path|
 
23
      expanded_path = File.expand_path(path)
 
24
      if File.exists?(expanded_path)
 
25
        Find.find(expanded_path) do |current|
 
26
          if File.directory?(current) && current.include?(directory)
 
27
            full_path = File.join(current, file)
 
28
            return self.escape_path(full_path) if File.exists?(full_path)
 
29
          end
 
30
        end
 
31
      end
 
32
    end
 
33
 
 
34
    false
 
35
  end
 
36
 
 
37
  def run_command(command, args=nil)
 
38
    # TODO switch to using posix-spawn instead
 
39
    result = %x{#{command} #{args} 2>&1}
 
40
    unless $?.exitstatus == 0
 
41
      if block_given?
 
42
        yield
 
43
      else
 
44
        raise "failed to execute #{command} #{args}:\n#{result}"
 
45
      end
 
46
    else
 
47
      result.split(/\n/)
 
48
    end
 
49
  end
 
50
 
 
51
  module_function :config_file
 
52
  module_function :escape_path
 
53
  module_function :locate_file
 
54
  module_function :run_command
 
55
 
 
56
end