create_virtualenv_bootstrap.py

Send to Kindle
home » snippets » python » virtualenv » create_virtualenv_bootstrap.py


#!/usr/bin/env python

import sys
import os, os.path
import virtualenv

abspath = os.path.abspath
dirname = os.path.dirname
join = os.path.join

default_virtualenv_bootstrap = join(
    dirname(abspath(__file__)),
    "default_virtualenv_bootstrap.py")

from ck2 import opster
@opster.command(usage='%name --bootstrap_script <bootstrap_script> [--output_file <output_file> --extra_paths <path1> --extra_paths <path2> ...]')
def main(
    bootstrap_script=('', default_virtualenv_bootstrap, 'Path to virtualenv bootstrap script.'),
    output_file     =('', '', 'Output file. Write to standard out if none given.'),
    extra_paths     =('', [], 'Extra paths to add to sys.path.')):
  if not output_file:
    output_fileobj = sys.stdout
  else:
    output_fileobj = file(output_file, "wt")
  bootstrap_items = []
  if extra_paths:
    bootstrap_items.append("extra_paths = [%s]" % (", ".join(map(repr, extra_paths)),))
  bootstrap_items.append(open(bootstrap_script, "rt").read())
  print >>output_fileobj, virtualenv.create_bootstrap_script(
      "\n\n".join(bootstrap_items))
  if output_file:
    orig_mode = os.stat(output_file).st_mode
    new_mode = orig_mode | 0100  # chmod u+x
    os.chmod(output_file, new_mode)

if __name__ == "__main__":
  main()