<s> import os <EOL> import subprocess <EOL> import json <EOL> import config <EOL> def command_exists ( cmd ) : <EOL> devnull = open ( os . devnull , '<STR_LIT:wb>' ) <EOL> rc = subprocess . call ( [ '<STR_LIT>' , cmd ] , stdout = devnull , stderr = devnull ) <EOL> return ( rc == <NUM_LIT:0> ) <EOL> def get_sudo ( cmd ) : <EOL> if type ( cmd ) == list : <EOL> cmd = "<STR_LIT>" . join ( cmd ) <EOL> if os . geteuid ( ) != <NUM_LIT:0> : <EOL> if "<STR_LIT>" in os . environ : <EOL> if command_exists ( "<STR_LIT>" ) : <EOL> sudo = "<STR_LIT>" <EOL> elif command_exists ( "<STR_LIT>" ) : <EOL> sudo = "<STR_LIT>" <EOL> elif "<STR_LIT>" in os . environ : <EOL> sudo = os . environ [ "<STR_LIT>" ] + "<STR_LIT>" <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> else : <EOL> sudo = "<STR_LIT>" <EOL> cmd = sudo + "<STR_LIT>" + cmd + "<STR_LIT>" <EOL> return cmd <EOL> def sudo ( cmd ) : <EOL> return subprocess . call ( get_sudo ( cmd ) , shell = True ) <EOL> class SudoQueue : <EOL> def __init__ ( self ) : <EOL> self . queue = [ ] <EOL> def append ( self , cmd ) : <EOL> self . queue . append ( cmd ) <EOL> def execute ( self ) : <EOL> return sudo ( self . queue ) <EOL> def reset ( self ) : <EOL> self . queue = [ ] <EOL> class Worker : <EOL> def __init__ ( self ) : <EOL> self . proc = None <EOL> def start ( self ) : <EOL> dirname = os . path . realpath ( os . path . dirname ( __file__ ) ) <EOL> cmd = "<STR_LIT>" + dirname + "<STR_LIT>" + config . file_path ( ) <EOL> self . proc = subprocess . Popen ( get_sudo ( cmd ) , shell = True , stdin = subprocess . PIPE , stdout = subprocess . PIPE ) <EOL> def send_command ( self , cmd ) : <EOL> if self . proc is None or self . proc . returncode is not None : <EOL> self . start ( ) <EOL> print ( '<STR_LIT>' , cmd ) <EOL> self . proc . stdin . write ( cmd + "<STR_LIT:\n>" ) <EOL> self . proc . stdin . flush ( ) <EOL> json_res = self . proc . stdout . readline ( ) . strip ( ) <EOL> print ( '<STR_LIT>' , json_res ) <EOL> try : <EOL> res = json . loads ( json_res ) <EOL> except ValueError , e : <EOL> res = { <EOL> "<STR_LIT:success>" : False , <EOL> "<STR_LIT>" : json_res <EOL> } <EOL> return res <EOL> def proxy_command ( self , cmd , callbacks ) : <EOL> res = self . send_command ( cmd ) <EOL> if res [ "<STR_LIT>" ] != "<STR_LIT>" : <EOL> callbacks [ "<STR_LIT>" ] ( res [ "<STR_LIT>" ] , { } ) <EOL> return res </s>
