<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import time <EOL> import atexit <EOL> import shutil <EOL> import logging <EOL> import threading <EOL> import subprocess <EOL> from . . util . icon import Icon <EOL> class BaseRuntime : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> if '<STR_LIT:url>' not in kwargs : <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> self . _kwargs = kwargs <EOL> self . _proc = None <EOL> self . _streamreader = None <EOL> atexit . register ( self . close ) <EOL> logging . info ( '<STR_LIT>' % self . __class__ . __name__ ) <EOL> self . _launch ( ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _proc is None : <EOL> return <EOL> self . _proc . we_closed_it = True <EOL> if self . _proc . poll ( ) is None : <EOL> if self . _proc . stdin : <EOL> self . _proc . stdin . close ( ) <EOL> self . _proc . terminate ( ) <EOL> timeout = time . time ( ) + <NUM_LIT> <EOL> while time . time ( ) < timeout : <EOL> time . sleep ( <NUM_LIT> ) <EOL> if self . _proc . poll ( ) is not None : <EOL> break <EOL> else : <EOL> self . _proc . kill ( ) <EOL> self . _proc = None <EOL> def _start_subprocess ( self , cmd , shell = False , ** env ) : <EOL> """<STR_LIT>""" <EOL> environ = os . environ . copy ( ) <EOL> environ . update ( env ) <EOL> try : <EOL> self . _proc = subprocess . Popen ( cmd , env = environ , shell = shell , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . STDOUT ) <EOL> except OSError as err : <EOL> raise RuntimeError ( '<STR_LIT>' % <EOL> ( cmd [ <NUM_LIT:0> ] , str ( err ) ) ) <EOL> self . _streamreader = StreamReader ( self . _proc ) <EOL> self . _streamreader . start ( ) <EOL> def _launch ( self ) : <EOL> raise NotImplementedError ( ) <EOL> class DesktopRuntime ( BaseRuntime ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> icon = kwargs . get ( '<STR_LIT>' , None ) <EOL> kwargs [ '<STR_LIT>' ] = iconize ( icon ) <EOL> BaseRuntime . __init__ ( self , ** kwargs ) <EOL> class StreamReader ( threading . Thread ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , process ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . _process = process <EOL> self . _exit = False <EOL> self . setDaemon ( True ) <EOL> atexit . register ( self . stop ) <EOL> def stop ( self , wait = None ) : <EOL> self . _exit = True <EOL> if wait is not None : <EOL> self . join ( wait ) <EOL> def run ( self ) : <EOL> msgs = [ ] <EOL> while not self . _exit : <EOL> time . sleep ( <NUM_LIT> ) <EOL> msg = self . _process . stdout . readline ( ) <EOL> if not msg : <EOL> break <EOL> if not isinstance ( msg , str ) : <EOL> msg = msg . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:ignore>' ) <EOL> msg = msg . rstrip ( ) <EOL> if msg == '<STR_LIT>' or not msg : <EOL> continue <EOL> msgs . append ( msg ) <EOL> msgs [ : - <NUM_LIT:32> ] = [ ] <EOL> logging . debug ( '<STR_LIT>' + msg ) <EOL> if self . _exit : <EOL> return <EOL> while self . _process . poll ( ) is None : <EOL> time . sleep ( <NUM_LIT> ) <EOL> code = self . _process . poll ( ) <EOL> if hasattr ( self . _process , '<STR_LIT>' ) and self . _process . we_closed_it : <EOL> logging . info ( '<STR_LIT>' ) <EOL> elif not code : <EOL> logging . info ( '<STR_LIT>' ) <EOL> else : <EOL> logging . error ( '<STR_LIT>' % <EOL> ( code , '<STR_LIT:\n>' . join ( msgs ) ) ) <EOL> def create_temp_app_dir ( prefix , suffix = '<STR_LIT>' , cleanup = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> maindir = os . path . join ( appdata_dir ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> if not os . path . isdir ( maindir ) : <EOL> os . mkdir ( maindir ) <EOL> prefix = prefix . strip ( '<STR_LIT>' ) + '<STR_LIT:_>' <EOL> suffix = '<STR_LIT>' if not suffix else '<STR_LIT:_>' + suffix . strip ( '<STR_LIT>' ) <EOL> for dname in os . listdir ( maindir ) : <EOL> if dname . startswith ( prefix ) : <EOL> dirname = os . path . join ( maindir , dname ) <EOL> if os . path . isdir ( dirname ) : <EOL> try : <EOL> dirtime = int ( dname . split ( '<STR_LIT:_>' ) [ <NUM_LIT:1> ] ) <EOL> except Exception : <EOL> pass <EOL> if ( time . time ( ) - dirtime ) > cleanup : <EOL> try : <EOL> shutil . rmtree ( dirname ) <EOL> except ( OSError , IOError ) : <EOL> pass <EOL> id = '<STR_LIT>' % ( time . time ( ) , os . getpid ( ) ) <EOL> path = os . path . join ( maindir , prefix + id + suffix ) <EOL> os . mkdir ( path ) <EOL> return path <EOL> def appdata_dir ( appname = None , roaming = False , macAsLinux = False ) : <EOL> """<STR_LIT>""" <EOL> userDir = os . path . expanduser ( '<STR_LIT>' ) <EOL> path = None <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> path1 , path2 = os . getenv ( '<STR_LIT>' ) , os . getenv ( '<STR_LIT>' ) <EOL> path = ( path2 or path1 ) if roaming else ( path1 or path2 ) <EOL> elif sys . platform . startswith ( '<STR_LIT>' ) and not macAsLinux : <EOL> path = os . path . join ( userDir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not ( path and os . path . isdir ( path ) ) : <EOL> path = userDir <EOL> prefix = sys . prefix <EOL> if getattr ( sys , '<STR_LIT>' , None ) : <EOL> prefix = os . path . abspath ( os . path . dirname ( sys . path [ <NUM_LIT:0> ] ) ) <EOL> for reldir in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> localpath = os . path . abspath ( os . path . join ( prefix , reldir ) ) <EOL> if os . path . isdir ( localpath ) : <EOL> try : <EOL> open ( os . path . join ( localpath , '<STR_LIT>' ) , '<STR_LIT:wb>' ) . close ( ) <EOL> os . remove ( os . path . join ( localpath , '<STR_LIT>' ) ) <EOL> except IOError : <EOL> pass <EOL> else : <EOL> path = localpath <EOL> break <EOL> if appname : <EOL> if path == userDir : <EOL> appname = '<STR_LIT:.>' + appname . lstrip ( '<STR_LIT:.>' ) <EOL> path = os . path . join ( path , appname ) <EOL> if not os . path . isdir ( path ) : <EOL> os . mkdir ( path ) <EOL> return path <EOL> _icon_template = """<STR_LIT>""" <EOL> def default_icon ( ) : <EOL> """<STR_LIT>""" <EOL> im = bytearray ( <NUM_LIT:4> * <NUM_LIT:16> * <NUM_LIT:16> ) <EOL> for y , line in enumerate ( _icon_template . splitlines ( ) ) : <EOL> y += <NUM_LIT:5> <EOL> if y < <NUM_LIT:16> : <EOL> for x , c in enumerate ( line ) : <EOL> if c == '<STR_LIT:x>' : <EOL> i = ( y * <NUM_LIT:16> + x ) * <NUM_LIT:4> <EOL> im [ i : i + <NUM_LIT:4> ] = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:255> ] <EOL> icon = Icon ( ) <EOL> icon . add ( im ) <EOL> return icon <EOL> def iconize ( icon ) : <EOL> """<STR_LIT>""" <EOL> if icon is None : <EOL> icon = default_icon ( ) <EOL> elif isinstance ( icon , Icon ) : <EOL> pass <EOL> elif isinstance ( icon , str ) : <EOL> icon = Icon ( icon ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> type ( icon ) ) <EOL> return icon </s>
