-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatcher.rb
More file actions
211 lines (169 loc) · 4.8 KB
/
Copy pathpatcher.rb
File metadata and controls
211 lines (169 loc) · 4.8 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
require 'rbconfig'
require 'tmpdir'
require 'rubygems/package'
##
# Gem::Patcher is used to patch .gem files
# by calling system patch command
class Gem::Patcher
include Gem::UserInteraction
if Gem::VERSION < '2.0'
require 'rubygems/package-1.8'
end
class PatchCommandMissing < StandardError; end
def initialize(gemfile, output_dir)
@gemfile = gemfile
@output_dir = output_dir
# @target_dir is a temporary directory where the gem files live
tmpdir = Dir.mktmpdir
basename = File.basename(gemfile, '.gem')
@target_dir = File.join(tmpdir, basename)
end
##
# Patch the gem, move the new patched gem
# to +options[:outfile]+ and return the path
def patch_with(patches, options)
@std, @output = [], []
check_patch_command_is_installed
extract_gem
copy_in(options[:copy_in], @target_dir) if options[:copy_in]
# Apply all patches
patches.each do |patch|
info 'Applying patch ' + patch
@std << apply_patch(patch, options)
end
remove(options[:remove], @target_dir) if options[:remove]
# Rebuild only if there weren't any problems
build_patched_gem unless failed?
options[:outfile] ||= File.join(@output_dir, @package.spec.file_name)
FileUtils.mv((File.join @target_dir, @package.spec.file_name), options[:outfile]) unless options[:dry_run]
# Return the path to the patched gem
options[:outfile]
end
##
# Apply one +patch+ at a time using +options+
#
# Default options:
# options[:strip] = 1
# options[:fuzz] = 2
def apply_patch(patch, options)
options[:strip] ||= 1
options[:fuzz] ||= 2
patch_path = File.expand_path(patch)
info 'Path to the patch to apply: ' + patch_path
copy_in(options[:copy_in], @target_dir) if options[:copy_in]
# Apply the patch by calling 'patch -pNUMBER < patch'
Dir.chdir @target_dir do
opts = ["--verbose", "-p#{options[:strip]}", "--fuzz=#{options[:fuzz]}", "#{options[:patch_options]}"]
IO.popen("patch #{opts.join(' ')} < #{patch_path} 2>&1") do |out|
std = out.readlines
out.close
info std
unless $?.nil?
if $?.exitstatus == 0
@output << "Successfully patched with #{patch}"
else
@output << "Error: Unable to patch with #{patch}."
unless Gem.configuration.really_verbose
@output << 'Run gem patch with --verbose option to swich to verbose mode.'
end
end
end
std
end
end
end
##
# Print results from patching if
# Gem.configuration.really_verbose
def print_results
@output.each do |msg|
say msg
end
end
##
# Return output lines
def output
@output
end
##
# Return standard output
# from patch command
def std
@std
end
##
# Return false if any of the pathes failed
def failed?
@std.join(' ') =~ /.*Hunk #[0-9]+ (ignored|failed).*/
end
private
def extract_gem
@package = Gem::Package.new @gemfile
# Unpack
info "Unpacking gem '#{@gemfile}' in " + @target_dir
@package.extract_files @target_dir
end
def build_patched_gem
patched_package = Gem::Package.new @package.spec.file_name
patched_package.spec = @package.spec.clone
patched_package.spec.files = files_in_gem
# Change dir and build the patched gem
Dir.chdir @target_dir do
patched_package.build false
end
end
def copy_in(paths, target)
origins = paths.split(',')
origins.each do |p|
if Dir.exists?(p) || File.exists?(p)
FileUtils.cp_r(p, File.join(target, File.basename(p)))
else
FileUtils.cp_r(File.join(Dir.pwd, p), File.join(target, p))
end
end
end
def remove(paths, target)
removals = paths.split(',')
removals.each do |r|
FileUtils.rm_r File.join(target, r)
end
end
def info(msg)
say msg if Gem.configuration.really_verbose
end
def files_in_gem
files = []
Dir.foreach(@target_dir) do |file|
if File.directory? File.join @target_dir, file
files += files_in_dir(file) unless /\./.match(file)
else
files << file
end
end
delete_original_files(files)
end
def files_in_dir(dir)
files = []
Dir.foreach(File.join @target_dir, dir) do |file|
if File.directory? File.join @target_dir, dir, file
files += files_in_dir(File.join dir, file) unless /\./.match(file)
else
files << File.join(dir, file)
end
end
files
end
def delete_original_files(files)
files.each do |file|
files.delete file if /\.orig/.match(file)
end
end
def check_patch_command_is_installed
begin
IO.popen('patch --version')
rescue Exception
raise PatchCommandMissing, \
'Calling `patch` command failed. Do you have it installed?'
end
end
end