<s> from ansible . module_utils . basic import * <EOL> DOCUMENTATION = '''<STR_LIT>''' <EOL> EXAMPLES = '''<STR_LIT>''' <EOL> try : <EOL> import os <EOL> import ConfigParser as cp <EOL> IMPORT_SUCCESS = True <EOL> except ImportError : <EOL> IMPORT_SUCCESS = False <EOL> class GirderPluginDirModule ( object ) : <EOL> _debug = True <EOL> def exit ( self , msg = None ) : <EOL> if not self . _debug : <EOL> del self . message [ '<STR_LIT>' ] <EOL> if msg is not None : <EOL> self . message [ '<STR_LIT>' ] = msg <EOL> self . module . exit_json ( changed = self . changed , ** self . message ) <EOL> def fail ( self , msg ) : <EOL> self . module . fail_json ( msg = msg ) <EOL> def __init__ ( self , module ) : <EOL> self . module = module <EOL> self . changed = False <EOL> self . message = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : { <EOL> "<STR_LIT>" : module . params <EOL> } } <EOL> dist_path = os . path . join ( self . module . params [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> local_path = os . path . join ( self . module . params [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . message [ '<STR_LIT>' ] [ '<STR_LIT>' ] = dist_path <EOL> self . message [ '<STR_LIT>' ] [ '<STR_LIT>' ] = local_path <EOL> self . config = cp . SafeConfigParser ( ) <EOL> if not os . path . exists ( local_path ) : <EOL> try : <EOL> self . message [ '<STR_LIT>' ] [ '<STR_LIT:src>' ] = dist_path <EOL> self . config . read ( dist_path ) <EOL> except IOError : <EOL> self . fail ( "<STR_LIT>" . format ( dist_path ) ) <EOL> else : <EOL> try : <EOL> self . message [ '<STR_LIT>' ] [ '<STR_LIT:src>' ] = local_path <EOL> self . config . read ( local_path ) <EOL> except IOError : <EOL> self . fail ( "<STR_LIT>" . format ( local_path ) ) <EOL> if self . module . params [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> self . add_plugin_dir ( self . module . params [ '<STR_LIT>' ] ) <EOL> elif self . module . params [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> self . remove_plugin_dir ( self . module . params [ '<STR_LIT>' ] ) <EOL> if self . changed : <EOL> with open ( local_path , "<STR_LIT:wb>" ) as fh : <EOL> self . config . write ( fh ) <EOL> self . exit ( ) <EOL> def add_plugin_dir ( self , path ) : <EOL> if isinstance ( path , basestring ) : <EOL> path = set ( [ path ] ) <EOL> else : <EOL> path = set ( path ) <EOL> girder_plugin_dir = set ( [ os . path . join ( self . module . params [ '<STR_LIT>' ] , <EOL> "<STR_LIT>" ) ] ) <EOL> self . message [ '<STR_LIT>' ] [ '<STR_LIT:path>' ] = list ( path ) <EOL> if not self . config . has_section ( '<STR_LIT>' ) : <EOL> self . config . add_section ( '<STR_LIT>' ) <EOL> try : <EOL> plugin_dirs = set ( self . config . get ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) . split ( "<STR_LIT::>" ) ) <EOL> plugin_dirs = plugin_dirs - girder_plugin_dir <EOL> self . changed = not set ( path ) <= set ( plugin_dirs ) <EOL> except cp . NoOptionError : <EOL> plugin_dirs = set ( [ ] ) <EOL> self . changed = True <EOL> if self . changed : <EOL> plugin_dirs |= path <EOL> plugin_dirs = list ( girder_plugin_dir ) + list ( plugin_dirs ) <EOL> self . config . set ( "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" + "<STR_LIT::>" . join ( plugin_dirs ) + "<STR_LIT>" ) <EOL> return <EOL> def remove_plugin_dir ( self , path ) : <EOL> if not self . config . has_section ( '<STR_LIT>' ) : <EOL> return <EOL> girder_plugin_dir = set ( [ os . path . join ( self . module . params [ '<STR_LIT>' ] , <EOL> "<STR_LIT>" ) ] ) <EOL> if isinstance ( path , basestring ) : <EOL> path = set ( [ path ] ) <EOL> else : <EOL> path = set ( path ) <EOL> self . message [ '<STR_LIT>' ] [ '<STR_LIT:path>' ] = list ( path ) <EOL> try : <EOL> plugin_dirs = set ( self . config . get ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) . split ( "<STR_LIT::>" ) ) <EOL> plugin_dirs = plugin_dirs - girder_plugin_dir <EOL> self . changed = bool ( len ( plugin_dirs & path ) ) <EOL> if self . changed : <EOL> leftover = plugin_dirs - path <EOL> if len ( leftover ) == <NUM_LIT:0> : <EOL> self . config . remove_option ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if len ( self . config . items ( "<STR_LIT>" ) ) == <NUM_LIT:0> : <EOL> self . config . remove_section ( "<STR_LIT>" ) <EOL> else : <EOL> plugin_dirs = list ( girder_plugin_dir ) + list ( leftover ) <EOL> self . config . set ( "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" + "<STR_LIT::>" . join ( plugin_dirs ) + "<STR_LIT>" ) <EOL> return <EOL> except cp . NoOptionError : <EOL> return <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> module = AnsibleModule ( <EOL> argument_spec = { <EOL> '<STR_LIT>' : dict ( required = True ) , <EOL> '<STR_LIT>' : dict ( required = True ) , <EOL> '<STR_LIT:state>' : dict ( default = "<STR_LIT>" , choices = [ '<STR_LIT>' , '<STR_LIT>' ] ) } ) <EOL> if IMPORT_SUCCESS is False : <EOL> module . fail_json ( msg = "<STR_LIT>" ) <EOL> try : <EOL> GirderPluginDirModule ( module ) <EOL> except Exception as e : <EOL> import traceback <EOL> module . fail_json ( msg = "<STR_LIT>" . format ( e . __class__ , <EOL> str ( e ) , <EOL> traceback . format_exc ( ) ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
