-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathkmpd.py
executable file
·291 lines (266 loc) · 8.32 KB
/
kmpd.py
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/python
# Downloads kernel from http://kernel.ubuntu.com/~kernel-ppa/mainline/
# Requires: python-bs4
# Copyright (c) 2012 Savvas Radevic <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import urlparse
import urllib
import os
import urllib2
import platform
from bs4 import BeautifulSoup
import re
import sys
import subprocess
import tempfile
# We need to use apt.VersionCompare(a,b) to compare debian package versions
import apt_pkg
import argparse
# MODULE INIT
apt_pkg.init()
# PARSE ARGUMENTS
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('-d', '--disable-filter', action='store_true',
help='Do not filter out release candidate versions')
parser.add_argument('-p', '--prefer-stable', action='store_true',
help='Prefer latest stable version instead of latest release candidate of the same version (e.g. prefer v3.9-raring instead of v3.9-rc8-raring)')
args = parser.parse_args()
print(args)
url = "http://kernel.ubuntu.com/~kernel-ppa/mainline/"
print("Contacting {0}".format(url))
source = urllib.urlopen(url).read()
#print(source)
soup = BeautifulSoup(source, "html.parser")
kernels = list()
rel = re.sub('-\w*', '', platform.release())
if rel.endswith('.0'): rel = rel[:-2]
print("Current system kernel release version: {0}".format(rel))
for link in soup.find_all('a'):
href = link.get('href')
if not args.disable_filter:
#If filter is not disabled, apply all filters
if re.search("rc\d", href):
#If the version is a release candidate, bypass
continue
if href[0] == "v":
kver = href[1:-1] #strip first and last characters
vc = apt_pkg.version_compare(kver, rel)
if vc > 0:
# If kernel newer than current one
#print("{0} > {1}".format(kver, rel))
kernels.append(href)
else:
kernels.append(href)
# SELECT KERNEL
i = 0
for k in kernels:
i += 1
print("{0}. {1}".format(i, k))
selk = -1
while not 0 < selk <= len(kernels):
try:
defaultk = len(kernels)
if args.prefer_stable:
if re.search('-rc\d+-', kernels[-1]):
# If a release candidate is the last item in list
teststable = re.sub("-rc\d+-","-",kernels[-1])
if teststable in kernels:
defaultk = kernels.index(teststable) + 1
sel = raw_input("Please enter an integer [{0}]: ".format(defaultk))
if sel == "":
selk = defaultk
break
selk = int(sel)
except ValueError:
continue
print("You chose: {0}".format(kernels[selk-1]))
# SELECT ARCH
i = 0
archs = ("i386", "amd64")
sysarch = platform.machine().replace(
"x86_64", "amd64").replace("i686", "i386")
print("Your system architecture: {0}".format(sysarch))
try:
defaultarch = archs.index(sysarch)+1
except:
defaultarch = 1
for a in archs:
i += 1
print("{0}. {1}".format(i, a))
sela = -1
while not 0 < sela <= len(archs):
try:
sela = raw_input("Please enter an integer [{0}]: ".format(defaultarch))
if sela == "":
sela = defaultarch
break
sela = int(sela)
except ValueError:
continue
print("You chose: {0}".format(archs[sela-1]))
# SELECT FLAVOR
i = 0
flavors = ("generic", "lowlatency")
defaultflavor = 1
for f in flavors:
i += 1
print("{0}. {1}".format(i, f))
self = -1
while not 0 < self <= len(flavors):
try:
self = raw_input("Please enter an integer [{0}]: ".format(defaultflavor))
if self == "":
self = defaultflavor
break
self = int(self)
except ValueError:
continue
print("You chose: {0}".format(flavors[self-1]))
# SELECT PACKAGES
sel1 = -1
while True:
sel1 = raw_input("Would you like to download kernel headers [Y/n]: ")
if sel1 == "":
selkh = True
break
if not sel1 in tuple("yYnN"):
continue
else:
if sel1 in tuple("yY"):
selkh = True
else:
selkh = False
break
sel2 = -1
while True:
sel2 = raw_input("Would you like to download kernel image [Y/n]: ")
if sel2 == "":
selki = True
break
if not sel2 in tuple("yYnN"):
continue
else:
if sel2 in tuple("yY"):
selki = True
else:
selki = False
break
sel3 = -1
while True:
sel3 = raw_input("Would you like to download kernel extras [Y/n]: ")
if sel3 == "":
selke = True
break
if not sel3 in tuple("yYnN"):
continue
else:
if sel3 in tuple("yY"):
selke = True
else:
selke = False
break
sel4 = -1
while True:
sel4 = raw_input("Would you like to download kernel modules [Y/n]: ")
if sel4 == "":
selkm = True
break
if not sel4 in tuple("yYnN"):
continue
else:
if sel4 in tuple("yY"):
selkm = True
else:
selkm = False
break
print("Kernel headers: {0}, Kernel image: {1}, Kernel extras: {2}, Kernel modules: {3}".
format(selkh, selki, selke, selkm))
# selk = selected kernel
# sela = selected arch
# self = selected flavor
# selkh = kernel headers? T/F
# selki = kernel image? T/F
# selke = kernel extra? T/F
# selkm = kernel modules? T/F
link = "http://kernel.ubuntu.com/~kernel-ppa/mainline/{0}".format(kernels[selk-1])
print("Contacting {0}".format(link))
source = urllib.urlopen(link).read()
soup = BeautifulSoup(source, 'html.parser')
files = set()
for l in soup.find_all('a'):
href = l.get('href')
rxstr = "linux-headers-[^_]*(?:-{0}_.*_{1}|.*_all)\.deb".format(flavors[self-1],archs[sela-1])
if selkh and re.search(rxstr, href):
url = "{0}{1}".format(link, href)
files.add(url)
rxstr = "linux-image-[^_]*-{0}_.*_{1}\.deb".format(flavors[self-1],archs[sela-1])
if selki and re.search(rxstr, href):
url = "{0}{1}".format(link, href)
files.add(url)
rxstr = "linux-image-extra-[^_]*-{0}_.*_{1}\.deb".format(flavors[self-1],archs[sela-1])
if selke and re.search(rxstr, href):
url = "{0}{1}".format(link, href)
files.add(url)
rxstr = "linux-modules-[^_]*-{0}_.*_{1}\.deb".format(flavors[self-1],archs[sela-1])
if selkm and re.search(rxstr, href):
url = "{0}{1}".format(link, href)
files.add(url)
#Create temp folder
tempfolder = tempfile.mkdtemp()
print("Using temporary folder: {0}".format(tempfolder))
for url in files:
#Change directory to temp folder
os.chdir(tempfolder)
file_name = url.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print("Downloading: {0} Bytes: {1}".format(url, file_size))
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
p = float(file_size_dl) / file_size
status = r"{0} [{1:.2%}]".format(file_size_dl, p)
status = "\r\033[K" + status
sys.stderr.write(status)
sys.stderr.write("\r\033[K")
f.close()
# INSTALL PACKAGES
sel6 = -1
while True:
sel6 = raw_input("Would you like to install the downloaded packages? [Y/n]: ")
if sel6 == "":
selinst = True
break
if not sel6 in tuple("yYnN"):
continue
else:
if sel6 in tuple("yY"):
selinst = True
else:
selinst = False
break
if selinst:
print("Installing packages... please type in your password if requested")
subprocess.call("sudo dpkg -i {0}/*.deb".format(tempfolder), shell=True)
else:
print("Will not install packages")
raw_input("All done! Press [Enter] key to exit.")