Use a semi-persistent script path

This commit is contained in:
Reid 'arrdem' McKenzie 2022-11-28 19:37:57 -07:00
parent 73c9e68423
commit f7bfe0eac3
3 changed files with 40 additions and 32 deletions

View file

@ -3,8 +3,6 @@
import logging
import os
import pickle
import platform
import re
from collections import defaultdict
from pathlib import Path
from pprint import pformat
@ -18,6 +16,7 @@ from vfs import Vfs
from . import __author__, __copyright__, __license__, __version__
from .v0 import PackageV0, ProfileV0
from .v1 import LightPackageV1, PackageV1, ProfileV1
from .common import expandvars
log = logging.getLogger(__name__)
@ -60,29 +59,6 @@ def upsearch(name: Union[str, Path], limit=10) -> Optional[Path]:
limit -= 1
def expandvars(s: str) -> str:
def _repl(m: re.Match) -> str:
var = m.group(0).lower()
match var:
case "${hostname}":
return os.uname()[1].split(".")[0]
case "${fqdn}":
return os.uname()[1]
case "${home}":
return os.path.expanduser("~")
case "${uname}" | "${sysname}":
return platform.system().lower()
case "${arch}":
return platform.machine()
case "${release}":
return platform.release()
case _:
return os.path.expandvars(m.group(0))
s = re.sub(r"\${?([^\s}]+)}?", "${\\1}", s)
return re.sub(r"\$\{.*?\}", _repl, s)
def handle_vars(_ctx, _param, s):
return expandvars(s)
@ -278,6 +254,8 @@ def configure(ctx, param, filename: Optional[Path]):
log.debug(f"Loading config from {filename}")
ctx.obj = cfg = toml.load(filename)
assert cfg["cram"]["version"] >= 1
cfg["cram"]["config"] = str(filename.absolute())
cfg["cram"]["root"] = str(filename.absolute().parent)
task_cfg = cfg.get("cram", {}).get("task", {})
defaults = task_cfg.get("default", {})

View file

@ -5,6 +5,8 @@ import sys
from pathlib import Path
from shlex import quote as sh_quote
from typing import List, Optional
import re
import platform
from vfs import Vfs
@ -86,3 +88,26 @@ class Package(object):
def post_install(self, fs: Vfs, dest: Path):
pass
def expandvars(s: str) -> str:
def _repl(m: re.Match) -> str:
var = m.group(0).lower()
match var:
case "${hostname}":
return os.uname()[1].split(".")[0]
case "${fqdn}":
return os.uname()[1]
case "${home}":
return os.path.expanduser("~")
case "${uname}" | "${sysname}":
return platform.system().lower()
case "${arch}":
return platform.machine()
case "${release}":
return platform.release()
case _:
return os.path.expandvars(m.group(0))
s = re.sub(r"\${?([^\s}]+)}?", "${\\1}", s)
return re.sub(r"\$\{.*?\}", _repl, s)

View file

@ -12,13 +12,17 @@ from typing import List, Optional, Union
import toml
from vfs import Vfs
from .common import Package, sh, stow
from .common import Package, sh, stow, expandvars
def tempf(name):
root = Path("/tmp/stow")
root.mkdir(exist_ok=True, parents=True)
return root / name
def tempf(global_config, name):
root = Path(global_config["cram"]["root"]) / ".cache" # Config root dir.
assert root.exists() and root.is_dir()
cache_dir = global_config.get("cram", {}).get("task", {}).get("default", {}).get("cache_dir")
cache_root = cache_dir and Path(expandvars(cache_dir)) or root
return cache_root / name[0:2] / name
class PackageV1(Package):
@ -76,10 +80,11 @@ class PackageV1(Package):
self.do_sh_or_script(fp.read(), fs, dest)
elif content:
f = tempf(f"{sum}.sh")
f = tempf(self.global_config, f"{sum}.sh")
f.parent.mkdir(exist_ok=True, parents=True)
with open(f, "w") as fp:
fp.write(content)
fs.exec(cwd, sh(self.global_config, [f]))
fs.exec(cwd, sh(self.global_config, [str(f)]))
def do_build(self, fs: Vfs, dest: Path):
self.do_sh_or_script(self.config().get("package", {}).get("build"), fs, dest)