<s> import os . path <EOL> import re <EOL> from quickunit . utils import clean_bytecode_extension <EOL> class FileChecker ( dict ) : <EOL> def __init__ ( self , rules = None , root = None ) : <EOL> self . rules = rules or [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> self . changed_files = set ( ) <EOL> self . compiled_rules = [ ] <EOL> self . root = root <EOL> dict . __init__ ( self ) <EOL> def __missing__ ( self , filepath ) : <EOL> self [ filepath ] = self . check ( filepath ) <EOL> return self [ filepath ] <EOL> def _get_root ( self ) : <EOL> try : <EOL> filepath = self . changed_files . pop ( ) <EOL> except IndexError : <EOL> return '<STR_LIT>' <EOL> else : <EOL> self . changed_files . add ( filepath ) <EOL> root = os . path . abspath ( filepath ) [ : - len ( filepath ) ] <EOL> self . __dict__ [ '<STR_LIT:root>' ] = root <EOL> return root <EOL> def _set_root ( self , value ) : <EOL> self . __dict__ [ '<STR_LIT:root>' ] = value <EOL> root = property ( _get_root , _set_root ) <EOL> def add_compiled_rules ( self , filepath ) : <EOL> try : <EOL> path , filename = filepath . rsplit ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> path , filename = '<STR_LIT>' , filepath <EOL> basename = filename . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> params = { <EOL> '<STR_LIT:path>' : path , <EOL> '<STR_LIT:filename>' : filename , <EOL> '<STR_LIT>' : basename , <EOL> } <EOL> c_rules = self . compiled_rules <EOL> for rule in self . rules : <EOL> c_rules . append ( re . compile ( rule . format ( ** params ) ) ) <EOL> def add ( self , filepath ) : <EOL> self . add_compiled_rules ( filepath ) <EOL> self . changed_files . add ( filepath ) <EOL> def check ( self , filepath ) : <EOL> filepath = clean_bytecode_extension ( filepath ) <EOL> if self . root and filepath . startswith ( self . root ) : <EOL> filepath = filepath [ len ( self . root ) : ] <EOL> if filepath in self . changed_files : <EOL> return None <EOL> for rule in self . compiled_rules : <EOL> if rule . search ( filepath ) : <EOL> return None <EOL> return False </s>
