Example #1
0
def get_srpm(pkgdirurl,
             mode="current",
             targetdirs=None,
             version=None,
             release=None,
             revision=None,
             packager="",
             revname=0,
             svnlog=0,
             scripts=[],
             submit=False,
             template=None,
             distro=None,
             macros=[],
             verbose=0,
             strict=False):
    svn = SVN()
    tmpdir = tempfile.mktemp()
    topdir = "_topdir %s" % tmpdir
    builddir = "_builddir %s/%s" % (tmpdir, "BUILD")
    rpmdir = "_rpmdir %s/%s" % (tmpdir, "RPMS")
    sourcedir = "_sourcedir %s/%s" % (tmpdir, "SOURCES")
    specdir = "_specdir %s/%s" % (tmpdir, "SPECS")
    srcrpmdir = "_srcrpmdir %s/%s" % (tmpdir, "SRPMS")
    patchdir = "_patchdir %s/%s" % (tmpdir, "SOURCES")
    temppath = "_tmppath %s" % (tmpdir)

    rpmdefs = [("--define", expr)
               for expr in (topdir, builddir, rpmdir, sourcedir, specdir,
                            srcrpmdir, patchdir, temppath)]

    try:
        if mode == "version":
            geturl = layout.checkout_url(pkgdirurl,
                                         version=version,
                                         release=release)
        elif mode == "pristine":
            geturl = layout.checkout_url(pkgdirurl, pristine=True)
        elif mode == "current" or mode == "revision":
            #FIXME we should handle revisions specified using @REV
            geturl = layout.checkout_url(pkgdirurl)
        else:
            raise Error, "unsupported get_srpm mode: %s" % mode
        strict = strict or config.getbool("submit", "strict-revision", False)
        if strict and not rev_touched_url(geturl, revision):
            #FIXME would be nice to have the revision number even when
            # revision is None
            raise Error, "the revision %s does not change anything "\
                    "inside %s" % (revision or "HEAD", geturl)
        mirror.info(geturl)
        svn.export(geturl, tmpdir, rev=revision)
        srpmsdir = os.path.join(tmpdir, "SRPMS")
        os.mkdir(srpmsdir)
        specsdir = os.path.join(tmpdir, "SPECS")
        speclist = glob.glob(os.path.join(specsdir, "*.spec"))
        if config.getbool("srpm", "run-prep", False):
            makefile = os.path.join(tmpdir, "Makefile")
            if os.path.exists(makefile):
                execcmd(("make", "-C", tmpdir, "srpm-prep"))
        if not speclist:
            raise Error, "no spec files found"
        spec = speclist[0]
        if svnlog:
            submit = not not revision
            log.specfile_svn2rpm(pkgdirurl,
                                 spec,
                                 revision,
                                 submit=submit,
                                 template=template,
                                 macros=macros,
                                 exported=tmpdir)
        for script in scripts:
            #FIXME revision can be "None"
            status, output = execcmd(script,
                                     tmpdir,
                                     spec,
                                     str(revision),
                                     noerror=1)
            if status != 0:
                raise Error, "script %s failed" % script
        if packager:
            packager = " --define 'packager %s'" % packager

        if distro:
            cmpdistro = distro.lower()
            for target in cgiutil.get_targets():
                if target.name.lower() == cmpdistro:
                    macros.extend(target.macros)
                    break
            else:
                raise Error, "no such submit target in configuration: %s" % (
                    distro)
        sourcecmd = config.get("helper", "rpmbuild", "rpmbuild")
        args = [sourcecmd, "-bs", "--nodeps"]
        for pair in rpmdefs:
            args.extend(pair)
        for pair in macros:
            args.extend(("--define", "%s %s" % pair))
        args.append(spec)
        try:
            execcmd(args)
        except CommandError, e:
            if config.getbool("global", "verbose"):
                cmdline = e.cmdline + "\n"
            else:
                cmdline = ""
            raise Error, ("error while creating the source RPM "
                          "(with %s):\n%s%s" % (sourcecmd, cmdline, e.output))

        # copy the generated SRPMs to their target locations
        targetsrpms = []
        urlrev = None
        if revname:
            urlrev = revision or layout.get_url_revision(geturl)
        if not targetdirs:
            targetdirs = (".", )
        srpms = glob.glob(os.path.join(srpmsdir, "*.src.rpm"))
        if not srpms:
            # something fishy happened
            raise Error, "no SRPMS were found at %s" % srpmsdir
        for srpm in srpms:
            name = os.path.basename(srpm)
            if revname:
                name = "@%s:%s" % (urlrev, name)
            for targetdir in targetdirs:
                newpath = os.path.join(targetdir, name)
                targetsrpms.append(newpath)
                if os.path.exists(newpath):
                    # should we warn?
                    os.unlink(newpath)
                shutil.copy(srpm, newpath)
                if verbose:
                    sys.stderr.write("Wrote: %s\n" % newpath)
        return targetsrpms
Example #2
0
def get_srpm(pkgdirurl,
             mode="current",
             targetdirs=None,
             version=None,
             release=None,
             revision=None,
             packager="",
             revname=0,
             svnlog=0,
             scripts=[],
             submit=False,
             template=None,
             macros=[],
             verbose=0,
             strict=False,
             fullnames=False):
    svn = detectVCS(pkgdirurl)
    tmpdir = tempfile.mktemp()
    topdir = "_topdir %s" % tmpdir
    builddir = "_builddir %s/%s" % (tmpdir, "BUILD")
    rpmdir = "_rpmdir %s/%s" % (tmpdir, "RPMS")
    sourcedir = "_sourcedir %s/%s" % (tmpdir, "SOURCES")
    specdir = "_specdir %s/%s" % (tmpdir, "SPECS")
    srcrpmdir = "_srcrpmdir %s/%s" % (tmpdir, "SRPMS")
    patchdir = "_patchdir %s/%s" % (tmpdir, "SOURCES")
    temppath = "_tmppath %s" % (tmpdir)

    rpmdefs = [("--define", expr)
               for expr in (topdir, builddir, rpmdir, sourcedir, specdir,
                            srcrpmdir, patchdir, temppath)]

    try:
        if mode == "version":
            geturl = layout.checkout_url(pkgdirurl,
                                         version=version,
                                         release=release)
        elif mode == "pristine":
            geturl = layout.checkout_url(pkgdirurl, pristine=True)
        elif mode == "current" or mode == "revision":
            #FIXME we should handle revisions specified using @REV
            geturl = layout.checkout_url(pkgdirurl)
        else:
            raise Error("unsupported get_srpm mode: %s" % mode)
        strict = strict or config.getbool("submit", "strict-revision", False)
        if strict and not rev_touched_url(geturl, revision):
            #FIXME would be nice to have the revision number even when
            # revision is None
            raise Error("the revision %s does not change anything "\
                    "inside %s" % (revision or "HEAD", geturl))
        mirror.info(geturl)
        svn.export(geturl, tmpdir, rev=revision)
        binrepo.download_binaries(tmpdir)
        srpmsdir = os.path.join(tmpdir, "SRPMS")
        os.mkdir(srpmsdir)
        specsdir = os.path.join(tmpdir, "SPECS")
        speclist = glob.glob(os.path.join(specsdir, "*.spec"))
        if config.getbool("srpm", "run-prep", False):
            makefile = os.path.join(tmpdir, "Makefile")
            if os.path.exists(makefile):
                execcmd(("make", "-C", tmpdir, "srpm-prep"))
        if not speclist:
            raise Error("no spec files found")
        spec = speclist[0]

        sourcecmd = config.get("helper", "rpmbuild", "rpmbuild")
        if packager:
            packager = " --define 'packager %s'" % packager
        sourcecmd = config.get("helper", "rpmbuild", "rpmbuild")
        args = [sourcecmd, "-bs", "--nodeps"]
        for pair in rpmdefs:
            args.extend(pair)
        for pair in macros:
            args.extend(("--define", "%s %s" % pair))
        args.append(spec)
        if svnlog:
            submit = not not revision
            try:
                log.specfile_svn2rpm(pkgdirurl,
                                     spec,
                                     revision,
                                     submit=submit,
                                     template=template,
                                     macros=macros,
                                     exported=tmpdir,
                                     fullnames=fullnames)
            except:
                #cmd = [sourcecmd, topdir, builddir, rpmdir, sourcedir, specdir
                execcmd(args)
                cp_srpms(revision, revname, geturl, targetdirs, srpmsdir,
                         verbose)
                log.specfile_svn2rpm(pkgdirurl,
                                     spec,
                                     revision,
                                     submit=submit,
                                     template=template,
                                     macros=macros,
                                     exported=tmpdir,
                                     create=True)

        for script in scripts:
            #FIXME revision can be "None"
            status, output = execcmd(script,
                                     tmpdir,
                                     spec,
                                     str(revision),
                                     noerror=1)
            if status != 0:
                raise Error("script %s failed" % script)

        try:
            execcmd(args)
        except CommandError as e:
            if config.getbool("global", "verbose"):
                cmdline = e.cmdline + "\n"
            else:
                cmdline = ""
            raise Error("error while creating the source RPM "
                        "(with %s):\n%s%s" % (sourcecmd, cmdline, e.output))

        # copy the generated SRPMs to their target locations
        targetsrpms = cp_srpms(revision, revname, geturl, targetdirs, srpmsdir,
                               verbose)
        return targetsrpms
    finally:
        if os.path.isdir(tmpdir):
            shutil.rmtree(tmpdir)
Example #3
0
def get_srpm(pkgdirurl,
             mode = "current",
             targetdirs = None,
             version = None,
             release = None,
             revision = None,
             packager = "",
             revname = 0,
             svnlog = 0,
             scripts = [], 
             submit = False,
             template = None,
             distro = None,
             macros = [],
             verbose = 0,
             strict = False):
    svn = SVN()
    tmpdir = tempfile.mktemp()
    topdir = "_topdir %s" % tmpdir
    builddir = "_builddir %s/%s" % (tmpdir, "BUILD")
    rpmdir = "_rpmdir %s/%s" % (tmpdir, "RPMS")
    sourcedir = "_sourcedir %s/%s" % (tmpdir, "SOURCES")
    specdir = "_specdir %s/%s" % (tmpdir, "SPECS")
    srcrpmdir = "_srcrpmdir %s/%s" % (tmpdir, "SRPMS")
    patchdir = "_patchdir %s/%s" % (tmpdir, "SOURCES")
    temppath = "_tmppath %s" % (tmpdir)

    rpmdefs = [("--define", expr) for expr in (topdir, builddir, rpmdir,
        sourcedir, specdir, srcrpmdir, patchdir, temppath)]

    try:
        if mode == "version":
            geturl = layout.checkout_url(pkgdirurl, version=version,
                    release=release)
        elif mode == "pristine":
            geturl = layout.checkout_url(pkgdirurl, pristine=True)
        elif mode == "current" or mode == "revision":
            #FIXME we should handle revisions specified using @REV
            geturl = layout.checkout_url(pkgdirurl)
        else:
            raise Error, "unsupported get_srpm mode: %s" % mode
        strict = strict or config.getbool("submit", "strict-revision", False)
        if strict and not rev_touched_url(geturl, revision):
            #FIXME would be nice to have the revision number even when
            # revision is None
            raise Error, "the revision %s does not change anything "\
                    "inside %s" % (revision or "HEAD", geturl)
        mirror.info(geturl)
        svn.export(geturl, tmpdir, rev=revision)
        srpmsdir = os.path.join(tmpdir, "SRPMS")
        os.mkdir(srpmsdir)
        specsdir = os.path.join(tmpdir, "SPECS")
        speclist = glob.glob(os.path.join(specsdir, "*.spec"))
        if config.getbool("srpm", "run-prep", False):
            makefile = os.path.join(tmpdir, "Makefile")
            if os.path.exists(makefile):
                execcmd(("make", "-C", tmpdir, "srpm-prep"))
        if not speclist:
            raise Error, "no spec files found"
        spec = speclist[0]
        if svnlog:
            submit = not not revision
            log.specfile_svn2rpm(pkgdirurl, spec, revision, submit=submit,
                    template=template, macros=macros, exported=tmpdir)
        for script in scripts:
            #FIXME revision can be "None"
            status, output = execcmd(script, tmpdir, spec, str(revision),
                                     noerror=1)
            if status != 0:
                raise Error, "script %s failed" % script
        if packager:
            packager = " --define 'packager %s'" % packager

        if distro:
            cmpdistro = distro.lower()
            for target in cgiutil.get_targets():
                if target.name.lower() == cmpdistro:
                    macros.extend(target.macros)
                    break
            else:
                raise Error, "no such submit target in configuration: %s" % (distro)
        sourcecmd = config.get("helper", "rpmbuild", "rpmbuild")
        args = [sourcecmd, "-bs", "--nodeps"]
        for pair in rpmdefs:
            args.extend(pair)
        for pair in macros:
            args.extend(("--define", "%s %s" % pair))
        args.append(spec)
        try:
            execcmd(args)
        except CommandError, e:
            if config.getbool("global", "verbose"):
                cmdline = e.cmdline + "\n"
            else:
                cmdline = ""
            raise Error, ("error while creating the source RPM "
                        "(with %s):\n%s%s" % (sourcecmd, cmdline, e.output))

        # copy the generated SRPMs to their target locations
        targetsrpms = []
        urlrev = None
        if revname:
            urlrev = revision or layout.get_url_revision(geturl)
        if not targetdirs:
            targetdirs = (".",)
        srpms = glob.glob(os.path.join(srpmsdir, "*.src.rpm"))
        if not srpms:
            # something fishy happened
            raise Error, "no SRPMS were found at %s" % srpmsdir
        for srpm in srpms:
            name = os.path.basename(srpm)
            if revname:
                name = "@%s:%s" % (urlrev, name)
            for targetdir in targetdirs:
                newpath = os.path.join(targetdir, name)
                targetsrpms.append(newpath)
                if os.path.exists(newpath):
                    # should we warn?
                    os.unlink(newpath)
                shutil.copy(srpm, newpath)
                if verbose:
                    sys.stderr.write("Wrote: %s\n" %  newpath)
        return targetsrpms
Example #4
0
def build_rpm(build_cmd="b",
              verbose=True,
              rpmlint=True,
              short_circuit=False,
              packager=None,
              installdeps=True,
              use_dnf=False,
              svnlog=False,
              fullnames=True,
              macros=[],
              **rpmargs):
    top = os.getcwd()
    vcs = detectVCS(top)
    topdir = "_topdir %s" % top
    builddir = "_builddir %s/%s" % (top, "BUILD")
    rpmdir = "_rpmdir %s/%s" % (top, "RPMS")
    srcrpmdir = "_srcrpmdir %s/%s" % (top, "SRPMS")
    specsdir = join(top, "SPECS")

    # fedora & abf layout
    if vcs.vcs_name == "git" and not os.path.exists(specsdir):
        sourcedir = "_sourcedir " + top
        specdir = "_specdir " + top
        patchdir = "_patchdir " + top
        specsdir = top
    else:
        # mageia layout
        sourcedir = "_sourcedir %s/%s" % (top, "SOURCES")
        specdir = "_specdir %s/%s" % (top, "SPECS")
        patchdir = "_patchdir %s/%s" % (top, "SOURCES")

    build = join(top, "BUILD")
    if not os.path.exists(build):
        os.mkdir(build)
    speclist = glob.glob(join(specsdir, "*.spec"))
    if not speclist:
        raise Error("no spec files found")
    spec = speclist[0]

    # If we're building package with %changelog, we'll make a temporary
    # copy of the spec file with %changelog applied that we'll use for
    # building. This way we avoid modifying files in repository.
    # TODO: implement support for external changelog in rpm
    if svnlog:
        specsdir = tempfile.mkdtemp()
        shutil.copy(spec, specsdir)
        specdir = "_specdir " + specsdir
        spec = os.path.join(specsdir, os.path.basename(spec))
        info = vcs.info2(top)
        pkgdirurl = layout.remove_current(info["URL"])
        log.specfile_svn2rpm(pkgdirurl,
                             spec,
                             rev=None,
                             submit=False,
                             template=None,
                             macros=macros,
                             exported=top,
                             fullnames=fullnames)

    rpmdefs = [("--define", expr)
               for expr in (topdir, builddir, rpmdir, sourcedir, specdir,
                            srcrpmdir, patchdir)]

    if packager:
        rpmdefs.append(("--define", "packager %s" % packager))

    if rpmlint:
        rpmdefs.append(("--define", "_build_pkgcheck_set %{_bindir}/rpmlint"))

    rpmbuild = config.get("helper", "rpmbuild", "rpmbuild")
    args = [rpmbuild, spec]
    if short_circuit:
        args.append("--short-circuit")
    for pair in rpmdefs:
        args.extend(pair)
    for pair in macros:
        args.extend(("--define", "%s %s" % pair))
    args.extend(("--define", "_disable_source_fetch 0"))
    args.extend(*rpmargs.values())
    os.environ["LC_ALL"] = "C"
    # First check whether dependencies are satisfied
    status, output = execcmd(*args + ["--nobuild"],
                             show=verbose,
                             collecterr=True,
                             noerror=True)
    if status:
        if "error: Failed build dependencies:" in output:
            if not installdeps:
                raise Error("Automatic installation of dependencies disabled,"
                            "aborting...")
            else:
                if verbose:
                    print("Installing missing build dependencies")
                if use_dnf:
                    pkg_mgr_base = ["dnf"]
                    pkg_mgr_builddep = pkg_mgr_base + [
                        "--assume-yes", "--setopt=install_weak_deps=False",
                        "builddep"
                    ]
                else:
                    pkg_mgr_base = ["urpmi"]
                    pkg_mgr_builddep = pkg_mgr_base + [
                        "--auto", "--buildrequires", "--no-recommends"
                    ]
                if os.getuid() != 0:
                    print(
                        "Trying to obtain privileges for installing build dependencies:"
                    )
                    sudocheck = ["sudo", "-l"] + pkg_mgr_base
                    status, output = execcmd(*sudocheck,
                                             collecter=True,
                                             noerror=True)
                    if status:
                        raise Error(
                            "%s\nFailed! Cannot proceed without, aborting..." %
                            output.splitlines()[-1])
                    cmd_base = ["sudo"] + pkg_mgr_builddep
                else:
                    cmd_base = pkg_mgr_builddep
                cmd = cmd_base + [spec]
                status, output = execcmd(*cmd,
                                         show=verbose,
                                         collecter=True,
                                         noerror=True)

    status, output = execcmd(*args + ["-b" + build_cmd], show=verbose)
    if svnlog:
        if os.path.isdir(specsdir):
            shutil.rmtree(specsdir)