1 | n/a | """Cache lines from Python source files. |
---|
2 | n/a | |
---|
3 | n/a | This is intended to read lines from modules imported -- hence if a filename |
---|
4 | n/a | is not found, it will look down the module search path for a file by |
---|
5 | n/a | that name. |
---|
6 | n/a | """ |
---|
7 | n/a | |
---|
8 | n/a | import functools |
---|
9 | n/a | import sys |
---|
10 | n/a | import os |
---|
11 | n/a | import tokenize |
---|
12 | n/a | |
---|
13 | n/a | __all__ = ["getline", "clearcache", "checkcache"] |
---|
14 | n/a | |
---|
15 | n/a | def getline(filename, lineno, module_globals=None): |
---|
16 | n/a | lines = getlines(filename, module_globals) |
---|
17 | n/a | if 1 <= lineno <= len(lines): |
---|
18 | n/a | return lines[lineno-1] |
---|
19 | n/a | else: |
---|
20 | n/a | return '' |
---|
21 | n/a | |
---|
22 | n/a | |
---|
23 | n/a | # The cache |
---|
24 | n/a | |
---|
25 | n/a | # The cache. Maps filenames to either a thunk which will provide source code, |
---|
26 | n/a | # or a tuple (size, mtime, lines, fullname) once loaded. |
---|
27 | n/a | cache = {} |
---|
28 | n/a | |
---|
29 | n/a | |
---|
30 | n/a | def clearcache(): |
---|
31 | n/a | """Clear the cache entirely.""" |
---|
32 | n/a | |
---|
33 | n/a | global cache |
---|
34 | n/a | cache = {} |
---|
35 | n/a | |
---|
36 | n/a | |
---|
37 | n/a | def getlines(filename, module_globals=None): |
---|
38 | n/a | """Get the lines for a Python source file from the cache. |
---|
39 | n/a | Update the cache if it doesn't contain an entry for this file already.""" |
---|
40 | n/a | |
---|
41 | n/a | if filename in cache: |
---|
42 | n/a | entry = cache[filename] |
---|
43 | n/a | if len(entry) != 1: |
---|
44 | n/a | return cache[filename][2] |
---|
45 | n/a | |
---|
46 | n/a | try: |
---|
47 | n/a | return updatecache(filename, module_globals) |
---|
48 | n/a | except MemoryError: |
---|
49 | n/a | clearcache() |
---|
50 | n/a | return [] |
---|
51 | n/a | |
---|
52 | n/a | |
---|
53 | n/a | def checkcache(filename=None): |
---|
54 | n/a | """Discard cache entries that are out of date. |
---|
55 | n/a | (This is not checked upon each call!)""" |
---|
56 | n/a | |
---|
57 | n/a | if filename is None: |
---|
58 | n/a | filenames = list(cache.keys()) |
---|
59 | n/a | else: |
---|
60 | n/a | if filename in cache: |
---|
61 | n/a | filenames = [filename] |
---|
62 | n/a | else: |
---|
63 | n/a | return |
---|
64 | n/a | |
---|
65 | n/a | for filename in filenames: |
---|
66 | n/a | entry = cache[filename] |
---|
67 | n/a | if len(entry) == 1: |
---|
68 | n/a | # lazy cache entry, leave it lazy. |
---|
69 | n/a | continue |
---|
70 | n/a | size, mtime, lines, fullname = entry |
---|
71 | n/a | if mtime is None: |
---|
72 | n/a | continue # no-op for files loaded via a __loader__ |
---|
73 | n/a | try: |
---|
74 | n/a | stat = os.stat(fullname) |
---|
75 | n/a | except OSError: |
---|
76 | n/a | del cache[filename] |
---|
77 | n/a | continue |
---|
78 | n/a | if size != stat.st_size or mtime != stat.st_mtime: |
---|
79 | n/a | del cache[filename] |
---|
80 | n/a | |
---|
81 | n/a | |
---|
82 | n/a | def updatecache(filename, module_globals=None): |
---|
83 | n/a | """Update a cache entry and return its list of lines. |
---|
84 | n/a | If something's wrong, print a message, discard the cache entry, |
---|
85 | n/a | and return an empty list.""" |
---|
86 | n/a | |
---|
87 | n/a | if filename in cache: |
---|
88 | n/a | if len(cache[filename]) != 1: |
---|
89 | n/a | del cache[filename] |
---|
90 | n/a | if not filename or (filename.startswith('<') and filename.endswith('>')): |
---|
91 | n/a | return [] |
---|
92 | n/a | |
---|
93 | n/a | fullname = filename |
---|
94 | n/a | try: |
---|
95 | n/a | stat = os.stat(fullname) |
---|
96 | n/a | except OSError: |
---|
97 | n/a | basename = filename |
---|
98 | n/a | |
---|
99 | n/a | # Realise a lazy loader based lookup if there is one |
---|
100 | n/a | # otherwise try to lookup right now. |
---|
101 | n/a | if lazycache(filename, module_globals): |
---|
102 | n/a | try: |
---|
103 | n/a | data = cache[filename][0]() |
---|
104 | n/a | except (ImportError, OSError): |
---|
105 | n/a | pass |
---|
106 | n/a | else: |
---|
107 | n/a | if data is None: |
---|
108 | n/a | # No luck, the PEP302 loader cannot find the source |
---|
109 | n/a | # for this module. |
---|
110 | n/a | return [] |
---|
111 | n/a | cache[filename] = ( |
---|
112 | n/a | len(data), None, |
---|
113 | n/a | [line+'\n' for line in data.splitlines()], fullname |
---|
114 | n/a | ) |
---|
115 | n/a | return cache[filename][2] |
---|
116 | n/a | |
---|
117 | n/a | # Try looking through the module search path, which is only useful |
---|
118 | n/a | # when handling a relative filename. |
---|
119 | n/a | if os.path.isabs(filename): |
---|
120 | n/a | return [] |
---|
121 | n/a | |
---|
122 | n/a | for dirname in sys.path: |
---|
123 | n/a | try: |
---|
124 | n/a | fullname = os.path.join(dirname, basename) |
---|
125 | n/a | except (TypeError, AttributeError): |
---|
126 | n/a | # Not sufficiently string-like to do anything useful with. |
---|
127 | n/a | continue |
---|
128 | n/a | try: |
---|
129 | n/a | stat = os.stat(fullname) |
---|
130 | n/a | break |
---|
131 | n/a | except OSError: |
---|
132 | n/a | pass |
---|
133 | n/a | else: |
---|
134 | n/a | return [] |
---|
135 | n/a | try: |
---|
136 | n/a | with tokenize.open(fullname) as fp: |
---|
137 | n/a | lines = fp.readlines() |
---|
138 | n/a | except OSError: |
---|
139 | n/a | return [] |
---|
140 | n/a | if lines and not lines[-1].endswith('\n'): |
---|
141 | n/a | lines[-1] += '\n' |
---|
142 | n/a | size, mtime = stat.st_size, stat.st_mtime |
---|
143 | n/a | cache[filename] = size, mtime, lines, fullname |
---|
144 | n/a | return lines |
---|
145 | n/a | |
---|
146 | n/a | |
---|
147 | n/a | def lazycache(filename, module_globals): |
---|
148 | n/a | """Seed the cache for filename with module_globals. |
---|
149 | n/a | |
---|
150 | n/a | The module loader will be asked for the source only when getlines is |
---|
151 | n/a | called, not immediately. |
---|
152 | n/a | |
---|
153 | n/a | If there is an entry in the cache already, it is not altered. |
---|
154 | n/a | |
---|
155 | n/a | :return: True if a lazy load is registered in the cache, |
---|
156 | n/a | otherwise False. To register such a load a module loader with a |
---|
157 | n/a | get_source method must be found, the filename must be a cachable |
---|
158 | n/a | filename, and the filename must not be already cached. |
---|
159 | n/a | """ |
---|
160 | n/a | if filename in cache: |
---|
161 | n/a | if len(cache[filename]) == 1: |
---|
162 | n/a | return True |
---|
163 | n/a | else: |
---|
164 | n/a | return False |
---|
165 | n/a | if not filename or (filename.startswith('<') and filename.endswith('>')): |
---|
166 | n/a | return False |
---|
167 | n/a | # Try for a __loader__, if available |
---|
168 | n/a | if module_globals and '__loader__' in module_globals: |
---|
169 | n/a | name = module_globals.get('__name__') |
---|
170 | n/a | loader = module_globals['__loader__'] |
---|
171 | n/a | get_source = getattr(loader, 'get_source', None) |
---|
172 | n/a | |
---|
173 | n/a | if name and get_source: |
---|
174 | n/a | get_lines = functools.partial(get_source, name) |
---|
175 | n/a | cache[filename] = (get_lines,) |
---|
176 | n/a | return True |
---|
177 | n/a | return False |
---|