Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip packages that installed from RPM #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 61 additions & 14 deletions pip_autoremove.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,69 @@
from __future__ import print_function

import optparse

import os
import pip
import platform

from pkg_resources import working_set, get_distribution


__version__ = '0.9.0'
__version__ = '0.9.1'

try:
raw_input
except NameError:
raw_input = input


try:
dict.iteritems
except AttributeError:

def iteritems(d):
return iter(d.items())
else:

def iteritems(d):
return d.iteritems()


WHITELIST = ['pip', 'setuptools']

def get_package_manager():
managers = {
'rpm' : ['/etc/redhat-release'],
'dpkg' : ['/etc/lsb-release', '/etc/debian_version']
}

if platform.system() == 'Linux':
for pkg, files in iteritems(managers):
for identy in files:
if os.path.isfile(identy):
return pkg


def get_package(dist):
try:
manager = get_package_manager()
egg = os.path.join(dist.location, (dist.egg_name() + '.egg-info'))

if manager == 'rpm':
import rpm

ts = rpm.TransactionSet()
return ",".join([n['name'] for n in ts.dbMatch('basenames', egg)])

if manager == 'dpkg':
# TODO: Add dpkg support
pass

except ImportError:
pass

def autoremove(names, yes=False):
dead = list_dead(names)
if dead and (yes or confirm("Uninstall (y/N)?")):
if dead and (yes or raw_input("Uninstall (y/N)?") == 'y'):
for d in dead:
remove_dist(d)

Expand All @@ -44,7 +88,7 @@ def show_tree(dist, dead, indent=0, visited=None):
return
visited.add(dist)
print(' ' * 4 * indent, end='')
show_dist(dist)
show_dist(dist, get_package(dist))
for req in requires(dist):
if req in dead:
show_tree(req, dead, indent + 1, visited)
Expand All @@ -71,19 +115,22 @@ def fixed_point(f, x):
x = y


def confirm(prompt):
return raw_input(prompt) == 'y'


def show_dist(dist):
print('%s %s (%s)' % (dist.project_name, dist.version, dist.location))
def show_dist(dist, pkg=None):
if pkg:
print('*> %s %s (%s) (Package: %s)' % (dist.project_name,
dist.version, dist.location, pkg))
else:
print('%s %s (%s)' % (dist.project_name, dist.version, dist.location))


def remove_dist(dist):
pip.main(['uninstall', '-y', dist.project_name])
# Avoid duplicate output caused by pip.logger.consumers being configured
# over and over again
pip.logger.consumers = []
if not get_package(dist):
pip.main(['uninstall', '-y', dist.project_name])
# Avoid duplicate output caused by pip.logger.consumers being configured
# over and over again
pip.logger.consumers = []
else:
print("Skiped %s, module installed from package" % dist.project_name)


def get_graph():
Expand Down