Created
November 23, 2016 09:06
-
-
Save lukefrasera/d7c82a49cd7705e6fb7262ef9e0d36d2 to your computer and use it in GitHub Desktop.
Simple Tool to download Ubuntu Mainline amd64 kernels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function | |
import sys, os | |
import requests | |
import re | |
import urllib2 | |
def download_kernel_files(url, folder): | |
r = requests.get(url) | |
lines = r.text.split('\n') | |
expr = ( | |
'<a\s+(?:[^>]*?\s+)?href="([^"]*)">([^"]*)</a>' | |
) | |
prog = re.compile(expr) | |
links = [] | |
for line in lines: | |
result = prog.search(line) | |
if result: | |
grp1 = result.group(1) | |
grp2 = result.group(2) | |
if 'amd64' in grp2 and 'generic' in grp2: | |
links.append(grp2) | |
if 'all' in grp2: | |
links.append(grp2) | |
for link in set(links): | |
# Download each file | |
print('Downloading...[%s]' % (link)) | |
u = urllib2.urlopen(os.path.join(url, link)) | |
with open(os.path.join(folder, link), 'w') as file: | |
while True: | |
buffer = u.read(8192) | |
if not buffer: | |
break | |
file.write(buffer) | |
sys.stdout.write('.') | |
sys.stdout.flush() | |
print('Done.') | |
def main(): | |
url = 'http://kernel.ubuntu.com/~kernel-ppa/mainline/' | |
r = requests.get(url) | |
folders = r.text.split('\n') | |
expr = ( | |
'<a\s+(?:[^>]*?\s+)?href="([^"]*)">([^"]*)</a>' | |
) | |
prog = re.compile(expr) | |
folder_list = [] | |
for line in folders: | |
result = prog.search(line) | |
if result: | |
folder_list.append((result.group(1), result.group(2))) | |
# Get input on selection list | |
selection = int(raw_input('Show selection:')) | |
selected_list = [[x, False] for x in folder_list[-selection:]] | |
# select download | |
selecting = True | |
while selecting: | |
for i, item in enumerate(selected_list): | |
print('[%2d] (%s) - %s' % (i, '*' if item[1] else ' ', item[0][0])) | |
value_str = raw_input('Select Downloads or Quit[q]') | |
if value_str == 'q': | |
selecting = False | |
break | |
else: | |
value = int(value_str) | |
selected_list[value][1] = True | |
# download each true link | |
for item in [x[0] for x in selected_list if x[1]]: | |
if not os.path.isdir(item[1]): | |
os.mkdir(item[1]) | |
download_kernel_files(os.path.join(url, item[1]), item[1]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment