-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdummy_game_instance.py
78 lines (72 loc) · 3.69 KB
/
dummy_game_instance.py
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
import logging
from pathlib import Path
from shutil import rmtree, copy, disk_usage
from subprocess import run
from types import TracebackType
from typing import Type, List
from .game import Game
from .game_version import GameVersion
class DummyGameInstance:
SAVED_REGISTRY=Path('/tmp/registry.json')
def __init__(self, where: Path, ckan_exe: Path, addl_repo: Path, main_ver: GameVersion, other_versions: List[GameVersion], cache_path: Path, game: Game) -> None:
self.where = where
self.registry_path = self.where.joinpath('CKAN').joinpath('registry.json')
self.ckan_exe = ckan_exe
self.addl_repo = addl_repo
self.main_ver = main_ver
self.other_versions = other_versions
self.cache_path = cache_path
self.game = game
# Hide ckan.exe output unless debugging is enabled
self.capture = not logging.getLogger().isEnabledFor(logging.DEBUG)
def __enter__(self) -> 'DummyGameInstance':
logging.info('Creating dummy game instance at %s', self.where)
self.where.mkdir()
logging.debug('Populating fake instance contents')
run(['mono', self.ckan_exe,
'instance', 'fake',
'--game', self.game.short_name,
'--set-default', '--headless',
'dummy', self.where, str(self.main_ver),
*self.game.dlc_cmdline_flags(self.main_ver)],
capture_output=self.capture)
for ver in self.other_versions:
logging.debug('Setting version %s compatible', ver)
run(['mono', self.ckan_exe, 'compat', 'add', str(ver)],
capture_output=self.capture)
self.where.joinpath('CKAN').joinpath('downloads').symlink_to(self.cache_path.absolute())
logging.debug('Setting cache location to %s', self.cache_path.absolute())
run(['mono', self.ckan_exe, 'cache', 'set', self.cache_path.absolute(), '--headless'],
capture_output=self.capture)
# Free space plus existing cache minus 1 GB padding
cache_mbytes = max(5000,
(((disk_usage(self.cache_path)[2] if self.cache_path.is_dir() else 0)
+ sum(f.stat().st_size for f in self.cache_path.rglob('*'))
) // 1024 // 1024 - 1024))
logging.debug('Setting cache limit to %s', cache_mbytes)
run(['mono', self.ckan_exe, 'cache', 'setlimit', str(cache_mbytes)],
capture_output=self.capture)
logging.debug('Adding repo %s', self.addl_repo.as_uri())
run(['mono', self.ckan_exe, 'repo', 'add', 'local', self.addl_repo.as_uri()],
capture_output=self.capture)
run(['mono', self.ckan_exe, 'repo', 'priority', 'local', '0'],
capture_output=self.capture)
if self.SAVED_REGISTRY.exists():
logging.debug('Restoring saved registry from %s', self.SAVED_REGISTRY)
copy(self.SAVED_REGISTRY, self.registry_path)
else:
logging.debug('Updating registry')
run(['mono', self.ckan_exe, 'update'],
capture_output=self.capture)
copy(self.registry_path, self.SAVED_REGISTRY)
logging.debug('Saving registry to %s', self.SAVED_REGISTRY)
logging.debug('Dummy instance is ready')
return self
def __exit__(self, exc_type: Type[BaseException],
exc_value: BaseException, traceback: TracebackType) -> None:
logging.debug('Removing instance from CKAN instance list')
run(['mono', self.ckan_exe, 'instance', 'forget', 'dummy'],
capture_output=self.capture)
logging.debug('Deleting instance contents')
rmtree(self.where)
logging.info('Dummy game instance deleted')