# Path: elbepack/filesystem.py
# def size_to_int(size):
#     def __init__(self, path, clean=False):
#     def fname(self, path):
#     def open(self, path, mode="r"):
#     def open_gz(self, path, mode="r"):
#     def isdir(self, path):
#     def islink(self, path):
#     def isfile(self, path):
#     def exists(self, path):
#     def lexists(self, path):
#     def mkdir(self, path):
#     def readlink(self, path):
#     def realpath(self, path):
#     def symlink(self, src, path, allow_exists=False):
#     def stat(self, path):
#     def chown(self, path, uid, gid):
#     def chmod(self, path, mode):
#     def utime(self, path, times=None):
#     def cat_file(self, inf):
#     def remove(self, path, noerr=False):
#     def rmtree(self, path):
#     def listdir(self, path='', ignore=None, skiplinks=False):
#     def glob(self, path):
#     def _write_file(path, f, cont, mode):
#     def write_file(self, path, mode, cont):
#     def append_file(self, path, cont, mode=None):
#     def read_file(self, path, gz=False):
#     def mkdir_p(self, newdir, mode=0o755):
#     def touch_file(self, fname):
#     def walk_files(self, directory='', exclude_dirs=None):
#     def mtime_snap(self, dirname='', exclude_dirs=None):
#     def __disk_usage(self, directory):
#     def disk_usage(self, dirname=''):
#     def __init__(self, debug=False):
#     def __del__(self):
#     def delete(self):
#     def __enter__(self):
#     def __exit__(self, exec_type, exec_value, tb):
#     def __init__(self, mntpoint, dev):
#     def __enter__(self):
#     def __exit__(self, typ, value, traceback):
# class Filesystem:
# class TmpdirFilesystem (Filesystem):
# class ImgMountFilesystem(Filesystem):
# 
# Path: elbepack/shellhelper.py
# def system(cmd, allow_fail=False, env_add=None):
#     """system() - Execute cmd in a shell.
# 
#     Throws a CommandError if cmd returns none-zero and allow_fail=False
# 
#     --
# 
#     >>> system("true")
# 
#     >>> system("false", allow_fail=True)
# 
#     >>> system("$FALSE", env_add={"FALSE":"false"}) # doctest: +ELLIPSIS
#     Traceback (most recent call last):
#     ...
#     elbepack.shellhelper.CommandError: ...
# 
#     """
#     new_env = os.environ.copy()
#     if env_add:
#         new_env.update(env_add)
# 
#     ret = call(cmd, shell=True, env=new_env)
# 
#     if ret != 0:
#         if not allow_fail:
#             raise CommandError(cmd, ret)


import os
from gpg import core
from gpg.constants import sigsum, sig, PROTOCOL_OpenPGP
from gpg.errors import GPGMEError, KeyNotFound, InvalidSigners
from elbepack.filesystem import hostfs
from elbepack.shellhelper import system
        else:
            outdata.seek(0, os.SEEK_SET)
            signature = outdata.read()
            with open(outfile, 'w') as fd:
                fd.write(signature)

def sign_file(fname, fingerprint):
    outfilename = fname + '.gpg'
    sign(fname, outfilename, fingerprint)

def get_fingerprints():
    ctx = core.Context()
    ctx.set_engine_info(PROTOCOL_OpenPGP,
                        None,
                        '/var/cache/elbe/gnupg')
    keys = ctx.op_keylist_all(None, False)
    fingerprints = []
    for k in keys:
        fingerprints.append(k.subkeys[0].fpr)
    return fingerprints

# End Of Time - Roughtly 136 years
#
# The argument parser of GPG use the type unsigned long for
# default-cache-ttl and max-cache-ttl values.  Thus we're setting the
# least maximum value of the type unsigned long to ensure that the
# passphrase is 'never' removed from gpg-agent.
EOT = 4294967295
def generate_elbe_internal_key():
    hostfs.mkdir_p("/var/cache/elbe/gnupg")

# Path: elbepack/filesystem.py
# def size_to_int(size):
#     def __init__(self, path, clean=False):
#     def fname(self, path):
#     def open(self, path, mode="r"):
#     def open_gz(self, path, mode="r"):
#     def isdir(self, path):
#     def islink(self, path):
#     def isfile(self, path):
#     def exists(self, path):
#     def lexists(self, path):
#     def mkdir(self, path):
#     def readlink(self, path):
#     def realpath(self, path):
#     def symlink(self, src, path, allow_exists=False):
#     def stat(self, path):
#     def chown(self, path, uid, gid):
#     def chmod(self, path, mode):
#     def utime(self, path, times=None):
#     def cat_file(self, inf):
#     def remove(self, path, noerr=False):
#     def rmtree(self, path):
#     def listdir(self, path='', ignore=None, skiplinks=False):
#     def glob(self, path):
#     def _write_file(path, f, cont, mode):
#     def write_file(self, path, mode, cont):
#     def append_file(self, path, cont, mode=None):
#     def read_file(self, path, gz=False):
#     def mkdir_p(self, newdir, mode=0o755):
#     def touch_file(self, fname):
#     def walk_files(self, directory='', exclude_dirs=None):
#     def mtime_snap(self, dirname='', exclude_dirs=None):
#     def __disk_usage(self, directory):
#     def disk_usage(self, dirname=''):
#     def __init__(self, debug=False):
#     def __del__(self):
#     def delete(self):
#     def __enter__(self):
#     def __exit__(self, exec_type, exec_value, tb):
#     def __init__(self, mntpoint, dev):
#     def __enter__(self):
#     def __exit__(self, typ, value, traceback):
# class Filesystem:
# class TmpdirFilesystem (Filesystem):
# class ImgMountFilesystem(Filesystem):
# 
# Path: elbepack/shellhelper.py
# def system(cmd, allow_fail=False, env_add=None):
#     """system() - Execute cmd in a shell.
# 
#     Throws a CommandError if cmd returns none-zero and allow_fail=False
# 
#     --
# 
#     >>> system("true")
# 
#     >>> system("false", allow_fail=True)
# 
#     >>> system("$FALSE", env_add={"FALSE":"false"}) # doctest: +ELLIPSIS
#     Traceback (most recent call last):
#     ...
#     elbepack.shellhelper.CommandError: ...
# 
#     """
#     new_env = os.environ.copy()
#     if env_add:
#         new_env.update(env_add)
# 
#     ret = call(cmd, shell=True, env=new_env)
# 
#     if ret != 0:
#         if not allow_fail:
#             raise CommandError(cmd, ret)


# Path: elbepack/egpg.py
import os
from gpg import core
from gpg.constants import sigsum, sig, PROTOCOL_OpenPGP
from gpg.errors import GPGMEError, KeyNotFound, InvalidSigners
from elbepack.filesystem import hostfs
from elbepack.shellhelper import system

        else:
            outdata.seek(0, os.SEEK_SET)
            signature = outdata.read()
            with open(outfile, 'w') as fd:
                fd.write(signature)

def sign_file(fname, fingerprint):
    outfilename = fname + '.gpg'
    sign(fname, outfilename, fingerprint)

def get_fingerprints():
    ctx = core.Context()
    ctx.set_engine_info(PROTOCOL_OpenPGP,
                        None,
                        '/var/cache/elbe/gnupg')
    keys = ctx.op_keylist_all(None, False)
    fingerprints = []
    for k in keys:
        fingerprints.append(k.subkeys[0].fpr)
    return fingerprints

# End Of Time - Roughtly 136 years
#
# The argument parser of GPG use the type unsigned long for
# default-cache-ttl and max-cache-ttl values.  Thus we're setting the
# least maximum value of the type unsigned long to ensure that the
# passphrase is 'never' removed from gpg-agent.
EOT = 4294967295
def generate_elbe_internal_key():
    hostfs.mkdir_p("/var/cache/elbe/gnupg")

    hostfs.write_file("/var/cache/elbe/gnupg/gpg-agent.conf", 0o600,