It seems like it should be available, but I just can't seem to find it.
Something like:
pkg = rpm.Package(name="some package")
dependencies = pkg.dependencies()
Is there something like this available, that would be cleaner than what I'm doing now?
Currently, I'm wrapping the rpm command with subprocess and manually parsing the output:
cmd = "rpm -qRp {file} | sort | uniq".format(file=filename)
cmd_output = subprocess.check_output(cmd, shell=True)
# ... long parse of cmd_output
Following scipt will list all Requires from a package provided on commandline (full path to rpm file):
import os
import rpm
import sys
ts = rpm.TransactionSet()
fd = os.open(sys.argv[1], os.O_RDONLY)
h = ts.hdrFromFdno(fd)
os.close(fd)
for dep in h[rpm.RPMTAG_REQUIRENAME]:
print dep
Or alternatively to work with package in rpm database:
import os
import rpm
import sys
ts = rpm.TransactionSet()
mi = ts.dbMatch('name', sys.argv[1])
for ind in range(mi.count()):
h = mi.next()
for dep in h[rpm.RPMTAG_REQUIRENAME]:
print dep
Related
is there any equivalent of
::set-output name=dir::sth
in python?
I've found module sh and I'm able to set it like this:
from sh import echo
echo(f"::set-output name=dir::sth")
but I'm wondering if there is any build in solution in python.
The python subprocess module is part of the standard library. I had success with it in GitHub actions by calling its Popen function:
steps:
- name: Create matrix object
id: create-matrix
shell: python
run: |
from os import getenv
from subprocess import Popen
import json
matrix_obj = {"prod-environment":[], "nice-app":[], "app-platform": []}
if getenv("ENV_PRODUCTION").lower() == "true":
matrix_obj["prod-environment"].append("production")
...
if getenv("APP_GA").lower() == "true":
matrix_obj["nice-app"].append("ga")
matrix_str = f'::set-output name=matrix::{json.dumps(matrix_obj)}'
print(f"Final matrix string: {matrix_str}")
Popen(['echo', matrix_str], bufsize=1)
env:
ENV_PRODUCTION: ${{inputs.env-production}}
APP_GA: ${{inputs.app-ga}}
...
I am trying to make a simple python program using GTK and gstreamer. For this purpose, I need the GES (Gstreamer Editing Services) but I seem to be unable to correctly install the dependencies I need.
So far, I have installed (sudo apt-get install...) gstreamer1.0 which works fine. I have done the same thing with libges-1.0-dev and libges-1.0-0. However, when I try to import GES (from gi.repository import GES) in my python script, I get the following error:
ImportError: cannot import name GES, introspection typelib not found
I am guessing that I am missing something about how to actually install the package, but it seems that I just don't quite know my way around python and Linux as well as I should.
Run the following to verify all Prerequisites:
def Prerequisites():
import re
from subprocess import check_output, CalledProcessError, STDOUT
Desired = {'u':'Unknow', 'i':'Install', 'r':'Remove', 'h':'Hold'}
Status = {'n':'Not', 'i':'Inst', 'c':'Conf-files', 'u':'Unpacked', 'f':'halF-conf', 'h':'Half-inst', 'w':'trig-aWait', 't':'Trig-pend'}
re_pip3 = re.compile('(.+?): (.*?)\n', re.S + re.MULTILINE)
re_dpkg = re.compile('(.+?)\n', re.S + re.MULTILINE)
for n, package in enumerate(["python-gst-1.0", "python-gst-1.0", "gir1.2-gstreamer-1.0", "gir1.2-gst-plugins-base-1.0",
"gstreamer1.0-plugins-good", "gstreamer1.0-plugins-ugly", "gstreamer1.0-tools"]):
try:
if n in [0]:
output = check_output("pip3 show {}".format(package), shell=True, stderr=STDOUT).decode()
print('OK: Name: {s[Name]}, Version: {s[Version]}, Location: {s[Location]}'.
format(s=dict(re_pip3.findall(output))))
else:
output = check_output("dpkg -l {}".format(package), shell=True, stderr=STDOUT).decode()
for p in re_dpkg.findall(output)[6:]:
if p[0:2] == 'ii':
print('OK: {} - Desired:{},\tStatus:{}'.format(p, Desired[p[0]], Status[p[1]]))
else:
print('[FAIL] {} - Desired:{},\tStatus:{}'.format(p, Desired[p[0]], Status[p[1]]))
except CalledProcessError as exp:
print('[FAIL] {} - CalledProcessError: {}'.format(package, exp.output.decode()[:-1] or exp))
Please confirm that you want to do something like this: Simple
Dependencies:
* GStreamer core
* gst-plugins-base
GStreamervModules
Installing from local archives
This should be ok: "the only relevant result seems to be (gstreamer-player)"
Try the following:
from gsp import GstreamerPlayer
player = GstreamerPlayer(None)
player.queue("/path/to/audio.mp3")
The project site gives this:
Prerequisites
Debian/Ubuntu/Rasbian:
sudo apt-get install python-gst-1.0 \
gir1.2-gstreamer-1.0 gir1.2-gst-plugins-base-1.0 \
gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly \
gstreamer1.0-tools
Sublime Text 3 uses its own python3 here: /opt/sublime_text/python3.3.zip and I'm making a plugin and I want to import a python2.7 (python3 compatible) package from /usr/local/lib/python2.7/dist-packages
So I use:
sys.path.append('/usr/local/lib/python2.7/dist-packages')
from rauth import OAuth1Service
And I get this exception:
ImportError: cannot import name OAuth1Service
So I guess it's able to find rauth but I don't know why rauth isn't able to find its imports.
There were too many problems with using ST's python.
So I put the non-ST-related code in its own file and am using it like this:
main.py
MYPATH = os.path.dirname(os.path.abspath(__file__))
OTHER = os.path.join(MYPATH, 'other.py')
p = subprocess.Popen(['python', OTHER, arg, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
p.wait()
if p.returncode == 11: # set with sys.exit(11)
print('success')
else:
print (p.communicate()[1]) # 0 is output, 1 is errors
other.py
if __name__ == '__main__':
input_ = sys.argv[1]
# p = run the code
sys.exit(p.result)
How would I create builders that runs epydoc or/and pylint from a scons built?
You can use the Command() builder instead of creating your own builder.
For instance, you could execute epydoc as follows:
# SCons will substitute $SOURCE and $TARGET accordingly
# add any extra cmd line args you need to the cmd string
cmd = 'epydoc $SOURCE $TARGET'
env.Command(target = yourTarget, source = yourSourceFile_s, action = cmd)
Here is what I ended up using, based on Brady's answer.
## Create epydoc!
import os.path
if os.path.isfile('/usr/bin/epydoc'):
sources = Split("__init__.py ook/ eek/ fubar/")
cmd = "epydoc -q --name 'Isotek Python Module collection' " + \
"--html --inheritance listed --graph all -o docs --css white " + \
"--parse-only --debug $SOURCES"
env.Command(target = Dir('docs'), source = sources, action = cmd)
else:
print "WARNING -- Cannot run epydoc so documentation will not be generated."
print "WARNING -- To install epydoc run 'sudo yum -y install epydoc'."
Note that I am running on fedora and do not need to worry about the code running elsewhere thus I can assume the path and how to install epydoc. A more general edit is welcome.
Here is another method, probably more portable to large projects.
First, define epydoc.py in site_scons/site_tools (or where ever you have those) to be:
# -*- coding: utf-8 -*-
import SCons.Builder
import SCons.Action
def complain_epydoc(target, source, env):
print 'INFORMATION: epydoc binary was not found (see above). Documentation has not been built.'
def generate(env):
env['EPYDOC'] = find_epydoc(env)
if env['EPYDOC'] != None:
opts = '--quiet --html --inheritance listed --graph all --css white --parse-only '
env['EPYDOCCOM'] = '$EPYDOC ' + opts + '-o $TARGET $SOURCES'
env['BUILDERS']['epydoc'] = SCons.Builder.Builder(action=env['EPYDOCCOM'])
else:
env['BUILDERS']['epydoc'] = SCons.Builder.Builder(action=env.Action(complain_epydoc))
def find_epydoc(env):
b=env.WhereIs('epydoc')
if b == None:
print 'Searching for epydoc: not found. Documentation will not be built'
else:
print 'Searching for epydoc: ', b
return b
def exists(env):
if find_epydoc(env) == None:
return 0
return 1
In the main SConstruct file, add:
import epdoc
env.Tool("epydoc")
Then, in your SConstruct file or SConscript files, you can build documentation like so:
Alias('epydoc', env.epydoc(source=python_code_files, target=Dir('docs')))
Note: you could do the same thing for ctags and pylint, just to name a few.
I am using the subprocess module in Python (python 2.6) with the Popen method.
What I am trying to achieve:
I am trying to use the following bash command with Popen that will return a match if it finds a file with "stderr" string.
The code:
bash
find . -exec grep "stderr" {} +
what I am doing in python
from subprocess import Popen, PIPE
command = "find . -exec grep 'stderr' {} +"
stream = Popen(command, stdout=PIPE, shell=True, cwd=dir)
stream_stdout, stream_stderr = stream.communicate()
What I get bacK:
It looks to have worked as stream_stdout and stream_stderr return what I suspect,
but I am getting this txt sent to the screen:
find: missing argument to `-exec'
Any ideas why?
*EDIT:
I did not have a space between {}+ this is why I was getting the above out. Apologies! *
Cheers,
Mike
Try making your command a list of arguments:
command = "find . -exec grep 'stderr' {} +".split(" ")
Edit: Sorry, I didn't realize Popen() could take a string. This answer is not correct.
Why are you using the subprocess module?
Would this code, from Mr. Beazley, do the job?
import os
import fnmatch
import re
def gen_find(filepat,top):
for path, dirlist, filelist in os.walk(top):
for name in fnmatch.filter(filelist,filepat):
yield os.path.join(path,name)
def grep(pat,lines):
patc = re.compile(pat)
for line in lines:
if patc.search(line): return True
for f in gen_find("*","."):
fopen = open(f, 'rb')
if grep("stderr",fopen.readlines()):
print f