2016-12-26 17:53:21 +00:00
|
|
|
#!/usr/bin/env python3
|
2016-11-06 11:18:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Name: uninstall_distro.py
|
|
|
|
# Purpose: Module to uninstall distros installed by multibootusb
|
|
|
|
# Authors: Sundar
|
|
|
|
# Licence: This file is a part of multibootusb package. You can redistribute it or modify
|
|
|
|
# under the terms of GNU General Public License, v.2 or above
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import shutil
|
|
|
|
import threading
|
|
|
|
import platform
|
|
|
|
from .usb import *
|
|
|
|
from . import config
|
2016-12-26 17:53:21 +00:00
|
|
|
from . import gen
|
2016-11-06 11:18:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def install_distro_list():
|
|
|
|
"""
|
|
|
|
List all distro names installed by previous install
|
|
|
|
:return: List of distro names as list
|
|
|
|
"""
|
|
|
|
usb_details = details(config.usb_disk)
|
|
|
|
config.usb_mount = usb_details['mount_point']
|
|
|
|
sys_cfg_file = os.path.join(config.usb_mount, "multibootusb", "syslinux.cfg")
|
|
|
|
|
|
|
|
if os.path.exists(sys_cfg_file):
|
|
|
|
distro_list = []
|
|
|
|
for line in open(sys_cfg_file):
|
|
|
|
if "#start " in line:
|
|
|
|
distro_list.append(line[7:])
|
|
|
|
return distro_list
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def unin_distro():
|
|
|
|
usb_details = details(config.usb_disk)
|
|
|
|
usb_mount = usb_details['mount_point']
|
|
|
|
config.uninstall_distro_dir_name = config.uninstall_distro_dir_name.replace('\n', '')
|
2016-12-26 17:53:21 +00:00
|
|
|
gen.log(os.path.join(usb_mount, "multibootusb", config.uninstall_distro_dir_name, "multibootusb.cfg"))
|
2016-11-06 11:18:53 +00:00
|
|
|
if os.path.exists(os.path.join(usb_mount, "multibootusb", config.uninstall_distro_dir_name, "multibootusb.cfg")):
|
|
|
|
with open(os.path.join(usb_mount, "multibootusb", config.uninstall_distro_dir_name, "multibootusb.cfg"), "r") as multibootusb_cfg:
|
|
|
|
config.distro = multibootusb_cfg.read().replace('\n', '')
|
|
|
|
if config.distro:
|
|
|
|
uninstall_distro()
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
|
def delete_frm_file_list():
|
|
|
|
"""
|
|
|
|
Generic way to remove files from USB disk.
|
|
|
|
:param config.usb_disk:
|
|
|
|
:param iso_file_list: List of files installed in the USB disk
|
|
|
|
:param config.uninstall_distro_dir_name: Directory where the distro is installed
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
usb_details = details(config.usb_disk)
|
|
|
|
usb_mount = usb_details['mount_point']
|
|
|
|
if config.iso_file_list is not None:
|
|
|
|
for f in config.iso_file_list:
|
|
|
|
if platform.system() == "Windows":
|
|
|
|
f = f.replace('\n', '').strip("/").replace("/", "\\")
|
|
|
|
else:
|
|
|
|
f = f.replace('\n', '').strip("/")
|
|
|
|
if os.path.exists(os.path.join(usb_mount, "ldlinux.sys")):
|
2017-02-17 16:36:06 +00:00
|
|
|
try:
|
|
|
|
os.chmod(os.path.join(usb_mount, "ldlinux.sys"), 0o777)
|
|
|
|
os.unlink(os.path.join(usb_mount, "ldlinux.sys"))
|
|
|
|
except:
|
|
|
|
gen.log('Could not remove ldlinux.sys')
|
2016-11-06 11:18:53 +00:00
|
|
|
|
|
|
|
if os.path.exists(os.path.join(usb_mount, f)):
|
|
|
|
if os.path.isfile(os.path.join(usb_mount, f)):
|
2017-04-10 16:00:38 +00:00
|
|
|
gen.log("Removing " + (os.path.join(usb_mount, f)))
|
|
|
|
os.remove(os.path.join(usb_mount, f))
|
|
|
|
'''
|
|
|
|
if 'bootx64.efi' in os.path.join(usb_mount, f) or 'grub.cfg' in os.path.join(usb_mount, f):
|
|
|
|
try:
|
|
|
|
gen.log("Removing " + (os.path.join(usb_mount, f)))
|
|
|
|
os.remove(os.path.join(usb_mount, f))
|
|
|
|
except:
|
|
|
|
gen.log('Could not remove ' + f)
|
|
|
|
else:
|
|
|
|
gen.log('Not removing ' + f)
|
|
|
|
'''
|
2016-11-06 11:18:53 +00:00
|
|
|
elif os.path.isdir(os.path.join(usb_mount, f)):
|
2017-04-10 16:00:38 +00:00
|
|
|
#if 'bootx64.efi' not in os.path.join(usb_mount, f) or 'grub.cfg' not in os.path.join(usb_mount, f):
|
|
|
|
shutil.rmtree(os.path.join(usb_mount, f))
|
|
|
|
|
2016-11-06 11:18:53 +00:00
|
|
|
if os.path.exists(os.path.join(usb_mount, "multibootusb", config.uninstall_distro_dir_name, "generic.cfg")):
|
|
|
|
with open(os.path.join(usb_mount, "multibootusb", config.uninstall_distro_dir_name, "generic.cfg"), "r") as generic_cfg:
|
|
|
|
if platform.system() == "Windows":
|
|
|
|
generic = generic_cfg.read().replace('\n', '').replace("/", "\\")
|
|
|
|
else:
|
|
|
|
generic = generic_cfg.read().replace('\n', '')
|
|
|
|
if os.path.exists(os.path.join(usb_mount, generic.strip("/"))):
|
|
|
|
os.remove(os.path.join(usb_mount, generic.strip("/")))
|
|
|
|
if platform.system() == 'Linux':
|
2017-02-17 16:36:06 +00:00
|
|
|
gen.log('Removed files from ' + config.uninstall_distro_dir_name)
|
2016-12-26 17:53:21 +00:00
|
|
|
gen.log('Syncing....')
|
2016-11-06 11:18:53 +00:00
|
|
|
os.system('sync')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def uninstall_distro():
|
|
|
|
"""
|
|
|
|
Uninstall selected distro from selected USB disk.
|
|
|
|
:param config.usb_disk: Path of the USB disk
|
|
|
|
:param config.uninstall_distro_dir_name: Directory where the distro is installed
|
|
|
|
:param _distro: Generic name applied to distro install by multibootusb
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
usb_details = details(config.usb_disk)
|
|
|
|
usb_mount = usb_details['mount_point']
|
|
|
|
|
|
|
|
if platform.system() == 'Linux':
|
|
|
|
os.system('sync')
|
|
|
|
|
|
|
|
if os.path.exists(os.path.join(usb_mount, "multibootusb", config.uninstall_distro_dir_name, "iso_file_list.cfg")):
|
|
|
|
with open(os.path.join(usb_mount, "multibootusb", config.uninstall_distro_dir_name, "iso_file_list.cfg"), "r") as f:
|
|
|
|
config.iso_file_list = f.readlines()
|
2016-12-26 17:53:21 +00:00
|
|
|
# gen.log iso_file_list
|
2016-11-06 11:18:53 +00:00
|
|
|
|
|
|
|
for path, subdirs, files in os.walk(os.path.join(usb_mount, "multibootusb", config.uninstall_distro_dir_name)):
|
|
|
|
for name in files:
|
|
|
|
if name.endswith('ldlinux.sys') or name.endswith('ldlinux.c32'):
|
|
|
|
os.chmod(os.path.join(path, name), 0o777)
|
|
|
|
os.unlink(os.path.join(path, name))
|
|
|
|
if config.distro == "opensuse":
|
|
|
|
if os.path.exists(os.path.join(usb_mount, config.uninstall_distro_dir_name + ".iso")):
|
|
|
|
os.remove(os.path.join(usb_mount, config.uninstall_distro_dir_name + ".iso"))
|
|
|
|
elif config.distro == "windows" or config.distro == "alpine" or config.distro == "generic":
|
|
|
|
delete_frm_file_list()
|
|
|
|
if config.distro == "ipfire":
|
|
|
|
files = os.listdir(usb_mount)
|
|
|
|
for f in files:
|
|
|
|
if f.endswith('.tlz'):
|
|
|
|
os.remove(os.path.join(usb_mount, f))
|
|
|
|
if os.path.exists(os.path.join(usb_mount, "distro.img")):
|
|
|
|
os.remove(os.path.join(usb_mount, "distro.img"))
|
|
|
|
elif config.distro == "trinity-rescue":
|
|
|
|
shutil.rmtree(os.path.join(usb_mount, "trk3"))
|
|
|
|
|
|
|
|
if os.path.exists(os.path.join(usb_mount, "multibootusb", config.uninstall_distro_dir_name)):
|
|
|
|
if platform.system() == 'Linux':
|
|
|
|
os.system('sync')
|
|
|
|
shutil.rmtree(os.path.join(usb_mount, "multibootusb", config.uninstall_distro_dir_name))
|
|
|
|
|
|
|
|
delete_frm_file_list()
|
|
|
|
|
|
|
|
update_sys_cfg_file()
|
2017-04-10 16:00:38 +00:00
|
|
|
update_grub_cfg_file()
|
2016-11-06 11:18:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def update_sys_cfg_file():
|
|
|
|
"""
|
|
|
|
Main function to remove uninstall distro specific operations.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if platform.system() == 'Linux':
|
|
|
|
os.system('sync')
|
|
|
|
|
|
|
|
sys_cfg_file = os.path.join(config.usb_mount, "multibootusb", "syslinux.cfg")
|
|
|
|
if not os.path.exists(sys_cfg_file):
|
2016-12-26 17:53:21 +00:00
|
|
|
gen.log("syslinux.cfg file not found for updating changes.")
|
2016-11-06 11:18:53 +00:00
|
|
|
else:
|
2016-12-26 17:53:21 +00:00
|
|
|
gen.log("Updating syslinux.cfg file...")
|
2016-11-06 11:18:53 +00:00
|
|
|
string = open(sys_cfg_file).read()
|
|
|
|
string = re.sub(r'#start ' + config.uninstall_distro_dir_name + '.*?' + '#end ' + config.uninstall_distro_dir_name + '\s*', '', string, flags=re.DOTALL)
|
|
|
|
config_file = open(sys_cfg_file, "w")
|
|
|
|
config_file.write(string)
|
|
|
|
config_file.close()
|
|
|
|
|
|
|
|
|
2017-04-10 16:00:38 +00:00
|
|
|
def update_grub_cfg_file():
|
|
|
|
"""
|
|
|
|
Main function to remove uninstall distro name from the grub.cfg file.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if platform.system() == 'Linux':
|
|
|
|
os.system('sync')
|
|
|
|
|
|
|
|
grub_cfg_file = os.path.join(config.usb_mount, "multibootusb", "grub", "grub.cfg")
|
|
|
|
if not os.path.exists(grub_cfg_file):
|
|
|
|
gen.log("grub.cfg file not found for updating changes.")
|
|
|
|
else:
|
|
|
|
gen.log("Updating grub.cfg file...")
|
|
|
|
string = open(grub_cfg_file).read()
|
|
|
|
string = re.sub(r'#start ' + config.uninstall_distro_dir_name + '.*?' + '#end ' + config.uninstall_distro_dir_name + '\s*', '', string, flags=re.DOTALL)
|
|
|
|
config_file = open(grub_cfg_file, "w")
|
|
|
|
config_file.write(string)
|
|
|
|
config_file.close()
|
|
|
|
|
|
|
|
|
2016-11-06 11:18:53 +00:00
|
|
|
def uninstall_progress():
|
|
|
|
"""
|
|
|
|
Calculate uninstall progress percentage.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
from . import progressbar
|
|
|
|
usb_details = details(config.usb_disk)
|
|
|
|
usb_mount = usb_details['mount_point']
|
|
|
|
if platform.system() == 'Linux':
|
|
|
|
os.system('sync')
|
|
|
|
|
|
|
|
if os.path.exists(os.path.join(usb_mount, "multibootusb", config.uninstall_distro_dir_name, "multibootusb.cfg")):
|
|
|
|
with open(os.path.join(usb_mount, "multibootusb", config.uninstall_distro_dir_name, "multibootusb.cfg"),
|
|
|
|
"r") as multibootusb_cfg:
|
|
|
|
config.distro = multibootusb_cfg.read().replace('\n', '')
|
|
|
|
else:
|
|
|
|
config.distro = ""
|
2016-12-26 17:53:21 +00:00
|
|
|
gen.log("Installed distro type is " + config.distro)
|
2016-11-06 11:18:53 +00:00
|
|
|
|
|
|
|
if config.distro == "opensuse":
|
|
|
|
if os.path.exists(os.path.join(usb_mount, config.uninstall_distro_dir_name) + ".iso"):
|
|
|
|
folder_size_to_remove = os.path.getsize(os.path.join(usb_mount, config.uninstall_distro_dir_name) + ".iso")
|
|
|
|
else:
|
|
|
|
folder_size_to_remove = 0
|
|
|
|
folder_size_to_remove += disk_usage(str(usb_mount) + "/multibootusb/" + config.uninstall_distro_dir_name).used
|
|
|
|
elif config.distro == "windows" or config.distro == "Windows":
|
2016-11-27 07:03:49 +00:00
|
|
|
if os.path.exists(os.path.join(usb_mount, "SOURCES")):
|
|
|
|
folder_size_to_remove = disk_usage(str(usb_mount) + "/SOURCES").used
|
|
|
|
else:
|
|
|
|
folder_size_to_remove = disk_usage(str(usb_mount) + "/SSTR").used
|
2016-11-06 11:18:53 +00:00
|
|
|
elif config.distro == "ipfire":
|
|
|
|
folder_size_to_remove = disk_usage(str(usb_mount) + "/multibootusb/" + config.uninstall_distro_dir_name).used
|
|
|
|
files = os.listdir(os.path.join(str(usb_mount)))
|
|
|
|
for f in files:
|
|
|
|
if f.endswith('.tlz'):
|
|
|
|
folder_size_to_remove += os.path.getsize(os.path.join(config.usb_mount, f))
|
|
|
|
elif config.distro == "trinity-rescue":
|
|
|
|
folder_size_to_remove = disk_usage(os.path.join(usb_mount, "trk3")).used
|
|
|
|
folder_size_to_remove += disk_usage(usb_mount + "/multibootusb/" + config.uninstall_distro_dir_name).used
|
|
|
|
else:
|
|
|
|
|
|
|
|
folder_size_to_remove = disk_usage(os.path.join(usb_mount, "multibootusb", config.uninstall_distro_dir_name)).used
|
|
|
|
|
|
|
|
thrd = threading.Thread(target=unin_distro, name="uninstall_progress")
|
|
|
|
initial_usb_size = disk_usage(usb_mount).used
|
|
|
|
thrd.start()
|
|
|
|
config.status_text = "Uninstalling " + config.uninstall_distro_dir_name
|
|
|
|
pbar = progressbar.ProgressBar(maxval=100).start() # bar = progressbar.ProgressBar(redirect_stdout=True)
|
|
|
|
while thrd.is_alive():
|
|
|
|
current_size = disk_usage(usb_mount).used
|
|
|
|
diff_size = int(initial_usb_size - current_size)
|
|
|
|
config.percentage = round(float(diff_size) / folder_size_to_remove * 100)
|
|
|
|
if config.percentage > 100:
|
|
|
|
config.percentage = 100
|
|
|
|
|
|
|
|
pbar.update(config.percentage)
|
|
|
|
|
|
|
|
if not thrd.is_alive():
|
|
|
|
config.persistence = 0
|
|
|
|
config.status_text = ""
|