<s> import logging <EOL> import os <EOL> from ajenti . plugins . main . api import SectionPlugin <EOL> from ajenti . ui import on <EOL> from ajenti . ui . binder import Binder <EOL> class AvailabilitySymlinks ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dir_a , dir_e , supports_activation , ignore_ext = [ ] ) : <EOL> self . dir_a , self . dir_e , self . ignore_ext = dir_a , dir_e , ignore_ext <EOL> self . supports_activation = supports_activation <EOL> def list_available ( self ) : <EOL> return [ x for x in sorted ( os . listdir ( self . dir_a ) ) if <EOL> not os . path . isdir ( os . path . join ( self . dir_a , x ) ) <EOL> and not os . path . splitext ( os . path . join ( self . dir_a , x ) ) [ - <NUM_LIT:1> ] in self . ignore_ext ] <EOL> def is_enabled ( self , entry ) : <EOL> if not self . supports_activation : <EOL> return True <EOL> return self . find_link ( entry ) is not None <EOL> def get_path ( self , entry ) : <EOL> return os . path . abspath ( os . path . join ( self . dir_a , entry ) ) <EOL> def find_link ( self , entry ) : <EOL> path = self . get_path ( entry ) <EOL> for e in os . listdir ( self . dir_e ) : <EOL> if os . path . abspath ( os . path . realpath ( os . path . join ( self . dir_e , e ) ) ) == path : <EOL> return e <EOL> def enable ( self , entry ) : <EOL> if not self . supports_activation : <EOL> return <EOL> e = self . find_link ( entry ) <EOL> if not e : <EOL> link_path = os . path . join ( self . dir_e , entry ) <EOL> if not os . path . exists ( link_path ) : <EOL> os . symlink ( self . get_path ( entry ) , link_path ) <EOL> def disable ( self , entry ) : <EOL> if not self . supports_activation : <EOL> return <EOL> e = self . find_link ( entry ) <EOL> if e : <EOL> os . unlink ( os . path . join ( self . dir_e , e ) ) <EOL> def rename ( self , old , new ) : <EOL> on = self . is_enabled ( old ) <EOL> self . disable ( old ) <EOL> os . rename ( self . get_path ( old ) , self . get_path ( new ) ) <EOL> if on : <EOL> self . enable ( new ) <EOL> def delete ( self , entry ) : <EOL> self . disable ( entry ) <EOL> os . unlink ( self . get_path ( entry ) ) <EOL> def open ( self , entry , mode = '<STR_LIT:r>' ) : <EOL> return open ( os . path . join ( self . dir_a , entry ) , mode ) <EOL> def exists ( self ) : <EOL> return os . path . exists ( self . dir_a ) and os . path . exists ( self . dir_e ) <EOL> class WebserverConf ( object ) : <EOL> def __init__ ( self , owner , dir , entry ) : <EOL> self . name = entry <EOL> self . owner = owner <EOL> self . dir = dir <EOL> self . active = dir . is_enabled ( entry ) <EOL> self . config = dir . open ( entry ) . read ( ) <EOL> def save ( self ) : <EOL> self . dir . open ( self . name , '<STR_LIT:w>' ) . write ( self . config ) <EOL> if self . active : <EOL> self . dir . enable ( self . name ) <EOL> else : <EOL> self . dir . disable ( self . name ) <EOL> class WebserverMainConf ( object ) : <EOL> def __init__ ( self , filename ) : <EOL> self . name = os . path . basename ( filename ) <EOL> self . configfile = filename <EOL> self . config = '<STR_LIT>' <EOL> def save ( self ) : <EOL> try : <EOL> open ( self . configfile , '<STR_LIT:w>' ) . write ( self . config ) <EOL> except IOError as e : <EOL> logging . error ( e . message ) <EOL> return self <EOL> def update ( self ) : <EOL> try : <EOL> self . config = '<STR_LIT>' . join ( open ( self . configfile ) . readlines ( ) ) <EOL> except IOError as e : <EOL> logging . error ( e . message ) <EOL> return self <EOL> class WebserverPlugin ( SectionPlugin ) : <EOL> service_name = '<STR_LIT>' <EOL> service_buttons = [ ] <EOL> hosts_available_dir = '<STR_LIT>' <EOL> hosts_enabled_dir = '<STR_LIT>' <EOL> hosts_dir = None <EOL> template = '<STR_LIT>' <EOL> supports_host_activation = True <EOL> configurable = False <EOL> main_conf_files = [ ] <EOL> configurations = [ ] <EOL> def log ( self , msg ) : <EOL> logging . info ( '<STR_LIT>' % ( self . service_name , msg ) ) <EOL> def init ( self ) : <EOL> self . append ( self . ui . inflate ( '<STR_LIT>' ) ) <EOL> self . binder = Binder ( None , self ) <EOL> self . find_type ( '<STR_LIT>' ) . buttons = self . service_buttons <EOL> self . hosts_dir = AvailabilitySymlinks ( <EOL> self . hosts_available_dir , <EOL> self . hosts_enabled_dir , <EOL> self . supports_host_activation <EOL> ) <EOL> def delete_host ( host , c ) : <EOL> self . log ( '<STR_LIT>' % host . name ) <EOL> c . remove ( host ) <EOL> self . hosts_dir . delete ( host . name ) <EOL> def on_host_bind ( o , c , host , u ) : <EOL> host . __old_name = host . name <EOL> def on_host_update ( o , c , host , u ) : <EOL> if host . __old_name != host . name : <EOL> self . log ( '<STR_LIT>' % ( host . __old_name , host . name ) ) <EOL> self . hosts_dir . rename ( host . __old_name , host . name ) <EOL> host . save ( ) <EOL> def new_host ( c ) : <EOL> name = '<STR_LIT>' <EOL> while os . path . exists ( self . hosts_dir . get_path ( name ) ) : <EOL> name += '<STR_LIT:_>' <EOL> self . log ( '<STR_LIT>' % name ) <EOL> self . hosts_dir . open ( name , '<STR_LIT:w>' ) . write ( self . template ) <EOL> return WebserverConf ( self , self . hosts_dir , name ) <EOL> self . find ( '<STR_LIT>' ) . delete_item = delete_host <EOL> self . find ( '<STR_LIT>' ) . new_item = new_host <EOL> self . find ( '<STR_LIT>' ) . post_item_bind = on_host_bind <EOL> self . find ( '<STR_LIT>' ) . post_item_update = on_host_update <EOL> self . find ( '<STR_LIT>' ) . visible = self . find ( '<STR_LIT>' ) . visible = self . supports_host_activation <EOL> def on_conf_bind ( o , c , conf , u ) : <EOL> conf . __old_name = conf . name <EOL> def on_conf_update ( o , c , conf , u ) : <EOL> conf . save ( ) <EOL> self . find ( '<STR_LIT>' ) . post_item_bind = on_conf_bind <EOL> self . find ( '<STR_LIT>' ) . post_item_update = on_conf_update <EOL> self . tabs = self . find ( '<STR_LIT>' ) <EOL> def on_page_load ( self ) : <EOL> self . refresh ( ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def save ( self ) : <EOL> self . log ( '<STR_LIT>' ) <EOL> self . binder . update ( ) <EOL> self . refresh ( ) <EOL> self . context . notify ( '<STR_LIT:info>' , '<STR_LIT>' ) <EOL> def refresh ( self ) : <EOL> self . hosts = [ WebserverConf ( self , self . hosts_dir , x ) for x in self . hosts_dir . list_available ( ) ] <EOL> self . configurations = [ WebserverMainConf ( y ) . update ( ) for y in <EOL> self . main_conf_files ] if self . configurable else [ ] <EOL> self . binder . setup ( self ) . populate ( ) <EOL> self . find_type ( '<STR_LIT>' ) . reload ( ) </s>
