CentOS

Interacting with YUM on CentOS Using Python

YUM is a utility for handling software packages that are used by several well-known operating systems such as Fedora and CentOS. YUM simplifies the software installations on a CVM instance and makes it simple to download and install the programs which saves time and effort. YUM can determine, fetch, and install all available dependents packages automatically as it automatically resolves the dependency while updating, adding, or removing the packages. We will learn more about YUM using Python on the CentOS system in this article.

Example 1: Update the YUM Module Using Python in CentOS

As we know, YUM is a command-line tool that enables the users to automatically install and download the updated RPM packages after checking for updates.

#!/usr/bin/ python

import os
cmd = "yum update"
os.system(cmd)

 

Here, we update YUM with the Python file to check whether the YUM module is updated within the system or it requires an update. The Python script is given where YUM is used to update. We import the “os” module to interact with the CentOS operating system. Then, we use “cmd” to interpret the statement that is assigned to it. We assign a command to update YUM.

The script is saved in the file “script.py” here. When we execute this Python file, it starts to update YUM. The output prints the statement which states that “no packages marked for update” because YUM is already updated in CentOS.

[root@Linux01 kalsoom]# python script.py
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.dc.uz
 * extras: mirror.dc.uz
 * updates: mirror.dc.uz
No packages marked for update

 

Example 2: Check for the YUM Configuration Using Python in CentOS

The previous approach is to update the YUM package using the Python script by interacting with CentOS. Now, the “yum” Python package must be imported to begin using YUM in programming which is preinstalled in the CentOS operating system. YUM is used to examine the YUM configuration in this case.

#!/usr/bin/ python

import os
import sys
import yum

yb = yum.YumBase()

print "****yum config file****"
print yb.conf.config_file_path

print "****logfile path****"
print yb.conf.logfile

print "****repos directories and files****"
for i in yb.conf.reposdir : print i

print "****skip-broken parameter****"
print yb.conf.skip_broken

print "****set the level of errors****"
print yb.conf.errorlevel

 

The previous source code is imported with the “yum” module along with the other required modules. After that, we create the “yb” variable where the yumBase() base class of the “yum” module is deployed. We use this line of code in each of our example scripts because YumBase is the root class that contains the methods and objects that are needed to carry out all of the package management operations using YUM.

The print statements are given to check the configuration of YUM. First, we have a print statement that displays the path of the YUM configuration file. Then, we set a print command of logFile using the “yb” variable to fetch the logfile path of YUM. After that, we use the “reposdir” command to output the repository’s directories and files. Then, we have a YUM command of a skip-broken parameter which returns false by default when no Boolean value is specified to it. When the skip-broken parameter is given to a yumBase configuration command with a true value, it enables YUM to automatically exclude the damaged packages from updates without the user input. The last command of the YUM configuration is “errorlevel” to set the level of error. It has a value that ranges from zero and 10, where 0 is only important and 10 comprises debug.

The YUM configuration prints the configuration file of YUM on the terminal. We also get the logFile path of the YUM configuration. Along with this, the directories and files of the YUM repository are displayed. Then, we obtain the false value from the skip-broken command since no value is set against this. The level of error results is also generated as “2”.

[root@Linux01 kalsoom]# python script.py
****yum config file****
Loaded plugins: fastestmirror, langpacks
/etc/yum.conf
****logfile path****
/var/log/yum.log
****repos directories and files****
/etc/yum/repos.d
/etc/yum.repos.d
****skip-broken parameter****
False
****set the level of errors****
2

 

Example 3: Search for Packages from the YUM Repositories Using Python in CentOS

YUM, in this case, is utilized to locate and print the YUM repositories for the specified packages.  We use some different methods of the yumBase class of the following YUM package:

import os
import sys
import yum

yb = yum.YumBase()
yb.setCacheDir()

results = yb.pkgSack.returnNewestByNameArch(patterns=["python", "php"])

for pkg in results:
    print "%s %s (%s) \n\t%s" % (pkg.name, pkg.version, pkg.arch, pkg.summary)

 

After deploying the YUM module in the previous Python script, we insert the yum.YumBase() line to begin the code. Then, we define the setCacheDir() command of yumBase to establish a new Yum cache directory to perform all operations as a regular user as well as the root user. The next line of the script is defined with the “results” variable where we use the yumBase which is invoked with the “pkgScak” command to run the queries on all accessible repositories. After that, we use the “returnNewstByNameArch” function of the “pkhScak” attribute of YUM. The returnNewstByNameArch() method retrieves the package objects that match the “python” or “PHP” pattern strings.

The information that is related to “python” and “PHP” packages is printed on the CentOS terminal by the YUM module.

[root@Linux01 kalsoom]# python script.py
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.dc.uz
 * extras: mirror.dc.uz
 * updates: mirror.dc.uz
python 2.7.5 (x86_64)
An interpreted, interactive, object-oriented programming language
php 5.4.16 (x86_64)
PHP scripting language for creating dynamic web sites

 

Example 4: Install the Package with the YUM Method Using Python in CentOS

The installation of the package with YUM requires setting up and completing a transaction. The YumBase class of the YUM module provides the method to install the packages in CentOS using Python.

import os
import sys
import yum

yb = yum.YumBase()
yb.install(name='php')

yb.resolveDeps()
yb.processTransaction()

 

We deploy the yumBase() class in the “yb” variable after importing the YUM modules in the previous Python script. Then, we utilize the yb.install command to simply install the specified package. The yb.install() method takes the “name” as an argument to which we assign the “php” package name to set the transaction. After that, to initiate the resolution of dependencies, the “yb.resolveDeps()” method is called. Next, the processTransaction() method triggers the installation of the “php” package.

We can see in the output that the PHP package is successfully installed in the CentOS operating system by interacting with the YUM methods.

[root@Linux01 kalsoom]# python script.py
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.dc.uz
 * extras: mirror.dc.uz
 * updates: mirror.dc.uz
Package php-5.4.16-48.el7.x86_64 already installed and latest version
Running Transaction Check

 

Example 5: Set a Repository at a Given Location with the YUM Method Using Python in CentOS

YUM can be configured to retrieve a repository at any location. It expects to accept a command line parameter which contains the repository’s url.

import os
import sys
import yum

url = sys.argv[1]

yb = yum.YumBase()
if not yb.setCacheDir(force=True, reuse=False): print >>sys.stderr, "enable to create a tmp. cachedir. "
sys.exit(1)

yb.repos.disableRepo('*')

yb.add_enable_repo('NewRepos', [url])

 

Here, we employ the yb.repos.disableRepo(‘*’) method which disables all the repositories as we give the asterisk symbol inside it. After that, we call the yb.add_enable_repo() method to enable the given repository where the url is set.

The previous Python script, when interacting with YUM, sets the given repository at the specific location on CentOS which generates the following results:

[root@Linux01 kalsoom]# python script.py url://to/New/Repos
Loaded plugins: fastestmirror, langpacks

 

Conclusion

This guide explored ways to use the Python API of the YUM package. We simply run the um command from the Python script to update the operation. Then, we utilized the YUM package for the configuration of YUM in CentOS. Furthermore, the specific package is queried using the YUM module. After that, we demonstrated the way to install the package with the YUM method of installation. In the same way, we can also uninstall that package using the yb.remove() method. In the end, we set the repository at the location by interacting with the YUM module in Python.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content