2

I am trying to write a python script to check and install packages in linux. I have two questions:

First, I tried to write the below code to check the packages, but non is working. What I am missing is how can I convert printing into terminal to a variable that i can detect. Please find the code.

Trial #1

import apt
import os

x = os.system('apt-cache policy python3.6-dev')
if x>0:
    print('installed')
else:
    print('not installed')

Trial #2

import os 

os.system('dpkg-query -l | grep python3.6-dev | wc -l')
x = os.system('echo $?')
if x>0:
    print('installed')
else:
    print('not installed')

Second, I am tried to write a python script to install. But, for linux we always use sudo. I wrote this code in the past which uses gksudo. It was working but now it's not working because they removed gksudu. How can I modify this code and what else can i use instead of gksudo

File #1 (call RAM.py file)

import os
os.system('gksudo python RAM.py')

File #2 (RAM file)

import os

# install
os.system('sudo apt-get install python3.6-dev')
Dr. Zezo
  • 395
  • 2
  • 17
  • 1
    ```os.system``` returns 0 if the command you're executing has been successful, and non-zero otherwise – RishiC Apr 07 '20 at 06:15
  • I know, the question is how can i use this output in if statement?. assigning to x variable is not working!! – Dr. Zezo Apr 07 '20 at 06:55

1 Answers1

0

First of all, I would recommend you to use subprocess module instead of os. In order to execute a shell command and capture the return code, stdout and stderr, you can try:

from subprocess import run, PIPE
res = run('apt-cache policy python3.6-dev', shell=True, stdout=PIPE, stderr=PIPE, check=True)
print(res.returncode, res.stdout, res.stderr)

where check(optional) is to raise exception if the execution failed (returncode>0).

In order to run a command with sudo, I would suggest the following:

password = "my_password"
base_command = "ls -l" # replace it with your command
command_with_sudo = f"echo {password} | sudo -S {base_command}"
res = run(command_with_sudo, shell=True, check=True, stdout=PIPE, stderr=PIPE)

Note that it's very insecure to store your password as a plain text in your code so try to avoid it (you can provide it as an argument to your script/ store it in your environment/ encrypt it somehow - just don't leave it as is in your code)

Gabio
  • 9,126
  • 3
  • 12
  • 32
  • Thanks a lot. Is there anyother way to replace a gksudo. Or force install gksudo?? Is linux community replaces gksudo with other interface. Thanks – Dr. Zezo Apr 07 '20 at 06:58
  • I had this error: Capture_output is not working.Traceback (most recent call last): File "/home/zezo/Documents/My_GUI/untitled66.py", line 3, in res = subprocess.run('apt-cache policy python3.6-dev', shell=True, capture_output=True, check=True) File "/usr/lib/python3.6/subprocess.py", line 423, in run with Popen(*popenargs, **kwargs) as process: TypeError: __init__() got an unexpected keyword argument 'capture_output' – Dr. Zezo Apr 07 '20 at 07:01
  • capture_output is for python3.7+. Which python version do you use? – Gabio Apr 07 '20 at 07:03
  • and instead of gksudo, you can use pkexec – Gabio Apr 07 '20 at 07:04
  • I have python3.6 – Dr. Zezo Apr 07 '20 at 07:04
  • I've updated the solution for your environment. Let me know if it works for you – Gabio Apr 07 '20 at 07:07
  • Thanks a lot. Sorry, but I still can't find a variable to use in if statement. The firts variable res.returncode (gives 0 whether installed or not). The second variable res.stdout (gives many thing among, installed (None)). How can I extract this information so I can use in if statement whether to install or pass. Thanks – Dr. Zezo Apr 07 '20 at 07:15
  • the returncode depends on if your command was executed successfully or not and in your case it will whether the packages installed or not (if you try to remove a file that doesn't exist, you will see that your return code will be != 0). So basically I would parse the stdout and check based on it. For example, you can run `dpkg -s package_name | grep Status` and check that the stdout matches `Status: install ok installed` – Gabio Apr 07 '20 at 07:26