parameters: (source_dir, args)
return_statement: return "#!" + ve_python
docstring: Create a Python runtime environment
      :return: shebang with path to the python executable
docstring_summary: Create a Python runtime environment
      :return: shebang with path to the python executable
docstring_tokens: Create a Python runtime environment : return : shebang with path to the python executable
function: def create_python_environment(source_dir, args):
      """ Create a Python runtime environment
      :return: shebang with path to the python executable
      """
      interp = args.python_interp
      pyver = subprocess.check_output(interp + " -c \"import sys; print ','.join(map(str, list(sys.version_info["
                                      "0:3])))\"", shell=True).strip().split(",")
      pyver = tuple(map(int, pyver))
  
      if pyver < (2, 7, 3):
          raise Exception("Python >= 2.7.3 is required for installation.")
  
      # system python -- just return interp
      if args.python == "system":
          return "#!" + interp
  
      if not args.python_venv_dir:
          raise Exception("Please specify a virtualenv target installation directory.")
  
      if args.python_venv_dir_force:
          try:
              shutil.rmtree(args.python_venv_dir)
          except:
              pass
  
      if os.path.exists(args.python_venv_dir) and not args.python_venv_dir_update:
          raise Exception("The virtual environment directory already exists.")
  
      virtualenv_tempdir = tempfile.mkdtemp(prefix="virtualenv", dir=args.scratch_path)
      try:
          ve_tgz = os.path.join(source_dir, "external", "virtualenv-12.0.7.tar.gz")
          to_run = "cd %s && tar xzf %s" % (virtualenv_tempdir, ve_tgz)
          print >>sys.stderr, to_run
          subprocess.check_call(to_run, shell=True)
  
          ve_exec = os.path.join(virtualenv_tempdir, "virtualenv-12.0.7", "virtualenv.py")
          to_run = "%s -p %s %s" % (ve_exec, interp, args.python_venv_dir)
          print >>sys.stderr, to_run
          subprocess.check_call(to_run, shell=True)
      finally:
          if not args.keep_scratch:
              try:
                  shutil.rmtree(virtualenv_tempdir)
              except:
                  pass
  
      # install requirements
      ve_python = os.path.join(args.python_venv_dir, "bin", "python")
      ve_pip = os.path.join(args.python_venv_dir, "bin", "pip")
  
      deleteme = None
      try:
          cmds = [ve_pip, "install", "--no-cache-dir"]
  
          if args.fix_cert:
              response = urllib2.urlopen('http://curl.haxx.se/ca/cacert.pem')
              certdata = response.read()
              f = tempfile.NamedTemporaryFile(delete=False)
              deleteme = f.name
              f.write(certdata)
              f.close()
              cmds.insert(1, " --cert")
              cmds.insert(2, deleteme)
  
          for x in open(os.path.join(source_dir, "happy.requirements.txt")):
              print >>sys.stderr, " ".join(cmds + [x])
              subprocess.check_call(" ".join(cmds + [x]), shell=True)
      finally:
          if deleteme:
              os.unlink(deleteme)
  
      return "#!" + ve_python
function_tokens: def create_python_environment ( source_dir , args ) : interp = args . python_interp pyver = subprocess . check_output ( interp + " -c \"import sys; print ','.join(map(str, list(sys.version_info[" "0:3])))\"" , shell = True ) . strip ( ) . split ( "," ) pyver = tuple ( map ( int , pyver ) ) if pyver < ( 2 , 7 , 3 ) : raise Exception ( "Python >= 2.7.3 is required for installation." ) # system python -- just return interp if args . python == "system" : return "#!" + interp if not args . python_venv_dir : raise Exception ( "Please specify a virtualenv target installation directory." ) if args . python_venv_dir_force : try : shutil . rmtree ( args . python_venv_dir ) except : pass if os . path . exists ( args . python_venv_dir ) and not args . python_venv_dir_update : raise Exception ( "The virtual environment directory already exists." ) virtualenv_tempdir = tempfile . mkdtemp ( prefix = "virtualenv" , dir = args . scratch_path ) try : ve_tgz = os . path . join ( source_dir , "external" , "virtualenv-12.0.7.tar.gz" ) to_run = "cd %s && tar xzf %s" % ( virtualenv_tempdir , ve_tgz ) print >> sys . stderr , to_run subprocess . check_call ( to_run , shell = True ) ve_exec = os . path . join ( virtualenv_tempdir , "virtualenv-12.0.7" , "virtualenv.py" ) to_run = "%s -p %s %s" % ( ve_exec , interp , args . python_venv_dir ) print >> sys . stderr , to_run subprocess . check_call ( to_run , shell = True ) finally : if not args . keep_scratch : try : shutil . rmtree ( virtualenv_tempdir ) except : pass # install requirements ve_python = os . path . join ( args . python_venv_dir , "bin" , "python" ) ve_pip = os . path . join ( args . python_venv_dir , "bin" , "pip" ) deleteme = None try : cmds = [ ve_pip , "install" , "--no-cache-dir" ] if args . fix_cert : response = urllib2 . urlopen ( 'http://curl.haxx.se/ca/cacert.pem' ) certdata = response . read ( ) f = tempfile . NamedTemporaryFile ( delete = False ) deleteme = f . name f . write ( certdata ) f . close ( ) cmds . insert ( 1 , " --cert" ) cmds . insert ( 2 , deleteme ) for x in open ( os . path . join ( source_dir , "happy.requirements.txt" ) ) : print >> sys . stderr , " " . join ( cmds + [ x ] ) subprocess . check_call ( " " . join ( cmds + [ x ] ) , shell = True ) finally : if deleteme : os . unlink ( deleteme ) return "#!" + ve_python