def submit_and_dl_result(xmlfile, cdrom, opt):

    # pylint: disable=too-many-statements
    # pylint: disable=too-many-branches

    try:
        with PreprocessWrapper(xmlfile, opt) as ppw:
            xmlfile = ppw.preproc

            ret, prjdir, err = command_out_stderr('%s control create_project' %
                                                  (elbe_exe))
            if ret != 0:
                print("elbe control create_project failed.", file=sys.stderr)
                print(err, file=sys.stderr)
                print("Giving up", file=sys.stderr)
                sys.exit(20)

            prjdir = prjdir.strip()

            cmd = '%s control set_xml %s %s' % (elbe_exe, prjdir, xmlfile)
            ret, _, err = command_out_stderr(cmd)
            if ret != 0:
                print("elbe control set_xml failed2", file=sys.stderr)
                print(err, file=sys.stderr)
                print("Giving up", file=sys.stderr)
                sys.exit(20)
    except CommandError:
        # this is the failure from PreprocessWrapper
        # it already printed the error message from
        # elbe preprocess
        print("Giving up", file=sys.stderr)
        sys.exit(20)

    if opt.writeproject:
        with open(opt.writeproject, "w") as wpf:
            wpf.write(prjdir)

    if cdrom is not None:
        print("Uploading CDROM. This might take a while")
        try:
            system('%s control set_cdrom "%s" "%s"' %
                   (elbe_exe, prjdir, cdrom))
        except CommandError:
            print("elbe control set_cdrom Failed", file=sys.stderr)
            print("Giving up", file=sys.stderr)
            sys.exit(20)

        print("Upload finished")

    build_opts = ''
    if opt.build_bin:
        build_opts += '--build-bin '
    if opt.build_sources:
        build_opts += '--build-sources '
    if cdrom:
        build_opts += '--skip-pbuilder '

    try:
        system('%s control build "%s" %s' % (elbe_exe, prjdir, build_opts))
    except CommandError:
        print("elbe control build Failed", file=sys.stderr)
        print("Giving up", file=sys.stderr)
        sys.exit(20)

    print("Build started, waiting till it finishes")

    try:
        system('%s control wait_busy "%s"' % (elbe_exe, prjdir))
    except CommandError:
        print('elbe control wait_busy Failed', file=sys.stderr)
        print('', file=sys.stderr)
        print('The project will not be deleted from the initvm.',
              file=sys.stderr)
        print('The files, that have been built, can be downloaded using:',
              file=sys.stderr)
        print('%s control get_files --output "%s" "%s"' %
              (elbe_exe, opt.outdir, prjdir),
              file=sys.stderr)
        print("", file=sys.stderr)
        print('The project can then be removed using:', file=sys.stderr)
        print('%s control del_project "%s"' % (elbe_exe, prjdir),
              file=sys.stderr)
        print("", file=sys.stderr)
        sys.exit(10)

    print("")
    print("Build finished !")
    print("")

    if opt.build_sdk:
        try:
            system('%s control build_sdk "%s" %s' %
                   (elbe_exe, prjdir, build_opts))
        except CommandError:
            print("elbe control build_sdk Failed", file=sys.stderr)
            print("Giving up", file=sys.stderr)
            sys.exit(20)

        print("SDK Build started, waiting till it finishes")

        try:
            system('%s control wait_busy "%s"' % (elbe_exe, prjdir))
        except CommandError:
            print('elbe control wait_busy Failed, while waiting for the SDK',
                  file=sys.stderr)
            print('', file=sys.stderr)
            print('The project will not be deleted from the initvm.',
                  file=sys.stderr)
            print('The files, that have been built, can be downloaded using:',
                  file=sys.stderr)
            print('%s control get_files --output "%s" "%s"' %
                  (elbe_exe, opt.outdir, prjdir),
                  file=sys.stderr)
            print("", file=sys.stderr)
            print('The project can then be removed using:', file=sys.stderr)
            print('%s control del_project "%s"' % (elbe_exe, prjdir),
                  file=sys.stderr)
            print("", file=sys.stderr)
            sys.exit(10)

        print("")
        print("SDK Build finished !")
        print("")

    try:
        system('%s control dump_file "%s" validation.txt' % (elbe_exe, prjdir))
    except CommandError:
        print("Project failed to generate validation.txt", file=sys.stderr)
        print("Getting log.txt", file=sys.stderr)
        try:
            system('%s control dump_file "%s" log.txt' % (elbe_exe, prjdir))
        except CommandError:

            print("Failed to dump log.txt", file=sys.stderr)
            print("Giving up", file=sys.stderr)
        sys.exit(20)

    if opt.skip_download:
        print("")
        print("Listing available files:")
        print("")
        try:
            system('%s control get_files "%s"' % (elbe_exe, prjdir))
        except CommandError:
            print("elbe control get_files Failed", file=sys.stderr)
            print("Giving up", file=sys.stderr)
            sys.exit(20)

        print("")
        print('Get Files with: elbe control get_file "%s" <filename>' % prjdir)
    else:
        print("")
        print("Getting generated Files")
        print("")

        ensure_outdir(opt)

        try:
            system('%s control get_files --output "%s" "%s"' %
                   (elbe_exe, opt.outdir, prjdir))
        except CommandError:
            print("elbe control get_files Failed", file=sys.stderr)
            print("Giving up", file=sys.stderr)
            sys.exit(20)

        if not opt.keep_files:
            try:
                system('%s control del_project "%s"' % (elbe_exe, prjdir))
            except CommandError:
                print("remove project from initvm failed", file=sys.stderr)
                sys.exit(20)
    def execute(self, initvmdir, opt, args):

        # pylint: disable=too-many-branches
        # pylint: disable=too-many-statements

        if self.initvm is not None:
            print("Initvm is already defined for the libvirt domain '%s'.\n" %
                  cfg['initvm_domain'])
            print("If you want to build in your old initvm, "
                  "use `elbe initvm submit <xml>`.")
            print("If you want to remove your old initvm from libvirt "
                  "run `virsh --connect qemu:///system undefine %s`.\n" %
                  cfg['initvm_domain'])
            print(
                "You can specify another libvirt domain by setting the "
                "ELBE_INITVM_DOMAIN environment variable to an unused domain name.\n"
            )
            print("Note:")
            print("\t1) You can reimport your old initvm via "
                  "`virsh --connect qemu:///system define <file>`")
            print("\t   where <file> is the corresponding libvirt.xml")
            print(
                "\t2) virsh --connect qemu:///system undefine does not delete the image "
                "of your old initvm.")
            sys.exit(20)

        # Upgrade from older versions which used tmux
        try:
            system("tmux has-session -t ElbeInitVMSession 2>/dev/null")
            print(
                "ElbeInitVMSession exists in tmux. "
                "It may belong to an old elbe version. "
                "Please stop it to prevent interfering with this version.",
                file=sys.stderr)
            sys.exit(20)
        except CommandError:
            pass

        # Init cdrom to None, if we detect it, we set it
        cdrom = None

        if len(args) == 1:
            if args[0].endswith('.xml'):
                # We have an xml file, use that for elbe init
                xmlfile = args[0]
                try:
                    xml = etree(xmlfile)
                except ValidationError as e:
                    print("XML file is invalid: %s" % str(e))
                # Use default XML if no initvm was specified
                if not xml.has("initvm"):
                    xmlfile = os.path.join(elbepack.__path__[0],
                                           "init/default-init.xml")

            elif args[0].endswith('.iso'):
                # We have an iso image, extract xml from there.
                tmp = extract_cdrom(args[0])

                xmlfile = tmp.fname('source.xml')
                cdrom = args[0]
            else:
                print("Unknown file ending (use either xml or iso)",
                      file=sys.stderr)
                sys.exit(20)
        else:
            # No xml File was specified, build the default elbe-init-with-ssh
            xmlfile = os.path.join(elbepack.__path__[0],
                                   "init/default-init.xml")

        try:
            init_opts = ''
            if opt.devel:
                init_opts += ' --devel'

            if opt.nesting:
                init_opts += ' --nesting'

            if not opt.build_bin:
                init_opts += ' --skip-build-bin'

            if not opt.build_sources:
                init_opts += ' --skip-build-source'

            with PreprocessWrapper(xmlfile, opt) as ppw:
                if cdrom:
                    system(
                        '%s init %s --directory "%s" --cdrom "%s" "%s"' %
                        (elbe_exe, init_opts, initvmdir, cdrom, ppw.preproc))
                else:
                    system('%s init %s --directory "%s" "%s"' %
                           (elbe_exe, init_opts, initvmdir, ppw.preproc))

        except CommandError:
            print("'elbe init' Failed", file=sys.stderr)
            print("Giving up", file=sys.stderr)
            sys.exit(20)

        # Read xml file for libvirt
        with open(os.path.join(initvmdir, 'libvirt.xml')) as f:
            xml = f.read()

        # Register initvm in libvirt
        try:
            self.conn.defineXML(xml)
        except CommandError:
            print('Registering initvm in libvirt failed', file=sys.stderr)
            print(
                'Try `virsh --connect qemu:///system undefine %s` to delete existing initvm'
                % cfg['initvm_domain'],
                file=sys.stderr)
            sys.exit(20)

        # Build initvm
        try:
            system('cd "%s"; make' % (initvmdir))
        except CommandError:
            print("Building the initvm Failed", file=sys.stderr)
            print("Giving up", file=sys.stderr)
            sys.exit(20)

        try:
            system('%s initvm start' % elbe_exe)
        except CommandError:
            print("Starting the initvm Failed", file=sys.stderr)
            print("Giving up", file=sys.stderr)
            sys.exit(20)

        if len(args) == 1:
            # if provided xml file has no initvm section xmlfile is set to a
            # default initvm XML file. But we need the original file here
            if args[0].endswith('.xml'):
                # stop here if no project node was specified
                try:
                    x = etree(args[0])
                except ValidationError as e:
                    print("XML file is invalid: %s" % str(e))
                    sys.exit(20)
                if not x.has('project'):
                    print("elbe initvm ready: use 'elbe initvm submit "
                          "myproject.xml' to build a project")
                    sys.exit(0)

                xmlfile = args[0]
            elif cdrom is not None:
                xmlfile = tmp.fname('source.xml')

            submit_and_dl_result(xmlfile, cdrom, opt)
    def execute(self, opt, _args):
        crossopt = ""
        if opt.cross:
            crossopt = "--cross"
        if opt.noccache:
            ccacheopt = "--no-ccache"
            ccachesize = ""
        else:
            ccacheopt = "--ccache-size"
            ccachesize = opt.ccachesize

        if opt.xmlfile:
            try:
                with PreprocessWrapper(opt.xmlfile, opt) as ppw:
                    ret, prjdir, err = command_out_stderr(
                        '%s control create_project' % (elbe_exe))
                    if ret != 0:
                        print("elbe control create_project failed.",
                              file=sys.stderr)
                        print(err, file=sys.stderr)
                        print("Giving up", file=sys.stderr)
                        sys.exit(20)

                    prjdir = prjdir.strip()
                    ret, _, err = command_out_stderr(
                        '%s control set_xml "%s" "%s"' %
                        (elbe_exe, prjdir, ppw.preproc))

                    if ret != 0:
                        print("elbe control set_xml failed.", file=sys.stderr)
                        print(err, file=sys.stderr)
                        print("Giving up", file=sys.stderr)
                        sys.exit(20)
            except CommandError:
                # this is the failure from PreprocessWrapper
                # it already printed the error message from
                # elbe preprocess
                print("Giving up", file=sys.stderr)
                sys.exit(20)

            if opt.writeproject:
                wpf = open(opt.writeproject, "w")
                wpf.write(prjdir)
                wpf.close()

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

        print("Creating pbuilder")

        try:
            system('%s control build_pbuilder "%s" %s %s %s' %
                   (elbe_exe, prjdir, crossopt, ccacheopt, ccachesize))
        except CommandError:
            print("elbe control build_pbuilder Failed", file=sys.stderr)
            print("Giving up", file=sys.stderr)
            sys.exit(20)

        try:
            system('%s control wait_busy "%s"' % (elbe_exe, prjdir))
        except CommandError:
            print("elbe control wait_busy Failed", file=sys.stderr)
            print("Giving up", file=sys.stderr)
            sys.exit(20)

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