<s> import locale <EOL> import sys <EOL> import os <EOL> import os . path <EOL> import subprocess <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> import ctypes <EOL> def safe_unicode ( s ) : <EOL> '''<STR_LIT>''' <EOL> if type ( s ) == unicode : <EOL> return s <EOL> elif isinstance ( s , Exception ) : <EOL> s = str ( s ) <EOL> try : <EOL> return s . decode ( '<STR_LIT>' ) <EOL> except UnicodeDecodeError : <EOL> pass <EOL> try : <EOL> lang , encoding = locale . getdefaultlocale ( ) <EOL> except ValueError : <EOL> lang , encoding = '<STR_LIT:C>' , '<STR_LIT>' <EOL> if encoding != '<STR_LIT>' : <EOL> try : <EOL> return s . decode ( encoding ) <EOL> except UnicodeDecodeError : <EOL> pass <EOL> return s . decode ( '<STR_LIT>' ) <EOL> def utf8_str ( s ) : <EOL> s = safe_unicode ( s ) <EOL> return s . encode ( '<STR_LIT>' ) <EOL> def invert_hash ( h ) : <EOL> ih = { } <EOL> for key , value in h . iteritems ( ) : <EOL> if value not in ih : <EOL> ih [ value ] = [ ] <EOL> ih [ value ] . append ( key ) <EOL> return ih <EOL> def find_binary ( locations ) : <EOL> searchpath_sep = '<STR_LIT:;>' if sys . platform == '<STR_LIT:win32>' else '<STR_LIT::>' <EOL> searchpaths = os . environ [ '<STR_LIT>' ] . split ( searchpath_sep ) <EOL> for location in locations : <EOL> if '<STR_LIT>' in location : <EOL> for searchpath in searchpaths : <EOL> s = location . replace ( '<STR_LIT>' , searchpath ) <EOL> if os . path . isfile ( s ) and os . access ( s , os . X_OK ) : <EOL> yield s <EOL> elif os . path . isfile ( location ) and os . access ( location , os . X_OK ) : <EOL> yield location <EOL> def is_binary_file ( file ) : <EOL> try : <EOL> f = open ( file ) <EOL> buf = f . read ( ) <EOL> f . close ( ) <EOL> except OSError : <EOL> return False <EOL> try : <EOL> ubuf = unicode ( buf , '<STR_LIT:utf-8>' ) <EOL> return False <EOL> except UnicodeDecodeError : <EOL> pass <EOL> treshold = len ( buf ) / <NUM_LIT:5> <EOL> binary_chars = <NUM_LIT:0> <EOL> for c in buf : <EOL> oc = ord ( c ) <EOL> if oc > <NUM_LIT> or ( oc < <NUM_LIT> and oc != '<STR_LIT:\r>' and oc != '<STR_LIT:\n>' ) : <EOL> binary_chars += <NUM_LIT:1> <EOL> if binary_chars > treshold : <EOL> return True <EOL> return False <EOL> PROCESS_TERMINATE = <NUM_LIT:1> <EOL> def kill_subprocess ( process ) : <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> handle = ctypes . windll . kernel32 . OpenProcess ( PROCESS_TERMINATE , False , process . pid ) <EOL> ctypes . windll . kernel32 . TerminateProcess ( handle , - <NUM_LIT:1> ) <EOL> ctypes . windll . kernel32 . CloseHandle ( handle ) <EOL> else : <EOL> os . kill ( process . pid , <NUM_LIT:9> ) <EOL> CREATE_NO_WINDOW = <NUM_LIT> <EOL> def Popen ( cmd , ** args ) : <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> process = subprocess . Popen ( cmd , creationflags = CREATE_NO_WINDOW , ** args ) <EOL> else : <EOL> process = subprocess . Popen ( cmd , ** args ) <EOL> if '<STR_LIT>' not in dir ( process ) : <EOL> process . kill = lambda : kill_subprocess ( process ) <EOL> return process </s>
