Tamarin/lib/tamarin.py

55 lines
2.5 KiB
Python

import os, glob, subprocess
import web, system
def get_workspace_dir():
"""Return the absolute path to the tamarin workspace ($HOME/.tamarin)"""
home = os.environ["HOME"]
return os.path.join(os.sep, home, '.tamarin')
def get_workspace_subdir(subdir):
"""Return the absolute path to a subdirectory in tamarin workspace"""
dir_path = os.path.join(os.sep, get_workspace_dir(), subdir)
os.makedirs(dir_path, exist_ok=True)
return dir_path
def get_acbuild_achive_dest_dir():
"""Return the first path matching the acbuild archive extraction destination in tamarin workspace"""
workspace_tmp = get_workspace_subdir('tmp')
return glob.glob(os.path.join(os.sep, workspace_tmp, 'acbuild-v*'))[0]
def get_rkt_achive_dest_dir():
"""Return the first path matching the rkt archive extraction destination in tamarin workspace"""
workspace_tmp = get_workspace_subdir('tmp')
return glob.glob(os.path.join(os.sep, workspace_tmp, 'rkt-v*'))[0]
def get_acbuild_workspace_dir():
"""Return the current path (linked to the process pid) to the acbuild workspace"""
return get_workspace_subdir("tmp/build_{:d}".format(os.getpid()))
def download_rkt():
"""Download a local copy of rkt in the tamarin workspace and return the absolute path to the archive"""
url = "https://github.com/coreos/rkt/releases/download/v1.22.0/rkt-v1.22.0.tar.gz"
file_path=os.path.join(os.sep, get_workspace_subdir('tmp'), "rkt.tar.gz")
web.download_file(file_url=url, dest_path=file_path)
return file_path
def download_acbuild():
"""Download a local copy of acbuild in the tamarin workspace and return the absolute path to the archive"""
url = "https://github.com/containers/build/releases/download/v0.4.0/acbuild-v0.4.0.tar.gz"
file_path=os.path.join(os.sep, get_workspace_subdir('tmp'), "acbuild.tar.gz")
web.download_file(file_url=url, dest_path=file_path)
return file_path
def run_rkt(args):
"""Run rkt with the specified args (use the local copy if rkt is not found in the $PATH)"""
rkt_bin = system.which('rkt', get_workspace_subdir('rkt'))
cmd = ( ["sudo", "-E", rkt_bin] if os.geteuid() != 0 else [rkt_bin] ) + args
print(" ".join(cmd))
return subprocess.call(cmd, stdin=subprocess.PIPE)
def run_acbuild(args):
"""Run acbuild with the specified args (use the local copy if acbuild is not found in the $PATH)"""
acbuild_bin = system.which('acbuild', get_workspace_subdir('acbuild'))
print(" ".join([acbuild_bin] + args))
return subprocess.call([acbuild_bin] + args, stdin=subprocess.PIPE)