2010-01-18 16:12:10 +01:00
|
|
|
"""Module for reading repodata directory (created with createrepo) for package
|
|
|
|
information instead of scanning individual rpms."""
|
|
|
|
|
2022-07-28 12:28:33 +02:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
import gzip
|
2022-07-28 12:28:33 +02:00
|
|
|
import os
|
|
|
|
from xml.etree import ElementTree as ET
|
2010-01-18 16:12:10 +01:00
|
|
|
|
2022-07-28 12:28:33 +02:00
|
|
|
from . import rpmquery
|
|
|
|
from . import packagequery
|
2010-01-18 16:12:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
def namespace(name):
|
|
|
|
return "{http://linux.duke.edu/metadata/%s}" % name
|
|
|
|
|
2022-09-12 15:40:48 +02:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
OPERATOR_BY_FLAGS = {
|
2022-04-20 19:22:35 +02:00
|
|
|
"EQ": "=",
|
|
|
|
"LE": "<=",
|
|
|
|
"GE": ">=",
|
|
|
|
"LT": "<",
|
|
|
|
"GT": ">"
|
2010-01-18 16:12:10 +01:00
|
|
|
}
|
|
|
|
|
2022-09-12 15:40:48 +02:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
def primaryPath(directory):
|
|
|
|
"""Returns path to the primary repository data file.
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2022-01-24 08:57:53 +01:00
|
|
|
:param directory: repository directory that contains the repodata subdirectory
|
|
|
|
:return: path to primary repository data file
|
|
|
|
:rtype: str
|
|
|
|
:raise IOError: if repomd.xml contains no primary location
|
2010-01-18 16:12:10 +01:00
|
|
|
"""
|
|
|
|
metaDataPath = os.path.join(directory, "repodata", "repomd.xml")
|
|
|
|
elementTree = ET.parse(metaDataPath)
|
|
|
|
root = elementTree.getroot()
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
for dataElement in root:
|
|
|
|
if dataElement.get("type") == "primary":
|
|
|
|
locationElement = dataElement.find(namespace("repo") + "location")
|
|
|
|
# even though the repomd.xml file is under repodata, the location a
|
|
|
|
# attribute is relative to parent directory (directory).
|
|
|
|
primaryPath = os.path.join(directory, locationElement.get("href"))
|
|
|
|
break
|
|
|
|
else:
|
2022-07-28 19:11:29 +02:00
|
|
|
raise OSError("'%s' contains no primary location" % metaDataPath)
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
return primaryPath
|
|
|
|
|
2022-09-12 15:40:48 +02:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
def queries(directory):
|
|
|
|
"""Returns a list of RepoDataQueries constructed from the repodata under
|
|
|
|
the directory.
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2022-01-24 08:57:53 +01:00
|
|
|
:param directory: path to a repository directory (parent directory of repodata directory)
|
|
|
|
:return: list of RepoDataQueryResult instances
|
|
|
|
:raise IOError: if repomd.xml contains no primary location
|
2010-01-18 16:12:10 +01:00
|
|
|
"""
|
|
|
|
path = primaryPath(directory)
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
gunzippedPrimary = gzip.GzipFile(path)
|
|
|
|
elementTree = ET.parse(gunzippedPrimary)
|
|
|
|
root = elementTree.getroot()
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
packageQueries = []
|
|
|
|
for packageElement in root:
|
2015-06-23 17:52:37 +02:00
|
|
|
packageQuery = RepoDataQueryResult(directory, packageElement)
|
2010-01-18 16:12:10 +01:00
|
|
|
packageQueries.append(packageQuery)
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
return packageQueries
|
|
|
|
|
2020-03-15 18:11:50 +01:00
|
|
|
|
|
|
|
def _to_bytes_or_None(method):
|
|
|
|
def _method(self, *args, **kwargs):
|
|
|
|
res = method(self, *args, **kwargs)
|
|
|
|
if res is None:
|
|
|
|
return None
|
|
|
|
return res.encode()
|
|
|
|
|
|
|
|
return _method
|
|
|
|
|
|
|
|
|
|
|
|
def _to_bytes_list(method):
|
|
|
|
def _method(self, *args, **kwargs):
|
|
|
|
res = method(self, *args, **kwargs)
|
|
|
|
return [data.encode() for data in res]
|
|
|
|
|
|
|
|
return _method
|
|
|
|
|
|
|
|
|
2022-07-28 12:28:33 +02:00
|
|
|
class RepoDataQueryResult(packagequery.PackageQueryResult):
|
2015-06-23 17:52:37 +02:00
|
|
|
"""PackageQueryResult that reads in data from the repodata directory files."""
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
def __init__(self, directory, element):
|
2015-06-23 17:52:37 +02:00
|
|
|
"""Creates a RepoDataQueryResult from the a package Element under a metadata
|
2010-01-18 16:12:10 +01:00
|
|
|
Element in a primary.xml file.
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2022-01-24 08:57:53 +01:00
|
|
|
:param directory: repository directory path. Used to convert relative paths to full paths.
|
|
|
|
:param element: package Element
|
2010-01-18 16:12:10 +01:00
|
|
|
"""
|
|
|
|
self.__directory = os.path.abspath(directory)
|
|
|
|
self.__element = element
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
def __formatElement(self):
|
|
|
|
return self.__element.find(namespace("common") + "format")
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
def __parseEntry(self, element):
|
|
|
|
entry = element.get("name")
|
|
|
|
flags = element.get("flags")
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
if flags is not None:
|
|
|
|
version = element.get("ver")
|
|
|
|
operator = OPERATOR_BY_FLAGS[flags]
|
|
|
|
entry += " %s %s" % (operator, version)
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
release = element.get("rel")
|
|
|
|
if release is not None:
|
2010-02-05 15:14:48 +01:00
|
|
|
entry += "-%s" % release
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
return entry
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
def __parseEntryCollection(self, collection):
|
|
|
|
formatElement = self.__formatElement()
|
|
|
|
collectionElement = formatElement.find(namespace("rpm") + collection)
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
entries = []
|
|
|
|
if collectionElement is not None:
|
2022-09-12 15:40:48 +02:00
|
|
|
for entryElement in collectionElement.findall(namespace("rpm") + "entry"):
|
2010-01-18 16:12:10 +01:00
|
|
|
entry = self.__parseEntry(entryElement)
|
|
|
|
entries.append(entry)
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
return entries
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
def __versionElement(self):
|
|
|
|
return self.__element.find(namespace("common") + "version")
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2020-03-15 18:11:50 +01:00
|
|
|
@_to_bytes_or_None
|
2010-01-18 16:12:10 +01:00
|
|
|
def arch(self):
|
|
|
|
return self.__element.find(namespace("common") + "arch").text
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2020-03-15 18:11:50 +01:00
|
|
|
@_to_bytes_or_None
|
2010-01-18 16:12:10 +01:00
|
|
|
def description(self):
|
|
|
|
return self.__element.find(namespace("common") + "description").text
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
def distribution(self):
|
|
|
|
return None
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2020-03-15 18:11:50 +01:00
|
|
|
@_to_bytes_or_None
|
2010-01-18 16:12:10 +01:00
|
|
|
def epoch(self):
|
|
|
|
return self.__versionElement().get("epoch")
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2020-03-15 18:11:50 +01:00
|
|
|
@_to_bytes_or_None
|
2010-01-18 16:12:10 +01:00
|
|
|
def name(self):
|
|
|
|
return self.__element.find(namespace("common") + "name").text
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
def path(self):
|
|
|
|
locationElement = self.__element.find(namespace("common") + "location")
|
|
|
|
relativePath = locationElement.get("href")
|
|
|
|
absolutePath = os.path.join(self.__directory, relativePath)
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
return absolutePath
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2020-03-15 18:11:50 +01:00
|
|
|
@_to_bytes_list
|
2010-01-18 16:12:10 +01:00
|
|
|
def provides(self):
|
|
|
|
return self.__parseEntryCollection("provides")
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2020-03-15 18:11:50 +01:00
|
|
|
@_to_bytes_or_None
|
2010-01-18 16:12:10 +01:00
|
|
|
def release(self):
|
|
|
|
return self.__versionElement().get("rel")
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2020-03-15 18:11:50 +01:00
|
|
|
@_to_bytes_list
|
2010-01-18 16:12:10 +01:00
|
|
|
def requires(self):
|
|
|
|
return self.__parseEntryCollection("requires")
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2020-03-15 18:11:50 +01:00
|
|
|
@_to_bytes_list
|
2015-06-23 17:52:37 +02:00
|
|
|
def conflicts(self):
|
|
|
|
return self.__parseEntryCollection('conflicts')
|
|
|
|
|
2020-03-15 18:11:50 +01:00
|
|
|
@_to_bytes_list
|
2015-06-23 17:52:37 +02:00
|
|
|
def obsoletes(self):
|
|
|
|
return self.__parseEntryCollection('obsoletes')
|
|
|
|
|
2020-03-15 18:11:50 +01:00
|
|
|
@_to_bytes_list
|
2017-12-26 23:14:47 +01:00
|
|
|
def recommends(self):
|
|
|
|
return self.__parseEntryCollection('recommends')
|
|
|
|
|
2020-03-15 18:11:50 +01:00
|
|
|
@_to_bytes_list
|
2017-12-26 23:14:47 +01:00
|
|
|
def suggests(self):
|
|
|
|
return self.__parseEntryCollection('suggests')
|
|
|
|
|
2020-03-15 18:11:50 +01:00
|
|
|
@_to_bytes_list
|
2017-12-26 23:14:47 +01:00
|
|
|
def supplements(self):
|
|
|
|
return self.__parseEntryCollection('supplements')
|
|
|
|
|
2020-03-15 18:11:50 +01:00
|
|
|
@_to_bytes_list
|
2017-12-26 23:14:47 +01:00
|
|
|
def enhances(self):
|
|
|
|
return self.__parseEntryCollection('enhances')
|
|
|
|
|
2015-06-23 17:52:37 +02:00
|
|
|
def canonname(self):
|
2019-07-26 10:45:12 +02:00
|
|
|
if self.release() is None:
|
|
|
|
release = None
|
|
|
|
else:
|
2020-03-15 18:11:50 +01:00
|
|
|
release = self.release()
|
2022-09-12 15:40:48 +02:00
|
|
|
return rpmquery.RpmQuery.filename(self.name(), None, self.version(), release, self.arch())
|
2015-06-23 17:52:37 +02:00
|
|
|
|
|
|
|
def gettag(self, tag):
|
|
|
|
# implement me, if needed
|
|
|
|
return None
|
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
def vercmp(self, other):
|
2020-03-15 18:11:50 +01:00
|
|
|
# if either self.epoch() or other.epoch() is None, the vercmp will do
|
|
|
|
# the correct thing because one is transformed into b'None' and the
|
|
|
|
# other one into b"b'<epoch>'" (and 'b' is greater than 'N')
|
2022-07-28 12:28:33 +02:00
|
|
|
res = rpmquery.RpmQuery.rpmvercmp(str(self.epoch()).encode(), str(other.epoch()).encode())
|
2010-01-18 16:12:10 +01:00
|
|
|
if res != 0:
|
|
|
|
return res
|
2022-07-28 12:28:33 +02:00
|
|
|
res = rpmquery.RpmQuery.rpmvercmp(self.version(), other.version())
|
2010-01-18 16:12:10 +01:00
|
|
|
if res != 0:
|
|
|
|
return res
|
2022-07-28 12:28:33 +02:00
|
|
|
res = rpmquery.RpmQuery.rpmvercmp(self.release(), other.release())
|
2010-01-18 16:12:10 +01:00
|
|
|
return res
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2020-03-15 18:11:50 +01:00
|
|
|
@_to_bytes_or_None
|
2010-01-18 16:12:10 +01:00
|
|
|
def version(self):
|
|
|
|
return self.__versionElement().get("ver")
|