Fork me on GitHub
  ▖     
▃▀▄▄▖▄▄▖
 ▀▖     
        


#!/usr/bin/env python3
#
# usage: plash nodepath CONTAINER [--allow-root-container]
# Prints the path to a given container. The --allow-root-container option
# allows the root container ("0") to be specified as container.
#
# Parameters may be interpreted as build instruction.
#
# Examples:
# $ plash nodepath 19
# /home/ihucos/.plashdata/layer/0/2/19
# $ plash nodepath 19 | xargs tree

import os
import sys

from plash.utils import (catch_and_die, die, die_with_usage, get_plash_data,
                         handle_build_args, handle_help_flag, nodepath_or_die)

handle_help_flag()
handle_build_args()

try:
    container = sys.argv[1]
except IndexError:
    die_with_usage()

if not container.isdigit():
    die("container id must be integer, got {}".format(repr(container)))

if container == '0' and not sys.argv[2:3] == ['--allow-root-container']:
    die("container must not be the special root container ('0')")

try:
    with catch_and_die([OSError], ignore=FileNotFoundError, debug='readlink'):
        nodepath = os.readlink(
            os.path.join(get_plash_data(), 'index', container))
    with catch_and_die([OSError], ignore=FileNotFoundError, debug='stat'):
        os.stat(nodepath)
except FileNotFoundError:
    die('no container {}'.format(repr(container)), exit=3)

print(nodepath)