comparison tests/tmpfs.py @ 411:e7b865f8f335

bake: Enable multiprocess baking. Baking is now done by running a worker per CPU, and sending jobs to them. This changes several things across the codebase: * Ability to not cache things related to pages other than the 'main' page (i.e. the page at the bottom of the execution stack). * Decouple the baking process from the bake records, so only the main process keeps track (and modifies) the bake record. * Remove the need for 'batch page getters' and loading a page directly from the page factories. There are various smaller changes too included here, including support for scope performance timers that are saved with the bake record and can be printed out to the console. Yes I got carried away. For testing, the in-memory 'mock' file-system doesn't work anymore, since we're spawning processes, so this is replaced by a 'tmpfs' file-system which is saved in temporary files on disk and deleted after tests have run.
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 12 Jun 2015 17:09:19 -0700
parents
children bab91fcef741
comparison
equal deleted inserted replaced
410:d1a472464e57 411:e7b865f8f335
1 import os
2 import os.path
3 import shutil
4 import random
5 from .basefs import TestFileSystemBase
6
7
8 class TempDirFileSystem(TestFileSystemBase):
9 def __init__(self, default_spec=True):
10 self._root = os.path.join(
11 os.path.dirname(__file__),
12 '__tmpfs__',
13 '%d' % random.randrange(1000))
14 self._done = False
15 if default_spec:
16 self._initDefaultSpec()
17
18 def path(self, p):
19 p = p.lstrip('/\\')
20 return os.path.join(self._root, p)
21
22 def getStructure(self, path=None):
23 path = self.path(path)
24 if not os.path.exists(path):
25 raise Exception("No such path: %s" % path)
26 if not os.path.isdir(path):
27 raise Exception("Path is not a directory: %s" % path)
28
29 res = {}
30 for item in os.listdir(path):
31 self._getStructureRecursive(res, path, item)
32 return res
33
34 def getFileEntry(self, path):
35 path = self.path(path)
36 with open(path, 'r', encoding='utf8') as fp:
37 return fp.read()
38
39 def _getStructureRecursive(self, target, parent, cur):
40 full_cur = os.path.join(parent, cur)
41 if os.path.isdir(full_cur):
42 e = {}
43 for item in os.listdir(full_cur):
44 self._getStructureRecursive(e, full_cur, item)
45 target[cur] = e
46 else:
47 with open(full_cur, 'r', encoding='utf8') as fp:
48 target[cur] = fp.read()
49
50 def _createDir(self, path):
51 if not os.path.exists(path):
52 os.makedirs(path)
53
54 def _createFile(self, path, contents):
55 dirpath = os.path.dirname(path)
56 if not os.path.exists(dirpath):
57 os.makedirs(dirpath)
58 with open(path, 'w', encoding='utf8') as fp:
59 fp.write(contents)
60
61 if not self._done:
62 import traceback
63 with open(os.path.join(self._root, 'where.txt'), 'w') as fp:
64 fp.write('\n'.join(traceback.format_stack(limit=10)))
65 self._done = True
66
67
68 class TempDirScope(object):
69 def __init__(self, fs, open_patches=None):
70 self._fs = fs
71 self._open = open
72
73 @property
74 def root(self):
75 return self._fs._root
76
77 def __enter__(self):
78 return self
79
80 def __exit__(self, type, value, traceback):
81 shutil.rmtree(self.root)
82