Skip to content

Instantly share code, notes, and snippets.

@mdippery
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdippery/9611506 to your computer and use it in GitHub Desktop.
Save mdippery/9611506 to your computer and use it in GitHub Desktop.
Quick script to determine if a set of Python packages work with Python 3
#!/usr/bin/env python
## Accepts a list of packages on stdin and prints whether each
## package is compatible with Python 3. The easiest way to
## use this script is by piping the output of `pip freeze`
## into it:
##
## $ pip freeze | py3-ready
##
##
## Author: Michael Dippery <michael@monkey-robot.com>
import requests
import json
import sys
from termcolor import colored
def pypi_url(package, version=None):
url = "https://pypi.python.org/pypi/%s/" % package
if version:
url = url + version + "/"
url += "json"
return url
def is_pypi_package(p):
forbidden_prefixes = ["-e", "## !!"]
for prefix in forbidden_prefixes:
if p.startswith(prefix):
return False
resp = requests.head(pypi_url(p))
return resp.status_code == requests.codes.ok
def is_py3_compatible(package, version=None):
url = pypi_url(package, version)
resp = requests.get(url)
if resp.status_code != requests.codes.ok:
print >>sys.stderr, "Could not find data for %s %s" % (package, "v" + version if version else "")
return False
data = resp.json()
for classifier in data['info']['classifiers']:
if classifier.startswith("Programming Language :: Python :: 3"):
return True
return False
def main():
for package_line in sys.stdin.readlines():
package_line = package_line.strip()
package_data = package_line.split("==")
package, version = None, None
if len(package_data) == 2:
if is_pypi_package(package_data[0]):
package, version = package_data
elif len(package_data) == 1:
if is_pypi_package(package_data[0]):
package = package_data[0]
if package:
if is_py3_compatible(package, version):
label = "is"
color = 'green'
else:
label = "is NOT"
color = 'red'
status = "%s %s Python 3-compatible" % (package, label)
status = colored(status, color)
print status
else:
print >>sys.stderr, "Invalid package: %s" % package_data
return 0
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment