diff options
Diffstat (limited to 'pym/gentoolkit/helpers.py')
-rw-r--r-- | pym/gentoolkit/helpers.py | 173 |
1 files changed, 0 insertions, 173 deletions
diff --git a/pym/gentoolkit/helpers.py b/pym/gentoolkit/helpers.py index e7185c3..236a379 100644 --- a/pym/gentoolkit/helpers.py +++ b/pym/gentoolkit/helpers.py @@ -9,7 +9,6 @@ """ __all__ = ( - 'ChangeLog', 'FileOwner', 'get_cpvs', 'get_installed_cpvs', @@ -43,178 +42,6 @@ from gentoolkit.versionmatch import VersionMatch # Classes # ======= -class ChangeLog: - """Provides methods for working with a Gentoo ChangeLog file. - - Example usage: - >>> from gentoolkit.helpers import ChangeLog - >>> portage = ChangeLog('/usr/portage/sys-apps/portage/ChangeLog') - >>> print(portage.latest.strip()) - *portage-2.2.0_alpha142 (26 Oct 2012) - <BLANKLINE> - 26 Oct 2012; Zac Medico <zmedico@gentoo.org> +portage-2.2.0_alpha142.ebuild: - 2.2.0_alpha142 version bump. This includes all of the fixes in 2.1.11.31. Bug - #210077 tracks all bugs fixed since portage-2.1.x. - >>> len(portage.full) - 270 - >>> len(portage.entries_matching_range( - ... from_ver='2.1.11.31', - ... to_ver='9999')) - 140 - """ - def __init__(self, changelog_path, invalid_entry_is_fatal=False): - if not (os.path.isfile(changelog_path) and - os.access(changelog_path, os.R_OK)): - raise errors.GentoolkitFatalError( - "%s does not exist or is unreadable" % pp.path(changelog_path) - ) - self.changelog_path = changelog_path - self.invalid_entry_is_fatal = invalid_entry_is_fatal - - # Process the ChangeLog: - self.entries = self._split_changelog() - self.indexed_entries = self._index_changelog() - self.full = self.entries - self.latest = self.entries[0] - - def __repr__(self): - return "<%s %r>" % (self.__class__.__name__, self.changelog_path) - - def entries_matching_atom(self, atom): - """Return entries whose header versions match atom's version. - - @type atom: L{gentoolkit.atom.Atom} or str - @param atom: a atom to find matching entries against - @rtype: list - @return: entries matching atom - @raise errors.GentoolkitInvalidAtom: if atom is a string and malformed - """ - result = [] - - if not isinstance(atom, Atom): - atom = Atom(atom) - - for entry_set in self.indexed_entries: - i, entry = entry_set - # VersionMatch doesn't store .cp, so we'll force it to match here: - i.cp = atom.cp - if atom.intersects(i): - result.append(entry) - - return result - - def entries_matching_range(self, from_ver=None, to_ver=None): - """Return entries whose header versions are within a range of versions. - - @type from_ver: str - @param from_ver: valid Gentoo version - @type to_ver: str - @param to_ver: valid Gentoo version - @rtype: list - @return: entries between from_ver and to_ver - @raise errors.GentoolkitFatalError: if neither vers are set - @raise errors.GentoolkitInvalidVersion: if either ver is invalid - """ - result = [] - - # Make sure we have at least one version set - if not (from_ver or to_ver): - raise errors.GentoolkitFatalError( - "Need to specifiy 'from_ver' or 'to_ver'" - ) - - # Create a VersionMatch instance out of from_ver - from_restriction = None - if from_ver: - try: - from_ver_rev = CPV("null-%s" % from_ver) - except errors.GentoolkitInvalidCPV: - raise errors.GentoolkitInvalidVersion(from_ver) - from_restriction = VersionMatch(from_ver_rev, op='>=') - - # Create a VersionMatch instance out of to_ver - to_restriction = None - if to_ver: - try: - to_ver_rev = CPV("null-%s" % to_ver) - except errors.GentoolkitInvalidCPV: - raise errors.GentoolkitInvalidVersion(to_ver) - to_restriction = VersionMatch(to_ver_rev, op='<=') - - # Add entry to result if version ranges intersect it - for entry_set in self.indexed_entries: - i, entry = entry_set - if from_restriction and not from_restriction.match(i): - continue - if to_restriction and not to_restriction.match(i): - # TODO: is it safe to break here? - continue - result.append(entry) - - return result - - def _index_changelog(self): - """Use the output of L{self._split_changelog} to create an index list - of L{gentoolkit.versionmatch.VersionMatch} objects. - - @rtype: list - @return: tuples containing a VersionMatch instance for the release - version of each entry header as the first item and the entire entry - as the second item - @raise ValueError: if self.invalid_entry_is_fatal is True and we hit an - invalid entry - """ - - result = [] - for entry in self.entries: - # Extract the package name from the entry header, ex: - # *xterm-242 (07 Mar 2009) => xterm-242 - pkg_name = entry.split(' ', 1)[0].lstrip('*') - if not pkg_name.strip(): - continue - try: - entry_ver = CPV(pkg_name, validate=True) - except errors.GentoolkitInvalidCPV: - if self.invalid_entry_is_fatal: - raise ValueError(entry_ver) - continue - - result.append((VersionMatch(entry_ver, op='='), entry)) - - return result - - def _split_changelog(self): - """Split the ChangeLog into individual entries. - - @rtype: list - @return: individual ChangeLog entries - """ - - result = [] - partial_entries = [] - with open(_unicode_encode(self.changelog_path, - encoding=_encodings['fs'], errors="replace"), - encoding=_encodings['content']) as log: - for line in log: - if line.startswith('#'): - continue - elif line.startswith('*'): - # Append last entry to result... - entry = ''.join(partial_entries) - if entry and not entry.isspace(): - result.append(entry) - # ... and start a new entry - partial_entries = [line] - else: - partial_entries.append(line) - else: - # Append the final entry - entry = ''.join(partial_entries) - result.append(entry) - - return result - - class FileOwner: """Creates a function for locating the owner of filename queries. |