Skip to content

Instantly share code, notes, and snippets.

@aodag
Created October 26, 2013 05:26
Show Gist options
  • Save aodag/7165613 to your computer and use it in GitHub Desktop.
Save aodag/7165613 to your computer and use it in GitHub Desktop.
pyvenv, setuptools, pip
#!/usr/bin/python3
import argparse
import os
import subprocess
import sys
import tempfile
import urllib.request
import venv
setuptools_version = "1.1.6"
EZ_SETUP_URL = 'https://bitbucket.org/pypa/setuptools/raw/{version}/ez_setup.py'.format(version=setuptools_version)
GET_PIP_URL = 'https://raw.github.com/pypa/pip/develop/contrib/get-pip.py'
def exec_env(context, script_file):
print("execute {0}".format(script_file))
subprocess.check_call([context.env_exe, script_file])
class MyEnvBuilder(venv.EnvBuilder):
def post_setup(self, context):
self.install_pypa(context)
def install_pypa(self, context):
with tempfile.TemporaryDirectory() as d:
self.install_setuptools(context, d)
self.install_pip(context, d)
def install_setuptools(self, context, d):
path = self.download(EZ_SETUP_URL, d)
exec_env(context, path)
def install_pip(self, context, d):
path = self.download(GET_PIP_URL, d)
exec_env(context, path)
def download(self, url, d):
print("downloading {url}".format(url=url))
filename = url.split("/")[-1]
path = os.path.join(d, filename)
with urllib.request.urlopen(url) as f:
with open(path, "wb") as out:
out.write(f.read())
print("done")
return path
def main(args=sys.argv[1:]):
parser = argparse.ArgumentParser()
parser.add_argument("envdir")
args = parser.parse_args(args)
builder = MyEnvBuilder()
builder.create(args.envdir)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment