<s> import os <EOL> import sys <EOL> import locale <EOL> from codecs import getwriter <EOL> from contextlib import contextmanager <EOL> from subprocess import check_call , Popen , PIPE <EOL> from collections import namedtuple <EOL> from functools import partial , wraps <EOL> from pathlib import Path <EOL> from tempfile import NamedTemporaryFile as _ntf <EOL> try : <EOL> from shutil import which <EOL> except ImportError : <EOL> from shutilwhich import which <EOL> py2 = sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:2> <EOL> windows = sys . platform == '<STR_LIT:win32>' <EOL> if py2 or windows : <EOL> locale . setlocale ( locale . LC_CTYPE , '<STR_LIT>' ) <EOL> encoding = locale . getlocale ( ) [ <NUM_LIT:1> ] <EOL> if py2 : <EOL> @ wraps ( _ntf ) <EOL> def NamedTemporaryFile ( mode ) : <EOL> return getwriter ( encoding ) ( _ntf ( mode ) ) <EOL> def to_unicode ( x ) : <EOL> return x . decode ( encoding ) <EOL> else : <EOL> NamedTemporaryFile = _ntf <EOL> to_unicode = str <EOL> def check_path ( ) : <EOL> parent = os . path . dirname <EOL> return parent ( parent ( which ( '<STR_LIT>' ) ) ) == os . environ [ '<STR_LIT>' ] <EOL> def resolve_path ( f ) : <EOL> def call ( cmd , ** kwargs ) : <EOL> ex = cmd [ <NUM_LIT:0> ] <EOL> ex = which ( ex ) or ex <EOL> return f ( [ ex ] + list ( cmd [ <NUM_LIT:1> : ] ) , ** kwargs ) <EOL> return call <EOL> if windows : <EOL> check_call = resolve_path ( check_call ) <EOL> Popen = resolve_path ( Popen ) <EOL> Result = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def invoke ( * args , ** kwargs ) : <EOL> inp = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) . encode ( encoding ) <EOL> popen = Popen ( args , stdin = PIPE , stdout = PIPE , stderr = PIPE , ** kwargs ) <EOL> out , err = [ o . strip ( ) . decode ( encoding ) for o in popen . communicate ( inp ) ] <EOL> return Result ( popen . returncode , out , err ) <EOL> invoke_pew = partial ( invoke , '<STR_LIT>' ) <EOL> env_bin_dir = '<STR_LIT>' if sys . platform != '<STR_LIT:win32>' else '<STR_LIT>' <EOL> def expandpath ( path ) : <EOL> return Path ( os . path . expanduser ( os . path . expandvars ( path ) ) ) <EOL> def own ( path ) : <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> return True <EOL> while not path . exists ( ) : <EOL> path = path . parent <EOL> return path . stat ( ) . st_uid == os . getuid ( ) <EOL> @ contextmanager <EOL> def temp_environ ( ) : <EOL> environ = dict ( os . environ ) <EOL> try : <EOL> yield <EOL> finally : <EOL> os . environ . clear ( ) <EOL> os . environ . update ( environ ) </s>
