-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathresources.py
More file actions
43 lines (38 loc) · 1.8 KB
/
resources.py
File metadata and controls
43 lines (38 loc) · 1.8 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
from pathlib import Path
import zipfile
import importlib.resources
import shutil
from .. import CORE_COMMIT
def path(*args: str) -> Path:
# Checks that the local static path ~/.ptx/ contains the static files needed for core, and installs them if they are missing (or if the version is different from the installed version of pretext). Then returns the absolute path to the static files (appending arguments)
local_base_path = Path.home() / ".ptx"
local_commit_file = Path(local_base_path) / ".commit"
if not Path.is_file(local_commit_file):
print("Static pretext files do not appear to be installed. Installing now.")
install(local_base_path)
# check that the static core_commit matches current core_commit
with open(local_commit_file, "r") as f:
static_commit = f.readline().strip()
if static_commit != CORE_COMMIT:
print("Static pretext files are out of date. Installing them now.")
install(local_base_path)
return local_base_path.joinpath(*args)
def install(local_base_path: Path) -> None:
backup_dir = local_base_path.with_name(local_base_path.name + ".bak")
if Path.is_dir(backup_dir):
# remove old backup:
shutil.rmtree(backup_dir)
if Path.is_dir(local_base_path):
print(f"Backing up old static files to {backup_dir}")
Path.rename(local_base_path, backup_dir)
Path.mkdir(local_base_path)
with importlib.resources.path("pretext.core", "resources.zip") as static_zip:
with zipfile.ZipFile(static_zip, "r") as zip:
zip.extractall(local_base_path)
# Write the current commit to local file
with open(local_base_path / ".commit", "w") as f:
f.write(CORE_COMMIT)
print(
f"Static files required for pretext have now been installed to {local_base_path}"
)
return