From 70ce59db9c05724181f0994729d723ca743b1b8f Mon Sep 17 00:00:00 2001 From: Dimitris Zlatanidis Date: Sat, 31 Jan 2015 02:47:36 +0200 Subject: [PATCH] update utils to class --- slpkg/binary/dependency.py | 4 +-- slpkg/binary/greps.py | 6 ++-- slpkg/binary/install.py | 15 ++++----- slpkg/binary/repo_init.py | 4 +-- slpkg/binary/search.py | 8 ++--- slpkg/blacklist.py | 4 +-- slpkg/config.py | 4 +-- slpkg/desc.py | 4 +-- slpkg/pkg/manager.py | 16 +++++----- slpkg/queue.py | 4 +-- slpkg/repoinfo.py | 11 ++++--- slpkg/repositories.py | 6 ++-- slpkg/sbo/greps.py | 4 +-- slpkg/sbo/search.py | 7 ++-- slpkg/sbo/slackbuild.py | 13 +++----- slpkg/tracking.py | 14 +++----- slpkg/utils.py | 65 +++++++++++++++++++------------------- 17 files changed, 88 insertions(+), 101 deletions(-) diff --git a/slpkg/binary/dependency.py b/slpkg/binary/dependency.py index 757c8efc..78cf9589 100644 --- a/slpkg/binary/dependency.py +++ b/slpkg/binary/dependency.py @@ -23,7 +23,7 @@ import sys -from slpkg.utils import package_name +from slpkg.utils import Utils from slpkg.toolbar import status from slpkg.blacklist import BlackList @@ -35,7 +35,7 @@ class Dependencies(object): def __init__(self, PACKAGES_TXT, repo): self.repo = repo self.dep_results = [] - self.packages = package_name(PACKAGES_TXT, repo) + self.packages = Utils().package_name(PACKAGES_TXT, repo) def binary(self, name): ''' diff --git a/slpkg/binary/greps.py b/slpkg/binary/greps.py index 86dd46c1..8467aa36 100644 --- a/slpkg/binary/greps.py +++ b/slpkg/binary/greps.py @@ -23,8 +23,8 @@ import os +from slpkg.utils import Utils from slpkg.toolbar import status -from slpkg.utils import read_file from slpkg.splitting import split_package from slpkg.slack.slack_version import slack_ver from slpkg.__metadata__ import MetaData as _m @@ -162,7 +162,7 @@ def fix_slackers_pkg(name): name in PACKAGES.TXT file use FILELIST.TXT to get. ''' - FILELIST_TXT = read_file(_m.lib_path + 'slackr_repo/FILELIST.TXT') + FILELIST_TXT = Utils().read_file(_m.lib_path + 'slackr_repo/FILELIST.TXT') for line in FILELIST_TXT.splitlines(): if name in line and line.endswith(".txz"): return line.split("/")[-1].strip() @@ -198,7 +198,7 @@ class Requires(object): else: return "" else: - PACKAGES_TXT = read_file('{0}{1}_repo/PACKAGES.TXT'.format( + PACKAGES_TXT = Utils().read_file('{0}{1}_repo/PACKAGES.TXT'.format( _m.lib_path, self.repo)) for line in PACKAGES_TXT.splitlines(): if line.startswith("PACKAGE NAME:"): diff --git a/slpkg/binary/install.py b/slpkg/binary/install.py index 2f7c8787..fab934e3 100644 --- a/slpkg/binary/install.py +++ b/slpkg/binary/install.py @@ -24,6 +24,7 @@ import os import sys +from slpkg.utils import Utils from slpkg.sizes import units from slpkg.messages import Msg from slpkg.remove import delete @@ -33,10 +34,6 @@ from slpkg.blacklist import BlackList from slpkg.downloader import Download from slpkg.grep_md5 import pkg_checksum from slpkg.splitting import split_package -from slpkg.utils import ( - remove_dbs, - dimensional_list -) from slpkg.__metadata__ import MetaData as _m from slpkg.pkg.find import find_package @@ -149,7 +146,7 @@ class BinaryInstall(object): or if added to install two or more times ''' packages = [] - for mas in remove_dbs(self.packages): + for mas in Utils().remove_dbs(self.packages): if mas not in self.dependencies: packages.append(mas) return packages @@ -205,11 +202,11 @@ class BinaryInstall(object): Msg().resolving() for dep in self.packages: dependencies = [] - dependencies = dimensional_list(Dependencies(self.PACKAGES_TXT, - self.repo).binary(dep)) + dependencies = Utils().dimensional_list(Dependencies( + self.PACKAGES_TXT, self.repo).binary(dep)) requires += dependencies - self.deps_dict[dep] = remove_dbs(dependencies) - return remove_dbs(requires) + self.deps_dict[dep] = Utils().remove_dbs(dependencies) + return Utils().remove_dbs(requires) def view_version(self, packages): ''' diff --git a/slpkg/binary/repo_init.py b/slpkg/binary/repo_init.py index d6133e46..98d0bed2 100644 --- a/slpkg/binary/repo_init.py +++ b/slpkg/binary/repo_init.py @@ -23,7 +23,7 @@ import os -from slpkg.utils import read_file +from slpkg.utils import Utils from slpkg.repositories import Repo from slpkg.__metadata__ import MetaData as _m @@ -46,7 +46,7 @@ class RepoInit(object): else: exec('self._init_custom()') self.lib = _m.lib_path + "{0}_repo/PACKAGES.TXT".format(self.repo) - PACKAGES_TXT = read_file(self.lib) + PACKAGES_TXT = Utils().read_file(self.lib) return PACKAGES_TXT, self.mirror def _init_custom(self): diff --git a/slpkg/binary/search.py b/slpkg/binary/search.py index 68e70445..2a836793 100644 --- a/slpkg/binary/search.py +++ b/slpkg/binary/search.py @@ -23,12 +23,10 @@ import sys +from slpkg.utils import Utils from slpkg.toolbar import status from slpkg.blacklist import BlackList from slpkg.splitting import split_package -from slpkg.utils import ( - read_file -) from slpkg.__metadata__ import MetaData as _m @@ -40,8 +38,8 @@ def search_pkg(name, repo): try: blacklist = BlackList().packages() toolbar_width, index = 2, 0 - PACKAGES_TXT = read_file(_m.lib_path + '{0}_repo/PACKAGES.TXT'.format( - repo)) + PACKAGES_TXT = Utils().read_file(_m.lib_path + '{0}_repo/' + 'PACKAGES.TXT'.format(repo)) for line in PACKAGES_TXT.splitlines(): index += 1 toolbar_width = status(index, toolbar_width, 1400) diff --git a/slpkg/blacklist.py b/slpkg/blacklist.py index 82aaa5ef..1eb5f7f8 100644 --- a/slpkg/blacklist.py +++ b/slpkg/blacklist.py @@ -21,7 +21,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from utils import read_file +from utils import Utils from __metadata__ import MetaData as _m @@ -33,7 +33,7 @@ class BlackList(object): def __init__(self): self.quit = False self.blackfile = "/etc/slpkg/blacklist" - self.black_conf = read_file(self.blackfile) + self.black_conf = Utils().read_file(self.blackfile) def packages(self): ''' diff --git a/slpkg/config.py b/slpkg/config.py index 37de053b..3d06c433 100644 --- a/slpkg/config.py +++ b/slpkg/config.py @@ -23,7 +23,7 @@ import subprocess -from utils import read_file +from utils import Utils from __metadata__ import MetaData as _m @@ -54,7 +54,7 @@ class Config(object): 'USE_COLORS', 'WGET_OPTION' ] - read_conf = read_file(self.config_file) + read_conf = Utils().read_file(self.config_file) for line in read_conf.splitlines(): if not line.startswith("#") and line.split("=")[0] in conf_args: print(line) diff --git a/slpkg/desc.py b/slpkg/desc.py index 12007ef6..61d6293f 100644 --- a/slpkg/desc.py +++ b/slpkg/desc.py @@ -22,8 +22,8 @@ # along with this program. If not, see . +from utils import Utils from messages import Msg -from utils import read_file from __metadata__ import MetaData as _m @@ -51,7 +51,7 @@ class PkgDesc(object): self.repo) def view(self): - PACKAGES_TXT = read_file(self.lib) + PACKAGES_TXT = Utils().read_file(self.lib) print("") # new line at start count = 0 if self.repo != "sbo": diff --git a/slpkg/pkg/manager.py b/slpkg/pkg/manager.py index a9334423..d4093602 100644 --- a/slpkg/pkg/manager.py +++ b/slpkg/pkg/manager.py @@ -25,8 +25,8 @@ import os import sys import subprocess +from slpkg.utils import Utils from slpkg.messages import Msg -from slpkg.utils import read_file from slpkg.__metadata__ import MetaData as _m from slpkg.pkg.find import find_package @@ -160,7 +160,7 @@ class PackageManager(object): ''' View dependencies for before remove ''' - dependencies = read_file(path + package) + dependencies = Utils().read_file(path + package) print("") # new line at start Msg().template(78) print("| Found dependencies for package {0}:".format(package)) @@ -224,7 +224,7 @@ class PackageManager(object): matching += 1 print("[ {0}installed{1} ] - {2}".format( _m.color['GREEN'], _m.color['ENDC'], match)) - data = read_file(_m.pkg_path + match) + data = Utils().read_file(_m.pkg_path + match) for line in data.splitlines(): if line.startswith("UNCOMPRESSED PACKAGE SIZE:"): if "M" in line[26:]: @@ -252,7 +252,7 @@ class PackageManager(object): for pkg in self.binary: find = find_package(pkg + _m.sp, _m.pkg_path) if find: - package = read_file(_m.pkg_path + "".join(find)) + package = Utils().read_file(_m.pkg_path + "".join(find)) for line in package.splitlines(): print(line).strip() print("") # new line per file @@ -275,13 +275,13 @@ class PackageManager(object): if repo == 'sbo': if (os.path.isfile( _m.lib_path + '{0}_repo/SLACKBUILDS.TXT'.format(repo))): - r = read_file(_m.lib_path + '{0}_repo/' - 'SLACKBUILDS.TXT'.format(repo)) + r = Utils().read_file(_m.lib_path + '{0}_repo/' + 'SLACKBUILDS.TXT'.format(repo)) else: if os.path.isfile(_m.lib_path + '{0}_repo/PACKAGES.TXT'.format( repo)): - r = read_file(_m.lib_path + '{0}_repo/PACKAGES.TXT'.format( - repo)) + r = Utils().read_file(_m.lib_path + '{0}_repo/' + 'PACKAGES.TXT'.format(repo)) for line in r.splitlines(): if repo == 'sbo': if line.startswith("SLACKBUILD NAME: "): diff --git a/slpkg/queue.py b/slpkg/queue.py index 637e3d45..87ecba5d 100644 --- a/slpkg/queue.py +++ b/slpkg/queue.py @@ -24,7 +24,7 @@ import os from collections import OrderedDict -from utils import read_file +from utils import Utils from downloader import Download from __metadata__ import MetaData as _m @@ -59,7 +59,7 @@ class QueuePkgs(object): for line in queue_file: queue.write(line) queue.close() - self.queued = read_file(self.queue_list) + self.queued = Utils().read_file(self.queue_list) def packages(self): ''' diff --git a/slpkg/repoinfo.py b/slpkg/repoinfo.py index 89d9c88e..d4ce81bf 100644 --- a/slpkg/repoinfo.py +++ b/slpkg/repoinfo.py @@ -25,7 +25,7 @@ import os import sys from sizes import units -from utils import read_file +from utils import Utils from repositories import Repo from repolist import RepoList from __metadata__ import MetaData as _m @@ -77,11 +77,11 @@ class RepoInfo(object): 'SLACKBUILDS.TXT'.format(repo))): status = '{0}enabled{1}'.format(_m.color['GREEN'], _m.color['ENDC']) sum_sbo_pkgs = 0 - for line in (read_file( + for line in (Utils().read_file( _m.lib_path + 'sbo_repo/SLACKBUILDS.TXT').splitlines()): if line.startswith('SLACKBUILD NAME: '): sum_sbo_pkgs += 1 - changelog_txt = read_file(_m.log_path + 'sbo/ChangeLog.txt') + changelog_txt = Utils().read_file(_m.log_path + 'sbo/ChangeLog.txt') last_upd = changelog_txt.split('\n', 1)[0] self.form['Repo id:'] = repo self.form['Repo url:'] = self.all_repos[repo] @@ -101,7 +101,7 @@ class RepoInfo(object): Grap data packages ''' sum_pkgs, size, unsize, last_upd = 0, [], [], '' - for line in (read_file( + for line in (Utils().read_file( _m.lib_path + repo + '_repo/PACKAGES.TXT').splitlines()): if line.startswith('PACKAGES.TXT;'): last_upd = line[14:].strip() @@ -112,6 +112,7 @@ class RepoInfo(object): if line.startswith('PACKAGE SIZE (uncompressed): '): unsize.append(line[30:-2].strip()) if repo in ['salix', 'slackl']: - log = read_file(_m.log_path + '{0}/ChangeLog.txt'.format(repo)) + log = Utils().read_file(_m.log_path + '{0}/ChangeLog.txt'.format( + repo)) last_upd = log.split('\n', 1)[0] return [sum_pkgs, size, unsize, last_upd] diff --git a/slpkg/repositories.py b/slpkg/repositories.py index f8251b83..6de700e9 100644 --- a/slpkg/repositories.py +++ b/slpkg/repositories.py @@ -25,7 +25,7 @@ import os import sys -from utils import read_file +from utils import Utils from __metadata__ import MetaData as _m @@ -33,7 +33,7 @@ class Repo(object): def __init__(self): self.repo_file = "/etc/slpkg/custom-repositories" - self.repositories_list = read_file(self.repo_file) + self.repositories_list = Utils().read_file(self.repo_file) def add(self, repo, url): ''' @@ -102,7 +102,7 @@ class Repo(object): ''' default = "http://mirrors.slackware.com/slackware/" if os.path.isfile("/etc/slpkg/slackware-mirrors"): - mirrors = read_file(_m.conf_path + 'slackware-mirrors') + mirrors = Utils().read_file(_m.conf_path + 'slackware-mirrors') for line in mirrors.splitlines(): line = line.rstrip() if not line.startswith("#") and line: diff --git a/slpkg/sbo/greps.py b/slpkg/sbo/greps.py index 679f00fb..430add22 100644 --- a/slpkg/sbo/greps.py +++ b/slpkg/sbo/greps.py @@ -21,7 +21,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from slpkg.utils import read_file +from slpkg.utils import Utils from slpkg.__metadata__ import MetaData as _m @@ -43,7 +43,7 @@ class SBoGrep(object): self.sbo_txt = _m.lib_path + "sbo_repo/SLACKBUILDS.TXT" self.answer = ['y', 'Y'] self.unst = ['UNSUPPORTED', 'UNTESTED'] - self.SLACKBUILDS_TXT = read_file(self.sbo_txt) + self.SLACKBUILDS_TXT = Utils().read_file(self.sbo_txt) def source(self): ''' diff --git a/slpkg/sbo/search.py b/slpkg/sbo/search.py index e1fff3ff..817cc64d 100644 --- a/slpkg/sbo/search.py +++ b/slpkg/sbo/search.py @@ -23,12 +23,10 @@ import sys +from slpkg.utils import Utils from slpkg.repositories import Repo from slpkg.blacklist import BlackList from slpkg.__metadata__ import MetaData as _m -from slpkg.utils import ( - read_file -) from slpkg.slack.slack_version import slack_ver @@ -41,7 +39,8 @@ def sbo_search_pkg(name): repo = Repo().sbo() blacklist = BlackList().packages() sbo_url = "{0}{1}/".format(repo, slack_ver()) - SLACKBUILDS_TXT = read_file(_m.lib_path + "sbo_repo/SLACKBUILDS.TXT") + SLACKBUILDS_TXT = Utils().read_file( + _m.lib_path + "sbo_repo/SLACKBUILDS.TXT") for line in SLACKBUILDS_TXT.splitlines(): if line.startswith("SLACKBUILD LOCATION"): sbo_name = (line[23:].split("/")[-1].replace("\n", "")).strip() diff --git a/slpkg/sbo/slackbuild.py b/slpkg/sbo/slackbuild.py index e14f93d1..70f10b19 100644 --- a/slpkg/sbo/slackbuild.py +++ b/slpkg/sbo/slackbuild.py @@ -25,15 +25,12 @@ import os import sys +from slpkg.utils import Utils +from slpkg.messages import Msg from slpkg.toolbar import status from slpkg.log_deps import write_deps from slpkg.downloader import Download from slpkg.splitting import split_package -from slpkg.utils import ( - dimensional_list, - remove_dbs -) -from slpkg.messages import Msg from slpkg.__metadata__ import MetaData as _m from slpkg.pkg.find import find_package @@ -144,7 +141,7 @@ class SBoInstall(object): or if added to install two or more times ''' slackbuilds = [] - for mas in remove_dbs(self.master_packages): + for mas in Utils().remove_dbs(self.master_packages): if mas not in self.dependencies: slackbuilds.append(mas) return slackbuilds @@ -183,11 +180,11 @@ class SBoInstall(object): Thus creating this loop create one-dimensional list. ''' requires, dependencies = [], [] - requires = dimensional_list(deps) + requires = Utils().dimensional_list(deps) # Inverting the list brings the # dependencies in order to be installed. requires.reverse() - dependencies = remove_dbs(requires) + dependencies = Utils().remove_dbs(requires) return dependencies def top_view(self): diff --git a/slpkg/tracking.py b/slpkg/tracking.py index b20f1260..006672ef 100644 --- a/slpkg/tracking.py +++ b/slpkg/tracking.py @@ -22,11 +22,7 @@ # along with this program. If not, see . -from utils import ( - read_file, - remove_dbs, - dimensional_list -) +from utils import Utils from messages import Msg from __metadata__ import MetaData as _m @@ -51,16 +47,16 @@ def track_dep(name, repo): dependencies_list = Requires().sbo(name) find_pkg = sbo_search_pkg(name) else: - PACKAGES_TXT = read_file(_m.lib_path + '{0}_repo/PACKAGES.TXT'.format( - repo)) + PACKAGES_TXT = Utils().read_file(_m.lib_path + '{0}_repo/' + 'PACKAGES.TXT'.format(repo)) dependencies_list = Dependencies(PACKAGES_TXT, repo).binary(name) find_pkg = search_pkg(name, repo) Msg().done() if find_pkg: requires, dependencies = [], [] - requires = dimensional_list(dependencies_list) + requires = Utils().dimensional_list(dependencies_list) requires.reverse() - dependencies = remove_dbs(requires) + dependencies = Utils().remove_dbs(requires) if dependencies == []: dependencies = ["No dependencies"] pkg_len = len(name) + 24 diff --git a/slpkg/utils.py b/slpkg/utils.py index f4b9b540..e388889c 100644 --- a/slpkg/utils.py +++ b/slpkg/utils.py @@ -25,40 +25,39 @@ from splitting import split_package -def dimensional_list(lists): - """ Create one dimensional list """ - one_list = [] - for lst in lists: - one_list += lst - return one_list +class Utils(object): + def dimensional_list(self, lists): + """ Create one dimensional list """ + one_list = [] + for lst in lists: + one_list += lst + return one_list -def remove_dbs(double): - """ Remove douuble item from list """ - one = [] - for dup in double: - if dup not in one: - one.append(dup) - return one + def remove_dbs(self, double): + """ Remove douuble item from list """ + one = [] + for dup in double: + if dup not in one: + one.append(dup) + return one + def read_file(self, registry): + """ Return reading file """ + with open(registry, "r") as file_txt: + read_file = file_txt.read() + file_txt.close() + return read_file -def read_file(registry): - """ Return reading file """ - with open(registry, "r") as file_txt: - read_file = file_txt.read() - file_txt.close() - return read_file - - -def package_name(PACKAGES_TXT, repo): - """ Return PACKAGE NAME """ - packages = [] - for line in PACKAGES_TXT.splitlines(): - # index += 1 - # toolbar_width = status(index, toolbar_width, step) - if line.startswith("PACKAGE NAME:"): - if repo == "slackr": - packages.append(line[14:].strip()) - else: - packages.append(split_package(line[14:].strip())[0]) - return packages + def package_name(self, PACKAGES_TXT, repo): + """ Return PACKAGE NAME """ + packages = [] + for line in PACKAGES_TXT.splitlines(): + # index += 1 + # toolbar_width = status(index, toolbar_width, step) + if line.startswith("PACKAGE NAME:"): + if repo == "slackr": + packages.append(line[14:].strip()) + else: + packages.append(split_package(line[14:].strip())[0]) + return packages