Skip to content

Instantly share code, notes, and snippets.

@farukuzun
Created July 15, 2014 11:35
Show Gist options
  • Save farukuzun/134a12397ac2609bbfc6 to your computer and use it in GitHub Desktop.
Save farukuzun/134a12397ac2609bbfc6 to your computer and use it in GitHub Desktop.
pip_find.py
from __future__ import absolute_import
import os
import re
import argparse
from functools import partial
import logging
import sys
import json
try:
import urllib2 as urllib_request # Python2
except ImportError:
import urllib.request as urllib_request
from pkg_resources import parse_version
try:
from subprocess import check_output as _check_output
except ImportError:
import subprocess
def _check_output(*args, **kwargs):
process = subprocess.Popen(stdout=subprocess.PIPE, *args, **kwargs)
output, _ = process.communicate()
retcode = process.poll()
if retcode:
error = subprocess.CalledProcessError(retcode, args[0])
error.output = output
raise error
return output
check_output = partial(_check_output, shell=True)
try:
import __builtin__
input = getattr(__builtin__, 'raw_input') # Python2
except (ImportError, AttributeError):
pass
def get_installed_pkgs_via_pip(local=False):
logger = logging.getLogger(u'pip-review')
command = 'pip freeze'
if local:
command += ' --local'
output = check_output(command).decode('utf-8')
python_dirs = os.listdir("/usr/local/lib/")
pkgs_from_dir = []
for python_dir in python_dirs:
if "python" in python_dir:
try:
pkgs_from_dir += os.listdir("/usr/local/lib/" + python_dir + "/dist-packages")
except:
logger.warning('Not a valid dir')
for line in output.split('\n'):
if not line or line.startswith('##'):
continue
if line.startswith('-e'):
name = line.split('#egg=', 1)[1]
if name.endswith('-dev'):
name = name[:-4]
p_dir = str(name) + "-" + str(version) + ".egg-info"
if p_dir in pkgs_from_dir:
yield name, 'dev', 'dev', True
else:
name, version = line.split('==')
p_dir = str(name) + "-" + str(version) + ".egg-info"
if p_dir in pkgs_from_dir:
yield name, version, False
for i in get_installed_pkgs_via_pip():
print i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment