public
Rubygem
Description: Piston is a utility that eases vendor branch management. This repository is a complete reimplementation of Piston to provide different backends, depending on the repositories and working copies you pistonize from.
Homepage: http://piston.rubyforge.org/
Clone URL: git://github.com/francois/piston.git
piston / lib / piston / svn / client.rb
100644 89 lines (73 sloc) 2.333 kb
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require "singleton"
 
module Piston
  module Svn
    class Client
      include Singleton
 
      class CommandError < RuntimeError; end
      class Failed < CommandError; end
      class BadCommand < CommandError; end
 
      def logger
        @logger ||= Log4r::Logger["handler::client"]
      end
 
      def out_logger
        @out_logger ||= Log4r::Logger["handler::client::out"]
      end
 
      def svnadmin(*args)
        run_cmd :svnadmin, *args
      end
 
      def svn(*args)
        run_cmd :svn, *args
      end
 
      def svnlook(*args)
        run_cmd :svnlook, *args
      end
 
      def svnversion(*args)
        run_cmd :svnversion, *args
      end
 
      private
      def run_cmd(executable, *args)
        args.collect! {|arg| arg.to_s =~ /\s|\*|\?|"|\n|\r/ ? %Q('#{arg}') : arg}
        args.collect! {|arg| arg ? arg : '""'}
        cmd = %Q|#{executable} #{args.join(' ')}|
        logger.debug {"> " + cmd}
 
        original_language = ENV["LANGUAGE"]
        begin
          ENV["LANGUAGE"] = "C"
          value = run_real(cmd)
          out_logger.info {"< " + value} unless (value || "").strip.empty?
          return value
        ensure
          ENV["LANGUAGE"] = original_language
        end
      end
 
      begin
        raise LoadError, "Not implemented on Win32 machines" if RUBY_PLATFORM =~ /mswin32/
 
          begin
            require "rubygems"
          rescue LoadError
            # NOP -- attempt to load without Rubygems
          end
 
        require "open4"
 
        def run_real(cmd)
          begin
            pid, stdin, stdout, stderr = Open4::popen4(cmd)
            _, cmdstatus = Process.waitpid2(pid)
            raise CommandError, "#{cmd.inspect} exited with status: #{cmdstatus.exitstatus}\n#{stderr.read}" unless cmdstatus.success?
            return stdout.read
          rescue Errno::ENOENT
            raise BadCommand, cmd.inspect
          end
        end
 
      rescue LoadError
        # On platforms where open4 is unavailable, we fallback to running using
        # the backtick method of Kernel.
        def run_real(cmd)
          out = `#{cmd}`
          raise BadCommand, cmd.inspect if $?.exitstatus == 127
          raise Failed, "#{cmd.inspect} exited with status: #{$?.exitstatus}" unless $?.success?
          out
        end
      end
    end
  end
end