Instantly share code, notes, and snippets.

Embed
What would you like to do?
Vootstrap Maker
#!/usr/bin/env python
import textwrap, virtualenv
def adjust_options(options):
voot_opts = {
'no_site_packages': not options.use_site_packages,
'unzip_setuptools': not options.zip_setuptools,
'use_distribute': not options.setuptools,
}
if options.prompt:
voot_opts['prompt'] = '"' + options.prompt + '"'
out_str = "def adjust_options(options, args):\n"
for vo in voot_opts.iteritems():
out_str += " options.%s = %s\n" % vo
out_str += " if not args:"
out_str += " args.append('virtualenv')"
return textwrap.dedent(out_str)
def after_install(options):
if options.no_requirements and not options.path:
return ''
out_str = "def after_install(options, home_dir):\n"
out_str += " import os.path\n"
if options.path:
out_str += " abs_home_dir = os.path.abspath(home_dir)\n"
out_str += " paths = [os.path.join(abs_home_dir, p) + \"\\n\" for p in " + str(options.path) + "]\n"
out_str += " with open(os.path.join(abs_home_dir, 'lib', py_version, 'site-packages', 'vootstrap.pth'), 'w') as pth:\n"
out_str += " for path in paths:\n"
out_str += " pth.write(path)\n\n"
if not options.no_requirements:
out_str += " import subprocess\n"
out_str += " requirements = os.path.join(home_dir, 'requirements.txt')\n"
out_str += " if os.path.exists(requirements):\n"
out_str += " subprocess.call([os.path.join(home_dir, 'bin', 'pip'), 'install' , '-r', 'requirements.txt'])\n"
return textwrap.dedent(out_str)
def vootify(options):
return virtualenv.create_bootstrap_script(
adjust_options(options) + after_install(options)
)
if __name__ == "__main__":
from optparse import OptionParser
import sys
parser = OptionParser(usage="usage: %prog [options] <outfile>")
parser.add_option(
"--no-requirements",
default=False,
action="store_true",
dest="no_requirements",
help="Don't install requirements.txt after vootstrapping. [default: %default]",
)
parser.add_option(
"--path",
action="append",
dest="path",
help="Extra path to add into vootstrapped sys.path. (Can be used multiple times)",
)
parser.add_option(
"--prompt",
dest="prompt",
help="Provides an alternative prompt prefix for this environment.",
type="string",
)
parser.add_option(
"--setuptools",
default=False,
action="store_true",
dest="setuptools",
help="Use Setuptools instead of Distribute. [default: %default]",
)
parser.add_option(
"--use-site-packages",
default=False,
action="store_true",
dest="use_site_packages",
help="Give access to the global site-packages dir to the virtual environment. [default: %default]",
)
parser.add_option(
"--zip-setuptools",
default=False,
action="store_true",
dest="zip_setuptools",
help="Leave Setuptools or Distribute zipped when installing it. [default: %default]",
)
(options, args) = parser.parse_args()
if not len(args):
parser.print_help()
sys.exit(1)
with open(args[0], 'w') as outfile:
outfile.write(vootify(options))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment