replacing sections of a string of stderr from subprocess

This is the place for queries that don't fit in any of the other categories.

replacing sections of a string of stderr from subprocess

Postby metulburr » Thu Apr 25, 2013 8:40 am

I could of swore this wouldo of worked, but i threw up print funcs all over trying to come up with why it is not working. I get try to str.replace, but the function returns the string as if str.replace was never used
code:
Code: Select all
def command(cmd=None, install_packages=None):
   if install_packages:
      cmd = 'sudo apt-get install {}'.format(install_packages)
      installing = True
   else:
      installing = False
   print(cmd)
   proc = subprocess.Popen(cmd.split(), stderr=subprocess.PIPE)
   error_string = proc.communicate()[1].decode()
   if installing and error_string:
      package_remove = []
      error_list = error_string.split('\n')
      print(error_list)
      for error in error_list:
         if 'Unable to locate package' in error:
            package_remove.append(error.split()[-1])
         print(error)
         
      proc.wait()
      print(package_remove)

      for bad_package in package_remove:
         print('replacing "{}" from str "{}"'.format(bad_package, install_packages))
         install_packages.replace(bad_package, '')
         
      print(install_packages)
      return install_packages
   else:
      proc.wait()


output:
setting up basic
sudo apt-get install non-existing-package non-existing-package2 non-existing-package3 man-db
[sudo] password for metulburr:
Reading package lists... Done
Building dependency tree
Reading state information... Done
['E: Unable to locate package non-existing-package', 'E: Unable to locate package non-existing-package2', 'E: Unable to locate package non-existing-package3', '']
E: Unable to locate package non-existing-package
E: Unable to locate package non-existing-package2
E: Unable to locate package non-existing-package3

['non-existing-package', 'non-existing-package2', 'non-existing-package3']
replacing "non-existing-package" from str "non-existing-package non-existing-package2 non-existing-package3 man-db"
replacing "non-existing-package2" from str "non-existing-package non-existing-package2 non-existing-package3 man-db"
replacing "non-existing-package3" from str "non-existing-package non-existing-package2 non-existing-package3 man-db"
non-existing-package non-existing-package2 non-existing-package3 man-db


the install_packagesargument is a string:
Code: Select all
sudo apt-get install non-existing-package non-existing-package2 non-existing-package3 man-db

and executing it in the shell, but when you execute a non existant or removed package, the program stops, so to coiunter this, i figured to remove the packages from the stderr output, which appears to be working up to the point where i return the new string. In which it still contains the non-existant packages in tthe string, where the string is:
Code: Select all
non-existing-package non-existing-package2 non-existing-package3 man-db


I really didnt htink i would have aproblems iwth this, as its a simple .str.replace()
Code: Select all
install_packages.replace(bad_package, '')


So the question i have is: Why is the string being returned still have: ?
Code: Select all
non-existing-package non-existing-package2 non-existing-package3

my expected output is just:
Code: Select all
' man-db'


EDIT:
I modified the code to be get the same unexpected output:
Code: Select all
import subprocess

def command(cmd=None, install_packages=None):
   if install_packages:
      cmd = 'sudo apt-get install {}'.format(install_packages)
      installing = True
   else:
      installing = False
   print(cmd)
   proc = subprocess.Popen(cmd.split(), stderr=subprocess.PIPE)
   error_string = proc.communicate()[1].decode()
   if installing and error_string:
      packages_to_remove = []
      error_list = error_string.split('\n')
      print('error_list: {}'.format(error_list))
      for error in error_list:
         if 'Unable to locate package' in error:
            test = packages_to_remove.append(error.split()[-1])
            print('test is {}'.format(test))
         print(error)
         
      proc.wait()
      print('packages_to_remove: {}'.format(packages_to_remove))
      print('install_packages: {}'.format(install_packages))
      for bad_package in packages_to_remove:
         print(type(bad_package))
         print(bad_package)
         print('replacing "{}" from str "{}"'.format(bad_package, install_packages))
         install_packages.replace(bad_package, '')
         
      print('install_packages: {}'.format(install_packages))
      return install_packages
   else:
      proc.wait()
      
command(install_packages='non-existing-package python3')
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1312
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: replacing sections of a string of stderr from subprocess

Postby micseydel » Thu Apr 25, 2013 9:33 am

Are you thinking that str.replace() modifies the string in-place? Remember, strings are immutable, any methods on them that have to do with making a different string will return a new one.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1119
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: replacing sections of a string of stderr from subprocess

Postby metulburr » Thu Apr 25, 2013 9:40 am

OMG i am retarded. I need to stop programming in the middle of the night. Thanks
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1312
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 3 guests