forked from nning/linux-grsec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.rb
executable file
·153 lines (118 loc) · 2.77 KB
/
update.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
#!/usr/bin/env ruby
require 'getoptlong'
require 'open-uri'
require 'open3'
begin
require 'nokogiri'
rescue LoadError
$stderr << "Install nokogiri (gem install nokogiri).\n"
exit 1
end
class Pkgbuild
attr_reader :version, :timestamp
def initialize(hash: false)
@version, @timestamp = versions
@hash = hash
end
def major
version.segments[0..1].join('.')
end
def update!(patch)
c = File.open('PKGBUILD').readlines
c.each_with_index do |line, i|
if line =~ /^_basekernel=/
c[i] = "_basekernel=#{patch.major}\n"
end
if line =~ /^pkgver=/
c[i] = "pkgver=${_basekernel}.#{patch.version.segments.last}\n"
end
if line =~ /^_timestamp=/
c[i] = "_timestamp=#{patch.timestamp}\n"
end
if line =~ /^pkgrel=/
new = 1
new = line.split('=').last.to_i + 1 if version == patch.version
c[i] = "pkgrel=#{new}\n"
end
if @hash && line =~ /^sha256sums=/
c = c.first(i)
end
end
write c.join
if @hash
o, e, s = Open3.capture3 'makepkg -g'
c << o if s.success?
end
write c.join
end
private
def versions
v, t = `bash PKGBUILD -v`.split ' '
[Gem::Version.new(v), t.to_i]
end
def write(contents)
File.open('PKGBUILD', 'w').write contents
end
end
class Patch
URI = 'http://grsecurity.net/download.php'
attr_reader :version, :timestamp
def initialize
@version, @timestamp = versions
end
def filename
unless @filename
doc = Nokogiri::HTML open URI
patches = doc.css('div.left a').map &:content
v = newest patches
@filename = patches.select { |x| x.include? v }.first
end
@filename
end
def major
version.segments[0..1].join('.')
end
private
def newest(patches)
select_version patches, :last
end
def select_version(patches, method)
a = patches.sort.map { |x| x.split('-')[2] }
a.select! { |x| x =~ /^[0-9]{1}\./ }
a.map! { |x| Gem::Version.new x rescue nil }.compact!
a.sort.send(method).to_s
end
def versions
v, t = filename.split('-')[2..3]
t = t.split('.').first
[Gem::Version.new(v), t.to_i]
end
end
def usage_message
$stderr << "#{File.basename $0} [options]
Update version and patch in linux-grsec PKGBUILD.
-G Generate new hashes and append them to PKGBUILD.
-h This help.
"
exit 1
end
hash = true
options = GetoptLong.new \
[ '-G', GetoptLong::NO_ARGUMENT ],
[ '-h', GetoptLong::NO_ARGUMENT ]
options.each do |option, argument|
case option
when '-G'
hash = false
when '-h'
usage_message
end
end
pkgbuild = Pkgbuild.new hash: hash
patch = Patch.new
if pkgbuild.timestamp < patch.timestamp
pkgbuild.update! patch
puts `git diff PKGBUILD`
else
puts 'PKGBUILD is up-to-date.'
end