def run_setup(setup_script, args):
 old_dir = os.getcwd()
 save_argv = sys.argv[:]
 save_path = sys.path[:]
 setup_dir = os.path.abspath(os.path.dirname(setup_script))
 temp_dir = os.path.join(setup_dir, 'temp')
 if (not os.path.isdir(temp_dir)):
  os.makedirs(temp_dir)
 save_tmp = tempfile.tempdir
 save_modules = sys.modules.copy()
 pr_state = pkg_resources.__getstate__()
 try:
  tempfile.tempdir = temp_dir
  os.chdir(setup_dir)
  try:
   sys.argv[:] = ([setup_script] + list(args))
   sys.path.insert(0, setup_dir)
   working_set.__init__()
   working_set.callbacks.append((lambda dist: dist.activate()))
   DirectorySandbox(setup_dir).run((lambda : execfile('setup.py', {'__file__': setup_script, '__name__': '__main__'})))
  except SystemExit:
   v = sys.exc_info()[1]
   if (v.args and v.args[0]):
    raise
 finally:
  pkg_resources.__setstate__(pr_state)
  sys.modules.update(save_modules)
  del_modules = [mod_name for mod_name in sys.modules if ((mod_name not in save_modules) and (not mod_name.startswith('encodings.')))]
  list(map(sys.modules.__delitem__, del_modules))
  os.chdir(old_dir)
  sys.path[:] = save_path
  sys.argv[:] = save_argv
  tempfile.tempdir = save_tmp

def run_setup ( setup_script , args ) : old_dir = os . getcwd ( ) save_argv = sys . argv [ : ] save_path = sys . path [ : ] setup_dir = os . path . abspath ( os . path . dirname ( setup_script ) ) temp_dir = os . path . join ( setup_dir , 'temp' ) if ( not os . path . isdir ( temp_dir ) ) : os . makedirs ( temp_dir ) save_tmp = tempfile . tempdir save_modules = sys . modules . copy ( ) pr_state = pkg_resources . __getstate__ ( ) try : tempfile . tempdir = temp_dir os . chdir ( setup_dir ) try : sys . argv [ : ] = ( [ setup_script ] + list ( args ) ) sys . path . insert ( 0 , setup_dir ) working_set . __init__ ( ) working_set . callbacks . append ( ( lambda dist : dist . activate ( ) ) ) DirectorySandbox ( setup_dir ) . run ( ( lambda : execfile ( 'setup.py' , { '__file__' : setup_script , '__name__' : '__main__' } ) ) ) except SystemExit : v = sys . exc_info ( ) [ 1 ] if ( v . args and v . args [ 0 ] ) : raise finally : pkg_resources . __setstate__ ( pr_state ) sys . modules . update ( save_modules ) del_modules = [ mod_name for mod_name in sys . modules if ( ( mod_name not in save_modules ) and ( not mod_name . startswith ( 'encodings.' ) ) ) ] list ( map ( sys . modules . __delitem__ , del_modules ) ) os . chdir ( old_dir ) sys . path [ : ] = save_path sys . argv [ : ] = save_argv tempfile . tempdir = save_tmp
run a setup script in a somewhat controlled environment .