<s> """<STR_LIT>""" <EOL> from coverage . backward import to_string <EOL> from coverage . misc import CoverageException <EOL> import fnmatch , os , os . path , re , sys <EOL> import ntpath , posixpath <EOL> class FileLocator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . relative_dir = os . path . normcase ( abs_file ( os . curdir ) + os . sep ) <EOL> self . canonical_filename_cache = { } <EOL> def relative_filename ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> fnorm = os . path . normcase ( filename ) <EOL> if fnorm . startswith ( self . relative_dir ) : <EOL> filename = filename [ len ( self . relative_dir ) : ] <EOL> return filename <EOL> def canonical_filename ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> if filename not in self . canonical_filename_cache : <EOL> if not os . path . isabs ( filename ) : <EOL> for path in [ os . curdir ] + sys . path : <EOL> if path is None : <EOL> continue <EOL> f = os . path . join ( path , filename ) <EOL> if os . path . exists ( f ) : <EOL> filename = f <EOL> break <EOL> cf = abs_file ( filename ) <EOL> self . canonical_filename_cache [ filename ] = cf <EOL> return self . canonical_filename_cache [ filename ] <EOL> def get_zip_data ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> import zipimport <EOL> markers = [ '<STR_LIT>' + os . sep , '<STR_LIT>' + os . sep ] <EOL> for marker in markers : <EOL> if marker in filename : <EOL> parts = filename . split ( marker ) <EOL> try : <EOL> zi = zipimport . zipimporter ( parts [ <NUM_LIT:0> ] + marker [ : - <NUM_LIT:1> ] ) <EOL> except zipimport . ZipImportError : <EOL> continue <EOL> try : <EOL> data = zi . get_data ( parts [ <NUM_LIT:1> ] ) <EOL> except IOError : <EOL> continue <EOL> return to_string ( data ) <EOL> return None <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> def actual_path ( path ) : <EOL> """<STR_LIT>""" <EOL> if path in actual_path . cache : <EOL> return actual_path . cache [ path ] <EOL> head , tail = os . path . split ( path ) <EOL> if not tail : <EOL> actpath = head <EOL> elif not head : <EOL> actpath = tail <EOL> else : <EOL> head = actual_path ( head ) <EOL> if head in actual_path . list_cache : <EOL> files = actual_path . list_cache [ head ] <EOL> else : <EOL> try : <EOL> files = os . listdir ( head ) <EOL> except OSError : <EOL> files = [ ] <EOL> actual_path . list_cache [ head ] = files <EOL> normtail = os . path . normcase ( tail ) <EOL> for f in files : <EOL> if os . path . normcase ( f ) == normtail : <EOL> tail = f <EOL> break <EOL> actpath = os . path . join ( head , tail ) <EOL> actual_path . cache [ path ] = actpath <EOL> return actpath <EOL> actual_path . cache = { } <EOL> actual_path . list_cache = { } <EOL> else : <EOL> def actual_path ( filename ) : <EOL> """<STR_LIT>""" <EOL> return filename <EOL> def abs_file ( filename ) : <EOL> """<STR_LIT>""" <EOL> path = os . path . expandvars ( os . path . expanduser ( filename ) ) <EOL> path = os . path . abspath ( os . path . realpath ( path ) ) <EOL> path = actual_path ( path ) <EOL> return path <EOL> def isabs_anywhere ( filename ) : <EOL> """<STR_LIT>""" <EOL> return ntpath . isabs ( filename ) or posixpath . isabs ( filename ) <EOL> def prep_patterns ( patterns ) : <EOL> """<STR_LIT>""" <EOL> prepped = [ ] <EOL> for p in patterns or [ ] : <EOL> if p . startswith ( "<STR_LIT:*>" ) or p . startswith ( "<STR_LIT:?>" ) : <EOL> prepped . append ( p ) <EOL> else : <EOL> prepped . append ( abs_file ( p ) ) <EOL> return prepped <EOL> class TreeMatcher ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , directories ) : <EOL> self . dirs = directories [ : ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . dirs <EOL> def info ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . dirs <EOL> def add ( self , directory ) : <EOL> """<STR_LIT>""" <EOL> self . dirs . append ( directory ) <EOL> def match ( self , fpath ) : <EOL> """<STR_LIT>""" <EOL> for d in self . dirs : <EOL> if fpath . startswith ( d ) : <EOL> if fpath == d : <EOL> return True <EOL> if fpath [ len ( d ) ] == os . sep : <EOL> return True <EOL> return False <EOL> class FnmatchMatcher ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pats ) : <EOL> self . pats = pats [ : ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . pats <EOL> def info ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . pats <EOL> def match ( self , fpath ) : <EOL> """<STR_LIT>""" <EOL> for pat in self . pats : <EOL> if fnmatch . fnmatch ( fpath , pat ) : <EOL> return True <EOL> return False <EOL> def sep ( s ) : <EOL> """<STR_LIT>""" <EOL> sep_match = re . search ( r"<STR_LIT>" , s ) <EOL> if sep_match : <EOL> the_sep = sep_match . group ( <NUM_LIT:0> ) <EOL> else : <EOL> the_sep = os . sep <EOL> return the_sep <EOL> class PathAliases ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , locator = None ) : <EOL> self . aliases = [ ] <EOL> self . locator = locator <EOL> def add ( self , pattern , result ) : <EOL> """<STR_LIT>""" <EOL> pattern = pattern . rstrip ( r"<STR_LIT>" ) <EOL> if pattern . endswith ( "<STR_LIT:*>" ) : <EOL> raise CoverageException ( "<STR_LIT>" ) <EOL> pattern_sep = sep ( pattern ) <EOL> if not pattern . startswith ( '<STR_LIT:*>' ) and not isabs_anywhere ( pattern ) : <EOL> pattern = abs_file ( pattern ) <EOL> pattern += pattern_sep <EOL> regex_pat = fnmatch . translate ( pattern ) . replace ( r'<STR_LIT>' , '<STR_LIT:(>' ) <EOL> if regex_pat . endswith ( "<STR_LIT:$>" ) : <EOL> regex_pat = regex_pat [ : - <NUM_LIT:1> ] <EOL> regex_pat = regex_pat . replace ( r"<STR_LIT>" , r"<STR_LIT>" ) <EOL> regex = re . compile ( r"<STR_LIT>" + regex_pat ) <EOL> result_sep = sep ( result ) <EOL> result = result . rstrip ( r"<STR_LIT>" ) + result_sep <EOL> self . aliases . append ( ( regex , result , pattern_sep , result_sep ) ) <EOL> def map ( self , path ) : <EOL> """<STR_LIT>""" <EOL> for regex , result , pattern_sep , result_sep in self . aliases : <EOL> m = regex . match ( path ) <EOL> if m : <EOL> new = path . replace ( m . group ( <NUM_LIT:0> ) , result ) <EOL> if pattern_sep != result_sep : <EOL> new = new . replace ( pattern_sep , result_sep ) <EOL> if self . locator : <EOL> new = self . locator . canonical_filename ( new ) <EOL> return new <EOL> return path <EOL> def find_python_files ( dirname ) : <EOL> """<STR_LIT>""" <EOL> for i , ( dirpath , dirnames , filenames ) in enumerate ( os . walk ( dirname ) ) : <EOL> if i > <NUM_LIT:0> and '<STR_LIT>' not in filenames : <EOL> del dirnames [ : ] <EOL> continue <EOL> for filename in filenames : <EOL> if re . match ( r"<STR_LIT>" , filename ) : <EOL> yield os . path . join ( dirpath , filename ) </s>
