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 |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 10 | |
Jeremy Hylton | 97b2e84 | 2003-06-29 16:59:43 +0000 | [diff] [blame] | 11 | __all__ = ["getline", "clearcache", "checkcache"] |
Skip Montanaro | 17ab123 | 2001-01-24 06:27:27 +0000 | [diff] [blame] | 12 | |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 13 | def getline(filename, lineno, module_globals=None): |
| 14 | lines = getlines(filename, module_globals) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 15 | if 1 <= lineno <= len(lines): |
| 16 | return lines[lineno-1] |
| 17 | else: |
| 18 | return '' |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 19 | |
| 20 | |
| 21 | # The cache |
| 22 | |
| 23 | cache = {} # The cache |
| 24 | |
| 25 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 26 | def clearcache(): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 27 | """Clear the cache entirely.""" |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 29 | global cache |
| 30 | cache = {} |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 32 | |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 33 | def getlines(filename, module_globals=None): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 34 | """Get the lines for a file from the cache. |
| 35 | 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] | 36 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 37 | if filename in cache: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 38 | return cache[filename][2] |
| 39 | else: |
Neal Norwitz | 0c46985 | 2006-04-11 07:21:20 +0000 | [diff] [blame] | 40 | return updatecache(filename, module_globals) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 42 | |
Hye-Shik Chang | 182ac85 | 2004-10-26 09:16:42 +0000 | [diff] [blame] | 43 | def checkcache(filename=None): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 44 | """Discard cache entries that are out of date. |
| 45 | (This is not checked upon each call!)""" |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 46 | |
Hye-Shik Chang | 182ac85 | 2004-10-26 09:16:42 +0000 | [diff] [blame] | 47 | if filename is None: |
| 48 | filenames = cache.keys() |
| 49 | else: |
| 50 | if filename in cache: |
| 51 | filenames = [filename] |
| 52 | else: |
| 53 | return |
| 54 | |
| 55 | for filename in filenames: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 56 | size, mtime, lines, fullname = cache[filename] |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 57 | if mtime is None: |
| 58 | continue # no-op for files loaded via a __loader__ |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 59 | try: |
| 60 | stat = os.stat(fullname) |
| 61 | except os.error: |
| 62 | del cache[filename] |
| 63 | continue |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 64 | if size != stat.st_size or mtime != stat.st_mtime: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 65 | del cache[filename] |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 67 | |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 68 | def updatecache(filename, module_globals=None): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 69 | """Update a cache entry and return its list of lines. |
| 70 | If something's wrong, print a message, discard the cache entry, |
| 71 | and return an empty list.""" |
| 72 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 73 | if filename in cache: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 74 | del cache[filename] |
| 75 | if not filename or filename[0] + filename[-1] == '<>': |
| 76 | return [] |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 78 | fullname = filename |
| 79 | try: |
| 80 | stat = os.stat(fullname) |
| 81 | except os.error, msg: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 82 | basename = os.path.split(filename)[1] |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 83 | |
Tim Peters | 413c922 | 2006-04-11 01:44:26 +0000 | [diff] [blame] | 84 | # Try for a __loader__, if available |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 85 | if module_globals and '__loader__' in module_globals: |
| 86 | name = module_globals.get('__name__') |
| 87 | loader = module_globals['__loader__'] |
Neal Norwitz | 0c46985 | 2006-04-11 07:21:20 +0000 | [diff] [blame] | 88 | get_source = getattr(loader, 'get_source', None) |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 89 | |
| 90 | if name and get_source: |
| 91 | if basename.startswith(name.split('.')[-1]+'.'): |
| 92 | try: |
| 93 | data = get_source(name) |
Neal Norwitz | 0c46985 | 2006-04-11 07:21:20 +0000 | [diff] [blame] | 94 | except (ImportError, IOError): |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 95 | pass |
| 96 | else: |
Ronald Oussoren | 9015b93 | 2006-06-13 11:19:56 +0000 | [diff] [blame] | 97 | if data is None: |
| 98 | # No luck, the PEP302 loader cannot find the source |
| 99 | # for this module. |
| 100 | return [] |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 101 | cache[filename] = ( |
Tim Peters | 413c922 | 2006-04-11 01:44:26 +0000 | [diff] [blame] | 102 | len(data), None, |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 103 | [line+'\n' for line in data.splitlines()], fullname |
| 104 | ) |
| 105 | return cache[filename][2] |
| 106 | |
| 107 | # Try looking through the module search path. |
| 108 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 109 | for dirname in sys.path: |
Tim Peters | 12f21ae | 2001-05-29 04:27:01 +0000 | [diff] [blame] | 110 | # When using imputil, sys.path may contain things other than |
| 111 | # strings; ignore them when it happens. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 112 | try: |
Tim Peters | 12f21ae | 2001-05-29 04:27:01 +0000 | [diff] [blame] | 113 | fullname = os.path.join(dirname, basename) |
| 114 | except (TypeError, AttributeError): |
| 115 | # Not sufficiently string-like to do anything useful with. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 116 | pass |
Tim Peters | 12f21ae | 2001-05-29 04:27:01 +0000 | [diff] [blame] | 117 | else: |
| 118 | try: |
| 119 | stat = os.stat(fullname) |
| 120 | break |
| 121 | except os.error: |
| 122 | pass |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 123 | else: |
| 124 | # No luck |
| 125 | ## print '*** Cannot stat', filename, ':', msg |
| 126 | return [] |
| 127 | try: |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 128 | fp = open(fullname, 'rU') |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 129 | lines = fp.readlines() |
| 130 | fp.close() |
| 131 | except IOError, msg: |
| 132 | ## print '*** Cannot open', fullname, ':', msg |
| 133 | return [] |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 134 | size, mtime = stat.st_size, stat.st_mtime |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 135 | cache[filename] = size, mtime, lines, fullname |
| 136 | return lines |