def __init__(self, pkg, do_format=True, custom_format=None): self._pkg = None self._do_format = do_format self._str = None self._location = None if not custom_format: if do_format: custom_format = self._tmpl_verbose else: custom_format = self._tmpl_quiet self.tmpl = Template(custom_format) self.format_vars = LazyItemsDict() self.pkg = pkg
class PackageFormatter(object): """When applied to a L{gentoolkit.package.Package} object, determine the location (Portage Tree vs. overlay), install status and masked status. That information can then be easily formatted and displayed. Example usage: >>> from gentoolkit.query import Query >>> from gentoolkit.package import PackageFormatter >>> import portage.output >>> q = Query('gcc') >>> pkgs = [PackageFormatter(x) for x in q.find()] >>> havecolor = portage.output.havecolor >>> portage.output.havecolor = False >>> for pkg in pkgs: ... # Only print packages that are installed and from the Portage ... # tree ... if set('IP').issubset(pkg.location): ... print(pkg) ... [IP-] [ ] sys-devel/gcc-4.5.4:4.5 >>> portage.output.havecolor = havecolor @type pkg: L{gentoolkit.package.Package} @param pkg: package to format @type do_format: bool @param do_format: Whether to format the package name or not. Essentially C{do_format} should be set to False when piping or when quiet output is desired. If C{do_format} is False, only the location attribute will be created to save time. """ _tmpl_verbose = "[$location] [$mask] $cpv:$slot" _tmpl_quiet = "$cpv" def __init__(self, pkg, do_format=True, custom_format=None): self._pkg = None self._do_format = do_format self._str = None self._location = None if not custom_format: if do_format: custom_format = self._tmpl_verbose else: custom_format = self._tmpl_quiet self.tmpl = Template(custom_format) self.format_vars = LazyItemsDict() self.pkg = pkg def __repr__(self): return "<%s %s @%#8x>" % (self.__class__.__name__, self.pkg, id(self)) def __str__(self): if self._str is None: self._str = self.tmpl.safe_substitute(self.format_vars) return self._str @property def location(self): if self._location is None: self._location = self.format_package_location() return self._location @property def pkg(self): """Package to format""" return self._pkg @pkg.setter def pkg(self, value): if self._pkg == value: return self._pkg = value self._location = None fmt_vars = self.format_vars self.format_vars.clear() fmt_vars.addLazySingleton("location", lambda: getattr(self, "location")) fmt_vars.addLazySingleton("mask", self.format_mask) fmt_vars.addLazySingleton("mask2", self.format_mask_status2) fmt_vars.addLazySingleton("cpv", self.format_cpv) fmt_vars.addLazySingleton("cp", self.format_cpv, "cp") fmt_vars.addLazySingleton("category", self.format_cpv, "category") fmt_vars.addLazySingleton("name", self.format_cpv, "name") fmt_vars.addLazySingleton("version", self.format_cpv, "version") fmt_vars.addLazySingleton("revision", self.format_cpv, "revision") fmt_vars.addLazySingleton("fullversion", self.format_cpv, "fullversion") fmt_vars.addLazySingleton("slot", self.format_slot) fmt_vars.addLazySingleton("repo", self.pkg.repo_name) fmt_vars.addLazySingleton("keywords", self.format_keywords) def format_package_location(self): """Get the install status (in /var/db/?) and origin (from an overlay and the Portage tree?). @rtype: str @return: one of: 'I--' : Installed but ebuild doesn't exist on system anymore '-P-' : Not installed and from the Portage tree '--O' : Not installed and from an overlay 'IP-' : Installed and from the Portage tree 'I-O' : Installed and from an overlay """ result = ['-', '-', '-'] if self.pkg.is_installed(): result[0] = 'I' overlay = self.pkg.is_overlay() if overlay is None: pass elif overlay: result[2] = 'O' else: result[1] = 'P' return ''.join(result) def format_mask_status(self): """Get the mask status of a given package. @rtype: tuple: (int, list) @return: int = an index for this list: [" ", " ~", " -", "M ", "M~", "M-", "??"] 0 = not masked 1 = keyword masked 2 = arch masked 3 = hard masked 4 = hard and keyword masked, 5 = hard and arch masked 6 = ebuild doesn't exist on system anymore list = original output of portage.getmaskingstatus """ result = 0 masking_status = self.pkg.mask_status() if masking_status is None: return (6, []) if ("~%s keyword" % self.pkg.settings("ARCH")) in masking_status: result += 1 if "missing keyword" in masking_status: result += 2 if set(('profile', 'package.mask')).intersection(masking_status): result += 3 return (result, masking_status) def format_mask_status2(self): """Get the mask status of a given package. """ mask = self.pkg.mask_status() if mask: return pp.masking(mask) else: arch = self.pkg.settings("ARCH") keywords = self.pkg.environment('KEYWORDS') mask = [ determine_keyword(arch, portage.settings["ACCEPT_KEYWORDS"], keywords) ] return pp.masking(mask) def format_mask(self): maskmodes = [' ', ' ~', ' -', 'M ', 'M~', 'M-', '??'] maskmode = maskmodes[self.format_mask_status()[0]] return pp.keyword(maskmode, stable=not maskmode.strip(), hard_masked=set( ('M', '?', '-')).intersection(maskmode)) def format_cpv(self, attr=None): if attr is None: value = self.pkg.cpv else: value = getattr(self.pkg, attr) if self._do_format: return pp.cpv(value) else: return value def format_slot(self): value = self.pkg.environment("SLOT") if self._do_format: return pp.slot(value) else: return value def format_keywords(self): value = self.pkg.environment("KEYWORDS") if self._do_format: return pp.keyword(value) else: return value
class PackageFormatter(object): """When applied to a L{gentoolkit.package.Package} object, determine the location (Portage Tree vs. overlay), install status and masked status. That information can then be easily formatted and displayed. Example usage: >>> from gentoolkit.query import Query >>> from gentoolkit.package import PackageFormatter >>> import portage.output >>> q = Query('gcc') >>> pkgs = [PackageFormatter(x) for x in q.find()] >>> havecolor = portage.output.havecolor >>> portage.output.havecolor = False >>> for pkg in pkgs: ... # Only print packages that are installed and from the Portage ... # tree ... if set('IP').issubset(pkg.location): ... print(pkg) ... [IP-] [ ] sys-devel/gcc-4.5.4:4.5 >>> portage.output.havecolor = havecolor @type pkg: L{gentoolkit.package.Package} @param pkg: package to format @type do_format: bool @param do_format: Whether to format the package name or not. Essentially C{do_format} should be set to False when piping or when quiet output is desired. If C{do_format} is False, only the location attribute will be created to save time. """ _tmpl_verbose = "[$location] [$mask] $cpv:$slot" _tmpl_quiet = "$cpv" def __init__(self, pkg, do_format=True, custom_format=None): self._pkg = None self._do_format = do_format self._str = None self._location = None if not custom_format: if do_format: custom_format = self._tmpl_verbose else: custom_format = self._tmpl_quiet self.tmpl = Template(custom_format) self.format_vars = LazyItemsDict() self.pkg = pkg def __repr__(self): return "<%s %s @%#8x>" % (self.__class__.__name__, self.pkg, id(self)) def __str__(self): if self._str is None: self._str = self.tmpl.safe_substitute(self.format_vars) return self._str @property def location(self): if self._location is None: self._location = self.format_package_location() return self._location @property def pkg(self): """Package to format""" return self._pkg @pkg.setter def pkg(self, value): if self._pkg == value: return self._pkg = value self._location = None fmt_vars = self.format_vars self.format_vars.clear() fmt_vars.addLazySingleton("location", lambda: getattr(self, "location")) fmt_vars.addLazySingleton("mask", self.format_mask) fmt_vars.addLazySingleton("mask2", self.format_mask_status2) fmt_vars.addLazySingleton("cpv", self.format_cpv) fmt_vars.addLazySingleton("cp", self.format_cpv, "cp") fmt_vars.addLazySingleton("category", self.format_cpv, "category") fmt_vars.addLazySingleton("name", self.format_cpv, "name") fmt_vars.addLazySingleton("version", self.format_cpv, "version") fmt_vars.addLazySingleton("revision", self.format_cpv, "revision") fmt_vars.addLazySingleton("fullversion", self.format_cpv, "fullversion") fmt_vars.addLazySingleton("slot", self.format_slot) fmt_vars.addLazySingleton("repo", self.pkg.repo_name) fmt_vars.addLazySingleton("keywords", self.format_keywords) def format_package_location(self): """Get the install status (in /var/db/?) and origin (from an overlay and the Portage tree?). @rtype: str @return: one of: 'I--' : Installed but ebuild doesn't exist on system anymore '-P-' : Not installed and from the Portage tree '--O' : Not installed and from an overlay 'IP-' : Installed and from the Portage tree 'I-O' : Installed and from an overlay """ result = ['-', '-', '-'] if self.pkg.is_installed(): result[0] = 'I' overlay = self.pkg.is_overlay() if overlay is None: pass elif overlay: result[2] = 'O' else: result[1] = 'P' return ''.join(result) def format_mask_status(self): """Get the mask status of a given package. @rtype: tuple: (int, list) @return: int = an index for this list: [" ", " ~", " -", "M ", "M~", "M-", "??"] 0 = not masked 1 = keyword masked 2 = arch masked 3 = hard masked 4 = hard and keyword masked, 5 = hard and arch masked 6 = ebuild doesn't exist on system anymore list = original output of portage.getmaskingstatus """ result = 0 masking_status = self.pkg.mask_status() if masking_status is None: return (6, []) if ("~%s keyword" % self.pkg.settings("ARCH")) in masking_status: result += 1 if "missing keyword" in masking_status: result += 2 if set(('profile', 'package.mask')).intersection(masking_status): result += 3 return (result, masking_status) def format_mask_status2(self): """Get the mask status of a given package. """ mask = self.pkg.mask_status() if mask: return pp.masking(mask) else: arch = self.pkg.settings("ARCH") keywords = self.pkg.environment('KEYWORDS') mask = [determine_keyword(arch, portage.settings["ACCEPT_KEYWORDS"], keywords)] return pp.masking(mask) def format_mask(self): maskmodes = [' ', ' ~', ' -', 'M ', 'M~', 'M-', '??'] maskmode = maskmodes[self.format_mask_status()[0]] return pp.keyword( maskmode, stable=not maskmode.strip(), hard_masked=set(('M', '?', '-')).intersection(maskmode) ) def format_cpv(self, attr=None): if attr is None: value = self.pkg.cpv else: value = getattr(self.pkg, attr) if self._do_format: return pp.cpv(value) else: return value def format_slot(self): value = self.pkg.environment("SLOT") if self._do_format: return pp.slot(value) else: return value def format_keywords(self): value = self.pkg.environment("KEYWORDS") if self._do_format: return pp.keyword(value) else: return value