Class: Drupid::Patch

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/drupid/patch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#blah, #bzr, #compare_paths, #curl, #cvs, #debug, #dont_debug, #git, #hg, #ignore_interrupts, #interactive_shell, #odie, #ofail, #ohai, #owarn, #runBabyRun, #svn, #tempdir, #uncompress, #which, #writeFile

Constructor Details

#initialize(url, descr, md5 = nil) ⇒ Patch

Returns a new instance of Patch.



32
33
34
35
36
37
# File 'lib/drupid/patch.rb', line 32

def initialize url, descr, md5 = nil
  @url = url
  @descr = descr
  @md5 = md5
  @cached_location = nil
end

Instance Attribute Details

#cached_locationObject (readonly)

Returns the value of attribute cached_location.



30
31
32
# File 'lib/drupid/patch.rb', line 30

def cached_location
  @cached_location
end

#descrObject (readonly)

Returns the value of attribute descr.



29
30
31
# File 'lib/drupid/patch.rb', line 29

def descr
  @descr
end

#md5Object (readonly)

Returns the value of attribute md5.



28
29
30
# File 'lib/drupid/patch.rb', line 28

def md5
  @md5
end

#urlObject (readonly)

Returns the value of attribute url.



27
28
29
# File 'lib/drupid/patch.rb', line 27

def url
  @url
end

Instance Method Details

#applyObject

Applies this patch in the current directory. Raises an error if the patch cannot be applied.



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
90
91
92
# File 'lib/drupid/patch.rb', line 55

def apply
  debug "Applying patch at #{Dir.pwd}"
  raise "Patch not fetched." if !(@cached_location and @cached_location.exist?)
  patch_levels = ['-p1', '-p0']
  patched = false
  output = ''
  # First try with git apply
  patch_levels.each do |pl|
    begin
      runBabyRun 'git', ['apply', '--check', pl, @cached_location], :redirect_stderr_to_stdout => true
      runBabyRun 'git', ['apply', pl, @cached_location], :redirect_stderr_to_stdout => true
      patched = true
      break
    rescue => ex
      output << ex.to_s
    end
  end
  if not patched
    patch_levels.each do |pl|
      begin
        runBabyRun 'patch', ['--no-backup-if-mismatch', '-f', pl, '-d', Dir.pwd, '-i', @cached_location], :redirect_stderr_to_stdout => true
        patched = true
        break
      rescue => ex
        output << ex.to_s
      end
    end
  end
  if not patched
    if descr and descr != @cached_location.basename.to_s
      d = " (#{descr})"
    else
      d = ''
    end
    raise "Patch #{@cached_location.basename}#{d} could not be applied.\n" + output
  end
  return true
end

#fetchObject

Downloads the patch into the current directory.



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/drupid/patch.rb', line 40

def fetch
  dst = Pathname.pwd+File.basename(@url.to_s)
  blah "Fetching patch..."
  begin
    curl @url.to_s, '-o', dst
  rescue => ex
    raise "Patch #{File.basename(@url.to_s)} could not be fetched."
    debug ex.message
  end
  @cached_location = dst
  debug "Patch downloaded into #{@cached_location}"
end