Repository navigation
Expand file tree
/
Copy pathderived.py
More file actions
121 lines (100 loc) · 4.05 KB
/
Copy pathderived.py
File metadata and controls
121 lines (100 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
A cache of what is slow to work out from a stored job: the parsed pages, and the names of the PDFs made from them.
Reading the job pickles is quick (0.1 s for a thousand); parsing them and drawing the PDFs is what makes a start
slow, so the next start can take those from here.
The cache is only ever an optimisation. The job pickles stay the record, and anything wrong with an entry (a
different job file, newer parser code, a missing PDF) means that job is worked out again. One file, written
atomically; one entry per job file name, so it follows the history: a job that leaves the loaded history is
dropped from the file the next time it is written.
An entry is only trusted if
* the job file is the one it was made from (size and modification time),
* it was parsed by the same parser (`parse_stamp`: docparser.py and textutil.py),
and the PDFs it names are still on disk. How the PDFs were drawn (`render_stamp`: stationery.py, the [Stationery]
company details and the logos) is recorded with it, but a change does not redraw them by itself: the Stationery
settings page counts the jobs drawn under older stationery and has the button that redraws them.
"""
import hashlib
import os
import pickle
import time
import traceback
from settings import BASE_DIR, HOME, config
PATH = os.path.join(HOME, 'derived.pickle')
# bumped if the layout of an entry changes
FORMAT = 1
# the file is rewritten at most this often (seconds) while something is changing
FLUSH_EVERY = 180
def _digest(parts):
h = hashlib.sha256()
for p in parts:
h.update(p if isinstance(p, bytes) else repr(p).encode('utf-8'))
h.update(b'\0')
return h.hexdigest()[:16]
def _source(name):
with open(os.path.join(BASE_DIR, name), 'rb') as f:
return f.read()
def parse_stamp():
return _digest([FORMAT, _source('docparser.py'), _source('textutil.py')])
def render_stamp():
parts = [FORMAT, _source('stationery.py'), sorted(config.items('Stationery'))]
for key in ('logo', 'logo2'):
path = os.path.expanduser(config.get('Stationery', key, fallback=''))
try:
st = os.stat(path) if path else None
except OSError:
st = None
parts.append((path, st and (st.st_size, st.st_mtime_ns)))
return _digest(parts)
class DerivedCache(object):
def __init__(self, path=PATH):
self.path = path
self.entries = {}
self.dirty = False
self.last_flush = 0
self.parse_stamp = parse_stamp()
self.render_stamp = render_stamp()
try:
with open(path, 'rb') as f:
data = pickle.load(f)
if data.get('format') == FORMAT:
self.entries = data['entries']
except FileNotFoundError:
pass
except Exception:
print('[derived] ignoring an unreadable cache %s' % path)
traceback.print_exc()
def lookup(self, name, stat):
"""The entry for this job file if it is still good, else None."""
e = self.entries.get(name)
if e and e['stat'] == stat and e['parse_stamp'] == self.parse_stamp:
return e
return None
def snapshot(self, jobs):
"""The entries for the jobs as they are now: what a flush writes."""
out = {}
for j in jobs:
if j.get('stat') and 'groupfiles' in j:
out[j['name']] = dict(stat=j['stat'], parse_stamp=self.parse_stamp, render_stamp=j.get('render_stamp'),
parsed=j['parsed'], groupkey=j['groupkey'], groupfiles=j['groupfiles'])
return out
def flush(self, jobs, force=False):
"""Write the cache if it changed, and not more than every FLUSH_EVERY seconds unless forced. Never raises: a
full disk mustn't stop the server."""
if not self.dirty or (not force and time.time() - self.last_flush < FLUSH_EVERY):
return False
try:
entries = self.snapshot(jobs)
tmp = self.path + '.tmp'
with open(tmp, 'wb') as f:
pickle.dump(dict(format=FORMAT, entries=entries), f, protocol=pickle.HIGHEST_PROTOCOL)
os.replace(tmp, self.path)
self.entries = entries
self.dirty = False
self.last_flush = time.time()
return True
except Exception:
traceback.print_exc()
return False
def stale(self, jobs):
"""The jobs whose PDFs were drawn under different stationery from today's."""
return [j for j in jobs if j.get('render_stamp') != self.render_stamp]