def show(self): global ts print '%s%d results (%d packages) found for "%s%s%s" in %s%s' % ( cs.headerStart, self.hits, len(self.results), cs.queryHighlight, self.query, cs.headerAfterQueryHighlight, self.dist, cs.headerEnd, ) if options.rpm: if options.rpmRoot: ts = rpm.ts(options.rpmRoot) else: ts = rpm.ts() pass ts.setVSFlags(rpm.RPMVSF_NORSA | rpm.RPMVSF_NODSA) # speeds up query pass for name in self.resultOrder: r = self.results[name] r.show() pass pass
def get_rpm_information(filename): ts = rpm.ts() f = open(filename, 'r') ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES) hdr = ts.hdrFromFdno(f) f.close() return { 'name': hdr[rpm.RPMTAG_NAME], 'version' : hdr[rpm.RPMTAG_VERSION], 'release': hdr[rpm.RPMTAG_RELEASE], 'epoch': hdr[rpm.RPMTAG_EPOCH], 'arch': hdr[rpm.RPMTAG_ARCH] }
def _ts(self): if self.__class__.__ts is None: import rpm flags = (rpm.RPMVSF_NOHDRCHK | rpm._RPMVSF_NODIGESTS | rpm._RPMVSF_NOSIGNATURES) self.__class__.__ts = rpm.ts("/", flags) return self.__class__.__ts
def spec_sources(topdir): specs = glob.glob(os.path.join(topdir, "SPECS/*.spec")) spec_path = specs[0] # FIXME use svn info to ensure which one ts = rpm.ts() spec = ts.parseSpec(spec_path) sources = [name for name, x, y in spec.sources()] return sources
def install_rpm(self, Callback = DummyCallback): '''This installs the Downloaded Local RPM. It returns 0 for sucess and 2 for failure''' try: import os, rpm TransactionSet = rpm.ts() # This Next Line turns off GPG key checks TransactionSet.setVSFlags(-1) # This next block gets the header from the rpm fdno = os.open(self.local_rpm, os.O_RDONLY) header = TransactionSet.hdrFromFdno(fdno) os.close(fdno) # Add install of file TransactionSet.addInstall(header,(header,self.local_rpm), "u") # Function cb.callback reports on progress cb = Callback() # Install rpm TransactionSet.run(cb.callback,"") # Set flag self.installed = True except: return 2 return 0
def __init__(self, dist): self.dist = dist # # where the build state files go (e.g., 'kernel*.src.rpm.done') # self.donedir = os.path.join(self.getRebuildPath(), 'spool', self.dist.getProductRelease(), self.dist.getLang(), 'os', self.dist.getArch()) if not os.path.exists(self.donedir): os.makedirs(self.donedir) # # location of the patch scripts (e.g., 'kernel/build') # self.patchdir = os.path.join(self.getRebuildPath(), 'patches') # # where all the built binary RPMS will go # self.completedir = os.path.join(self.getRebuildPath(), self.dist.getProductRelease(), self.dist.getLang(), 'os', self.dist.getArch(), 'RedHat') if not os.path.exists(self.completedir): os.makedirs(self.completedir) # # support data structures that allow us to query individual # RPM and SRPM packages # self.ts = rpm.ts() self.ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES) return
def _load_installed_packages(self): """Visit rpmdb and load data from installed packages.""" log.info("reading installed packages.") if self._rpm_dbpath is not None: rpm.addMacro("_dbpath", self._rpm_dbpath) for pkg in rpm.ts().dbMatch(): # TODO Load capabilities information in the same manner # Media.list_medias() will return. pkgdict = {} for attr in ( "name", "version", "release", "arch", "epoch", "size", "group", "summary", "installtime", "disttag", "distepoch", ): value = pkg[attr] if type(value) is list and len(value) == 0: value = "" pkgdict[attr] = value if type(pkg["installtime"]) is list: pkgdict["installtime"] = pkg["installtime"][0] if pkgdict["epoch"] is None: pkgdict["epoch"] = 0 self._on_package_data(pkgdict) rpm.delMacro("_dbpath")
def _get_nvr(self): tset = rpm.ts() tset.parseSpec(self.specfile) name = rpm.expandMacro("%{name}") version = rpm.expandMacro("%{version}") release = rpm.expandMacro("%{release}") return (name, version, release)
def is_production_pkg(): """ Checks that current Kuberdock package is one of stable KD releases based on package signature (Any public release signed with CL key) and some folder existence :return: Bool """ cloudlinux_sig_key = '8c55a6628608cb71' try: # This check is needed for cases when developer install signed # release package and then modify sources with # sshfs/server-side_git/IDE_remote_deploy/etc. # This folder is never exists in production package/env so we can rely # on this check for most such cases if os.path.exists('/var/opt/kuberdock/dev-utils'): return False import rpm from rpmUtils.miscutils import getSigInfo ts = rpm.ts() hdr = list(ts.dbMatch('name', 'kuberdock'))[0] err, res = getSigInfo(hdr) if err: return False if cloudlinux_sig_key not in res[2]: return False except Exception: return False return True
def getTS(new=False): if sysconf.get("rpm-extra-macros"): for key, value in sysconf.get("rpm-extra-macros").items(): rpm.addMacro(key, str(value)) rpm_root = os.path.abspath(sysconf.get("rpm-root", "/")) if not hasattr(getTS, "ts") or getTS.root != rpm_root: getTS.root = rpm_root if sysconf.get("rpm-dbpath"): rpm.addMacro('_dbpath', "/" + sysconf.get("rpm-dbpath")) getTS.ts = rpm.ts(getTS.root) #if not sysconf.get("rpm-check-signatures", False): # getTS.ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES) rpm_dbpath = sysconf.get("rpm-dbpath", "var/lib/rpm") dbdir = rpm_join_dbpath(getTS.root, rpm_dbpath) if not os.path.isdir(dbdir): try: os.makedirs(dbdir) except (OSError), e: raise Error, _("Could not create rpm-root at %s: %s") \ % (dbdir, unicode(e)) if not os.path.isfile(os.path.join(dbdir, "Packages")): try: getTS.ts.initDB() except (rpm.error, OSError): raise Error, _("Couldn't initizalize rpm database at %s") \ % getTS.root else: iface.warning( _("Initialized new rpm database at %s") % getTS.root)
class DistGitFetcher(GitFetcher): name = 'distgit' def _checkout(self, branch): self.cmd = 'git checkout "%s"' % branch errc, out, err = run(self.cmd) if errc: raise RuntimeError("git checkout failed: %s" % err or out) self.cmd = 'git pull' errc, out, err = run(self.cmd) if errc: raise RuntimeError("git pull failed: %s" % err or out) def _get_version(self, pkg_name, branch): self.cmd = None try: self._prepare_repo(pkg_name) self._checkout(branch) except RuntimeError, e: return {'error': e.args[0], 'cmd': self.cmd} specs = [f for f in os.listdir('.') \ if os.path.isfile(f) and f.endswith('.spec')] if not specs: return {'error': "No .spec files found."} if len(specs) != 1: return {'error': "Multiple .spec files present."} spec_fn = specs[0] try: spec = rpm.ts().parseSpec(spec_fn) except ValueError, e: return {'error': "Error parsing '%s': %s" % (spec_fn, e.args[0])}
def loadFileProvides(self, fndict): # FIXME (20050321): Solaris rpm 4.1 hack if sys.platform[:5] == "sunos": rpm.addMacro("_dbPath", sysconf.get("rpm-root", "/")) ts = rpm.TransactionSet() else: ts = rpm.ts() bfp = self.buildFileProvides for i, filename in enumerate(self._filenames): if i not in self._offsets: continue filepath = os.path.join(self._dir, filename) file = open(filepath) try: h = ts.hdrFromFdno(file.fileno()) except rpm.error, e: file.close() iface.error("%s: %s" % (os.path.basename(filepath), e)) else: file.close() # FIXME (20050321): Solaris rpm 4.1 hack f = h[1027] # RPMTAG_OLDFILENAMES if f == None: f = [] for fn in f: fn = fndict.get(fn) if fn: bfp(self._offsets[i], (RPMProvides, fn, None))
def getTS(new=False): if sysconf.get("rpm-extra-macros"): for key, value in sysconf.get("rpm-extra-macros").items(): rpm.addMacro(key, str(value)) rpm_root = os.path.abspath(sysconf.get("rpm-root", "/")) if not hasattr(getTS, "ts") or getTS.root != rpm_root: getTS.root = rpm_root if sysconf.get("rpm-dbpath"): rpm.addMacro('_dbpath', "/" + sysconf.get("rpm-dbpath")) getTS.ts = rpm.ts(getTS.root) if not sysconf.get("rpm-check-signatures", False): if hasattr(rpm, '_RPMVSF_NOSIGNATURES'): getTS.ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES) else: raise Error, _("rpm requires checking signatures") rpm_dbpath = sysconf.get("rpm-dbpath", "var/lib/rpm") dbdir = rpm_join_dbpath(getTS.root, rpm_dbpath) if not os.path.isdir(dbdir): try: os.makedirs(dbdir) except (OSError), e: raise Error, _("Could not create rpm-root at %s: %s") \ % (dbdir, unicode(e)) if not os.path.isfile(os.path.join(dbdir, "Packages")): try: getTS.ts.initDB() except (rpm.error, OSError): raise Error, _("Couldn't initizalize rpm database at %s") \ % getTS.root else: iface.warning(_("Initialized new rpm database at %s") % getTS.root)
def parse_spec_quietly(path): """ Parse spec file at 'path' and return an rpm.spec object. This function suppresses any errors about missing sources which librpm writes to stderr. """ with tempfile.TemporaryFile() as nullfh: try: # collect all output to stderr then filter out # errors about missing sources errcpy = os.dup(2) try: os.dup2(nullfh.fileno(), 2) return rpm.ts().parseSpec(path) finally: os.dup2(errcpy, 2) os.close(errcpy) except ValueError as exn: nullfh.seek(0, os.SEEK_SET) # https://github.com/PyCQA/pylint/issues/1435 # pylint: disable=E1133 for line in nullfh: line = line.strip() if not line.endswith(': No such file or directory'): print(line, file=sys.stderr) exn.args = (exn.args[0].rstrip() + ' ' + path, ) raise
def store_rpm_provides(db: Database, package: Package, nogpgcheck: bool = False) -> None: """ Save RPM provides of `package` to storage. Expects pyfaf.storage.opsys.Package object. """ pkg_id = package.id ts = rpm.ts() rpm_file = package.get_lob_fd("package") if not rpm_file: raise FafError("Package {0} has no lob stored".format(package.name)) if nogpgcheck: ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES) #pylint: disable=protected-access try: header = ts.hdrFromFdno(rpm_file.fileno()) except rpm.error as exc: rpm_file.close() raise FafError("rpm error: {0}".format(exc)) from exc files = header.fiFromHeader() log.debug("%s contains %d files", package.nvra(), len(files)) # Invalid name for type variable # pylint: disable-msg=C0103 for f in files: new = PackageDependency() new.package_id = pkg_id new.type = "PROVIDES" new.name = f[0] new.flags = 0 db.session.add(new) provides = header.dsFromHeader('providename') for p in provides: if len(p.N()) > 1024: log.warning("Provides item in RPM header of %s longer than 1024 " "characters. Skipping", package.name) continue new = PackageDependency() new.package_id = pkg_id new.type = "PROVIDES" new.name = p.N() new.flags = p.Flags() evr = p.EVR() if evr: try: new.epoch, new.version, new.release = parse_evr(evr) except ValueError as ex: log.warning("Unparsable EVR ‘%s’ of %s in Provides of %s: %s. " "Skipping", evr, p.N(), package.name, ex) continue db.session.add(new) rpm_file.close() db.session.flush()
def main(): ts=rpm.ts() mi=ts.dbMatch() # Comment the following line to check all rpms mi.pattern("release", rpm.RPMMIRE_GLOB, "*centos*") ret=True print "Searching for CentOS branding issues in installed rpm..." for hdr in mi: if hdr['buildhost'][-11:] != '.centos.org': print " Build host is not centos.org machine in: %s" % hdr['name'] ret=False if hdr['vendor'] != 'CentOS': print " Vendor is not CentOS in: %s" % hdr['name'] ret=False if hdr['packager'] != 'CentOS BuildSystem <http://bugs.centos.org>': print " Packager is not CentOS BuildSystem in: %s" % hdr['name'] ret=False try: changelog = hdr['changelogname'][0] if not is_valid_changelog_entry(changelog): print " Bad changelog entry in: %s" % hdr['name'] ret=False except Exception, e: print " Errors found when reading changelog entry of: %s" % hdr['name'] ret=False
def main(): ts = rpm.ts() mi = ts.dbMatch() # Comment the following line to check all rpms mi.pattern("release", rpm.RPMMIRE_GLOB, "*centos*") ret = True print "Searching for CentOS branding issues in installed rpm..." for hdr in mi: if hdr['buildhost'][-11:] != '.centos.org': print " Build host is not centos.org machine in: %s" % hdr['name'] ret = False if hdr['vendor'] != 'CentOS': print " Vendor is not CentOS in: %s" % hdr['name'] ret = False if hdr['packager'] != 'CentOS BuildSystem <http://bugs.centos.org>': print " Packager is not CentOS BuildSystem in: %s" % hdr['name'] ret = False try: changelog = hdr['changelogname'][0] if not is_valid_changelog_entry(changelog): print " Bad changelog entry in: %s" % hdr['name'] ret = False except Exception, e: print " Errors found when reading changelog entry of: %s" % hdr[ 'name'] ret = False
def getTS(new=False): rpm_root = os.path.abspath(sysconf.get("rpm-root", "/")) if not hasattr(getTS, "ts") or getTS.root != rpm_root: getTS.root = rpm_root getTS.ts = rpm.ts(getTS.root) if not sysconf.get("rpm-check-signatures", False): getTS.ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES) dbdir = os.path.join(getTS.root, "var/lib/rpm") if not os.path.isdir(dbdir): try: os.makedirs(dbdir) except (OSError), e: raise Error, _("Could not create rpm-root at %s: %s") \ % (dbdir, unicode(e)) if not os.path.isfile(os.path.join(dbdir, "Packages")): try: getTS.ts.initDB() except (rpm.error, OSError): raise Error, _("Couldn't initizalize rpm database at %s") \ % getTS.root else: iface.warning(_("Initialized new rpm database at %s") % getTS.root) tmpdir = os.path.join(getTS.root, "var/tmp") if not os.path.isdir(tmpdir): try: os.makedirs(tmpdir) except OSError: pass
def __init__(self, path, target="rpm", map_name=None, dist=""): if target == "rpm": self.rpmfilenamepat = rpm.expandMacro("%_build_name_fmt") self.srpmfilenamepat = rpm.expandMacro("%_build_name_fmt") self.map_arch = identity # '%dist' in the host (where we build the source package) # might not match '%dist' in the chroot (where we build # the binary package). We must override it on the host, # otherwise the names of packages in the dependencies won't # match the files actually produced by mock. self.dist = dist else: self.rpmfilenamepat = "%{NAME}_%{VERSION}-%{RELEASE}_%{ARCH}.deb" self.srpmfilenamepat = "%{NAME}_%{VERSION}-%{RELEASE}.dsc" self.map_arch = map_arch_deb self.dist = "" rpm.addMacro("dist", self.dist) if map_name: self.map_package_name = map_name else: self.map_package_name = identity_list self.path = os.path.join(SPECDIR, os.path.basename(path)) with open(path) as spec: self.spectext = spec.readlines() self.spec = rpm.ts().parseSpec(path)
def __init__(self, path, dist="", check_package_name=True, topdir=None): # _topdir defaults to $HOME/rpmbuild if topdir: rpm.addMacro('_topdir', topdir) rpm.addMacro('_specdir', os.path.dirname(path)) self.path = os.path.join(specdir(), os.path.basename(path)) with open(path) as spec: self.spectext = spec.readlines() # '%dist' in the host (where we build the source package) # might not match '%dist' in the chroot (where we build # the binary package). We must override it on the host, # otherwise the names of packages in the dependencies won't # match the files actually produced by mock. rpm.addMacro('dist', dist) try: self.spec = rpm.ts().parseSpec(path) except ValueError as exn: exn.args = (exn.args[0].rstrip() + ' ' + path, ) raise if check_package_name: file_basename = os.path.basename(path).split(".")[0] if file_basename != self.name(): raise SpecNameMismatch( "spec file name '%s' does not match package name '%s'" % (path, self.name())) self.rpmfilenamepat = rpm.expandMacro('%_build_name_fmt') self.srpmfilenamepat = rpm.expandMacro('%_build_name_fmt')
def updateInstalledSoftware(self, name=None): import rpm ts=rpm.ts() mi=ts.dbMatch() if name: mi.pattern("name", rpm.RPMMIRE_GLOB, name) for hdr in mi: self.installedsoftware.append(hdr)
def order(self, packages): ts_tmp = rpm.ts() for package in packages.values(): ts_tmp.addInstall(*self.__package_instalation_data(package)) ts_tmp.check() ts_tmp.order() return [te.N() for te in ts_tmp]
def updateInstalledSoftware(self, name=None): import rpm ts = rpm.ts() mi = ts.dbMatch() if name: mi.pattern("name", rpm.RPMMIRE_GLOB, name) for hdr in mi: self.installedsoftware.append(hdr)
def rpmqf(path): ts = rpm.ts() found = list(ts.dbMatch("basenames", path)) if found: name = (found[0]["name"] + "-" + (found[0]["version"] or "") + "-" + (found[0]["release"] or "")) return name
def __init__(self, specFileName): self.specFileName = specFileName rpm.addMacro('_ipv6', '1') ts = rpm.ts() self.spec = ts.parseSpec(specFileName) self.header = self.spec.header() self.sources = self.spec.sources() ts.clean()
def getHeader(self, pkg): # FIXME (20050321): Solaris rpm 4.1 hack if sys.platform[:5] == "sunos": rpm.addMacro("_dbPath", sysconf.get("rpm-root", "/")) ts = rpm.TransactionSet() else: ts = rpm.ts(sysconf.get("rpm-root", "/")) mi = ts.dbMatch(0, pkg.loaders[self]) return mi.next()
def getRPMInformation(rpmPath): long_variable_name = rpm.ts() try: file_descriptor_number = os.open(rpmPath, os.O_RDONLY) rpmInfo = long_variable_name.hdrFromFdno(file_descriptor_number) os.close(file_descriptor_number) except: print "error trying to get rpm info" return False return rpmInfo
def getRPMInformation (rpmPath): long_variable_name = rpm.ts(); try: file_descriptor_number = os.open(rpmPath, os.O_RDONLY) rpmInfo = long_variable_name.hdrFromFdno(file_descriptor_number); os.close(file_descriptor_number) except: print "error trying to get rpm info" return False return rpmInfo
def show(self): global ts print "%s%d results (%d packages) found for \"%s%s%s\" in %s%s" % ( cs.headerStart, self.hits, len(self.results), cs.queryHighlight, self.query, cs.headerAfterQueryHighlight, self.dist, cs.headerEnd) if options.rpm: if options.rpmRoot: ts = rpm.ts(options.rpmRoot) else: ts = rpm.ts() pass ts.setVSFlags(rpm.RPMVSF_NORSA | rpm.RPMVSF_NODSA) # speeds up query pass for name in self.resultOrder: r = self.results[name] r.show() pass pass
def store_package_deps(db, package_obj): pkg_id = package_obj.id ts = rpm.ts() rpm_file = package_obj.get_lob_fd("package") header = ts.hdrFromFdno(rpm_file.fileno()) files = header.fiFromHeader() logging.debug("{0} contains {1} files".format(package_obj.nvra(), len(files))) for f in files: new = PackageDependency() new.package_id = pkg_id new.type = "PROVIDES" new.name = f[0] new.flags = 0 db.session.add(new) provides = header.dsFromHeader('providename') for p in provides: new = PackageDependency() new.package_id = pkg_id new.type = "PROVIDES" new.name = p.N() new.flags = p.Flags() evr = p.EVR() if len(evr): new.epoch, new.version, new.release = rpmutils.stringToVersion(evr) db.session.add(new) requires = header.dsFromHeader('requirename') for r in requires: new = PackageDependency() new.package_id = pkg_id new.type = "REQUIRES" new.name = r.N() new.flags = r.Flags() evr = r.EVR() if len(evr): new.epoch, new.version, new.release = rpmutils.stringToVersion(evr) db.session.add(new) conflicts = header.dsFromHeader('conflictname') for c in conflicts: new = PackageDependency() new.package_id = pkg_id new.type = "CONFLICTS" new.name = c.N() new.flags = c.Flags() evr = c.EVR() if len(evr): new.epoch, new.version, new.release = rpmutils.stringToVersion(evr) db.session.add(new) rpm_file.close() db.session.flush()
def logRpmVersion(package): jrnl = Journal.openJournal() log = Journal.getLogEl(jrnl) add_to = Journal.getLastUnfinishedPhase(log) ts = rpm.ts() rpms = Journal.getRpmVersion(jrnl, package, ts) for pkg in rpms: pkgEl, pkgCon = pkg pkgEl.appendChild(pkgCon) add_to.appendChild(pkgEl) return Journal.saveJournal(jrnl)
def parse_specdeb(self): # Get spec infos self.logger.info("Find informations from spec file") spec_file_path = os.path.join(self.folder, 'sources', self.config['root_folder'], self.config['spec']) # Prepare datas self.config['deps'] = self.config.get('deps', []) self.config['deps_pip'] = self.config.get('deps_pip', []) self.config['ccache'] = self.config.get('ccache', False) self.config['version'] = '' self.config['release'] = '' self.config['name'] = '' self.config['source'] = '' self.config['sources'] = '' self.config['source_folder'] = '' self.config['source_archived'] = self.config.get( 'source_archived', False) raw_sources = [] # Prepare spec datas mapping = { 1000: 'name', 1001: 'version', 1002: 'release', 1049: 'deps', 1018: 'sources', } spec = rpm.ts().parseSpec(spec_file_path) # Get spec data for id_, attr_name in mapping.items(): if isinstance(self.config.get(attr_name), list): self.config[attr_name] += spec.sourceHeader[id_] else: self.config[attr_name] = spec.sourceHeader[id_] # Get only the first source self.config['source'] = self.config['sources'][0] # Try to find the name of the folder which rpmbuild needs to find # in the source tarball match = re.match(".*\/BUILD\'\nrm -rf '(.*)'", spec.prep) if match: self.config['source_folder'] = match.group(1) else: self.config['source_folder'] = self.config[ 'name'] + "-" + self.config['version'] # Log informations self.logger.info("Name: %(name)s", self.config) self.logger.info("Source: %(source)s", self.config) self.logger.info("Source folder: %(source_folder)s", self.config) self.logger.info("Version: %(version)s", self.config) self.logger.info("Release: %(release)s", self.config) self.logger.info("Buildrequires: %s", ", ".join(self.config['deps'])) return True
def checkout(self, commit, job): job.run_hook('pre-spec-checkout') job.shell.git('checkout', commit, '--', job.specfile) job.run_hook('post-spec-checkout') for source in rpm.ts().parseSpec(job.specfile).sources: try: job.shell.git('checkout', commit, '--', os.path.basename(source[0])) except GolemError: if source[0].startswith(('http://', 'https://')): job.shell.wget(source[0]) job.run_hook('post-source-download')
def get_standard_directories(): lst = [] import rpm try: ts = rpm.ts() h = ts.dbMatch("name", "filesystem").next() for i in h.fiFromHeader(): lst.append(i[0]) except: syslog.syslog(syslog.LOG_ERR, "failed to get filesystem list from rpm") return lst
def _set_srpm_details(self, srpm): logging.info("== Querying srpm ==") ts = rpm.ts() ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES) fdno = open(u"%s" % srpm, 'r') try: hdr = ts.hdrFromFdno(fdno) except rpm.error, e: if str(e) == "public key not available": print str(e)
def build_tarballs(self): tset = rpm.ts() spec = tset.parseSpec(self.specfile) spec_sources = dict([(src[1], src[0]) for src in spec.sources if src[2] == 1]) for (i, source) in self.sources.iteritems(): if i in spec_sources: tarball = os.path.basename(spec_sources[i]) logging.info("Building Source%i: %s from %s", i, tarball, source.tree) source.create_tarball(tarball, ".") else: logging.warn("Spec file does not contain Source%i; skipping " "tarball build for url %s", i, source.url)
def get_standard_directories(): lst = [] import rpm try: ts = rpm.ts() h = next(ts.dbMatch("name", "filesystem")) for i in h.fiFromHeader(): lst.append(i[0]) except: syslog.syslog(syslog.LOG_ERR, "failed to get filesystem list from rpm") return lst
def get_rpm_info(rpm_file): """Returns rpm information by querying a rpm""" ts = rpm.ts() fdno = os.open(rpm_file, os.O_RDONLY) try: hdr = ts.hdrFromFdno(fdno) except rpm.error: fdno = os.open(rpm_file, os.O_RDONLY) ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES) hdr = ts.hdrFromFdno(fdno) os.close(fdno) return {'name': hdr[rpm.RPMTAG_NAME], 'version': hdr[rpm.RPMTAG_VERSION], 'package_release': hdr[rpm.RPMTAG_RELEASE], 'epoch': hdr[rpm.RPMTAG_EPOCH], 'arch': hdr[rpm.RPMTAG_ARCH]}
def resolve_deps(args): srpm = args.rpm # rpm.ts is an alias for rpm.TransactionSet ts = rpm.ts() ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES) fdno = os.open(srpm, os.O_RDONLY) try: hdr = ts.hdrFromFdno(fdno) except rpm.error, e: if str(e) == "public key not available": print str(e)
def redis_version(): ts = rpm.ts() mi = ts.dbMatch(rpm.RPMTAG_PROVIDES, 'redis') try: if not mi: return None for hdr in mi: version = hdr[rpm.RPMTAG_VERSION] return version finally: del mi ts.closeDB()
def __init__(self, path, check_package_name=True, defines=None): self.macros = OrderedDict(defines) if defines else OrderedDict() # _topdir defaults to $HOME/rpmbuild # If present, it needs to be applied once at the beginning if '_topdir' in self.macros: rpm.addMacro('_topdir', self.macros['_topdir']) # '%dist' in the host (where we build the source package) # might not match '%dist' in the chroot (where we build # the binary package). We must override it on the host, # otherwise the names of packages in the dependencies won't # match the files actually produced by mock. if 'dist' not in self.macros: self.macros['dist'] = "" hardcoded_macros = OrderedDict([('_specdir', os.path.dirname(path))]) with rpm_macros(append_macros(self.macros, hardcoded_macros)): self.path = os.path.join(specdir(), os.path.basename(path)) with open(path) as spec: self.spectext = spec.readlines() with tempfile.TemporaryFile() as nullfh: try: # collect all output to stderr then filter out # errors about missing sources errcpy = os.dup(2) try: os.dup2(nullfh.fileno(), 2) self.spec = rpm.ts().parseSpec(path) finally: os.dup2(errcpy, 2) os.close(errcpy) except ValueError as exn: nullfh.seek(0, os.SEEK_SET) for line in nullfh: line = line.strip() if not line.endswith(': No such file or directory'): print >> sys.stderr, line exn.args = (exn.args[0].rstrip() + ' ' + path, ) raise if check_package_name: file_basename = os.path.basename(path).split(".")[0] if file_basename != self.name(): raise SpecNameMismatch( "spec file name '%s' does not match package name '%s'" % (path, self.name())) self.rpmfilenamepat = rpm.expandMacro('%_build_name_fmt') self.srpmfilenamepat = rpm.expandMacro('%_build_name_fmt')
def __init__(self, file, timestamp=None, size=None, ext=1): File.__init__(self, file, timestamp, size) self.list = [] self.version = None self.release = None fd = os.open(file, os.O_RDONLY) try: ts = rpm.ts() ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES) rpminfo = ts.hdrFromFdno(fd) basename = rpminfo[rpm.RPMTAG_NAME] arch = rpminfo[rpm.RPMTAG_ARCH] self.version = rpminfo[rpm.RPMTAG_VERSION] self.release = rpminfo[rpm.RPMTAG_RELEASE] self.list = [ basename, self.version, self.release, arch ] except: pass os.close(fd) if not len(self.list): # Remove ext count extensions, the default is 1, but for # rolls we remove two (.diskN.iso) s = self.filename # name-ver-rpmver.arch.rpm for x in range(0, ext): i = string.rfind(s, ".") s = self.filename[:i] i = string.rfind(s, ".") self.list.append(s[i+1:]) # get architecture string s = self.filename[:i] i = string.rfind(s, "-") # get RPM version string self.release = s[i+1:] self.list.append(self.versionList(s[i+1:])) s = self.filename[:i] i = string.rfind(s, "-") # get software version string self.version = s[i+1:] self.list.append(self.versionList(s[i+1:])) self.list.append(self.filename[:i]) # get package name self.list.reverse() # we built the list backwards
def parse_specdeb(self): # Get spec infos self.logger.info("Find informations from spec file") spec_file_path = os.path.join(self.folder, 'sources', self.config['root_folder'], self.config['spec']) # Prepare datas self.config['deps'] = self.config.get('deps', []) self.config['deps_pip'] = self.config.get('deps_pip', []) self.config['ccache'] = self.config.get('ccache', False) self.config['version'] = '' self.config['release'] = '' self.config['name'] = '' self.config['source'] = '' self.config['sources'] = '' self.config['source_folder'] = '' self.config['source_archived'] = self.config.get('source_archived', False) raw_sources = [] # Prepare spec datas mapping = {1000: 'name', 1001: 'version', 1002: 'release', 1049: 'deps', 1018: 'sources', } spec = rpm.ts().parseSpec(spec_file_path) # Get spec data for id_, attr_name in mapping.items(): if isinstance(self.config.get(attr_name), list): self.config[attr_name] += spec.sourceHeader[id_] else: self.config[attr_name] = spec.sourceHeader[id_] # Get only the first source self.config['source'] = self.config['sources'][0] # Try to find the name of the folder which rpmbuild needs to find # in the source tarball match = re.match(".*\/BUILD\'\nrm -rf '(.*)'", spec.prep) if match: self.config['source_folder'] = match.group(1) else: self.config['source_folder'] = self.config['name'] + "-" + self.config['version'] # Log informations self.logger.info("Name: %(name)s", self.config) self.logger.info("Source: %(source)s", self.config) self.logger.info("Source folder: %(source_folder)s", self.config) self.logger.info("Version: %(version)s", self.config) self.logger.info("Release: %(release)s", self.config) self.logger.info("Buildrequires: %s", ", ".join(self.config['deps'])) return True
def get_rpm_hdr(rpm_file): """ Возращаем заголовок пакета RPM """ ts = rpm.ts() ts.setVSFlags(-1) fdno = os.open(rpm_file, os.O_RDONLY) try: hdr = ts.hdrFromFdno(fdno) except rpm.error: fdno = os.open(rpm_file, os.O_RDONLY) ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES) hdr = ts.hdrFromFdno(fdno) os.close(fdno) return hdr
def fetch_spec_sources(self): tset = rpm.ts() spec = tset.parseSpec(self.specfile) for source in spec.sources: srcuri = source[0] srcno = source[1] srcname = os.path.basename(srcuri) if not os.path.exists(srcname): if urlparse.urlparse(srcuri)[0]: logging.info("Downloading Source%i: %s from %s", srcno, srcname, srcuri) fetch_file(srcuri) else: logging.warn("Unable to obtain Source%i: %s", srcno, srcname)
def get_rpm_info(rpm_file): ts = rpm.ts() fdno = os.open(rpm_file, os.O_RDONLY) try: hdr = ts.hdrFromFdno(fdno) except rpm.error: ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES) hdr = ts.hdrFromFdno(fdno) os.close(fdno) info = { 'name': hdr[rpm.RPMTAG_NAME], 'ver': "%s-%s" % (hdr[rpm.RPMTAG_VERSION], hdr[rpm.RPMTAG_RELEASE]) } return info
def __init__(self, file, timestamp=None, size=None, ext=1): File.__init__(self, file, timestamp, size) self.list = [] self.version = None self.release = None fd = os.open(file, os.O_RDONLY) try: ts = rpm.ts() ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES) rpminfo = ts.hdrFromFdno(fd) basename = rpminfo[rpm.RPMTAG_NAME] arch = rpminfo[rpm.RPMTAG_ARCH] self.version = rpminfo[rpm.RPMTAG_VERSION] self.release = rpminfo[rpm.RPMTAG_RELEASE] self.list = [basename, self.version, self.release, arch] except: pass os.close(fd) if not len(self.list): # Remove ext count extensions, the default is 1, but for # rolls we remove two (.diskN.iso) s = self.filename # name-ver-rpmver.arch.rpm for x in range(0, ext): i = string.rfind(s, ".") s = self.filename[:i] i = string.rfind(s, ".") self.list.append(s[i + 1:]) # get architecture string s = self.filename[:i] i = string.rfind(s, "-") # get RPM version string self.release = s[i + 1:] self.list.append(self.versionList(s[i + 1:])) s = self.filename[:i] i = string.rfind(s, "-") # get software version string self.version = s[i + 1:] self.list.append(self.versionList(s[i + 1:])) self.list.append(self.filename[:i]) # get package name self.list.reverse() # we built the list backwards
def get_rpm_info(rpm_file): """Returns rpm information by querying a rpm""" ts = rpm.ts() fdno = os.open(rpm_file, os.O_RDONLY) try: hdr = ts.hdrFromFdno(fdno) except rpm.error: fdno = os.open(rpm_file, os.O_RDONLY) ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES) hdr = ts.hdrFromFdno(fdno) os.close(fdno) return { 'name': hdr[rpm.RPMTAG_NAME], 'ver' : "%s-%s" % (hdr[rpm.RPMTAG_VERSION],\ hdr[rpm.RPMTAG_RELEASE]), 'epoch': hdr[rpm.RPMTAG_EPOCH],\ 'arch': hdr[rpm.RPMTAG_ARCH] }
def kernel_package_info(): """ Return the newest installed version of the currently running kernel package's info. """ boot_image_path = kernel_cmdline().get('BOOT_IMAGE') if boot_image_path is None: return None if is_debian(): # For apt-based distros. kernel_pkg = get_kernel_deb_package(boot_image_path) if kernel_pkg is not None: match = DEBIAN_KERNEL_PKG_NAME_RE.match(kernel_pkg.name) if match: name_parts = match.groups( ) # E.g. ('linux-image-4.4.0-', '174', '-generic') latest_kernel_pkg = get_latest_same_kernel_deb( name_parts[0], name_parts[2]) return { 'name': latest_kernel_pkg.name, 'version': latest_kernel_pkg.installed.version, 'source_name': latest_kernel_pkg.installed.source_name, 'source_version': latest_kernel_pkg.installed.source_version, 'arch': latest_kernel_pkg.installed.architecture } elif is_amazon_linux2(): # For Amazon Linux 2. import rpm ts = rpm.ts() package_iterator = ts.dbMatch('name', 'kernel') if package_iterator.count() > 0: latest_kernel_pkg = sorted( [package_header for package_header in package_iterator], key=cmp_to_key(rpm.versionCompare), reverse=True)[0] return { 'name': latest_kernel_pkg[rpm.RPMTAG_NAME].decode(), 'version': latest_kernel_pkg[rpm.RPMTAG_EVR].decode(), 'arch': latest_kernel_pkg[rpm.RPMTAG_ARCH].decode() if latest_kernel_pkg[rpm.RPMTAG_ARCH] is not None else 'noarch', # Looks like there's no source name/version in the rpm package info. # TEMP: pass package name and version. 'source_name': latest_kernel_pkg[rpm.RPMTAG_NAME].decode(), 'source_version': latest_kernel_pkg[rpm.RPMTAG_EVR].decode() } return None
def get_kernel_rpm_package(boot_image_path): """ Return an rpm package instance for the currently running kernel. """ import rpm ts = rpm.ts() package_iterator = ts.dbMatch() boot_image_path_bytes = boot_image_path.encode() packages = [ package_header for package_header in package_iterator if boot_image_path_bytes in package_header[rpm.RPMTAG_FILENAMES] ] if packages: return packages[0] return None
def test_multipleTransactions(self): for pl in self.first, self.second: for p in pl: if len(p) == 2: filename = p[0] else: filename = p ts = rpm.ts(self.topdir) f = open(filename) h = ts.hdrFromFdno(f.fileno()) ts.addInstall(h, filename, 'u') ts.check() ts.order() ts.run(runCallback, 1) del ts ts = rpm.ts(self.topdir) mi = ts.dbMatch() expected = ["simple", "simple2"] got = [] for h in mi: got.append('%(name)s' % h) got.sort() self.assertEqual(expected, got)
def getHeaders(self, prog): # FIXME (20050321): Solaris rpm 4.1 hack if sys.platform[:5] == "sunos": rpm.addMacro("_dbPath", sysconf.get("rpm-root", "/")) ts = rpm.TransactionSet() else: ts = rpm.ts(sysconf.get("rpm-root", "/")) mi = ts.dbMatch() for h in mi: if h[1000] != "gpg-pubkey": # RPMTAG_NAME yield h, mi.instance() prog.addTotal(1) prog.add(1) prog.show() prog.add(1)
def getHeader(self, pkg): filename = self._filenames[pkg.loaders[self]] filepath = os.path.join(self._dir, filename) file = open(filepath) # FIXME (20050321): Solaris rpm 4.1 hack if sys.platform[:5] == "sunos": rpm.addMacro("_dbPath", sysconf.get("rpm-root", "/")) ts = rpm.TransactionSet() else: ts = rpm.ts() try: h = ts.hdrFromFdno(file.fileno()) except rpm.error, e: iface.error("%s: %s" % (os.path.basename(filepath), e)) h = None