Skip to content

Instantly share code, notes, and snippets.

@EnigmaCurry
Created March 25, 2011 02:31
Show Gist options
  • Save EnigmaCurry/886264 to your computer and use it in GitHub Desktop.
Save EnigmaCurry/886264 to your computer and use it in GitHub Desktop.
An easy_install that forces packages to undergo 2to3 before installing.
import os
import os.path
import urllib
import tarfile
import tempfile
import shlex
def easy_install_2to3(package_name):
"""A lot of packages work just fine in py3k if you run them
through 2to3, but for whatever reason the authors have been too
lazy to build 2to3 into their setup.py. This downloads the package
and runs 2to3 on it first, then installs it."""
import xmlrpc.client
from setuptools.command import easy_install
from lib2to3 import main as two2three
pypi = xmlrpc.client.ServerProxy("http://pypi.python.org/pypi")
version = pypi.package_releases(package_name)[0]
for p in pypi.release_urls(package_name,version):
if p["packagetype"] == "sdist":
url = p["url"]
break
else:
raise Exception("Can't install {0}. No URL to "
"download from.".format(package_name))
#Download the package to a temporary staging directory
staging_dir = tempfile.mkdtemp()
print(staging_dir)
current_directory = os.path.abspath(os.curdir)
try:
os.chdir(staging_dir)
tarball = os.path.join(staging_dir,"package.tar.gz")
urllib.request.urlretrieve(url,tarball)
tar = tarfile.open(tarball,mode="r:*")
tar.extractall()
package_dir = os.walk(os.curdir).__next__()[1][0]
two2three.main("lib2to3.fixes",shlex.split("-w {0}".format(package_dir)))
easy_install.main(shlex.split(package_dir))
finally:
os.chdir(current_directory)
>>> from easy_install_2to3 import easy_install_2to3
>>> easy_install_2to3("Markdown")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment