-
Notifications
You must be signed in to change notification settings - Fork 0
/
astpatch.rb
executable file
·188 lines (160 loc) · 3.6 KB
/
astpatch.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
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
#!/usr/bin/ruby
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), "."))
require 'work'
require 'patch'
require 'getopt/long'
$VERBOSE = nil
TERM::SPINNER = [ '[|]', '[/]', '[-]', '[\]' ]
trap("INT") {
TERM.check_eol
TERM.eol
TERM.warn "exiting"
exit
}
def get_patches(patches_d)
ret = Hash.new
pobj = Patch.new
begin
Dir.foreach(patches_d) { |patch|
next if patch.eql? "."
next if patch.eql? ".."
pobj.load(patches_d + '/' + patch)
if not pobj.target.nil? and not pobj.number.nil?
if ret[pobj.target].nil?
ret[pobj.target] = Array.new
end
ret[pobj.target][pobj.number.to_i] = patch
end
}
rescue => ex
TERM.error "error while reading patches from `#{patches_d}'"
TERM.error ex.to_s
exit 1
end
ret
end
# parsing command line arguments {{{
begin
opt = Getopt::Long.getopts(
["--file", "-f", Getopt::REQUIRED],
["--start", "-s", Getopt::REQUIRED],
["--end", "-e", Getopt::REQUIRED],
["--patches", "-p", Getopt::REQUIRED],
["--source", "-o", Getopt::REQUIRED],
["--vanilla", "-l", Getopt::REQUIRED],
["--continue", "-c", Getopt::BOOLEAN],
["--init", "-i", Getopt::BOOLEAN],
["--sync", "", Getopt::BOOLEAN]
)
rescue => ex
TERM.error("error parsing command line arguments")
TERM.error(ex.to_s)
exit 1
end
# }}}
if opt["p"]
$patches_d = opt["p"]
else
$patches_d = CONFIG['workspace_d'] + '/itsd/devel/nix/' + CONFIG['branch'] + \
'/low_level/' + CONFIG['asterisk_patches_d'] + '/' + \
CONFIG['asterisk_version']
end
if opt["o"]
$source_d = opt["o"]
else
$source_d = CONFIG['src_d'] + '/' + CONFIG['branch'] + '/asterisk/' + CONFIG['asterisk_version'] + \
'/' + CONFIG['patchroot_d']
end
if opt["l"]
vanilla_d = opt["l"]
else
vanilla_d = CONFIG['src_d'] + '/' + CONFIG['branch'] + '/asterisk/' + CONFIG['asterisk_version'] + \
'/' + CONFIG['vanilla_d']
end
start_idx = opt["s"].to_i if not opt["s"].nil?
stop_idx = opt["e"].to_i if not opt["e"].nil?
file = opt["f"] if not opt["f"].nil?
init = opt["i"]
if opt["sync"]
PERF.sync($patches_d + '/...')
end
patches = get_patches($patches_d)
if patches.nil? or patches.empty?
TERM.error "no patches found in `#{$patches_d}'"
exit
end
def apply_patch_set(target, set, start, stop)
ok = true
code = 0
errors = ""
n = 0
TERM.action target.dup
p = Patch.new('', $source_d)
set.each_index { |idx|
next if set[idx].nil?
next if not start.nil? and idx < start
break if not stop.nil? and idx > stop
TERM.reset_line
TERM.action target.dup + " : patch "
TERM.print idx.to_s.dup, :light_yellow
p.load($patches_d + '/' + set[idx])
n += 1
exit_code = p.apply
if exit_code > 0
code = exit_code
errors = p.stdout
TERM.failure
TERM.error p.file
ok = false
break
end
}
if ok
TERM.reset_line
TERM.action target.dup + " ("
TERM.print n.to_s, :light_yellow
if n > 1
TERM.print " patches)"
else
TERM.print " patch)"
end
TERM.ok
end
[code, errors]
end
if init
TERM.h1 "initializing"
TERM.action "initializing source "
TERM.spinner_start
begin
if not file.nil?
FileUtils.copy(vanilla_d + '/' + file, $source_d + '/' + file)
else
FileUtils.cp_r(vanilla_d + '/.', $source_d)
end
rescue => ex
TERM.failure
TERM.error ex.to_s
exit
ensure
TERM.spinner_stop
end
TERM.ok
end
TERM.h1 "applying patches"
if not file.nil?
code, errors = apply_patch_set(file, patches[file], start_idx, stop_idx)
if code > 0
TERM.error errors
exit
end
else
patches.each_pair { |target, spec|
next if target.nil? or target.empty?
code, errors = apply_patch_set(target, spec, start_idx, stop_idx)
if code > 0 and not opt["c"]
TERM.error errors
exit
end
}
end