# Path: elbepack/directories.py
# def init_directories(elbe_relpath):
# def get_cmdlist():
# 
# Path: elbepack/shellhelper.py
# class CommandError(Exception):
# 
#     def __init__(self, cmd, returncode):
#         super(CommandError, self).__init__(cmd, returncode)
#         self.returncode = returncode
#         self.cmd = cmd
# 
#     def __str__(self):
#         return "Error: %d returned from Command %s" % (
#             self.returncode, self.cmd)
# 
# 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)
# 
# def command_out_stderr(cmd, stdin=None, env_add=None):
#     """command_out_stderr() - Execute cmd in a shell.
# 
#     Returns a tuple of the exitcode, stdout and stderr of cmd.
# 
#     --
# 
#     >>> command_out_stderr("$TRUE && cat -", stdin=b"ELBE", env_add={"TRUE":"true"})
#     (0, 'ELBE', '')
# 
#     >>> command_out_stderr("1>&2 cat - && false", stdin=b"ELBE")
#     (1, '', 'ELBE')
# 
#     >>> command_out_stderr("1>&2 cat - && false", stdin="ELBE")
#     (1, '', 'ELBE')
# 
#     >>> command_out_stderr("true")
#     (0, '', '')
# 
#     """
#     new_env = os.environ.copy()
#     if env_add:
#         new_env.update(env_add)
# 
#     if isinstance(stdin, str):
#         stdin = stdin.encode()
# 
#     if stdin is None:
#         p = Popen(cmd, shell=True,
#                   stdout=PIPE, stderr=PIPE, env=new_env)
#         output, stderr = p.communicate()
#     else:
#         p = Popen(cmd, shell=True,
#                   stdout=PIPE, stderr=PIPE, stdin=PIPE, env=new_env)
#         output, stderr = p.communicate(input=stdin)
# 
#     output = TextIOWrapper(BytesIO(output), encoding='utf-8', errors='replace').read()
#     stderr = TextIOWrapper(BytesIO(stderr), encoding='utf-8', errors='replace').read()
# 
#     return p.returncode, output, stderr
# 
# 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/xmlpreprocess.py
# class PreprocessWrapper:
#     def __init__(self, xmlfile, opt):
#         self.xmlfile = xmlfile
#         self.outxml = None
#         self.options = ""
# 
#         if opt.variant:
#             self.options += ' --variants "%s"' % opt.variant
# 
#     def __enter__(self):
#         self.outxml = NamedTemporaryFile(prefix='elbe', suffix='xml')
# 
#         cmd = '%s preprocess %s -o %s %s' % (elbe_exe,
#                                              self.options,
#                                              self.outxml.name,
#                                              self.xmlfile)
#         ret, _, err = command_out_stderr(cmd)
#         if ret != 0:
#             print("elbe preprocess failed.", file=sys.stderr)
#             print(err, file=sys.stderr)
#             raise CommandError(cmd, ret)
# 
#         return self
# 
#     def __exit__(self, _typ, _value, _traceback):
#         self.outxml = None
# 
#     @staticmethod
#     def add_options(oparser):
#         # import it here because of cyclic imports
#         # pylint: disable=cyclic-import
#         from elbepack.commands.preprocess import add_pass_through_options
# 
#         group = OptionGroup(oparser,
#                             'Elbe preprocess options',
#                             'Options passed through to invocation of '
#                             '"elbe preprocess"')
#         add_pass_through_options(group)
#         oparser.add_option_group(group)
# 
#     @property
#     def preproc(self):
#         return self.outxml.name


import sys
import os
from elbepack.directories import elbe_exe
from elbepack.shellhelper import CommandError, system, command_out_stderr
from elbepack.filesystem import TmpdirFilesystem
from elbepack.xmlpreprocess import PreprocessWrapper
            print("elbe control wait_busy Failed", file=sys.stderr)
            print("Giving up", file=sys.stderr)
            sys.exit(20)

        print("")
        print("Building Pbuilder finished !")
        print("")


PBuilderAction.register(CreateAction)


class UpdateAction(PBuilderAction):

    tag = 'update'

    def __init__(self, node):
        PBuilderAction.__init__(self, node)

    def execute(self, opt, _args):

        if not opt.project:
            print("you need to specify --project option", file=sys.stderr)
            sys.exit(20)

        prjdir = opt.project

        print("Updating pbuilder")

        try:

# Path: elbepack/directories.py
# def init_directories(elbe_relpath):
# def get_cmdlist():
# 
# Path: elbepack/shellhelper.py
# class CommandError(Exception):
# 
#     def __init__(self, cmd, returncode):
#         super(CommandError, self).__init__(cmd, returncode)
#         self.returncode = returncode
#         self.cmd = cmd
# 
#     def __str__(self):
#         return "Error: %d returned from Command %s" % (
#             self.returncode, self.cmd)
# 
# 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)
# 
# def command_out_stderr(cmd, stdin=None, env_add=None):
#     """command_out_stderr() - Execute cmd in a shell.
# 
#     Returns a tuple of the exitcode, stdout and stderr of cmd.
# 
#     --
# 
#     >>> command_out_stderr("$TRUE && cat -", stdin=b"ELBE", env_add={"TRUE":"true"})
#     (0, 'ELBE', '')
# 
#     >>> command_out_stderr("1>&2 cat - && false", stdin=b"ELBE")
#     (1, '', 'ELBE')
# 
#     >>> command_out_stderr("1>&2 cat - && false", stdin="ELBE")
#     (1, '', 'ELBE')
# 
#     >>> command_out_stderr("true")
#     (0, '', '')
# 
#     """
#     new_env = os.environ.copy()
#     if env_add:
#         new_env.update(env_add)
# 
#     if isinstance(stdin, str):
#         stdin = stdin.encode()
# 
#     if stdin is None:
#         p = Popen(cmd, shell=True,
#                   stdout=PIPE, stderr=PIPE, env=new_env)
#         output, stderr = p.communicate()
#     else:
#         p = Popen(cmd, shell=True,
#                   stdout=PIPE, stderr=PIPE, stdin=PIPE, env=new_env)
#         output, stderr = p.communicate(input=stdin)
# 
#     output = TextIOWrapper(BytesIO(output), encoding='utf-8', errors='replace').read()
#     stderr = TextIOWrapper(BytesIO(stderr), encoding='utf-8', errors='replace').read()
# 
#     return p.returncode, output, stderr
# 
# 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/xmlpreprocess.py
# class PreprocessWrapper:
#     def __init__(self, xmlfile, opt):
#         self.xmlfile = xmlfile
#         self.outxml = None
#         self.options = ""
# 
#         if opt.variant:
#             self.options += ' --variants "%s"' % opt.variant
# 
#     def __enter__(self):
#         self.outxml = NamedTemporaryFile(prefix='elbe', suffix='xml')
# 
#         cmd = '%s preprocess %s -o %s %s' % (elbe_exe,
#                                              self.options,
#                                              self.outxml.name,
#                                              self.xmlfile)
#         ret, _, err = command_out_stderr(cmd)
#         if ret != 0:
#             print("elbe preprocess failed.", file=sys.stderr)
#             print(err, file=sys.stderr)
#             raise CommandError(cmd, ret)
# 
#         return self
# 
#     def __exit__(self, _typ, _value, _traceback):
#         self.outxml = None
# 
#     @staticmethod
#     def add_options(oparser):
#         # import it here because of cyclic imports
#         # pylint: disable=cyclic-import
#         from elbepack.commands.preprocess import add_pass_through_options
# 
#         group = OptionGroup(oparser,
#                             'Elbe preprocess options',
#                             'Options passed through to invocation of '
#                             '"elbe preprocess"')
#         add_pass_through_options(group)
#         oparser.add_option_group(group)
# 
#     @property
#     def preproc(self):
#         return self.outxml.name


# Path: elbepack/pbuilderaction.py
import sys
import os
from elbepack.directories import elbe_exe
from elbepack.shellhelper import CommandError, system, command_out_stderr
from elbepack.filesystem import TmpdirFilesystem
from elbepack.xmlpreprocess import PreprocessWrapper

            print("elbe control wait_busy Failed", file=sys.stderr)
            print("Giving up", file=sys.stderr)
            sys.exit(20)

        print("")
        print("Building Pbuilder finished !")
        print("")


PBuilderAction.register(CreateAction)


class UpdateAction(PBuilderAction):

    tag = 'update'

    def __init__(self, node):
        PBuilderAction.__init__(self, node)

    def execute(self, opt, _args):

        if not opt.project:
            print("you need to specify --project option", file=sys.stderr)
            sys.exit(20)

        prjdir = opt.project

        print("Updating pbuilder")

        try:

            system('%s control update_pbuilder "%s"' % (elbe_exe, prjdir))