Skip to content

Commit

Permalink
Version 1.18 - using logging instead of custom Log class
Browse files Browse the repository at this point in the history
  • Loading branch information
Javinator9889 committed May 10, 2018
1 parent c34b6a6 commit 26730e6
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 282 deletions.
117 changes: 73 additions & 44 deletions kernel_upgrader/__init__.py
@@ -1,17 +1,19 @@
import argparse
import time
import logging

from .utils import (isRunningLinux,
Log,
isUserAdmin,
getLinuxVersion,
getFreeSpaceAvailable,
cleanupSpace,
isNewVersionAvailable
isNewVersionAvailable,
cleanupOldLogs
)
from .utils.colors import OutputColors as Colors
from .utils.anim import Animation
from .values.Constants import REPO_URL, USAGE, VERSION
from .utils.logger import setup_logging
from .values.Constants import OP_REPO_URL, EXU_USAGE, OP_VERSION, LOG_FILE_PATH, LOG_FILENAME, LOG_COMPILER_FILENAME
from .exceptions import (LinuxSystemNotFound,
RootPrivilegesNotGiven,
raiserModuleNotFound,
Expand All @@ -25,92 +27,115 @@
__program_name = """Kernel Upgrader for Linux"""
__program_description = """Download, compile and install the latest stable kernel for your Linux system. Automate
this tool for upgrading your kernel periodically"""
__program_version = "Current running version: " + VERSION + " - " + REPO_URL
__program_version = "Current running version: " + OP_VERSION + " - " + OP_REPO_URL


def application(arg):
usage = arg.usage
show_version = arg.version
check_kernel_updates = arg.check
if usage:
print(USAGE)
print(EXU_USAGE)
elif show_version:
print(__program_version)
elif check_kernel_updates:
if not isRunningLinux():
raise LinuxSystemNotFound(Colors.FAIL +
"Your OS is not running under a Linux installation. It is not possible"
" to update the kernel" + Colors.ENDC)
else:
from packaging import version
info = Connection()
current_version = getLinuxVersion()
new_version = info.getLatestVersionCode()
if version.parse(current_version) >= version.parse(new_version):
print(Colors.FAIL + "You already have the latest version: " + current_version + Colors.ENDC)
exit(1)
else:
print(Colors.OKGREEN + "There is a new version available: " + Colors.ENDC + Colors.BOLD + new_version +
Colors.ENDC + "\n\nRun this program without \"-c\" (\"--check\") option to upgrade")
exit(0)
else:
if isNewVersionAvailable():
print(Colors.HEADER + "New version available" + Colors.ENDC + Colors.OKBLUE + " | Download it with pip"
" or go to this URL: "
+ Colors.ENDC + Colors.UNDERLINE + REPO_URL + Colors.ENDC + "\n")
+ Colors.ENDC + Colors.UNDERLINE + OP_REPO_URL + Colors.ENDC + "\n")
time.sleep(8)
if not isUserAdmin():
raise RootPrivilegesNotGiven(Colors.FAIL + "This application needs root rights in order to work properly."
" Run with \"-u\" option to get more information" + Colors.ENDC)
else:
__log = Log.instance()
cleanupOldLogs()
setup_logging("kernel_logging", LOG_FILE_PATH + LOG_FILENAME)
setup_logging("compiler_logging", LOG_FILE_PATH + LOG_COMPILER_FILENAME)
__log = logging.getLogger("kernel_logging")
animator = Animation(0.1)
try:
if not isRunningLinux():
__log.e("OS is not under a Linux installation. Aborting kernel upgrade...")
__log.error("OS is not under a Linux installation. Aborting kernel upgrade...")
raise LinuxSystemNotFound(Colors.FAIL +
"Your OS is not running under a Linux installation. It is not possible"
" to update the kernel" + Colors.ENDC)
else:
__log.i("Checking for free space available...")
__log.info("Checking for free space available...")
free_space = float(getFreeSpaceAvailable())
if free_space < 20:
__log.e("There is not enough free space available. Current free space (in GB): "
+ str(free_space))
# __log.finish()
__log.error("There is not enough free space available. Current free space (in GB): "
+ str(free_space))
raise NotEnoughFreeSpaceAvailable(Colors.FAIL + "There is not enough free space available on "
"drive which mounts \"/home\" 20GB are needed "
"at least" + Colors.ENDC)
__log.i("There is enough free space available. Current free space: " + str(free_space) + " GB")
__log.i("Starting kernel compiling")
__log.d("Checking versions")
__log.info("There is enough free space available. Current free space: " + str(free_space) + " GB")
__log.info("Starting kernel compiling")
__log.debug("Checking versions")
current_version = getLinuxVersion()
__log.i("Current version detected: " + current_version)
__log.info("Current version detected: " + current_version)
info = Connection()
new_version = info.getLatestVersionCode()
from packaging import version
if version.parse(current_version) >= version.parse(new_version):
__log.d("The version installed is the same or greater than the available one. "
"Current version: " + current_version + " | Available version: " + new_version)
__log.debug("The version installed is the same or greater than the available one. "
"Current version: " + current_version + " | Available version: " + new_version)
print(Colors.FAIL + "You already have the latest version" + Colors.ENDC)
exit(1)
else:
__log.d("There is a new version available - new kernel upgrade process started")
__log.debug("There is a new version available - new kernel upgrade process started")
print(Colors.OKGREEN + "There is a new version available." +
Colors.ENDC)
print(Colors.OKBLUE + "Downloading new version... " + Colors.ENDC + "| New version: " +
new_version)
__log.d("Starting new version download... | New version: " + new_version)
__log.debug("Starting new version download... | New version: " + new_version)
version_url = info.getLatestVersionURL()
__log.i("Downloading from: " + version_url)
__log.info("Downloading from: " + version_url)
downloader = Downloader(version_url, new_version)
download_path, current_date = downloader.startDownload()
__log.i("Downloaded to path: \"" + download_path + "\"")
__log.d("Starting dependencies installation...")
__log.info("Downloaded to path: \"" + download_path + "\"")
__log.debug("Starting dependencies installation...")
print(Colors.OKBLUE + "Installing required dependencies... " + Colors.ENDC)
Dependencies.installRequiredDependencies()
__log.d("Required dependencies installed/satisfied")
__log.d("Starting kernel decompression")
__log.debug("Required dependencies installed/satisfied")
__log.debug("Starting kernel decompression")
animator.animate(Colors.OKBLUE + "Decompressing downloaded kernel..." + Colors.ENDC,
Colors.OKBLUE)
unzipper = UnZipper(download_path)
kernel_folder = unzipper.unzip()
animator.stop()
__log.d("Finished kernel decompression")
__log.debug("Finished kernel decompression")
time.sleep(1)
__log.d("Starting kernel compilation...")
__log.debug("Starting kernel compilation...")
print(Colors.OKBLUE + "Copying old configuration..." + Colors.ENDC)
compiler = Compiler(kernel_folder, new_version, current_date)
__log.d("Copying old kernel boot config")
__log.debug("Copying old kernel boot config")
if compiler.copy_latest_config():
__log.d("Copied old kernel boot configuration")
__log.d("Adapting latest config for the new kernel version")
__log.debug("Copied old kernel boot configuration")
__log.debug("Adapting latest config for the new kernel version")
animator.animate(Colors.OKBLUE + "Adapting old configuration to the new kernel..."
+ Colors.ENDC, Colors.OKBLUE)
compiler.adaptOldConfig()
animator.stop()
__log.d("Adapted old kernel configuration to the newer version")
__log.debug("Adapted old kernel configuration to the newer version")
time.sleep(1)
__log.d("Performing kernel compilation...")
__log.debug("Performing kernel compilation...")
print(Colors.OKBLUE + "Starting kernel compilation..." + Colors.ENDC)
print(Colors.WARNING + "This process will take a long time to finish. You can do it "
"in background by pressing \"Ctrl + Z\" and then, type \"bg\" at"
Expand All @@ -119,42 +144,38 @@ def application(arg):
Colors.BOLD)
compiler.compileKernel()
animator.stop()
__log.d("Kernel compilation finished")
__log.debug("Kernel compilation finished")
time.sleep(1)
__log.d("Starting kernel installation...")
__log.debug("Starting kernel installation...")
animator.animate(Colors.OKBLUE + "Installing the new kernel..." + Colors.ENDC,
Colors.OKBLUE)
compiler.installKernel()
animator.stop()
__log.d("Finished correctly kernel installation. New version installed: " + new_version)
__log.debug("Finished correctly kernel installation. New version installed: " + new_version)
time.sleep(1)
print(Colors.OKGREEN + "Kernel completely installed. Now you should reboot in order to "
"apply changes. New version: " + new_version + Colors.ENDC)
__log.d("Cleaning-up space for downloaded & compiled files")
__log.debug("Cleaning-up space for downloaded & compiled files")
animator.animate(Colors.UNDERLINE + "Cleaning up used space..." + Colors.ENDC, None)
cleanupSpace()
animator.stop()
__log.d("Space cleaned-up")
__log.debug("Space cleaned-up")
time.sleep(1)
__log.finish()
exit(0)
else:
__log.e("Impossible to copy latest kernel configuration. Aborting...")
__log.finish()
__log.error("Impossible to copy latest kernel configuration. Aborting...")
except ImportError as e:
raiserModuleNotFound(e)
except KeyboardInterrupt:
animator.force_stop()
print("\n")
print(Colors.FAIL + "User pressed Ctrl + C - stopping..." + Colors.ENDC)
__log.e("User pressed keyboard interrupt. Stopping program...")
__log.finish()
__log.error("User pressed keyboard interrupt. Stopping program...")
exit(2)
except Exception as e:
print(e)
animator.force_stop()
__log.e("Exception catch | " + str(e))
__log.finish()
__log.error("Exception catch | " + str(e))
exit(3)


Expand All @@ -166,6 +187,14 @@ def main():
"--usage",
action="store_true",
help="Show full usage of this program")
arguments.add_argument("-v",
"--version",
action="store_true",
help="Show program version")
arguments.add_argument("-c",
"--check",
action="store_true",
help="Only checks if there is any new version available")
args = arguments.parse_args()
application(args)

Expand Down

0 comments on commit 26730e6

Please sign in to comment.