I decided to try to write a small script to search for a new version of the rpm package.

Here is the code:

#!/usr/bin/env python3.4 import os import sys import rpm def readRpmHeader(ts, filename): """ read an rpm header. """ fd = os.open(filename, os.O_RDONLY) h = None try: h = ts.hdrFromFdno(fd) except rpm.error as e: if str(e) == "error reading package header": sys.stderr.write(str(e)) h = None finally: os.close(fd) return h def main(argv): if len(argv) < 2: sys.stderr.write("Usage: %s PACKAGE_NAME...\n" % (argv[0],)) return 1 ts = rpm.TransactionSet() ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES | rpm._RPMVSF_NODIGESTS) fresh_rpms = {} for f in argv[1:]: if not os.path.exist(f): sys.stderr.write("Error: file %r was not found!" % f) return 1 h = readRpmHeader(ts, f) name = h[rpm.RPMTAG_NAME] if (name not in fresh_rpms or rpm.versionCompare(h, fresh_rpms[name]['header']) > 0): fresh_rpms[name] = {'header': h, 'filename': f} for n, v in fresh_rpms.iteritems(): print (v['filename']) return 0 if __name__ == '__main__': sys.exit(main(sys.argv)) 

Gives an error message

ImportError: No module named 'rpm'

I changed the version of python to 2.7 and to 3.3 - did not help ...

I ipython and did all the operations manually, everything works, import rpm did not swear and let it execute commands.

Here is what it is:

 rpm -qa | grep rpm rpm-build-4.11.1-25.el7.x86_64 python-deltarpm-3.6-3.el7.x86_64 atrpms-repo-7-7.el7.x86_64 redhat-rpm-config-9.1.0-68.el7.centos.noarch rpm-python-4.11.1-25.el7.x86_64 rpm-4.11.1-25.el7.x86_64 rpm-sign-4.11.1-25.el7.x86_64 perl-srpm-macros-1-8.el7.noarch rpm-libs-4.11.1-25.el7.x86_64 deltarpm-3.6-3.el7.x86_64 rpmforge-release-0.5.3-1.el7.rf.x86_64 rpm-build-libs-4.11.1-25.el7.x86_64 

How can I solve the problem? or what, apart from laying between the monitor and the chair, is the problem?

But rpm -ql rpm-python shows. what is such a module.

 # rpm -ql rpm-python /usr/lib64/python2.7/site-packages/rpm /usr/lib64/python2.7/site-packages/rpm/__init__.py /usr/lib64/python2.7/site-packages/rpm/__init__.pyc /usr/lib64/python2.7/site-packages/rpm/__init__.pyo /usr/lib64/python2.7/site-packages/rpm/_rpmbmodule.so /usr/lib64/python2.7/site-packages/rpm/_rpmmodule.so /usr/lib64/python2.7/site-packages/rpm/_rpmsmodule.so /usr/lib64/python2.7/site-packages/rpm/transaction.py /usr/lib64/python2.7/site-packages/rpm/transaction.pyc /usr/lib64/python2.7/site-packages/rpm/transaction.pyo 

Checked in folders /usr/lib64/python3/site-packages/ and /usr/lib64/python3.4/site-packages/ - in both there is no such.

but running inside the python3.4 interpreter

 help('modules') # получаю в ответ что rpm есть 

Then I overloaded the machine and get an error when starting the script

 # запуск скрипта с папкой с rpm пакетами ./rpm.py /home/firefedot/pts/files/ Traceback (most recent call last): File "./rpm.py", line 46, in <module> sys.exit(main(sys.argv)) File "./rpm.py", line 27, in main ts = rpm.TransactionSet() AttributeError: 'module' object has no attribute 'TransactionSet' 

there is no type atribatu, but it is worth running the same script or any IDE (I have NetBeans ) in which there is an import rpm - I get:

 File "/home/firefedot/NetBeansProjects/hello/src/hello.py", line 5, in <module> import rpm ImportError: No module named 'rpm' 

If you run with debugger system

 gdb ./rpm.py /home/firefedot/pts/files/ "/home/firefedot/pts/./rpm.py": not in executable format: Формат файла не распознан "/home/firefedot/pts/files/389-ds-base-devel-1.3.3.1-15.el7_1.x86_64.rpm" is not a core dump: Формат файла не распознан 

Then he swears at the first file, although the code says to look from the second

 for f in argv[1:]: if not os.path.exist(f): sys.stderr.write("Error: file %r was not found!" % f) return 1 

but it doesn't use rpm either.

And if you run ipython and run pdb in it, this is what it says:

 In [1]: import pdb In [2]: pdb.run('rpm.py /home/firefedot/pts/files') > <string>(1)<module>() (Pdb) c --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-2-47133b27bb55> in <module>() ----> 1 pdb.run('rpm.py /home/firefedot/pts/files') /usr/lib64/python2.7/pdb.pyc in run(statement, globals, locals) 1236 1237 def run(statement, globals=None, locals=None): -> 1238 Pdb().run(statement, globals, locals) 1239 1240 def runeval(expression, globals=None, locals=None): /usr/lib64/python2.7/bdb.pyc in run(self, cmd, globals, locals) 398 cmd = cmd+'\n' 399 try: --> 400 exec cmd in globals, locals 401 except BdbQuit: 402 pass <string> in <module>() NameError: name 'rpm' is not defined 

not defined / but says rpm .

I have a CentOS7x64 system, tried the same thing on Ubuntu 15.04 - the same result ...

What could be how to show him that he saw everything?

  • to the word: ipython and python are “two big differences”. - aleksandr barakin
  • So in the course ... I checked it everywhere .. but it doesn’t get any simpler) - Sober
  • rename the file did not try? - aleksandr barakin
  • I tried, the result is the same - Sober
  • By the way, I tried to run your script on a couple of machines - syntax error in the except ... line except ... - aleksandr barakin

1 answer 1

In general, I solved the problem in Python 2.7.5 (default, Jun 24 2015, 00:41:19)

Surprisingly, I had to tinker a bit ... here’s the final code, in which except was removed and the entire try block commented out the lines for clarity. (then everything will be back and the code will work almost completely, but it will reveal the problem, and the fact that the code is clumsy is already solved)

 #!/usr/bin/env python2.7 import os import sys import rpm def readRpmHeader(ts, filename): print("fnc-readRpmHeader") """ read an rpm header. """ fd = os.open(filename, os.O_RDONLY) h = None try: h = ts.hdrFromFdno(fd) except rpm.error, e: if str(e) == "error reading package header": sys.stderr.write(str(e)) h = None finally: os.close(fd) return h def main(argv): if len(argv) < 2: sys.stderr.write("Usage: %s PACKAGE_NAME...\n" % (argv[0],)) return 1 ts = rpm.TransactionSet() ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES | rpm._RPMVSF_NODIGESTS) fresh_rpms = {} for f in argv[1:]: if not os.path.exists(f): sys.stderr.write("Error: file %r was not found!" % f) return 1 h = readRpmHeader(ts, f) name = h[rpm.RPMTAG_NAME] if (name not in fresh_rpms or rpm.versionCompare(h, fresh_rpms[name]['header']) > 0): fresh_rpms[name] = {'header': h, 'filename': f} for n, v in fresh_rpms.iteritems(): print (v['filename']) return 0 if __name__ == '__main__': sys.exit(main(sys.argv)) 

The most incomprehensible. this is something that this code with minor edits to disable tags and except wrap on python2.7, which is out of the box on CentOS7. And as soon as he added Python3 to that car, he immediately stopped working ... it is not clear what exactly the problem and conflict is. And it is interesting that the code worked once on ubuntu 15.04, as I wrote in the comment above, after which I stopped seeing the rpm methods.

I’ll say the result ... that on a raw machine with version 2.7, the code works as it should. I can make a conclusion, maybe it’s wrong, but in version 3, they either refuse rpm / yum and the like, or put it in some other module.

Thanks for helping out. I think you can somehow close this thread. thanks again.