@@ -0,0 +1,2293 @@
+#!/usr/bin/env python%0A%0Aimport logging%0Aimport re%0Aimport os%0Aimport shutil%0A%0Alogger = logging.getLogger(__name__)%0A%0ABOOT_FILE_PREFIXES = (u'initrd', u'System.map', u'vmlinuz', )%0A%0Adef discover_in_dir(directory, file_prefix):%0A    version_pattern = u'(?P%3Cversion%3E%5B%5C%5Cw.-%5D+catalin%5B%5C%5Cw.-%5D*)'%0A    kernel_file_pattern = u'%7B0%7D-%7B1%7D'.format(file_prefix, version_pattern)%0A%0A    for dir_entry in os.listdir(directory):%0A        match = re.match(kernel_file_pattern, dir_entry)%0A        if match is not None:%0A            version = match.group(u'version')%0A            yield version%0A%0Adef discover_kernel_versions():%0A    for file_prefix in BOOT_FILE_PREFIXES:%0A        for version in discover_in_dir(u'/boot', file_prefix):%0A            yield version%0A%0A    for version in discover_in_dir(u'/lib/modules', u''):%0A        yield version%0A%0Adef remove_file_report_errors(file_path):%0A    try:%0A        os.remove(file_path)%0A    except OSError as e:%0A        logger.warning(u'Cannot remove %25s: %25s', file_path, e)%0A%0Adef remove_kernel(version):%0A    for file_prefix in BOOT_FILE_PREFIXES:%0A        file_path = u'%7B0%7D-%7B1%7D'.format(file_prefix, version)%0A        remove_file_report_errors(os.path.join(u'/boot', file_path))%0A%0A    modules_path = os.path.join(u'/lib/modules', version)%0A    shutil.rmtree(u'/lib/modules/%7B0%7D'.format(candidate), ignore_errors=True)%0A%0Aif __name__ == u'__main__':%0A    logging.basicConfig(level=logging.DEBUG)%0A%0A    versions = list(sorted(set(discover_kernel_versions())))%0A    logger.debug(u'Found kernel versions %25s', u', '.join(versions))%0A%0A    running_version = os.uname()%5B2%5D%0A    removal_candidates = versions%5B:%5D%0A    if running_version in removal_candidates:%0A        removal_candidates.remove(running_version)%0A    logger.debug(u'Candidates for removal %25s', u', '.join(removal_candidates))%0A%0A    removed = %5B%5D%0A    for candidate in removal_candidates:%0A        known_input = False%0A        while not known_input:%0A            input = raw_input(u'Remove %7B0%7D? %5By/n%5D '.format(candidate))%0A            if input == u'y':%0A                logger.info(u'Removing kernel %25s', candidate)%0A                remove_kernel(candidate)%0A                removed.append(candidate)%0A%0A            if input in (u'y', u'n'):%0A                known_input = True%0A    %0A    logger.info(u'Removed kernels, update your bootloader:%5Cn%25s', u'%5Cn'.join(removed))%0A

Add script that deletes previously installed kernels