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