<s> """<STR_LIT>""" <EOL> import os <EOL> import re <EOL> from cvs2svn_lib . log import logger <EOL> from cvs2svn_lib . common import FatalError <EOL> from cvs2svn_lib . common import IllegalSVNPathError <EOL> from cvs2svn_lib . common import normalize_svn_path <EOL> from cvs2svn_lib . common import is_branch_revision_number <EOL> class SymbolTransform : <EOL> """<STR_LIT>""" <EOL> def transform ( self , cvs_file , symbol_name , revision ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> class ReplaceSubstringsSymbolTransform ( SymbolTransform ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , old , new ) : <EOL> self . old = old <EOL> self . new = new <EOL> def transform ( self , cvs_file , symbol_name , revision ) : <EOL> return symbol_name . replace ( self . old , self . new ) <EOL> class NormalizePathsSymbolTransform ( SymbolTransform ) : <EOL> def transform ( self , cvs_file , symbol_name , revision ) : <EOL> try : <EOL> return normalize_svn_path ( symbol_name ) <EOL> except IllegalSVNPathError , e : <EOL> raise FatalError ( '<STR_LIT>' % ( symbol_name , e , ) ) <EOL> class CompoundSymbolTransform ( SymbolTransform ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , symbol_transforms ) : <EOL> """<STR_LIT>""" <EOL> self . symbol_transforms = list ( symbol_transforms ) <EOL> def transform ( self , cvs_file , symbol_name , revision ) : <EOL> for symbol_transform in self . symbol_transforms : <EOL> symbol_name = symbol_transform . transform ( <EOL> cvs_file , symbol_name , revision <EOL> ) <EOL> if symbol_name is None : <EOL> break <EOL> return symbol_name <EOL> class RegexpSymbolTransform ( SymbolTransform ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pattern , replacement ) : <EOL> """<STR_LIT>""" <EOL> self . pattern = re . compile ( '<STR_LIT>' + pattern + '<STR_LIT:$>' ) <EOL> self . replacement = replacement <EOL> def transform ( self , cvs_file , symbol_name , revision ) : <EOL> return self . pattern . sub ( self . replacement , symbol_name ) <EOL> class SymbolMapper ( SymbolTransform ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , items = [ ] ) : <EOL> """<STR_LIT>""" <EOL> self . _map = { } <EOL> for ( cvs_filename , symbol_name , revision , new_name ) in items : <EOL> self [ cvs_filename , symbol_name , revision ] = new_name <EOL> def __setitem__ ( self , ( cvs_filename , symbol_name , revision ) , new_name ) : <EOL> """<STR_LIT>""" <EOL> cvs_filename = os . path . normcase ( os . path . normpath ( cvs_filename ) ) <EOL> key = ( cvs_filename , symbol_name , revision ) <EOL> if key in self . _map : <EOL> logger . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( cvs_filename , symbol_name , revision , ) <EOL> ) <EOL> self . _map [ key ] = new_name <EOL> def transform ( self , cvs_file , symbol_name , revision ) : <EOL> cvs_filename = os . path . normcase ( cvs_file . rcs_path ) <EOL> return self . _map . get ( <EOL> ( cvs_filename , symbol_name , revision ) , symbol_name <EOL> ) <EOL> class SubtreeSymbolMapper ( SymbolTransform ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , items = [ ] ) : <EOL> """<STR_LIT>""" <EOL> self . _map = { } <EOL> for ( cvs_path , symbol_name , new_name ) in items : <EOL> self [ cvs_path , symbol_name ] = new_name <EOL> def __setitem__ ( self , ( cvs_path , symbol_name ) , new_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> symbol_map = self . _map [ symbol_name ] <EOL> except KeyError : <EOL> symbol_map = { } <EOL> self . _map [ symbol_name ] = symbol_map <EOL> cvs_path = os . path . normcase ( os . path . normpath ( cvs_path ) ) <EOL> if cvs_path in symbol_map : <EOL> logger . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( cvs_path , symbol_name , ) <EOL> ) <EOL> symbol_map [ cvs_path ] = new_name <EOL> def transform ( self , cvs_file , symbol_name , revision ) : <EOL> try : <EOL> symbol_map = self . _map [ symbol_name ] <EOL> except KeyError : <EOL> return symbol_name <EOL> cvs_path = os . path . normcase ( cvs_file . rcs_path ) <EOL> while True : <EOL> try : <EOL> return symbol_map [ cvs_path ] <EOL> except KeyError : <EOL> new_cvs_path = os . path . dirname ( cvs_path ) <EOL> if new_cvs_path == cvs_path : <EOL> return symbol_name <EOL> else : <EOL> cvs_path = new_cvs_path <EOL> class IgnoreSymbolTransform ( SymbolTransform ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pattern ) : <EOL> """<STR_LIT>""" <EOL> self . pattern = re . compile ( '<STR_LIT>' + pattern + '<STR_LIT:$>' ) <EOL> def transform ( self , cvs_file , symbol_name , revision ) : <EOL> if self . pattern . match ( symbol_name ) : <EOL> return None <EOL> else : <EOL> return symbol_name <EOL> class SubtreeSymbolTransform ( SymbolTransform ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cvs_path , inner_symbol_transform ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( cvs_path , str ) <EOL> self . __subtree = os . path . normcase ( os . path . normpath ( cvs_path ) ) <EOL> self . __subtree_len = len ( self . __subtree ) <EOL> self . __inner = inner_symbol_transform <EOL> def __does_rule_apply_to ( self , cvs_file ) : <EOL> cvs_path = os . path . normcase ( cvs_file . rcs_path ) <EOL> if not cvs_path . startswith ( self . __subtree ) : <EOL> return False <EOL> if len ( cvs_path ) == self . __subtree_len : <EOL> return True <EOL> return cvs_path [ self . __subtree_len ] == os . path . sep <EOL> def transform ( self , cvs_file , symbol_name , revision ) : <EOL> if self . __does_rule_apply_to ( cvs_file ) : <EOL> return self . __inner . transform ( cvs_file , symbol_name , revision ) <EOL> else : <EOL> return symbol_name <EOL> class TagOnlyTransform ( SymbolTransform ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , inner_symbol_transform ) : <EOL> """<STR_LIT>""" <EOL> self . __inner = inner_symbol_transform <EOL> def transform ( self , cvs_file , symbol_name , revision ) : <EOL> if is_branch_revision_number ( revision ) : <EOL> return symbol_name <EOL> else : <EOL> return self . __inner . transform ( cvs_file , symbol_name , revision ) <EOL> class BranchOnlyTransform ( SymbolTransform ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , inner_symbol_transform ) : <EOL> """<STR_LIT>""" <EOL> self . __inner = inner_symbol_transform <EOL> def transform ( self , cvs_file , symbol_name , revision ) : <EOL> if is_branch_revision_number ( revision ) : <EOL> return self . __inner . transform ( cvs_file , symbol_name , revision ) <EOL> else : <EOL> return symbol_name </s>
