Class: Rpatch::Patch

Inherits:
Object
  • Object
show all
Defined in:
lib/rpatch/patch.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, level) ⇒ Patch

Returns a new instance of Patch.



35
36
37
38
39
40
41
# File 'lib/rpatch/patch.rb', line 35

def initialize(file, level)
  @name = file.is_a?(String) ? file : "<#{file.class.to_s}>"
  @patch = file
  @level = level
  @patch_entries = []
  load_patch
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



9
10
11
# File 'lib/rpatch/patch.rb', line 9

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/rpatch/patch.rb', line 9

def name
  @name
end

#patch_entriesObject (readonly)

Returns the value of attribute patch_entries.



9
10
11
# File 'lib/rpatch/patch.rb', line 9

def patch_entries
  @patch_entries
end

Class Method Details

.apply(path, patches, patch_level) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rpatch/patch.rb', line 12

def apply(path, patches, patch_level)
  unless File.exist? path
    raise FileNotExistError, "File or directory \"#{path}\" does not exist"
  end
  patch_status = true
  patches.each do |patch_file|
    if patch_file.is_a? IO or patch_file.is_a? StringIO
      apply_one(path, patch_file, patch_level) || patch_status = false
    elsif File.file? patch_file
      apply_one(path, patch_file, patch_level) || patch_status = false
    elsif File.directory? patch_file
      apply_quilt(path, patch_file, patch_level) || patch_status = false
    else
      raise FileNotExistError, "Can not find patch file: #{patch_file}"
    end
  end
  patch_status
rescue Exception => e
  STDERR.puts "Error: #{e.message}"
  patch_status = false
end

.apply_one(path, patch_file, patch_level) ⇒ Object



68
69
70
71
# File 'lib/rpatch/patch.rb', line 68

def apply_one(path, patch_file, patch_level)
  patch = Patch.new(patch_file, patch_level)
  patch.apply_to(path)
end

.apply_quilt(path, quilt_dir, patch_level) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rpatch/patch.rb', line 73

def apply_quilt(path, quilt_dir, patch_level)
  patch_status = true
  if File.exist?("#{quilt_dir}/series")
    File.open("#{quilt_dir}/series") do |io|
      io.readlines.each do |line|
        line.strip!
        filename = line
        level = patch_level
        if line =~ /^(.*?)[\s]+-p([0-9]*)$/
          filename = $1
          level = $2
        end
        unless filename.start_with? '#'
          apply_one(path, filename, level) || patch_status = false
        end
      end
    end
  else
    raise FileNotExistError, "Can not find (quilt) patchs in dir: #{quilt_dir}"
  end
  patch_status
end

Instance Method Details

#apply_to(input, output = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rpatch/patch.rb', line 43

def apply_to(input, output=nil)
  patch_status = true
  patch_entries.each do |patch_entry|
    begin
      # input maybe a IO, StringIO, directory, file, or in-exist file.
      if input.is_a? String and File.directory? input
        patch_entry.patch_on_directory(input, output) || patch_status = false
      else
        if patch_entries.size > 1
          raise PatchOneWithManyError, "Multiple patch entries (#{patch_entries.size}) have been found in patch #{name}"
        end
        # a IO, StringIO, file, or inexist file.
        patch_entry.patch_on_file(input, output) || patch_status = false
      end
    rescue Exception => e
      STDERR.puts "Error: #{e.message}"
      patch_status = false
    end
  end
  patch_status
end