Make code more readable

This commit is contained in:
Dimitris Zlatanidis 2022-05-30 21:52:39 +03:00
parent 6d7ebf9e74
commit 79c619beba

View file

@ -86,8 +86,10 @@ class SBoInstall(BlackList, Utils):
"""Flags initialization """Flags initialization
""" """
for fl in self.flag: for fl in self.flag:
if fl.startswith("--directory-prefix="): if fl.startswith("--directory-prefix="):
self.build_folder = fl.split("=")[1] self.build_folder = fl.split("=")[1]
if not self.build_folder.endswith("/"): if not self.build_folder.endswith("/"):
self.build_folder += "/" self.build_folder += "/"
@ -98,6 +100,7 @@ class SBoInstall(BlackList, Utils):
self.is_upgrade = is_upgrade self.is_upgrade = is_upgrade
self.case_insensitive() self.case_insensitive()
for _sbo in self.slackbuilds: for _sbo in self.slackbuilds:
if _sbo in self.data and _sbo not in self.blacklist: if _sbo in self.data and _sbo not in self.blacklist:
sbo_deps = Requires(self.flag).sbo(_sbo) sbo_deps = Requires(self.flag).sbo(_sbo)
self.deps += sbo_deps self.deps += sbo_deps
@ -105,6 +108,7 @@ class SBoInstall(BlackList, Utils):
self.package_found.append(_sbo) self.package_found.append(_sbo)
else: else:
self.package_not_found.append(_sbo) self.package_not_found.append(_sbo)
self.update_deps() self.update_deps()
if not self.package_found: if not self.package_found:
@ -114,19 +118,23 @@ class SBoInstall(BlackList, Utils):
self.master_packages, mas_src = self.sbo_version_source( self.master_packages, mas_src = self.sbo_version_source(
self.package_found) self.package_found)
self.msg.done() self.msg.done()
if (self.meta.rsl_deps in ["on", "ON"] and if (self.meta.rsl_deps in ["on", "ON"] and
self.flag != "--resolve-off" and not self.match): self.flag != "--resolve-off" and not self.match):
self.msg.resolving() self.msg.resolving()
self.dependencies, dep_src = self.sbo_version_source( self.dependencies, dep_src = self.sbo_version_source(
self.one_for_all(self.deps)) self.one_for_all(self.deps))
if (self.meta.rsl_deps in ["on", "ON"] and if (self.meta.rsl_deps in ["on", "ON"] and
self.flag != "--resolve-off" and not self.match): self.flag != "--resolve-off" and not self.match):
self.msg.done() self.msg.done()
self.clear_masters() self.clear_masters()
if self.package_found: if self.package_found:
if self.match and [""] != self.slackbuilds: if self.match and [""] != self.slackbuilds:
self.msg.matching(self.slackbuilds) self.msg.matching(self.slackbuilds)
else: else:
print("\nThe following packages will be automatically " print("\nThe following packages will be automatically "
"installed or upgraded \nwith new version:\n") "installed or upgraded \nwith new version:\n")
@ -150,6 +158,7 @@ class SBoInstall(BlackList, Utils):
count_total = sum([self.count_ins, self.count_upg, count_total = sum([self.count_ins, self.count_upg,
self.count_uni]) self.count_uni])
if self.match and [""] != self.slackbuilds: if self.match and [""] != self.slackbuilds:
print("\nMatching summary") print("\nMatching summary")
print("=" * 79) print("=" * 79)
@ -164,6 +173,7 @@ class SBoInstall(BlackList, Utils):
f"{self.count_upg} {self.msg.pkg(self.count_upg)}") f"{self.count_upg} {self.msg.pkg(self.count_upg)}")
print(f"will be upgraded.{self.endc}\n") print(f"will be upgraded.{self.endc}\n")
self.continue_to_install() self.continue_to_install()
else: else:
self.msg.not_found(self.is_upgrade) self.msg.not_found(self.is_upgrade)
raise SystemExit() raise SystemExit()
@ -174,9 +184,12 @@ class SBoInstall(BlackList, Utils):
""" """
if "--case-ins" in self.flag: if "--case-ins" in self.flag:
data_dict = self.case_sensitive(self.data) data_dict = self.case_sensitive(self.data)
for name in self.slackbuilds: for name in self.slackbuilds:
index = self.slackbuilds.index(name) index = self.slackbuilds.index(name)
for key, value in data_dict.items(): for key, value in data_dict.items():
if key == name.lower(): if key == name.lower():
self.slackbuilds[index] = value self.slackbuilds[index] = value
@ -195,8 +208,10 @@ class SBoInstall(BlackList, Utils):
""" """
if (self.count_uni > 0 or self.count_upg > 0 or if (self.count_uni > 0 or self.count_upg > 0 or
"--download-only" in self.flag or "--rebuild" in self.flag): "--download-only" in self.flag or "--rebuild" in self.flag):
if self.master_packages and self.msg.answer() in ["y", "Y"]: if self.master_packages and self.msg.answer() in ["y", "Y"]:
installs, upgraded = self.build_install() installs, upgraded = self.build_install()
if "--download-only" in self.flag: if "--download-only" in self.flag:
raise SystemExit() raise SystemExit()
self.msg.reference(installs, upgraded) self.msg.reference(installs, upgraded)
@ -215,6 +230,7 @@ class SBoInstall(BlackList, Utils):
""" """
self.master_packages = self.remove_dbs(self.master_packages) self.master_packages = self.remove_dbs(self.master_packages)
for mas in self.master_packages: for mas in self.master_packages:
if mas in self.dependencies: if mas in self.dependencies:
self.master_packages.remove(mas) self.master_packages.remove(mas)
@ -222,7 +238,9 @@ class SBoInstall(BlackList, Utils):
"""Return found matching SBo packages """Return found matching SBo packages
""" """
for sbo in self.package_not_found: for sbo in self.package_not_found:
for pkg in self.data: for pkg in self.data:
if sbo in pkg and pkg not in self.blacklist: if sbo in pkg and pkg not in self.blacklist:
self.package_found.append(pkg) self.package_found.append(pkg)
@ -230,10 +248,12 @@ class SBoInstall(BlackList, Utils):
"""Create sbo name with version """Create sbo name with version
""" """
sbo_versions, sources = [], [] sbo_versions, sources = [], []
for sbo in slackbuilds: for sbo in slackbuilds:
sbo_ver = f"{sbo}-{SBoGrep(sbo).version()}" sbo_ver = f"{sbo}-{SBoGrep(sbo).version()}"
sbo_versions.append(sbo_ver) sbo_versions.append(sbo_ver)
sources.append(SBoGrep(sbo).source()) sources.append(SBoGrep(sbo).source())
return [sbo_versions, sources] return [sbo_versions, sources]
def one_for_all(self, deps): def one_for_all(self, deps):
@ -248,18 +268,21 @@ class SBoInstall(BlackList, Utils):
# dependencies in order to be installed. # dependencies in order to be installed.
requires = self.dimensional_list(deps) requires = self.dimensional_list(deps)
dependencies = self.remove_dbs(requires) dependencies = self.remove_dbs(requires)
return dependencies return dependencies
def top_view(self): def top_view(self):
"""View top template """View top template
""" """
self.msg.template(78) self.msg.template(78)
print(f"| Packages{' ' * 16}" print(f"| Packages{' ' * 16}"
f"New version{' ' * 8}" f"New version{' ' * 8}"
f"Arch{' ' * 4}" f"Arch{' ' * 4}"
f"Build{' ' * 2}" f"Build{' ' * 2}"
f"Repos{' ' * 10}" f"Repos{' ' * 10}"
f"Size") f"Size")
self.msg.template(78) self.msg.template(78)
def view_packages(self, *args): def view_packages(self, *args):
@ -270,8 +293,10 @@ class SBoInstall(BlackList, Utils):
args[3] arch args[3] arch
""" """
ver = GetFromInstalled(args[1]).version() ver = GetFromInstalled(args[1]).version()
if ver: if ver:
ver = f"-{ver}" ver = f"-{ver}"
print(f" {args[0]}{args[1] + ver} {self.endc}" print(f" {args[0]}{args[1] + ver} {self.endc}"
f"{' ' * (24-len(args[1] + ver))}{args[2]}" f"{' ' * (24-len(args[1] + ver))}{args[2]}"
f"{' ' * (18-len(args[2]))} {args[3]}" f"{' ' * (18-len(args[2]))} {args[3]}"
@ -286,17 +311,22 @@ class SBoInstall(BlackList, Utils):
# split sbo name with version and get name # split sbo name with version and get name
sbo_name = "-".join(sbo.split("-")[:-1]) sbo_name = "-".join(sbo.split("-")[:-1])
find = GetFromInstalled(sbo_name).name() find = GetFromInstalled(sbo_name).name()
if find_package(sbo, self.meta.pkg_path): if find_package(sbo, self.meta.pkg_path):
paint = self.meta.color["GREEN"] paint = self.meta.color["GREEN"]
self.count_ins += 1 self.count_ins += 1
if "--rebuild" in self.flag: if "--rebuild" in self.flag:
self.count_upg += 1 self.count_upg += 1
elif sbo_name == find: elif sbo_name == find:
paint = self.meta.color["YELLOW"] paint = self.meta.color["YELLOW"]
self.count_upg += 1 self.count_upg += 1
else: else:
paint = self.meta.color["RED"] paint = self.meta.color["RED"]
self.count_uni += 1 self.count_uni += 1
return paint return paint
def select_arch(self, src): def select_arch(self, src):
@ -304,9 +334,11 @@ class SBoInstall(BlackList, Utils):
from arch else select arch. from arch else select arch.
""" """
arch = self.arch arch = self.arch
for item in self.unst: for item in self.unst:
if item in src: if item in src:
arch = item arch = item
return arch return arch
def filenames(self, sources): def filenames(self, sources):
@ -320,56 +352,72 @@ class SBoInstall(BlackList, Utils):
""" """
slackbuilds = self.dependencies + self.master_packages slackbuilds = self.dependencies + self.master_packages
installs, upgraded, = [], [] installs, upgraded, = [], []
if not os.path.exists(self.build_folder): if not os.path.exists(self.build_folder):
os.makedirs(self.build_folder) os.makedirs(self.build_folder)
if not os.path.exists(self._SOURCES): if not os.path.exists(self._SOURCES):
os.makedirs(self._SOURCES) os.makedirs(self._SOURCES)
os.chdir(self.build_folder) os.chdir(self.build_folder)
for prgnam in slackbuilds: for prgnam in slackbuilds:
if (self.meta.not_downgrade == "on" and if (self.meta.not_downgrade == "on" and
self.not_downgrade(prgnam) is True): self.not_downgrade(prgnam) is True):
continue continue
pkg = "-".join(prgnam.split("-")[:-1]) pkg = "-".join(prgnam.split("-")[:-1])
installed = "".join(find_package(prgnam, self.meta.pkg_path)) installed = "".join(find_package(prgnam, self.meta.pkg_path))
src_link = SBoGrep(pkg).source().split() src_link = SBoGrep(pkg).source().split()
if (installed and "--download-only" not in self.flag and if (installed and "--download-only" not in self.flag and
"--rebuild" not in self.flag): "--rebuild" not in self.flag):
self.msg.template(78) self.msg.template(78)
self.msg.pkg_found(prgnam) self.msg.pkg_found(prgnam)
self.msg.template(78) self.msg.template(78)
elif self.unst[0] in src_link or self.unst[1] in src_link: elif self.unst[0] in src_link or self.unst[1] in src_link:
self.msg.template(78) self.msg.template(78)
print(f"| Package {prgnam} {self.red}" print(f"| Package {prgnam} {self.red}"
f"{''.join(src_link)}{self.endc}") f"{''.join(src_link)}{self.endc}")
self.msg.template(78) self.msg.template(78)
else: else:
sbo_url = sbo_search_pkg(pkg) sbo_url = sbo_search_pkg(pkg)
sbo_link = SBoLink(sbo_url).tar_gz() sbo_link = SBoLink(sbo_url).tar_gz()
script = sbo_link.split("/")[-1] script = sbo_link.split("/")[-1]
if self.meta.sbosrcarch in ["on", "ON"]: if self.meta.sbosrcarch in ["on", "ON"]:
src_link = list(self.sbosrcarsh(prgnam, sbo_link, src_link = list(self.sbosrcarsh(prgnam, sbo_link,
src_link)) src_link))
Download(self.build_folder, sbo_link.split(), Download(self.build_folder, sbo_link.split(),
repo="sbo").start() repo="sbo").start()
Download(self._SOURCES, src_link, repo="sbo").start() Download(self._SOURCES, src_link, repo="sbo").start()
if "--download-only" in self.flag: if "--download-only" in self.flag:
continue continue
sources = list(self.filenames(src_link)) sources = list(self.filenames(src_link))
BuildPackage(script, sources, self.build_folder, BuildPackage(script, sources, self.build_folder,
auto=False).build() auto=False).build()
binary = slack_package(prgnam) binary = slack_package(prgnam)
if os.path.isfile("".join(binary)): if os.path.isfile("".join(binary)):
if GetFromInstalled(pkg).name() == pkg: if GetFromInstalled(pkg).name() == pkg:
print(f"[ {self.yellow}Upgrading{self.endc} ] --> " print(f"[ {self.yellow}Upgrading{self.endc} ] --> "
f"{prgnam}") f"{prgnam}")
upgraded.append(prgnam) upgraded.append(prgnam)
else: else:
print(f"[ {self.green}Installing{self.endc} ] --> " print(f"[ {self.green}Installing{self.endc} ] --> "
f"{prgnam}") f"{prgnam}")
installs.append(prgnam) installs.append(prgnam)
if ("--rebuild" in self.flag and if ("--rebuild" in self.flag and
GetFromInstalled(pkg).name() == pkg): GetFromInstalled(pkg).name() == pkg):
PackageManager(binary).upgrade(flag="--reinstall") PackageManager(binary).upgrade(flag="--reinstall")
else: else:
PackageManager(binary).upgrade(flag="--install-new") PackageManager(binary).upgrade(flag="--install-new")
return installs, upgraded return installs, upgraded
@ -380,8 +428,10 @@ class SBoInstall(BlackList, Utils):
name = "-".join(prgnam.split("-")[:-1]) name = "-".join(prgnam.split("-")[:-1])
sbo_ver = prgnam.split("-")[-1] sbo_ver = prgnam.split("-")[-1]
ins_ver = GetFromInstalled(name).version() ins_ver = GetFromInstalled(name).version()
if not ins_ver: if not ins_ver:
ins_ver = "0" ins_ver = "0"
if parse_version(sbo_ver) < parse_version(ins_ver): if parse_version(sbo_ver) < parse_version(ins_ver):
self.msg.template(78) self.msg.template(78)
print(f"| Package {name} does not downgrade, " print(f"| Package {name} does not downgrade, "
@ -393,6 +443,7 @@ class SBoInstall(BlackList, Utils):
"""Alternative repository for sbo sources""" """Alternative repository for sbo sources"""
name = "-".join(prgnam.split("-")[:-1]) name = "-".join(prgnam.split("-")[:-1])
category = f"{sbo_link.split('/')[-2]}/{name}/" category = f"{sbo_link.split('/')[-2]}/{name}/"
for link in src_link: for link in src_link:
source = link.split("/")[-1] source = link.split("/")[-1]
yield f"{self.meta.sbosrcarch_link}{category}{source}" yield f"{self.meta.sbosrcarch_link}{category}{source}"