<s> import os <EOL> import zipfile <EOL> import sys <EOL> import tempfile <EOL> import unittest <EOL> import shutil <EOL> import stat <EOL> import unicodedata <EOL> from subprocess import Popen as _Popen , PIPE as _PIPE <EOL> def _extract ( self , member , path = None , pwd = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( member , zipfile . ZipInfo ) : <EOL> member = self . getinfo ( member ) <EOL> if path is None : <EOL> path = os . getcwd ( ) <EOL> return _extract_member ( self , member , path , pwd ) <EOL> def _extract_from_zip ( self , name , dest_path ) : <EOL> dest_file = open ( dest_path , '<STR_LIT:wb>' ) <EOL> try : <EOL> dest_file . write ( self . read ( name ) ) <EOL> finally : <EOL> dest_file . close ( ) <EOL> def _extract_member ( self , member , targetpath , pwd ) : <EOL> """<STR_LIT>""" <EOL> if ( targetpath [ - <NUM_LIT:1> : ] in ( os . path . sep , os . path . altsep ) <EOL> and len ( os . path . splitdrive ( targetpath ) [ <NUM_LIT:1> ] ) > <NUM_LIT:1> ) : <EOL> targetpath = targetpath [ : - <NUM_LIT:1> ] <EOL> if member . filename [ <NUM_LIT:0> ] == '<STR_LIT:/>' : <EOL> targetpath = os . path . join ( targetpath , member . filename [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> targetpath = os . path . join ( targetpath , member . filename ) <EOL> targetpath = os . path . normpath ( targetpath ) <EOL> upperdirs = os . path . dirname ( targetpath ) <EOL> if upperdirs and not os . path . exists ( upperdirs ) : <EOL> os . makedirs ( upperdirs ) <EOL> if member . filename [ - <NUM_LIT:1> ] == '<STR_LIT:/>' : <EOL> if not os . path . isdir ( targetpath ) : <EOL> os . mkdir ( targetpath ) <EOL> return targetpath <EOL> _extract_from_zip ( self , member . filename , targetpath ) <EOL> return targetpath <EOL> def _remove_dir ( target ) : <EOL> for dir_path , dirs , files in os . walk ( target ) : <EOL> os . chmod ( dir_path , stat . S_IWRITE ) <EOL> for filename in files : <EOL> os . chmod ( os . path . join ( dir_path , filename ) , stat . S_IWRITE ) <EOL> shutil . rmtree ( target ) <EOL> class ZippedEnvironment ( unittest . TestCase ) : <EOL> datafile = None <EOL> dataname = None <EOL> old_cwd = None <EOL> def setUp ( self ) : <EOL> if self . datafile is None or self . dataname is None : <EOL> return <EOL> if not os . path . isfile ( self . datafile ) : <EOL> self . old_cwd = None <EOL> return <EOL> self . old_cwd = os . getcwd ( ) <EOL> self . temp_dir = tempfile . mkdtemp ( ) <EOL> zip_file , source , target = [ None , None , None ] <EOL> try : <EOL> zip_file = zipfile . ZipFile ( self . datafile ) <EOL> for files in zip_file . namelist ( ) : <EOL> _extract ( zip_file , files , self . temp_dir ) <EOL> finally : <EOL> if zip_file : <EOL> zip_file . close ( ) <EOL> del zip_file <EOL> os . chdir ( os . path . join ( self . temp_dir , self . dataname ) ) <EOL> def tearDown ( self ) : <EOL> if self . dataname is None or self . datafile is None : <EOL> return <EOL> try : <EOL> if self . old_cwd : <EOL> os . chdir ( self . old_cwd ) <EOL> _remove_dir ( self . temp_dir ) <EOL> except OSError : <EOL> pass <EOL> def _which_dirs ( cmd ) : <EOL> result = set ( ) <EOL> for path in os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( os . pathsep ) : <EOL> filename = os . path . join ( path , cmd ) <EOL> if os . access ( filename , os . X_OK ) : <EOL> result . add ( path ) <EOL> return result <EOL> def run_setup_py ( cmd , pypath = None , path = None , <EOL> data_stream = <NUM_LIT:0> , env = None ) : <EOL> """<STR_LIT>""" <EOL> if env is None : <EOL> env = dict ( ) <EOL> for envname in os . environ : <EOL> env [ envname ] = os . environ [ envname ] <EOL> if pypath is not None : <EOL> env [ "<STR_LIT>" ] = pypath <EOL> if path is not None : <EOL> env [ "<STR_LIT>" ] = path <EOL> if not env . get ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> env [ "<STR_LIT>" ] = _which_dirs ( "<STR_LIT>" ) . union ( _which_dirs ( "<STR_LIT>" ) ) <EOL> env [ "<STR_LIT>" ] = os . pathsep . join ( env [ "<STR_LIT>" ] ) <EOL> cmd = [ sys . executable , "<STR_LIT>" ] + list ( cmd ) <EOL> try : <EOL> proc = _Popen ( cmd , stdout = _PIPE , stderr = _PIPE , <EOL> shell = ( sys . platform == '<STR_LIT:win32>' ) , env = env ) <EOL> data = proc . communicate ( ) [ data_stream ] <EOL> except OSError : <EOL> return <NUM_LIT:1> , '<STR_LIT>' <EOL> if hasattr ( data , "<STR_LIT>" ) : <EOL> data = data . decode ( ) <EOL> data = unicodedata . normalize ( '<STR_LIT>' , data ) <EOL> return proc . returncode , data </s>
