<s> """<STR_LIT>""" <EOL> import re <EOL> import os <EOL> import argparse <EOL> import sys <EOL> import logging <EOL> from collections import defaultdict <EOL> logging . basicConfig ( level = logging . INFO ) <EOL> parser = argparse . ArgumentParser ( description = __doc__ ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = None , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = "<STR_LIT>" ) <EOL> args = parser . parse_args ( ) <EOL> if args . modules_dir is None : <EOL> modules_home = os . environ . get ( '<STR_LIT>' ) <EOL> if modules_home is None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> args . modules_dir = os . path . join ( modules_home , '<STR_LIT>' ) <EOL> def get_tool_version ( dir_string ) : <EOL> """<STR_LIT>""" <EOL> match = re . match ( '<STR_LIT>' . format ( args . tools_dir ) , os . path . normpath ( dir_string ) ) <EOL> if not match : <EOL> raise Exception ( "<STR_LIT>" + dir_string + "<STR_LIT:\n>" ) <EOL> directories = match . group ( <NUM_LIT:1> ) . split ( '<STR_LIT:/>' ) <EOL> if len ( directories ) < <NUM_LIT:2> : <EOL> raise Exception ( "<STR_LIT>" + match . group ( <NUM_LIT:1> ) + "<STR_LIT:\n>" ) <EOL> tool , version = directories [ : <NUM_LIT:2> ] <EOL> return ( tool , version ) <EOL> delete_list = [ ] <EOL> delete_regex = re . compile ( "<STR_LIT>" ) <EOL> for ( dir , subdirs , files ) in os . walk ( args . modules_dir ) : <EOL> for file in files : <EOL> filepath = os . path . join ( dir , file ) <EOL> for line in open ( filepath , '<STR_LIT:rb>' ) : <EOL> if delete_regex . search ( line ) : <EOL> delete_list . append ( filepath ) <EOL> break <EOL> if len ( delete_list ) > <NUM_LIT:0> : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT:\n>" . join ( delete_list ) <EOL> if not args . force : <EOL> print "<STR_LIT>" <EOL> if raw_input ( ) . lower ( ) != '<STR_LIT:y>' : <EOL> sys . exit ( ) <EOL> logging . info ( "<STR_LIT>" ) <EOL> for file in delete_list : <EOL> os . remove ( file ) <EOL> tool_shells = defaultdict ( list ) <EOL> for ( dir , subdirs , files ) in os . walk ( args . tools_dir ) : <EOL> for file in files : <EOL> if file == '<STR_LIT>' : <EOL> tool , version = get_tool_version ( dir ) <EOL> tool_shells [ ( tool , version ) ] . append ( dir ) <EOL> for ( ( tool_name , tool_version ) , dirs ) in tool_shells . iteritems ( ) : <EOL> module_lines = [ ] <EOL> for dir in dirs : <EOL> env_path = os . path . join ( dir , '<STR_LIT>' ) <EOL> logging . debug ( "<STR_LIT>" + env_path ) <EOL> with open ( env_path ) as f : <EOL> for row in f : <EOL> ifmatch = re . match ( "<STR_LIT>" , row . strip ( ) ) <EOL> if ifmatch : <EOL> continue <EOL> for line in row . split ( '<STR_LIT:;>' ) : <EOL> envmatch = re . match ( "<STR_LIT>" , line . strip ( ) ) <EOL> exportmatch = re . match ( "<STR_LIT>" , line . strip ( ) ) <EOL> if envmatch : <EOL> variable = envmatch . group ( <NUM_LIT:1> ) . strip ( ) <EOL> parts = [ x . strip ( ) for x in envmatch . group ( <NUM_LIT:2> ) . split ( '<STR_LIT::>' ) ] <EOL> parts = [ x for x in parts if x != '<STR_LIT>' ] <EOL> logging . debug ( "<STR_LIT>" + str ( variable ) + "<STR_LIT>" + str ( parts ) ) <EOL> bashvar = '<STR_LIT:$>' + variable <EOL> if bashvar not in set ( parts ) : <EOL> module_lines . append ( "<STR_LIT>" . format ( variable , <EOL> '<STR_LIT::>' . join ( parts ) ) ) <EOL> elif bashvar == parts [ <NUM_LIT:0> ] : <EOL> module_lines . append ( "<STR_LIT>" . format ( variable , <EOL> '<STR_LIT::>' . join ( parts [ <NUM_LIT:1> : ] ) ) ) <EOL> elif bashvar == parts [ - <NUM_LIT:1> ] : <EOL> module_lines . append ( "<STR_LIT>" . format ( variable , <EOL> '<STR_LIT::>' . join ( parts [ : - <NUM_LIT:1> ] ) ) ) <EOL> else : <EOL> logging . warn ( "<STR_LIT>" "<STR_LIT>" . format ( variable , env_path ) ) <EOL> elif exportmatch : <EOL> pass <EOL> elif len ( line . strip ( ) ) == <NUM_LIT:0> : <EOL> pass <EOL> else : <EOL> logging . warn ( "<STR_LIT>" + env_path ) <EOL> logging . debug ( "<STR_LIT>" + line . strip ( ) ) <EOL> module_contents = "<STR_LIT>" <EOL> module_contents += "<STR_LIT>" <EOL> module_contents += "<STR_LIT>" . join ( module_lines ) <EOL> module_contents += "<STR_LIT>" . format ( tool_name , tool_version ) <EOL> module_tool_path = os . path . join ( args . modules_dir , tool_name ) <EOL> if not os . path . exists ( module_tool_path ) : <EOL> os . makedirs ( module_tool_path ) <EOL> module_full_path = os . path . join ( module_tool_path , tool_version ) <EOL> logging . info ( "<STR_LIT>" + module_full_path ) <EOL> f = open ( module_full_path , '<STR_LIT:wb>' ) <EOL> f . write ( module_contents ) <EOL> f . close ( ) </s>
