blob: 4838625f08d54011a89ecbd6ef98da93c40e88a5 [file] [log] [blame]
Guido van Rossum54f22ed2000-02-04 15:10:34 +00001"""Cache lines from files.
2
3This is intended to read lines from modules imported -- hence if a filename
4is not found, it will look down the module search path for a file by
5that name.
6"""
Guido van Rossum921c8241992-01-10 14:54:42 +00007
Guido van Rossumc341c621992-03-27 15:12:43 +00008import sys
Guido van Rossum921c8241992-01-10 14:54:42 +00009import os
Guido van Rossum921c8241992-01-10 14:54:42 +000010
Jeremy Hylton97b2e842003-06-29 16:59:43 +000011__all__ = ["getline", "clearcache", "checkcache"]
Skip Montanaro17ab1232001-01-24 06:27:27 +000012
Phillip J. Eby47032112006-04-11 01:07:43 +000013def getline(filename, lineno, module_globals=None):
14 lines = getlines(filename, module_globals)
Guido van Rossum54f22ed2000-02-04 15:10:34 +000015 if 1 <= lineno <= len(lines):
16 return lines[lineno-1]
17 else:
18 return ''
Guido van Rossum921c8241992-01-10 14:54:42 +000019
20
21# The cache
22
23cache = {} # The cache
24
25
Guido van Rossum921c8241992-01-10 14:54:42 +000026def clearcache():
Guido van Rossum54f22ed2000-02-04 15:10:34 +000027 """Clear the cache entirely."""
Guido van Rossum921c8241992-01-10 14:54:42 +000028
Guido van Rossum54f22ed2000-02-04 15:10:34 +000029 global cache
30 cache = {}
Guido van Rossum921c8241992-01-10 14:54:42 +000031
Guido van Rossum921c8241992-01-10 14:54:42 +000032
Phillip J. Eby47032112006-04-11 01:07:43 +000033def getlines(filename, module_globals=None):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000034 """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 Rossum921c8241992-01-10 14:54:42 +000036
Raymond Hettinger54f02222002-06-01 14:18:47 +000037 if filename in cache:
Guido van Rossum54f22ed2000-02-04 15:10:34 +000038 return cache[filename][2]
39 else:
Neal Norwitz0c469852006-04-11 07:21:20 +000040 return updatecache(filename, module_globals)
Guido van Rossum921c8241992-01-10 14:54:42 +000041
Guido van Rossum921c8241992-01-10 14:54:42 +000042
Hye-Shik Chang182ac852004-10-26 09:16:42 +000043def checkcache(filename=None):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000044 """Discard cache entries that are out of date.
45 (This is not checked upon each call!)"""
Guido van Rossum921c8241992-01-10 14:54:42 +000046
Hye-Shik Chang182ac852004-10-26 09:16:42 +000047 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 Rossum54f22ed2000-02-04 15:10:34 +000056 size, mtime, lines, fullname = cache[filename]
Phillip J. Eby47032112006-04-11 01:07:43 +000057 if mtime is None:
58 continue # no-op for files loaded via a __loader__
Guido van Rossum54f22ed2000-02-04 15:10:34 +000059 try:
60 stat = os.stat(fullname)
61 except os.error:
62 del cache[filename]
63 continue
Raymond Hettinger32200ae2002-06-01 19:51:15 +000064 if size != stat.st_size or mtime != stat.st_mtime:
Guido van Rossum54f22ed2000-02-04 15:10:34 +000065 del cache[filename]
Guido van Rossum921c8241992-01-10 14:54:42 +000066
Guido van Rossum921c8241992-01-10 14:54:42 +000067
Phillip J. Eby47032112006-04-11 01:07:43 +000068def updatecache(filename, module_globals=None):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000069 """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 Hettinger54f02222002-06-01 14:18:47 +000073 if filename in cache:
Guido van Rossum54f22ed2000-02-04 15:10:34 +000074 del cache[filename]
75 if not filename or filename[0] + filename[-1] == '<>':
76 return []
Phillip J. Eby47032112006-04-11 01:07:43 +000077
Guido van Rossum54f22ed2000-02-04 15:10:34 +000078 fullname = filename
79 try:
80 stat = os.stat(fullname)
81 except os.error, msg:
Guido van Rossum54f22ed2000-02-04 15:10:34 +000082 basename = os.path.split(filename)[1]
Phillip J. Eby47032112006-04-11 01:07:43 +000083
Tim Peters413c9222006-04-11 01:44:26 +000084 # Try for a __loader__, if available
Phillip J. Eby47032112006-04-11 01:07:43 +000085 if module_globals and '__loader__' in module_globals:
86 name = module_globals.get('__name__')
87 loader = module_globals['__loader__']
Neal Norwitz0c469852006-04-11 07:21:20 +000088 get_source = getattr(loader, 'get_source', None)
Phillip J. Eby47032112006-04-11 01:07:43 +000089
90 if name and get_source:
91 if basename.startswith(name.split('.')[-1]+'.'):
92 try:
93 data = get_source(name)
Neal Norwitz0c469852006-04-11 07:21:20 +000094 except (ImportError, IOError):
Phillip J. Eby47032112006-04-11 01:07:43 +000095 pass
96 else:
Ronald Oussoren9015b932006-06-13 11:19:56 +000097 if data is None:
98 # No luck, the PEP302 loader cannot find the source
99 # for this module.
100 return []
Phillip J. Eby47032112006-04-11 01:07:43 +0000101 cache[filename] = (
Tim Peters413c9222006-04-11 01:44:26 +0000102 len(data), None,
Phillip J. Eby47032112006-04-11 01:07:43 +0000103 [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 Rossum54f22ed2000-02-04 15:10:34 +0000109 for dirname in sys.path:
Tim Peters12f21ae2001-05-29 04:27:01 +0000110 # When using imputil, sys.path may contain things other than
111 # strings; ignore them when it happens.
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000112 try:
Tim Peters12f21ae2001-05-29 04:27:01 +0000113 fullname = os.path.join(dirname, basename)
114 except (TypeError, AttributeError):
115 # Not sufficiently string-like to do anything useful with.
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000116 pass
Tim Peters12f21ae2001-05-29 04:27:01 +0000117 else:
118 try:
119 stat = os.stat(fullname)
120 break
121 except os.error:
122 pass
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000123 else:
124 # No luck
125## print '*** Cannot stat', filename, ':', msg
126 return []
127 try:
Jack Jansen7b8c7542002-04-14 20:12:41 +0000128 fp = open(fullname, 'rU')
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000129 lines = fp.readlines()
130 fp.close()
131 except IOError, msg:
132## print '*** Cannot open', fullname, ':', msg
133 return []
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000134 size, mtime = stat.st_size, stat.st_mtime
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000135 cache[filename] = size, mtime, lines, fullname
136 return lines