def transfer_file(r, b):
    local = path.srpms_dir + '/' + r.id + "/" + b.src_rpm
    f = b.src_rpm_file
    # export files from chroot
    chroot.cp(f, outfile = local, rm = True)
    os.chmod(local, 0644)
    ftp.add(local)

    if config.gen_upinfo and 'test-build' not in r.flags:
        fname = path.srpms_dir + '/' + r.id + "/" + b.src_rpm + ".uploadinfo"
        f = open(fname, "w")
        f.write("info:build:%s:requester:%s\ninfo:build:%s:requester_email:%s\nfile:SRPMS:%s\nEND\n" % (b.gb_id, b.requester, b.gb_id, b.requester_email, b.src_rpm))
        f.close()
        ftp.add(fname, "uploadinfo")
def transfer_file(r, b):
    local = path.srpms_dir + '/' + r.id + "/" + b.src_rpm
    f = b.src_rpm_file
    # export files from chroot
    chroot.cp(f, outfile=local, rm=True)
    os.chmod(local, 0644)
    ftp.add(local)

    if config.gen_upinfo and 'test-build' not in r.flags:
        fname = path.srpms_dir + '/' + r.id + "/" + b.src_rpm + ".uploadinfo"
        f = open(fname, "w")
        f.write(
            "info:build:%s:requester:%s\ninfo:build:%s:requester_email:%s\nfile:SRPMS:%s\nEND\n"
            % (b.gb_id, b.requester, b.gb_id, b.requester_email, b.src_rpm))
        f.close()
        ftp.add(fname, "uploadinfo")
def build_rpm(r, b):
    packagename = b.get_package_name()
    if not packagename:
        # should not really get here
        b.log_line("error: No .spec not given of malformed: '%s'" % b.spec)
        res = "FAIL_INTERNAL"
        return res

    status.push("building %s (%s)" % (b.spec, packagename))
    b.log_line("request from: %s" % r.requester)

    if check_skip_build(r, b):
        b.log_line("build skipped due to src builder request")
        res = "SKIP_REQUESTED"
        return res

    b.log_line("started at: %s" % time.asctime())
    fetch_src(r, b)
    b.log_line("installing srpm: %s" % b.src_rpm)
    res = chroot.run("""
        set -ex;
        install -d %(topdir)s/{BUILD,RPMS};
        rpm -Uhv --nodeps %(rpmdefs)s %(src_rpm)s;
        rm -f %(src_rpm)s;
    """ % {
        'topdir': b._topdir,
        'rpmdefs': b.rpmbuild_opts(),
        'src_rpm': b.src_rpm
    },
                     logfile=b.logfile)
    b.files = []

    tmpdir = b.tmpdir()
    if res:
        b.log_line("error: installing src rpm failed")
        res = "FAIL_SRPM_INSTALL"
    else:
        prepare_env()
        chroot.run("set -x; install -m 700 -d %s" % tmpdir, logfile=b.logfile)
        b.default_target(config.arch)
        # check for build arch before filling BR
        cmd = "set -ex; TMPDIR=%(tmpdir)s exec nice -n %(nice)s " \
            "rpmbuild -bp --short-circuit --nodeps %(rpmdefs)s --define 'prep exit 0' %(topdir)s/%(spec)s" % {
            'tmpdir': tmpdir,
            'nice' : config.nice,
            'topdir' : b._topdir,
            'rpmdefs' : b.rpmbuild_opts(),
            'spec': b.spec,
        }
        res = chroot.run(cmd, logfile=b.logfile)
        if res:
            res = "UNSUPP"
            b.log_line("error: build arch check (%s) failed" % cmd)

        if not res:
            if ("no-install-br"
                    not in r.flags) and not install.uninstall_self_conflict(b):
                res = "FAIL_DEPS_UNINSTALL"
            if ("no-install-br"
                    not in r.flags) and not install.install_br(r, b):
                res = "FAIL_DEPS_INSTALL"
            if not res:
                max_jobs = max(
                    min(int(os.sysconf('SC_NPROCESSORS_ONLN') + 1),
                        config.max_jobs), 1)
                if r.max_jobs > 0:
                    max_jobs = max(min(config.max_jobs, r.max_jobs), 1)
                cmd = "set -ex; : build-id: %(r_id)s; TMPDIR=%(tmpdir)s exec nice -n %(nice)s " \
                    "rpmbuild -bb --define '_smp_mflags -j%(max_jobs)d' %(rpmdefs)s %(topdir)s/%(spec)s" % {
                    'r_id' : r.id,
                    'tmpdir': tmpdir,
                    'nice' : config.nice,
                    'rpmdefs' : b.rpmbuild_opts(),
                    'topdir' : b._topdir,
                    'max_jobs' : max_jobs,
                    'spec': b.spec,
                }
                b.log_line("building RPM using: %s" % cmd)
                begin_time = time.time()
                res = chroot.run(cmd, logfile=b.logfile)
                end_time = time.time()
                b.log_line("ended at: %s, done in %s" %
                           (time.asctime(),
                            datetime.timedelta(0, end_time - begin_time)))
                if res:
                    res = "FAIL"
                files = util.collect_files(b.logfile, basedir=b._topdir)
                if len(files) > 0:
                    r.chroot_files.extend(files)
                else:
                    b.log_line("error: No files produced.")
                    last_section = util.find_last_section(b.logfile)
                    if last_section == None:
                        res = "FAIL"
                    else:
                        res = "FAIL_%s" % last_section.upper()
                b.files = files

    # cleanup tmp and build files
    chroot.run("""
        set -ex;
        chmod -R u+rwX %(topdir)s/BUILD;
        rm -rf %(topdir)s/{tmp,BUILD}
    """ % {
        'topdir': b._topdir,
    },
               logfile=b.logfile)

    def ll(l):
        util.append_to(b.logfile, l)

    if b.files != []:
        rpm_cache_dir = config.rpm_cache_dir
        if "test-build" not in r.flags:
            # NOTE: copying to cache dir doesn't mean that build failed, so ignore result
            b.log_line("copy rpm files to cache_dir: %s" % rpm_cache_dir)
            chroot.run(
                    "cp -f %s %s && poldek --mo=nodiff --mkidxz -s %s/" % \
                        (string.join(b.files), rpm_cache_dir, rpm_cache_dir),
                     logfile = b.logfile, user = "******"
            )
        else:
            ll("test-build: not copying to " + rpm_cache_dir)
        ll("Begin-PLD-Builder-Info")
        if "upgrade" in r.flags:
            b.upgraded = install.upgrade_from_batch(r, b)
        else:
            ll("not upgrading")
        ll("End-PLD-Builder-Info")

    for f in b.files:
        local = r.tmp_dir + os.path.basename(f)
        chroot.cp(f, outfile=local, rm=True)
        ftp.add(local)

    # cleanup all remains from this build
    chroot.run("""
        set -ex;
        rm -rf %(topdir)s;
    """ % {
        'topdir': b._topdir,
    },
               logfile=b.logfile)

    def uploadinfo(b):
        c = "file:SRPMS:%s\n" % b.src_rpm
        for f in b.files:
            c = c + "file:ARCH:%s\n" % os.path.basename(f)
        c = c + "END\n"
        return c

    if config.gen_upinfo and b.files != [] and 'test-build' not in r.flags:
        fname = r.tmp_dir + b.src_rpm + ".uploadinfo"
        f = open(fname, "w")
        f.write(uploadinfo(b))
        f.close()
        ftp.add(fname, "uploadinfo")

    status.pop()

    return res
def build_rpm(r, b):
    packagename = b.get_package_name()
    if not packagename:
        # should not really get here
        b.log_line("error: No .spec not given of malformed: '%s'" % b.spec)
        res = "FAIL_INTERNAL"
        return res

    status.push("building %s (%s)" % (b.spec, packagename))
    b.log_line("request from: %s" % r.requester)

    if check_skip_build(r, b):
        b.log_line("build skipped due to src builder request")
        res = "SKIP_REQUESTED"
        return res

    b.log_line("started at: %s" % time.asctime())
    fetch_src(r, b)
    b.log_line("installing srpm: %s" % b.src_rpm)
    res = chroot.run("""
        set -ex;
        install -d %(topdir)s/{BUILD,RPMS};
        rpm -Uhv --nodeps %(rpmdefs)s %(src_rpm)s;
        rm -f %(src_rpm)s;
    """ % {
        'topdir' : b._topdir,
        'rpmdefs' : b.rpmbuild_opts(),
        'src_rpm' : b.src_rpm
    }, logfile = b.logfile)
    b.files = []

    tmpdir = b.tmpdir()
    if res:
        b.log_line("error: installing src rpm failed")
        res = "FAIL_SRPM_INSTALL"
    else:
        prepare_env()
        chroot.run("set -x; install -m 700 -d %s" % tmpdir, logfile=b.logfile)
        b.default_target(config.arch)
        # check for build arch before filling BR
        cmd = "set -ex; TMPDIR=%(tmpdir)s exec nice -n %(nice)s " \
            "rpmbuild -bp --short-circuit --nodeps %(rpmdefs)s --define 'prep exit 0' %(topdir)s/%(spec)s" % {
            'tmpdir': tmpdir,
            'nice' : config.nice,
            'topdir' : b._topdir,
            'rpmdefs' : b.rpmbuild_opts(),
            'spec': b.spec,
        }
        res = chroot.run(cmd, logfile = b.logfile)
        if res:
            res = "UNSUPP"
            b.log_line("error: build arch check (%s) failed" % cmd)

        if not res:
            if ("no-install-br" not in r.flags) and not install.uninstall_self_conflict(b):
                res = "FAIL_DEPS_UNINSTALL"
            if ("no-install-br" not in r.flags) and not install.install_br(r, b):
                res = "FAIL_DEPS_INSTALL"
            if not res:
                max_jobs = max(min(int(os.sysconf('SC_NPROCESSORS_ONLN') + 1), config.max_jobs), 1)
                if r.max_jobs > 0:
                    max_jobs = max(min(config.max_jobs, r.max_jobs), 1)
                cmd = "set -ex; : build-id: %(r_id)s; TMPDIR=%(tmpdir)s exec nice -n %(nice)s " \
                    "rpmbuild -bb --define '_smp_mflags -j%(max_jobs)d' %(rpmdefs)s %(topdir)s/%(spec)s" % {
                    'r_id' : r.id,
                    'tmpdir': tmpdir,
                    'nice' : config.nice,
                    'rpmdefs' : b.rpmbuild_opts(),
                    'topdir' : b._topdir,
                    'max_jobs' : max_jobs,
                    'spec': b.spec,
                }
                b.log_line("building RPM using: %s" % cmd)
                begin_time = time.time()
                res = chroot.run(cmd, logfile = b.logfile)
                end_time = time.time()
                b.log_line("ended at: %s, done in %s" % (time.asctime(), datetime.timedelta(0, end_time - begin_time)))
                if res:
                    res = "FAIL"
                files = util.collect_files(b.logfile, basedir = b._topdir)
                if len(files) > 0:
                    r.chroot_files.extend(files)
                else:
                    b.log_line("error: No files produced.")
                    last_section = util.find_last_section(b.logfile)
                    if last_section == None:
                        res = "FAIL"
                    else:
                        res = "FAIL_%s" % last_section.upper()
                b.files = files

    # cleanup tmp and build files
    chroot.run("""
        set -ex;
        chmod -R u+rwX %(topdir)s/BUILD;
        rm -rf %(topdir)s/{tmp,BUILD}
    """ % {
        'topdir' : b._topdir,
    }, logfile = b.logfile)

    def ll(l):
        util.append_to(b.logfile, l)

    if b.files != []:
        rpm_cache_dir = config.rpm_cache_dir
        if "test-build" not in r.flags:
            # NOTE: copying to cache dir doesn't mean that build failed, so ignore result
            b.log_line("copy rpm files to cache_dir: %s" % rpm_cache_dir)
            chroot.run(
                    "cp -f %s %s && poldek --mo=nodiff --mkidxz -s %s/" % \
                        (string.join(b.files), rpm_cache_dir, rpm_cache_dir),
                     logfile = b.logfile, user = "******"
            )
        else:
            ll("test-build: not copying to " + rpm_cache_dir)
        ll("Begin-PLD-Builder-Info")
        if "upgrade" in r.flags:
            b.upgraded = install.upgrade_from_batch(r, b)
        else:
            ll("not upgrading")
        ll("End-PLD-Builder-Info")

    for f in b.files:
        local = r.tmp_dir + os.path.basename(f)
        chroot.cp(f, outfile = local, rm = True)
        ftp.add(local)

    # cleanup all remains from this build
    chroot.run("""
        set -ex;
        rm -rf %(topdir)s;
    """ % {
        'topdir' : b._topdir,
    }, logfile = b.logfile)

    def uploadinfo(b):
        c="file:SRPMS:%s\n" % b.src_rpm
        for f in b.files:
            c=c + "file:ARCH:%s\n" % os.path.basename(f)
        c=c + "END\n"
        return c

    if config.gen_upinfo and b.files != [] and 'test-build' not in r.flags:
        fname = r.tmp_dir + b.src_rpm + ".uploadinfo"
        f = open(fname, "w")
        f.write(uploadinfo(b))
        f.close()
        ftp.add(fname, "uploadinfo")

    status.pop()

    return res