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.



39
40
41
42
43
44
45
# File 'lib/rpatch/patch.rb', line 39

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.



13
14
15
# File 'lib/rpatch/patch.rb', line 13

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/rpatch/patch.rb', line 13

def name
  @name
end

#patch_entriesObject (readonly)

Returns the value of attribute patch_entries.



13
14
15
# File 'lib/rpatch/patch.rb', line 13

def patch_entries
  @patch_entries
end

Class Method Details

.apply(path, patches, patch_level) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rpatch/patch.rb', line 16

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
  Tty.error e.message
  patch_status = false
end

.apply_one(path, patch_file, patch_level) ⇒ Object



72
73
74
75
# File 'lib/rpatch/patch.rb', line 72

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rpatch/patch.rb', line 77

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, File.join(quilt_dir, 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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rpatch/patch.rb', line 47

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
      Tty.error e.message
      patch_status = false
    end
  end
  patch_status
end