Monday, July 11, 2022

[SOLVED] install .rpm or .msi file through python script

Issue

I'm a newbie to python. I have a python script to download a rpm file from S3 bucket.

import platform
import boto3
import botocore

BUCKET_NAME = 'tempdownload'
KEY = 'temp.rpm' # packaged using golang

platformName = platform.system()
s3 = boto3.resource('s3')

print(platformName)

if platformName == 'Linux':
    try:
        bucket = s3.Bucket(BUCKET_NAME)
        bucket.download_file(KEY, 'temp.rpm')
    except botocore.exceptions.ClientError as e:
        if e.response['Error']['Code'] == "404":
            print("The object does not exist.")
        else:
            raise
else:
    print("not valid operating system")

I want to add a script in the same file to install the downloaded rpm file or msi package for windows. I tried searching online but didn't get any information. Would appreciate if anyone provide some pointers.

Thanks!


Solution

You have to call system command - rpm -Ivh yourpackage.rpm

import subprocess
package_path = '/home/mypackage.rpm'
command = ['rpm', '-Ivh', package_path]
p = subprocess.Popen(command)
p.wait()
if p.returncode == 0:
    print("OK")
else:
    print("Something went wrong")


Answered By - Szymon P.
Answer Checked By - Robin (WPSolving Admin)