Skip to content

Instantly share code, notes, and snippets.

@idkrn123
Last active November 16, 2023 14:54
Show Gist options
  • Save idkrn123/6351ece52125b3907f32dd5a458723c5 to your computer and use it in GitHub Desktop.
Save idkrn123/6351ece52125b3907f32dd5a458723c5 to your computer and use it in GitHub Desktop.
a cronnable little script to get the latest stable version from kernel.org, compare against current version, and run a build script
import os
import re
import click
import requests
from bs4 import BeautifulSoup
# Constants
KERNEL_ORG_URL = "https://www.kernel.org/"
VERSION_FILE = "latest_kernel_version.txt"
DOWNLOAD_DIR = "./downloads"
# Ensure the download directory exists
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
@click.command()
@click.option('--build-script', default='build_linux.sh', help='The build script to run after downloading.')
def check_for_update(build_script):
"""
Check for a new Linux kernel version and download it if available.
"""
# Fetch the HTML content from kernel.org
response = requests.get(KERNEL_ORG_URL)
response.raise_for_status() # Raise an exception for HTTP errors
# Parse the HTML content
soup = BeautifulSoup(response.text, 'html.parser')
latest_button = soup.find(id="latest_button")
if not latest_button:
click.echo("Could not find the latest version button on kernel.org")
return
# Extract the download link and version number
download_link = latest_button.find('a')['href']
version_match = re.search(r'linux-(\d+\.\d+\.\d+)\.tar\.xz', download_link)
if not version_match:
click.echo("Could not extract the kernel version from the download link")
return
current_version = version_match.group(1)
click.echo(f"Current latest kernel version: {current_version}")
# Read the stored version
previous_version = None
if os.path.exists(VERSION_FILE):
with open(VERSION_FILE, 'r') as file:
previous_version = file.read().strip()
# Compare versions and download if there's a new one
if previous_version != current_version:
click.echo("New kernel version available. Downloading...")
download_response = requests.get(download_link)
download_response.raise_for_status()
# Save the downloaded file
filename = os.path.join(DOWNLOAD_DIR, f"linux-{current_version}.tar.xz")
with open(filename, 'wb') as file:
file.write(download_response.content)
# Update the stored version
with open(VERSION_FILE, 'w') as file:
file.write(current_version)
# Run the build script
os.system(f"./{build_script} {filename}")
else:
click.echo("No new kernel version available.")
if __name__ == "__main__":
check_for_update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment