2013-04-09 12:51:28 +02:00
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2009-09-12 21:02:58 +02:00
|
|
|
class PackageError(Exception):
|
|
|
|
"""base class for all package related errors"""
|
2010-10-21 21:46:08 +02:00
|
|
|
def __init__(self, fname, msg):
|
2009-09-12 21:02:58 +02:00
|
|
|
Exception.__init__(self)
|
2010-10-21 21:46:08 +02:00
|
|
|
self.fname = fname
|
2009-09-12 21:02:58 +02:00
|
|
|
self.msg = msg
|
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
class PackageQueries(dict):
|
|
|
|
"""Dict of package name keys and package query values. When assigning a
|
|
|
|
package query, to a name, the package is evaluated to see if it matches the
|
|
|
|
wanted architecture and if it has a greater version than the current value.
|
|
|
|
"""
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2016-08-04 12:40:36 +02:00
|
|
|
# map debian and rpm arches to common obs arches
|
|
|
|
architectureMap = {'i386': ['i586', 'i686'], 'amd64': ['x86_64'], 'ppc64el': ['ppc64le'], 'armv6hl': ['armv6l'], 'armv7hl': ['armv7l']}
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-02-27 20:11:15 +01:00
|
|
|
def __init__(self, wanted_architecture):
|
|
|
|
self.wanted_architecture = wanted_architecture
|
2010-01-18 16:12:10 +01:00
|
|
|
super(PackageQueries, self).__init__()
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
def add(self, query):
|
|
|
|
"""Adds package query to dict if it is of the correct architecture and
|
|
|
|
is newer (has a greater version) than the currently assigned package.
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
@param a PackageQuery
|
|
|
|
"""
|
|
|
|
self.__setitem__(query.name(), query)
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
def __setitem__(self, name, query):
|
|
|
|
if name != query.name():
|
|
|
|
raise ValueError("key '%s' does not match "
|
|
|
|
"package query name '%s'" % (name, query.name()))
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
architecture = query.arch()
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2014-04-03 17:41:41 +02:00
|
|
|
if (architecture in [self.wanted_architecture, 'noarch', 'all', 'any']
|
|
|
|
or self.wanted_architecture in self.architectureMap.get(architecture,
|
2010-01-18 16:12:10 +01:00
|
|
|
[])):
|
2010-02-27 20:11:15 +01:00
|
|
|
current_query = self.get(name)
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
# if current query does not exist or is older than this new query
|
2010-02-27 20:11:15 +01:00
|
|
|
if current_query is None or current_query.vercmp(query) <= 0:
|
2010-01-18 16:12:10 +01:00
|
|
|
super(PackageQueries, self).__setitem__(name, query)
|
|
|
|
|
2009-09-20 19:19:33 +02:00
|
|
|
class PackageQuery:
|
2009-09-12 21:02:58 +02:00
|
|
|
"""abstract base class for all package types"""
|
2010-01-14 19:51:09 +01:00
|
|
|
def read(self, all_tags = False, *extra_tags):
|
2015-06-23 17:52:37 +02:00
|
|
|
"""Returns a PackageQueryResult instance"""
|
2009-09-12 21:02:58 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2015-06-23 17:52:37 +02:00
|
|
|
# Hmmm... this should be a module function (inherting this stuff
|
|
|
|
# does not make much sense) (the same is true for the queryhdrmd5 method)
|
|
|
|
@staticmethod
|
|
|
|
def query(filename, all_tags=False, extra_rpmtags=(), extra_debtags=(), self_provides=True):
|
|
|
|
f = open(filename, 'rb')
|
|
|
|
magic = f.read(7)
|
|
|
|
f.seek(0)
|
|
|
|
extra_tags = ()
|
|
|
|
pkgquery = None
|
|
|
|
if magic[:4] == '\xed\xab\xee\xdb':
|
|
|
|
from . import rpmquery
|
|
|
|
pkgquery = rpmquery.RpmQuery(f)
|
|
|
|
extra_tags = extra_rpmtags
|
|
|
|
elif magic == '!<arch>':
|
|
|
|
from . import debquery
|
|
|
|
pkgquery = debquery.DebQuery(f)
|
|
|
|
extra_tags = extra_debtags
|
|
|
|
elif magic[:5] == '<?xml':
|
|
|
|
f.close()
|
|
|
|
return None
|
|
|
|
elif magic[:5] == '\375\067zXZ' or magic[:2] == '\037\213':
|
|
|
|
from . import archquery
|
|
|
|
pkgquery = archquery.ArchQuery(f)
|
|
|
|
else:
|
|
|
|
raise PackageError(filename, 'unsupported package type. magic: \'%s\'' % magic)
|
|
|
|
pkgqueryresult = pkgquery.read(all_tags, self_provides, *extra_tags)
|
|
|
|
f.close()
|
|
|
|
return pkgqueryresult
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def queryhdrmd5(filename):
|
|
|
|
f = open(filename, 'rb')
|
|
|
|
magic = f.read(7)
|
|
|
|
f.seek(0)
|
|
|
|
if magic[:4] == '\xed\xab\xee\xdb':
|
|
|
|
from . import rpmquery
|
|
|
|
f.close()
|
|
|
|
return rpmquery.RpmQuery.queryhdrmd5(filename)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
class PackageQueryResult:
|
|
|
|
"""abstract base class that represents the result of a package query"""
|
2009-09-12 21:02:58 +02:00
|
|
|
def name(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def version(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def release(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2009-09-29 14:32:26 +02:00
|
|
|
def epoch(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2009-09-12 21:02:58 +02:00
|
|
|
def arch(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def description(self):
|
|
|
|
raise NotImplementedError
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2010-01-18 16:12:10 +01:00
|
|
|
def path(self):
|
|
|
|
raise NotImplementedError
|
2010-02-28 02:30:13 +01:00
|
|
|
|
2009-09-12 21:02:58 +02:00
|
|
|
def provides(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def requires(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2014-12-05 18:36:44 +01:00
|
|
|
def conflicts(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def obsoletes(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2015-06-23 17:52:37 +02:00
|
|
|
def gettag(self, tag):
|
2009-09-12 21:02:58 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2010-02-27 20:11:15 +01:00
|
|
|
def vercmp(self, pkgquery):
|
2009-09-21 18:47:54 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2010-03-24 13:20:09 +01:00
|
|
|
def canonname(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2014-12-05 18:36:44 +01:00
|
|
|
def evr(self):
|
2015-07-02 15:08:39 +02:00
|
|
|
evr = self.version()
|
|
|
|
|
|
|
|
if self.release():
|
|
|
|
evr += "-" + self.release()
|
|
|
|
|
2014-12-05 18:36:44 +01:00
|
|
|
epoch = self.epoch()
|
|
|
|
if epoch is not None and epoch != 0:
|
|
|
|
evr = epoch + ":" + evr
|
|
|
|
return evr
|
|
|
|
|
2009-09-12 21:02:58 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
try:
|
|
|
|
pkgq = PackageQuery.query(sys.argv[1])
|
2013-04-09 11:27:02 +02:00
|
|
|
except PackageError as e:
|
2013-04-09 12:51:28 +02:00
|
|
|
print(e.msg)
|
2009-09-12 21:02:58 +02:00
|
|
|
sys.exit(2)
|
2013-04-09 12:51:28 +02:00
|
|
|
print(pkgq.name())
|
|
|
|
print(pkgq.version())
|
|
|
|
print(pkgq.release())
|
|
|
|
print(pkgq.description())
|
|
|
|
print('##########')
|
|
|
|
print('\n'.join(pkgq.provides()))
|
|
|
|
print('##########')
|
|
|
|
print('\n'.join(pkgq.requires()))
|