<s> """<STR_LIT>""" <EOL> import re <EOL> from cvs2svn_lib . common import FatalError <EOL> from cvs2svn_lib . common import path_join <EOL> from cvs2svn_lib . common import normalize_svn_path <EOL> from cvs2svn_lib . log import logger <EOL> from cvs2svn_lib . symbol import Trunk <EOL> from cvs2svn_lib . symbol import TypedSymbol <EOL> from cvs2svn_lib . symbol import Branch <EOL> from cvs2svn_lib . symbol import Tag <EOL> from cvs2svn_lib . symbol import ExcludedSymbol <EOL> from cvs2svn_lib . symbol_statistics import SymbolPlanError <EOL> class StrategyRule : <EOL> """<STR_LIT>""" <EOL> def start ( self , symbol_statistics ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def get_symbol ( self , symbol , stats ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def finish ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class _RegexpStrategyRule ( StrategyRule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pattern , action ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . regexp = re . compile ( '<STR_LIT>' + pattern + '<STR_LIT>' ) <EOL> except re . error : <EOL> raise FatalError ( "<STR_LIT>" % ( pattern , ) ) <EOL> self . action = action <EOL> def log ( self , symbol ) : <EOL> raise NotImplementedError ( ) <EOL> def get_symbol ( self , symbol , stats ) : <EOL> if isinstance ( symbol , ( Trunk , TypedSymbol ) ) : <EOL> return symbol <EOL> elif self . regexp . match ( symbol . name ) : <EOL> self . log ( symbol ) <EOL> return self . action ( symbol ) <EOL> else : <EOL> return symbol <EOL> class ForceBranchRegexpStrategyRule ( _RegexpStrategyRule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pattern ) : <EOL> _RegexpStrategyRule . __init__ ( self , pattern , Branch ) <EOL> def log ( self , symbol ) : <EOL> logger . verbose ( <EOL> '<STR_LIT>' <EOL> % ( symbol , self . regexp . pattern , ) <EOL> ) <EOL> class ForceTagRegexpStrategyRule ( _RegexpStrategyRule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pattern ) : <EOL> _RegexpStrategyRule . __init__ ( self , pattern , Tag ) <EOL> def log ( self , symbol ) : <EOL> logger . verbose ( <EOL> '<STR_LIT>' <EOL> % ( symbol , self . regexp . pattern , ) <EOL> ) <EOL> class ExcludeRegexpStrategyRule ( _RegexpStrategyRule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pattern ) : <EOL> _RegexpStrategyRule . __init__ ( self , pattern , ExcludedSymbol ) <EOL> def log ( self , symbol ) : <EOL> logger . verbose ( <EOL> '<STR_LIT>' <EOL> % ( symbol , self . regexp . pattern , ) <EOL> ) <EOL> class ExcludeTrivialImportBranchRule ( StrategyRule ) : <EOL> """<STR_LIT>""" <EOL> def get_symbol ( self , symbol , stats ) : <EOL> if isinstance ( symbol , ( Trunk , TypedSymbol ) ) : <EOL> return symbol <EOL> if stats . tag_create_count == <NUM_LIT:0> and stats . branch_create_count == stats . trivial_import_count : <EOL> logger . verbose ( <EOL> '<STR_LIT>' <EOL> % ( symbol , ) <EOL> ) <EOL> return ExcludedSymbol ( symbol ) <EOL> else : <EOL> return symbol <EOL> class ExcludeVendorBranchRule ( StrategyRule ) : <EOL> """<STR_LIT>""" <EOL> def get_symbol ( self , symbol , stats ) : <EOL> if isinstance ( symbol , ( Trunk , TypedSymbol ) ) : <EOL> return symbol <EOL> if stats . tag_create_count == <NUM_LIT:0> and stats . branch_create_count == stats . pure_ntdb_count : <EOL> logger . verbose ( <EOL> '<STR_LIT>' <EOL> % ( symbol , ) <EOL> ) <EOL> return ExcludedSymbol ( symbol ) <EOL> else : <EOL> return symbol <EOL> class UnambiguousUsageRule ( StrategyRule ) : <EOL> """<STR_LIT>""" <EOL> def get_symbol ( self , symbol , stats ) : <EOL> if isinstance ( symbol , ( Trunk , TypedSymbol ) ) : <EOL> return symbol <EOL> is_tag = stats . tag_create_count > <NUM_LIT:0> <EOL> is_branch = stats . branch_create_count > <NUM_LIT:0> or stats . branch_commit_count > <NUM_LIT:0> <EOL> if is_tag and is_branch : <EOL> return symbol <EOL> elif is_branch : <EOL> logger . verbose ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( symbol , ) <EOL> ) <EOL> return Branch ( symbol ) <EOL> elif is_tag : <EOL> logger . verbose ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( symbol , ) <EOL> ) <EOL> return Tag ( symbol ) <EOL> else : <EOL> return symbol <EOL> class BranchIfCommitsRule ( StrategyRule ) : <EOL> """<STR_LIT>""" <EOL> def get_symbol ( self , symbol , stats ) : <EOL> if isinstance ( symbol , ( Trunk , TypedSymbol ) ) : <EOL> return symbol <EOL> elif stats . branch_commit_count > <NUM_LIT:0> : <EOL> logger . verbose ( <EOL> '<STR_LIT>' <EOL> % ( symbol , ) <EOL> ) <EOL> return Branch ( symbol ) <EOL> else : <EOL> return symbol <EOL> class HeuristicStrategyRule ( StrategyRule ) : <EOL> """<STR_LIT>""" <EOL> def get_symbol ( self , symbol , stats ) : <EOL> if isinstance ( symbol , ( Trunk , TypedSymbol ) ) : <EOL> return symbol <EOL> elif stats . tag_create_count >= stats . branch_create_count : <EOL> logger . verbose ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( symbol , ) <EOL> ) <EOL> return Tag ( symbol ) <EOL> else : <EOL> logger . verbose ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( symbol , ) <EOL> ) <EOL> return Branch ( symbol ) <EOL> class _CatchAllRule ( StrategyRule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , action ) : <EOL> self . _action = action <EOL> def log ( self , symbol ) : <EOL> raise NotImplementedError ( ) <EOL> def get_symbol ( self , symbol , stats ) : <EOL> if isinstance ( symbol , ( Trunk , TypedSymbol ) ) : <EOL> return symbol <EOL> else : <EOL> self . log ( symbol ) <EOL> return self . _action ( symbol ) <EOL> class AllBranchRule ( _CatchAllRule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> _CatchAllRule . __init__ ( self , Branch ) <EOL> def log ( self , symbol ) : <EOL> logger . verbose ( <EOL> '<STR_LIT>' <EOL> % ( symbol , ) <EOL> ) <EOL> class AllTagRule ( _CatchAllRule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> _CatchAllRule . __init__ ( self , Tag ) <EOL> def log ( self , symbol ) : <EOL> logger . verbose ( <EOL> '<STR_LIT>' <EOL> % ( symbol , ) <EOL> ) <EOL> class AllExcludedRule ( _CatchAllRule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> _CatchAllRule . __init__ ( self , ExcludedSymbol ) <EOL> def log ( self , symbol ) : <EOL> logger . verbose ( <EOL> '<STR_LIT>' % ( symbol , ) <EOL> ) <EOL> class TrunkPathRule ( StrategyRule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , trunk_path ) : <EOL> self . trunk_path = trunk_path <EOL> def get_symbol ( self , symbol , stats ) : <EOL> if isinstance ( symbol , Trunk ) and symbol . base_path is None : <EOL> symbol . base_path = self . trunk_path <EOL> return symbol <EOL> class SymbolPathRule ( StrategyRule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , symbol_type , base_path ) : <EOL> self . symbol_type = symbol_type <EOL> self . base_path = base_path <EOL> def get_symbol ( self , symbol , stats ) : <EOL> if isinstance ( symbol , self . symbol_type ) and symbol . base_path is None : <EOL> symbol . base_path = path_join ( self . base_path , symbol . name ) <EOL> return symbol <EOL> class BranchesPathRule ( SymbolPathRule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , branch_path ) : <EOL> SymbolPathRule . __init__ ( self , Branch , branch_path ) <EOL> class TagsPathRule ( SymbolPathRule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , tag_path ) : <EOL> SymbolPathRule . __init__ ( self , Tag , tag_path ) <EOL> class HeuristicPreferredParentRule ( StrategyRule ) : <EOL> """<STR_LIT>""" <EOL> def _get_preferred_parent ( self , stats ) : <EOL> """<STR_LIT>""" <EOL> best_count = - <NUM_LIT:1> <EOL> best_symbol = None <EOL> for ( symbol , count ) in stats . possible_parents . items ( ) : <EOL> if count > best_count or ( count == best_count and symbol < best_symbol ) : <EOL> best_count = count <EOL> best_symbol = symbol <EOL> if best_symbol is None : <EOL> return None <EOL> else : <EOL> return best_symbol <EOL> def get_symbol ( self , symbol , stats ) : <EOL> if isinstance ( symbol , TypedSymbol ) and symbol . preferred_parent_id is None : <EOL> preferred_parent = self . _get_preferred_parent ( stats ) <EOL> if preferred_parent is None : <EOL> logger . verbose ( '<STR_LIT>' % ( symbol , ) ) <EOL> else : <EOL> symbol . preferred_parent_id = preferred_parent . id <EOL> logger . verbose ( <EOL> '<STR_LIT>' % ( symbol , preferred_parent , ) <EOL> ) <EOL> return symbol <EOL> class ManualTrunkRule ( StrategyRule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , project_id , svn_path ) : <EOL> self . project_id = project_id <EOL> self . svn_path = normalize_svn_path ( svn_path , allow_empty = True ) <EOL> def get_symbol ( self , symbol , stats ) : <EOL> if ( self . project_id is not None <EOL> and self . project_id != stats . lod . project . id ) : <EOL> return symbol <EOL> if isinstance ( symbol , Trunk ) : <EOL> symbol . base_path = self . svn_path <EOL> return symbol <EOL> def convert_as_branch ( symbol ) : <EOL> logger . verbose ( <EOL> '<STR_LIT>' <EOL> % ( symbol , ) <EOL> ) <EOL> return Branch ( symbol ) <EOL> def convert_as_tag ( symbol ) : <EOL> logger . verbose ( <EOL> '<STR_LIT>' <EOL> % ( symbol , ) <EOL> ) <EOL> return Tag ( symbol ) <EOL> def exclude ( symbol ) : <EOL> logger . verbose ( <EOL> '<STR_LIT>' <EOL> % ( symbol , ) <EOL> ) <EOL> return ExcludedSymbol ( symbol ) <EOL> class ManualSymbolRule ( StrategyRule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( <EOL> self , project_id , symbol_name , conversion , svn_path , parent_lod_name <EOL> ) : <EOL> self . project_id = project_id <EOL> self . symbol_name = symbol_name <EOL> self . conversion = conversion <EOL> if svn_path is None : <EOL> self . svn_path = None <EOL> else : <EOL> self . svn_path = normalize_svn_path ( svn_path , allow_empty = True ) <EOL> self . parent_lod_name = parent_lod_name <EOL> def _get_parent_by_id ( self , parent_lod_name , stats ) : <EOL> """<STR_LIT>""" <EOL> for pp in stats . possible_parents . keys ( ) : <EOL> if isinstance ( pp , Trunk ) : <EOL> pass <EOL> elif pp . name == parent_lod_name : <EOL> return pp <EOL> else : <EOL> parent_counts = stats . possible_parents . items ( ) <EOL> parent_counts . sort ( lambda a , b : - cmp ( a [ <NUM_LIT:1> ] , b [ <NUM_LIT:1> ] ) ) <EOL> lines = [ <EOL> '<STR_LIT>' <EOL> % ( parent_lod_name , stats . lod , ) , <EOL> '<STR_LIT>' <EOL> ] <EOL> for ( symbol , count ) in parent_counts : <EOL> if isinstance ( symbol , Trunk ) : <EOL> lines . append ( '<STR_LIT>' % count ) <EOL> else : <EOL> lines . append ( '<STR_LIT>' % ( symbol . name , count ) ) <EOL> raise SymbolPlanError ( '<STR_LIT:\n>' . join ( lines ) ) <EOL> def get_symbol ( self , symbol , stats ) : <EOL> if ( self . project_id is not None <EOL> and self . project_id != stats . lod . project . id ) : <EOL> return symbol <EOL> elif isinstance ( symbol , Trunk ) : <EOL> return symbol <EOL> elif self . symbol_name == stats . lod . name : <EOL> if self . conversion is not None : <EOL> symbol = self . conversion ( symbol ) <EOL> if self . parent_lod_name is None : <EOL> pass <EOL> elif self . parent_lod_name == '<STR_LIT>' : <EOL> symbol . preferred_parent_id = stats . lod . project . trunk_id <EOL> else : <EOL> symbol . preferred_parent_id = self . _get_parent_by_id ( <EOL> self . parent_lod_name , stats <EOL> ) . id <EOL> if self . svn_path is not None : <EOL> symbol . base_path = self . svn_path <EOL> return symbol <EOL> class SymbolHintsFileRule ( StrategyRule ) : <EOL> """<STR_LIT>""" <EOL> comment_re = re . compile ( r'<STR_LIT>' ) <EOL> conversion_map = { <EOL> '<STR_LIT>' : convert_as_branch , <EOL> '<STR_LIT>' : convert_as_tag , <EOL> '<STR_LIT>' : exclude , <EOL> '<STR_LIT:.>' : None , <EOL> } <EOL> def __init__ ( self , filename ) : <EOL> self . filename = filename <EOL> def start ( self , symbol_statistics ) : <EOL> self . _rules = [ ] <EOL> f = open ( self . filename , '<STR_LIT:r>' ) <EOL> try : <EOL> for l in f : <EOL> l = l . rstrip ( ) <EOL> s = l . lstrip ( ) <EOL> if self . comment_re . match ( s ) : <EOL> continue <EOL> fields = s . split ( ) <EOL> if len ( fields ) < <NUM_LIT:3> : <EOL> raise FatalError ( <EOL> '<STR_LIT>' <EOL> % ( self . filename , l , ) <EOL> ) <EOL> project_id = fields . pop ( <NUM_LIT:0> ) <EOL> symbol_name = fields . pop ( <NUM_LIT:0> ) <EOL> conversion = fields . pop ( <NUM_LIT:0> ) <EOL> if fields : <EOL> svn_path = fields . pop ( <NUM_LIT:0> ) <EOL> if svn_path == '<STR_LIT:.>' : <EOL> svn_path = None <EOL> elif svn_path [ <NUM_LIT:0> ] == '<STR_LIT:/>' : <EOL> svn_path = svn_path [ <NUM_LIT:1> : ] <EOL> else : <EOL> svn_path = None <EOL> if fields : <EOL> parent_lod_name = fields . pop ( <NUM_LIT:0> ) <EOL> else : <EOL> parent_lod_name = '<STR_LIT:.>' <EOL> if fields : <EOL> raise FatalError ( <EOL> '<STR_LIT>' <EOL> % ( self . filename , l , ) <EOL> ) <EOL> if project_id == '<STR_LIT:.>' : <EOL> project_id = None <EOL> else : <EOL> try : <EOL> project_id = int ( project_id ) <EOL> except ValueError : <EOL> raise FatalError ( <EOL> '<STR_LIT>' % ( l , ) <EOL> ) <EOL> if symbol_name == '<STR_LIT>' : <EOL> if conversion not in [ '<STR_LIT:.>' , '<STR_LIT>' ] : <EOL> raise FatalError ( '<STR_LIT>' ) <EOL> if parent_lod_name != '<STR_LIT:.>' : <EOL> raise FatalError ( '<STR_LIT>' ) <EOL> if svn_path is None : <EOL> pass <EOL> else : <EOL> self . _rules . append ( ManualTrunkRule ( project_id , svn_path ) ) <EOL> else : <EOL> try : <EOL> conversion = self . conversion_map [ conversion ] <EOL> except KeyError : <EOL> raise FatalError ( <EOL> '<STR_LIT>' % ( l , ) <EOL> ) <EOL> if parent_lod_name == '<STR_LIT:.>' : <EOL> parent_lod_name = None <EOL> if conversion is None and svn_path is None and parent_lod_name is None : <EOL> pass <EOL> else : <EOL> self . _rules . append ( <EOL> ManualSymbolRule ( <EOL> project_id , symbol_name , <EOL> conversion , svn_path , parent_lod_name <EOL> ) <EOL> ) <EOL> finally : <EOL> f . close ( ) <EOL> for rule in self . _rules : <EOL> rule . start ( symbol_statistics ) <EOL> def get_symbol ( self , symbol , stats ) : <EOL> for rule in self . _rules : <EOL> symbol = rule . get_symbol ( symbol , stats ) <EOL> return symbol <EOL> def finish ( self ) : <EOL> for rule in self . _rules : <EOL> rule . finish ( ) <EOL> del self . _rules </s>
