-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch.rb
66 lines (58 loc) · 1.22 KB
/
patch.rb
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
require 'rubygems'
require 'session'
class Patch
attr_accessor :file, :number, :target, :root, :stdout, :stderr, :exitcode, :patch_bin
def initialize(file = '', root = '.', patch = 'patch')
@file = file
@root = root
@patch_bin = patch
parse if not @file.empty?
end
def parse
@number = nil
@target = ""
begin
(f = open(self.file)).each do |line|
line.strip!
if (/^\+\+\+/.match(line))
junk1, @target, junk2 = line.split(/\s+/)
break
end
unless (! /^\#/.match(line))
if (/\@Patch number:/.match(line))
junk1, @number = line.split(/\@Patch number:/)
end
end
end
if @number.nil?
match_data = /_(\d\d)_/.match(file)
if not match_data.nil? and not match_data[1].nil?
@number = match_data[1]
end
end
@number = @number.to_i
@target.strip!
rescue => ex
nil
ensure
f.close
end
end
def load(file)
@file = file
parse
end
def apply
begin
sess = Session.new
input = open(@file, 'r')
@stdout, @stderr = sess.execute "#{@patch_bin} #{@root}/#{@target}", :stdin => input
@exitcode = sess.exit_status
rescue
raise $!
ensure
input.close if not input.nil?
sess.close
end
end
end