Refactory of the code

This commit is contained in:
Domenico 2022-08-31 16:24:18 +02:00 committed by GitHub
parent 069a5f04db
commit 1ae7be08de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,13 +12,13 @@ if os.path.exists(APP_DIR):
print(f"{APP_DIR}/ already exists. Please remove it then run the script again.") print(f"{APP_DIR}/ already exists. Please remove it then run the script again.")
sys.exit(1) sys.exit(1)
print() print("\nConvert any Arch linux package (official/AUR) to AppImage!!")
print("Convert any Arch linux package (official/AUR) to AppImage!!")
utils = Utils() utils = Utils()
main_pkg = None main_pkg = None
while True: while True:
main_pkg = utils.user_text("Enter the name of the package (leave empty to quit)") main_pkg = utils.user_text(
"Enter the name of the package (leave empty to quit)")
if not main_pkg: if not main_pkg:
print("Exiting...") print("Exiting...")
@ -29,9 +29,7 @@ while True:
if main_pkg_url: if main_pkg_url:
break break
print("Package not found. Please check the name and try again") print("Package not found. Please check the name and try again\n")
print()
print() print()
os.mkdir(APP_DIR) os.mkdir(APP_DIR)
@ -40,17 +38,20 @@ utils.download(main_pkg_url, "download", main_pkg)
utils.extract_zst("download", APP_DIR) utils.extract_zst("download", APP_DIR)
utils.rm("download") utils.rm("download")
desktop_file = glob.glob(os.path.join(APP_DIR, "usr/share/applications/*.desktop")) desktop_file = glob.glob(os.path.join(
APP_DIR, "usr/share/applications/*.desktop"))
if not desktop_file: if not desktop_file:
print("No .desktop file was found in the package.") print("No .desktop file was found in the package.")
desktop_file = utils.user_path("Please enter the path to a .desktop file", required=True) desktop_file = utils.user_path(
"Please enter the path to a .desktop file", required=True)
elif len(desktop_file) == 1: elif len(desktop_file) == 1:
desktop_file = desktop_file[0] desktop_file = desktop_file[0]
else: else:
desktop_file = utils.user_select("Select the .desktop file to be used", desktop_file) desktop_file = utils.user_select(
"Select the .desktop file to be used", desktop_file)
while True: while True:
@ -58,44 +59,42 @@ while True:
if vfd: if vfd:
break break
print() print(f"\n.desktop file validation failed due to the error below:\n{msg}")
print(".desktop file validation failed due to the error below:") desktop_file = utils.user_path(
print(msg) "Please enter the path to a valid .desktop file", required=True)
desktop_file = utils.user_path("Please enter the path to a valid .desktop file", required=True)
utils.copy_file(desktop_file, APP_DIR) utils.copy_file(desktop_file, APP_DIR)
with open(desktop_file, "r") as f: with open(desktop_file, "r") as f:
icon_name = re.findall('Icon=(.*)', f.read()) icon_name = re.findall('Icon=(.*)', f.read())
if icon_name: icon_name = icon_name[0] if icon_name else None
icon_name = icon_name[0]
else:
icon_name = None
icon_file = None icon_file = None
if not icon_name: if not icon_name:
print("The .desktop file doesn't have an Icon attribute") print("The .desktop file doesn't have an Icon attribute")
icon_file = utils.user_path("Please enter the path to the icon file to be used", required=True) icon_file = utils.user_path(
"Please enter the path to the icon file to be used", required=True)
if icon_file is not None: if icon_file is not None:
icon_name = ".".join(icon_file.split("/")[-1].split(".")[:-1]) icon_name = ".".join(icon_file.split("/")[-1].split(".")[:-1])
utils.set_icon_desktop_file(desktop_file, icon_name) utils.set_icon_desktop_file(desktop_file, icon_name)
else: else:
i_files = os.path.join(APP_DIR, f"usr/share/icons/**/*.*") i_files = os.path.join(APP_DIR, "usr/share/icons/**/*.*")
icon_file = glob.glob(i_files, recursive=True) icon_file = glob.glob(i_files, recursive=True)
if not icon_file: if not icon_file:
print(f"No icon file was found in {APP_DIR}/usr/share/icons/") print(f"No icon file was found in {APP_DIR}/usr/share/icons/")
icon_file = utils.user_path("Please enter the path to the icon file to be used", required=True) icon_file = utils.user_path(
"Please enter the path to the icon file to be used", required=True)
elif len(icon_file) == 1: elif len(icon_file) == 1:
icon_file = icon_file[0] icon_file = icon_file[0]
else: else:
icon_file = utils.user_select("Please select the icon file to be used", sorted(icon_file)) icon_file = utils.user_select(
"Please select the icon file to be used", sorted(icon_file))
utils.copy_file(icon_file, APP_DIR) utils.copy_file(icon_file, APP_DIR)
@ -124,24 +123,20 @@ while True:
if pkgs[pkg]: if pkgs[pkg]:
continue continue
url = utils.get_pkg(pkg) if url := utils.get_pkg(pkg):
if url:
pkgs[pkg] = url pkgs[pkg] = url
else: else:
not_found.append(pkg) not_found.append(pkg)
print() print("\nThese packages (and their dependencies) will be downloaded:")
print("These packages (and their dependencies) will be downloaded:")
for i, p in enumerate(pkgs): for i, p in enumerate(pkgs):
if pkgs[p]: if pkgs[p]:
print(f"{i+1}. {p}") print(f"{i+1}. {p}")
if not_found: if not_found:
print() print("\nThese packages could not be found: " + " ".join(not_found))
print("These packages could not be found: " + " ".join(not_found))
new_pkgs = utils.user_text("If you would like to add additional packages " + new_pkgs = utils.user_text("If you would like to add additional packages " +
"please enter them below (space seperated). Leave empty to start downloading") "please enter them below (space seperated). Leave empty to start downloading")
@ -205,23 +200,20 @@ while True:
if pkgs[pkg]: if pkgs[pkg]:
continue continue
url = utils.get_pkg(pkg) if url := utils.get_pkg(pkg):
if url:
pkgs[pkg] = url pkgs[pkg] = url
else: else:
not_found.append(pkg) not_found.append(pkg)
print() print("\nThese packages will be downloaded:")
print("These packages will be downloaded:")
for i, p in enumerate(pkgs): for i, p in enumerate(pkgs):
if pkgs[p]: if pkgs[p]:
print(f"{i+1}. {p}") print(f"{i+1}. {p}")
if not_found: if not_found:
print() print("\nThese packages could not be found: " + " ".join(not_found))
print("These packages could not be found: " + " ".join(not_found))
if pkgs: if pkgs:
for name, url in pkgs.items(): for name, url in pkgs.items():
@ -239,8 +231,7 @@ latest AppImageTool? If you select No the existing one will be used."):
utils.make_executable(appimagetool) utils.make_executable(appimagetool)
while True: while True:
print("Running AppImageTool...") print("Running AppImageTool...\n")
print()
if not os.path.exists(OUT_DIR): if not os.path.exists(OUT_DIR):
os.mkdir(OUT_DIR) os.mkdir(OUT_DIR)