<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import logging <EOL> import os <EOL> import shutil <EOL> import stat <EOL> import tempfile <EOL> from . templates import source_template <EOL> logger = logging . getLogger ( __name__ ) <EOL> class DirectoryException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class Directory ( object ) : <EOL> root_dir = None <EOL> manifest_path = None <EOL> new = False <EOL> rewrite_config = True <EOL> rc_file = None <EOL> env_file = None <EOL> shell_util_path = None <EOL> logger = logger <EOL> def __init__ ( self , root_dir , <EOL> rewrite_config = True , <EOL> shell_util_path = None ) : <EOL> """<STR_LIT>""" <EOL> self . root_dir = root_dir <EOL> self . new = not os . path . exists ( self . root_dir ) <EOL> self . manifest_path = os . path . join ( self . root_dir , "<STR_LIT>" ) <EOL> self . rewrite_config = rewrite_config <EOL> self . shell_util_path = shell_util_path <EOL> def __del__ ( self ) : <EOL> if self . rc_file : <EOL> self . rc_file . close ( ) <EOL> if self . env_file : <EOL> self . env_file . close ( ) <EOL> def initialize ( self ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( self . root_dir ) : <EOL> os . makedirs ( self . root_dir ) <EOL> assert os . path . isdir ( self . root_dir ) , "<STR_LIT>" % self . root_dir <EOL> for d in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> target_path = os . path . join ( self . root_dir , d ) <EOL> if not os . path . exists ( target_path ) : <EOL> os . makedirs ( target_path ) <EOL> if not os . path . exists ( self . manifest_path ) : <EOL> open ( self . manifest_path , "<STR_LIT>" ) . close ( ) <EOL> self . new = False <EOL> def finalize ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . rc_file : <EOL> self . rc_file . close ( ) <EOL> if self . env_file : <EOL> self . env_file . close ( ) <EOL> def remove ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . rc_file : <EOL> self . rc_file . close ( ) <EOL> if self . env_file : <EOL> self . env_file . close ( ) <EOL> shutil . rmtree ( self . root_dir ) <EOL> def symlink_to_bin ( self , name , path ) : <EOL> """<STR_LIT>""" <EOL> self . __symlink_dir ( "<STR_LIT>" , name , path ) <EOL> os . chmod ( os . path . join ( self . root_dir , "<STR_LIT>" , name ) , os . stat ( path ) . st_mode | stat . S_IXUSR | stat . S_IRUSR ) <EOL> def remove_from_bin ( self , name ) : <EOL> """<STR_LIT>""" <EOL> self . __remove_path ( os . path . join ( self . root_dir , "<STR_LIT>" , name ) ) <EOL> def remove_from_lib ( self , name ) : <EOL> """<STR_LIT>""" <EOL> self . __remove_path ( os . path . join ( self . root_dir , "<STR_LIT>" , name ) ) <EOL> def remove_feature ( self , feature_name ) : <EOL> """<STR_LIT>""" <EOL> self . clear_feature_symlinks ( feature_name ) <EOL> if os . path . exists ( self . install_directory ( feature_name ) ) : <EOL> self . __remove_path ( self . install_directory ( feature_name ) ) <EOL> def symlink_to_lib ( self , name , path ) : <EOL> """<STR_LIT>""" <EOL> self . __symlink_dir ( "<STR_LIT>" , name , path ) <EOL> def symlink_to_include ( self , name , path ) : <EOL> """<STR_LIT>""" <EOL> self . __symlink_dir ( "<STR_LIT>" , name , path ) <EOL> def bin_path ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . path . join ( self . root_dir , "<STR_LIT>" ) <EOL> def lib_path ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . path . join ( self . root_dir , "<STR_LIT>" ) <EOL> def include_path ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . path . join ( self . root_dir , "<STR_LIT>" ) <EOL> def clear_feature_symlinks ( self , feature_name ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( "<STR_LIT>" % feature_name ) <EOL> feature_path = self . install_directory ( feature_name ) <EOL> for d in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if os . path . exists ( os . path . join ( self . root_dir , d ) ) : <EOL> for link in os . listdir ( os . path . join ( self . root_dir , d ) ) : <EOL> path = os . path . join ( self . root_dir , d , link ) <EOL> if feature_path in os . path . realpath ( path ) : <EOL> getattr ( self , '<STR_LIT>' % d ) ( link ) <EOL> def install_directory ( self , feature_name ) : <EOL> """<STR_LIT>""" <EOL> return os . path . join ( self . root_dir , "<STR_LIT>" , feature_name ) <EOL> def add_to_env ( self , content ) : <EOL> """<STR_LIT>""" <EOL> if not self . rewrite_config : <EOL> raise DirectoryException ( "<STR_LIT>" ) <EOL> if not self . env_file : <EOL> self . env_path , self . env_file = self . __get_env_handle ( self . root_dir ) <EOL> self . env_file . write ( content + '<STR_LIT:\n>' ) <EOL> def add_to_rc ( self , content ) : <EOL> """<STR_LIT>""" <EOL> if not self . rewrite_config : <EOL> raise DirectoryException ( "<STR_LIT>" ) <EOL> if not self . rc_file : <EOL> self . rc_path , self . rc_file = self . __get_rc_handle ( self . root_dir ) <EOL> self . rc_file . write ( content + '<STR_LIT:\n>' ) <EOL> def add_to_gui ( self , content ) : <EOL> """<STR_LIT>""" <EOL> if not self . rewrite_config : <EOL> raise DirectoryException ( "<STR_LIT>" ) <EOL> if not self . gui_file : <EOL> self . gui_path , self . gui_file = self . __get_gui_handle ( self . root_dir ) <EOL> self . gui_file . write ( content + '<STR_LIT:\n>' ) <EOL> def __remove_path ( self , path ) : <EOL> """<STR_LIT>""" <EOL> curpath = os . path . abspath ( os . curdir ) <EOL> if not os . path . exists ( path ) : <EOL> logger . warn ( "<STR_LIT>" % path ) <EOL> return <EOL> try : <EOL> if os . path . islink ( path ) : <EOL> os . unlink ( path ) <EOL> elif os . path . isdir ( path ) : <EOL> shutil . rmtree ( path ) <EOL> else : <EOL> os . unlink ( path ) <EOL> if curpath == path : <EOL> os . chdir ( tempfile . gettempdir ( ) ) <EOL> except OSError : <EOL> logger . error ( "<STR_LIT>" % path ) <EOL> raise DirectoryException ( "<STR_LIT>" % path ) <EOL> def __get_env_handle ( self , root_dir ) : <EOL> """<STR_LIT>""" <EOL> env_path = os . path . join ( root_dir , '<STR_LIT>' ) <EOL> gui_path = os . path . join ( root_dir , '<STR_LIT>' ) <EOL> fh = open ( env_path , "<STR_LIT>" ) <EOL> fh . write ( source_template % ( gui_path , gui_path ) ) <EOL> fh . write ( source_template % ( self . shell_util_path , <EOL> self . shell_util_path ) ) <EOL> return ( env_path , fh ) <EOL> def __get_rc_handle ( self , root_dir ) : <EOL> """<STR_LIT>""" <EOL> rc_path = os . path . join ( root_dir , '<STR_LIT>' ) <EOL> env_path = os . path . join ( root_dir , '<STR_LIT>' ) <EOL> fh = open ( rc_path , "<STR_LIT>" ) <EOL> fh . write ( source_template % ( env_path , env_path ) ) <EOL> return ( rc_path , fh ) <EOL> def __get_gui_handle ( self , root_dir ) : <EOL> """<STR_LIT>""" <EOL> gui_path = os . path . join ( root_dir , '<STR_LIT>' ) <EOL> fh = open ( gui_path , "<STR_LIT>" ) <EOL> return ( gui_path , fh ) <EOL> def __symlink_dir ( self , dir_name , name , path ) : <EOL> """<STR_LIT>""" <EOL> target_dir = os . path . join ( self . root_dir , dir_name ) <EOL> if not os . path . exists ( target_dir ) : <EOL> os . makedirs ( target_dir ) <EOL> target_path = os . path . join ( self . root_dir , dir_name , name ) <EOL> logger . debug ( "<STR_LIT>" % ( path , target_path ) ) <EOL> if os . path . exists ( target_path ) : <EOL> if os . path . islink ( target_path ) : <EOL> os . remove ( target_path ) <EOL> else : <EOL> logger . warn ( "<STR_LIT>" % target_path ) <EOL> return <EOL> os . symlink ( path , target_path ) </s>
