{"id":220,"date":"2012-03-04T11:12:32","date_gmt":"2012-03-04T11:12:32","guid":{"rendered":"http:\/\/www.plugged.in\/?p=220"},"modified":"2012-03-04T11:12:32","modified_gmt":"2012-03-04T11:12:32","slug":"using-the-python-api-for-yum","status":"publish","type":"post","link":"https:\/\/www.veriteknik.net.tr\/en\/using-the-python-api-for-yum\/","title":{"rendered":"Using the Python API for Yum"},"content":{"rendered":"<p>Here&#8217;s something very basic yet poorly documented. That&#8217;s why it looks like almost everybody&#8217;s trying to hack their way into this module, the yum module for Python.<\/p>\n<p>First of all don&#8217;t forget that this module is only available if you&#8217;re using a Red Hat branch of distribution (fedora, centos etc.) For other distributions, such as Debian, there are other api&#8217;s, such as apt itself.<\/p>\n<p>Below you&#8217;ll find some basic ways of getting configuration settings of the current yum.<\/p>\n<pre class=\"brush: python; gutter: true; first-line: 1\">import yum\nyb = yum.YumBase()\nprint yb.conf.logfile # this will obviously printout the logfile's path\nfor i in yb.conf.reposdir : print i # and this will printout the directories and files for the repositories\nprint yb.conf.skip_broken # usually false. when set to true, your yum commands will take action of is the --skip-broken parameters was given to the yum itself.\nprint yb.conf.errorlevel # this is the level of errors you'd like to get as an output. it's between 0-10 and 0 is only critical ones, while 10 is more like a debug feature. Usually it is set to default 2, but since you'll be running in a script, after your script gets stable, its a good idea to set this to 0 and then distribute it.\nprint yb.conf.config_file_path # obvious again, the file path for your yum's config file.<\/pre>\n<p>&nbsp;<\/p>\n<p>for more configuration options, you can always see like this;<\/p>\n<pre class=\"brush: python; gutter: true; first-line: 1\">dir(yb.conf)<\/pre>\n<p>and don&#8217;t forget this perfect module, to reverse engineer some function if you don&#8217;t know what arguments it gets and if it&#8217;s poorly documented (ex: it doesn&#8217;t have a valid __doc__ attribute) :<\/p>\n<pre class=\"brush: python; gutter: true; first-line: 1\">import inspect\ninspect.getargspec(somefunction)<\/pre>\n<p>And about the arch method. What yum knows about our architecture.<\/p>\n<pre class=\"brush: python; gutter: true; first-line: 1\">import yum\nyb = yum.YumBase()\nprint yb.arch.compatarches # these are the compatible architectures with our system.\n# below are the arch types that yum thinks we are usig (hopefully true!)\nprint yb.arch.canonarch\nprint yb.arch.basearch\n# With the function below, you can get a list of architectures which are compatible with each other.\n# Let's say you're using an x86_64 arch, then u can use packages from these ones,\nyb.arch.get_arch_list('x86_64')\n['x86_64', 'athlon', 'i686', 'i586', 'i486', 'i386', 'noarch']<\/pre>\n<p>If you&#8217;d like to get the currently installed packages on your system, rpm is the module you&#8217;d like to load.<\/p>\n<pre class=\"brush: python; gutter: true; first-line: 1\">import rpm\nbold = \"\\033[1m\"\nreset = \"\\033[0;0m\"\ntrans = rpm.TransactionSet()\nfor header in trans.dbMatch() : print \"%s%s%s-%s:%s-%s.%s%s%s\" % (bold,header['name'],reset,header['epochnum'],header['version'],header['release'],bold,header['arch'],reset)<\/pre>\n<p>Note that this script outputs various parts in bold. You can use this in any script you&#8217;d like.<\/p>\n<p>Yet here&#8217;s a nice method to install new packages.<\/p>\n<pre class=\"brush: python; gutter: true; first-line: 1\">import yum\nyb=yum.YumBase()\nsearchlist=['name']\narg=['gedit']\nmatches = yb.searchGenerator(searchlist,arg)\nfor (package, matched_value) in matches :\n    if package.name == 'gedit' : yb.install(package)\n    yb.buildTransaction()\n    yb.processTransaction()<\/pre>\n<p>&nbsp;<\/p>\n<p>Experiment, enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s something very basic yet poorly documented. That&#8217;s why it looks like almost everybody&#8217;s trying to hack their way into this module, the yum module for Python. First of all don&#8217;t forget that this module is only available if you&#8217;re using a Red Hat branch of distribution (fedora, centos etc.) For other distributions, such as [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[370,448,450],"tags":[447,449,424],"yst_prominent_words":[],"class_list":["post-220","post","type-post","status-publish","format-standard","hentry","category-linux","category-programm-in","category-python-programming","tag-api","tag-python-2","tag-yum"],"jetpack_featured_media_url":"","uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Mustafa Emre Ayd\u0131n","author_link":"https:\/\/www.veriteknik.net.tr\/en\/author\/eaydin\/"},"uagb_comment_info":0,"uagb_excerpt":"Here&#8217;s something very basic yet poorly documented. That&#8217;s why it looks like almost everybody&#8217;s trying to hack their way into this module, the yum module for Python. First of all don&#8217;t forget that this module is only available if you&#8217;re using a Red Hat branch of distribution (fedora, centos etc.) For other distributions, such as&hellip;","_links":{"self":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/posts\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/comments?post=220"}],"version-history":[{"count":0,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/posts\/220\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/media?parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/categories?post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/tags?post=220"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/yst_prominent_words?post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}