Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """Cache lines from files. |
| 2 | |
| 3 | This is intended to read lines from modules imported -- hence if a filename |
| 4 | is not found, it will look down the module search path for a file by |
| 5 | that name. |
| 6 | """ |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | c341c62 | 1992-03-27 15:12:43 +0000 | [diff] [blame] | 8 | import sys |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 9 | import os |
Benjamin Peterson | 9b8d24b | 2009-03-24 22:30:15 +0000 | [diff] [blame] | 10 | import tokenize |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 11 | |
Jeremy Hylton | 97b2e84 | 2003-06-29 16:59:43 +0000 | [diff] [blame] | 12 | __all__ = ["getline", "clearcache", "checkcache"] |
Skip Montanaro | 17ab123 | 2001-01-24 06:27:27 +0000 | [diff] [blame] | 13 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14 | def getline(filename, lineno, module_globals=None): |
| 15 | lines = getlines(filename, module_globals) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 16 | if 1 <= lineno <= len(lines): |
| 17 | return lines[lineno-1] |
| 18 | else: |
| 19 | return '' |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 20 | |
| 21 | |
| 22 | # The cache |
| 23 | |
| 24 | cache = {} # The cache |
| 25 | |
| 26 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 27 | def clearcache(): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 28 | """Clear the cache entirely.""" |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 30 | global cache |
| 31 | cache = {} |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 33 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 34 | def getlines(filename, module_globals=None): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 35 | """Get the lines for a file from the cache. |
| 36 | Update the cache if it doesn't contain an entry for this file already.""" |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 37 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 38 | if filename in cache: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 39 | return cache[filename][2] |
| 40 | else: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 41 | return updatecache(filename, module_globals) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 43 | |
Hye-Shik Chang | 182ac85 | 2004-10-26 09:16:42 +0000 | [diff] [blame] | 44 | def checkcache(filename=None): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 45 | """Discard cache entries that are out of date. |
| 46 | (This is not checked upon each call!)""" |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 47 | |
Hye-Shik Chang | 182ac85 | 2004-10-26 09:16:42 +0000 | [diff] [blame] | 48 | if filename is None: |
Guido van Rossum | f543348 | 2007-02-26 22:21:25 +0000 | [diff] [blame] | 49 | filenames = list(cache.keys()) |
Hye-Shik Chang | 182ac85 | 2004-10-26 09:16:42 +0000 | [diff] [blame] | 50 | else: |
| 51 | if filename in cache: |
| 52 | filenames = [filename] |
| 53 | else: |
| 54 | return |
| 55 | |
| 56 | for filename in filenames: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 57 | size, mtime, lines, fullname = cache[filename] |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 58 | if mtime is None: |
| 59 | continue # no-op for files loaded via a __loader__ |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 60 | try: |
| 61 | stat = os.stat(fullname) |
| 62 | except os.error: |
| 63 | del cache[filename] |
| 64 | continue |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 65 | if size != stat.st_size or mtime != stat.st_mtime: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 66 | del cache[filename] |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 68 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 69 | def updatecache(filename, module_globals=None): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 70 | """Update a cache entry and return its list of lines. |
| 71 | If something's wrong, print a message, discard the cache entry, |
| 72 | and return an empty list.""" |
| 73 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 74 | if filename in cache: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 75 | del cache[filename] |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 76 | if not filename or (filename.startswith('<') and filename.endswith('>')): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 77 | return [] |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 79 | fullname = filename |
| 80 | try: |
| 81 | stat = os.stat(fullname) |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 82 | except OSError: |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 83 | basename = filename |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 84 | |
| 85 | # Try for a __loader__, if available |
| 86 | if module_globals and '__loader__' in module_globals: |
| 87 | name = module_globals.get('__name__') |
| 88 | loader = module_globals['__loader__'] |
| 89 | get_source = getattr(loader, 'get_source', None) |
| 90 | |
| 91 | if name and get_source: |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 92 | try: |
| 93 | data = get_source(name) |
| 94 | except (ImportError, IOError): |
| 95 | pass |
| 96 | else: |
| 97 | if data is None: |
| 98 | # No luck, the PEP302 loader cannot find the source |
| 99 | # for this module. |
| 100 | return [] |
| 101 | cache[filename] = ( |
| 102 | len(data), None, |
| 103 | [line+'\n' for line in data.splitlines()], fullname |
| 104 | ) |
| 105 | return cache[filename][2] |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 106 | |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 107 | # Try looking through the module search path, which is only useful |
| 108 | # when handling a relative filename. |
| 109 | if os.path.isabs(filename): |
| 110 | return [] |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 111 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 112 | for dirname in sys.path: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 113 | try: |
Tim Peters | 12f21ae | 2001-05-29 04:27:01 +0000 | [diff] [blame] | 114 | fullname = os.path.join(dirname, basename) |
| 115 | except (TypeError, AttributeError): |
| 116 | # Not sufficiently string-like to do anything useful with. |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 117 | continue |
| 118 | try: |
| 119 | stat = os.stat(fullname) |
| 120 | break |
| 121 | except os.error: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 122 | pass |
| 123 | else: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 124 | return [] |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 125 | try: |
Victor Stinner | 58c0752 | 2010-11-09 01:08:59 +0000 | [diff] [blame] | 126 | with tokenize.open(fullname) as fp: |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 127 | lines = fp.readlines() |
| 128 | except IOError: |
Victor Stinner | 41a64a5 | 2010-09-29 01:30:45 +0000 | [diff] [blame] | 129 | return [] |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 130 | if lines and not lines[-1].endswith('\n'): |
| 131 | lines[-1] += '\n' |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 132 | size, mtime = stat.st_size, stat.st_mtime |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 133 | cache[filename] = size, mtime, lines, fullname |
| 134 | return lines |