<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> import re <EOL> import sys <EOL> class Watchlists ( object ) : <EOL> """<STR_LIT>""" <EOL> _RULES = "<STR_LIT>" <EOL> _RULES_FILENAME = _RULES <EOL> _repo_root = None <EOL> _defns = { } <EOL> _watchlists = { } <EOL> def __init__ ( self , repo_root ) : <EOL> self . _repo_root = repo_root <EOL> self . _LoadWatchlistRules ( ) <EOL> def _GetRulesFilePath ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . path . join ( self . _repo_root , self . _RULES_FILENAME ) <EOL> def _HasWatchlistsFile ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . path . exists ( self . _GetRulesFilePath ( ) ) <EOL> def _ContentsOfWatchlistsFile ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> watchlists_file = open ( self . _GetRulesFilePath ( ) ) <EOL> contents = watchlists_file . read ( ) <EOL> watchlists_file . close ( ) <EOL> return contents <EOL> except IOError , e : <EOL> logging . error ( "<STR_LIT>" % ( self . _GetRulesFilePath ( ) , e ) ) <EOL> return '<STR_LIT>' <EOL> def _LoadWatchlistRules ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _HasWatchlistsFile ( ) : <EOL> return <EOL> contents = self . _ContentsOfWatchlistsFile ( ) <EOL> watchlists_data = None <EOL> try : <EOL> watchlists_data = eval ( contents , { '<STR_LIT>' : None } , None ) <EOL> except SyntaxError , e : <EOL> logging . error ( "<STR_LIT>" % ( self . _GetRulesFilePath ( ) , e ) ) <EOL> return <EOL> defns = watchlists_data . get ( "<STR_LIT>" ) <EOL> if not defns : <EOL> logging . error ( "<STR_LIT>" % <EOL> self . _GetRulesFilePath ( ) ) <EOL> return <EOL> watchlists = watchlists_data . get ( "<STR_LIT>" ) <EOL> if not watchlists : <EOL> logging . error ( "<STR_LIT>" % self . _GetRulesFilePath ( ) ) <EOL> return <EOL> self . _defns = defns <EOL> self . _watchlists = watchlists <EOL> for name in watchlists : <EOL> if name not in defns : <EOL> logging . error ( "<STR_LIT>" % ( name , self . _GetRulesFilePath ( ) ) ) <EOL> def GetWatchersForPaths ( self , paths ) : <EOL> """<STR_LIT>""" <EOL> watchers = set ( ) <EOL> for path in paths : <EOL> path = path . replace ( os . sep , '<STR_LIT:/>' ) <EOL> for name , rule in self . _defns . iteritems ( ) : <EOL> if name not in self . _watchlists : continue <EOL> rex_str = rule . get ( '<STR_LIT>' ) <EOL> if not rex_str : continue <EOL> if re . search ( rex_str , path ) : <EOL> map ( watchers . add , self . _watchlists [ name ] ) <EOL> return list ( watchers ) <EOL> def main ( argv ) : <EOL> if len ( argv ) < <NUM_LIT:2> : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" % argv [ <NUM_LIT:0> ] <EOL> return <NUM_LIT:1> <EOL> wl = Watchlists ( os . getcwd ( ) ) <EOL> watchers = wl . GetWatchersForPaths ( argv [ <NUM_LIT:1> : ] ) <EOL> print watchers <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( sys . argv ) </s>
