# Path: elbepack/filesystem.py
# class TmpdirFilesystem (Filesystem):
#     def __init__(self, debug=False):
#         tmpdir = mkdtemp()
#         Filesystem.__init__(self, tmpdir)
#         self.debug = debug
# 
#     def __del__(self):
#         # dont delete files in debug mode
#         if self.debug:
#             print('leaving TmpdirFilesystem in "%s"' % self.path)
#         else:
#             self.delete()
# 
#     def delete(self):
#         shutil.rmtree(self.path, True)
# 
#     def __enter__(self):
#         return self
# 
#     def __exit__(self, exec_type, exec_value, tb):
#         shutil.rmtree(self.path)
#         return False
# 
# 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
import re
from apt_pkg import TagFile
from elbepack.filesystem import TmpdirFilesystem
from elbepack.shellhelper import system
                sz += int(f[1])
            break

    return sz

class ChangelogNeedsDependency(Exception):
    def __init__(self, pkgname):
        Exception.__init__(self,
                           'Changelog extraction depends on "%s"' % (pkgname))
        self.pkgname = pkgname


re_pkgfilename = r'(?P<name>.*)_(?P<ver>.*)_(?P<arch>.*).deb'


def extract_pkg_changelog(fname, extra_pkg=None):
    pkgname = os.path.basename(fname)
    m = re.match(re_pkgfilename, pkgname)

    pkgname = m.group('name')
    pkgarch = m.group('arch')

    print('pkg: %s, arch: %s' % (pkgname, pkgarch))

    fs = TmpdirFilesystem()

    if extra_pkg:
        print('with extra ' + extra_pkg)
        system('dpkg -x "%s" "%s"' % (extra_pkg, fs.fname('/')))


# Path: elbepack/filesystem.py
# class TmpdirFilesystem (Filesystem):
#     def __init__(self, debug=False):
#         tmpdir = mkdtemp()
#         Filesystem.__init__(self, tmpdir)
#         self.debug = debug
# 
#     def __del__(self):
#         # dont delete files in debug mode
#         if self.debug:
#             print('leaving TmpdirFilesystem in "%s"' % self.path)
#         else:
#             self.delete()
# 
#     def delete(self):
#         shutil.rmtree(self.path, True)
# 
#     def __enter__(self):
#         return self
# 
#     def __exit__(self, exec_type, exec_value, tb):
#         shutil.rmtree(self.path)
#         return False
# 
# 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/pkgutils.py
import os
import re
from apt_pkg import TagFile
from elbepack.filesystem import TmpdirFilesystem
from elbepack.shellhelper import system

                sz += int(f[1])
            break

    return sz

class ChangelogNeedsDependency(Exception):
    def __init__(self, pkgname):
        Exception.__init__(self,
                           'Changelog extraction depends on "%s"' % (pkgname))
        self.pkgname = pkgname


re_pkgfilename = r'(?P<name>.*)_(?P<ver>.*)_(?P<arch>.*).deb'


def extract_pkg_changelog(fname, extra_pkg=None):
    pkgname = os.path.basename(fname)
    m = re.match(re_pkgfilename, pkgname)

    pkgname = m.group('name')
    pkgarch = m.group('arch')

    print('pkg: %s, arch: %s' % (pkgname, pkgarch))

    fs = TmpdirFilesystem()

    if extra_pkg:
        print('with extra ' + extra_pkg)
        system('dpkg -x "%s" "%s"' % (extra_pkg, fs.fname('/')))


    system('dpkg -x "%s" "%s"' % (fname, fs.fname('/')))