yselkowitz / rpms / condor

Forked from rpms/condor 6 years ago
Clone
a7cfd51
#!/usr/bin/python
a7cfd51
a7cfd51
import os
a7cfd51
import sys
a7cfd51
import shutil
a7cfd51
import optparse
a7cfd51
a7cfd51
def prep_dirs(base_dir):
a7cfd51
    for i in ["BUILD", "INSTALL", "RPMS", "SOURCES", "SPECS", "SRPMS"]:
a7cfd51
        new_dir = os.path.join(base_dir, i)
a7cfd51
        if not os.path.exists(new_dir):
a7cfd51
            os.makedirs(new_dir)
a7cfd51
a7cfd51
def prep_source(base_dir):
a7cfd51
    source_dir = os.path.join(base_dir, "_build", "SOURCES")
a7cfd51
    for file in os.listdir(base_dir):
a7cfd51
        if file == "_build":
a7cfd51
            continue
a7cfd51
        full_name = os.path.join(base_dir, file)
a7cfd51
        if not os.path.isfile(full_name):
a7cfd51
            continue
a7cfd51
        shutil.copy(full_name, os.path.join(source_dir, file))
a7cfd51
a7cfd51
def prepare_condor_tarball(build_dir, source_dir, branch):
a7cfd51
    cur_dir = os.getcwd()
a7cfd51
    tarball_dir = os.path.join(build_dir, "SOURCES")
a7cfd51
    fd = open(os.path.join(tarball_dir, "condor.tar.gz"), "w")
a7cfd51
    fdnum = fd.fileno()
a7cfd51
    try:
a7cfd51
        os.chdir(source_dir)
a7cfd51
        pid = os.fork()
a7cfd51
        if not pid:
a7cfd51
            try:
a7cfd51
                os.dup2(fdnum, 1)
a7cfd51
                os.execvp("/bin/sh", ["sh", "-c", "git archive %s | gzip -7" % branch])
a7cfd51
            finally:
a7cfd51
                os._exit(1)
a7cfd51
        else:
a7cfd51
            (pid, status) = os.waitpid(pid, 0)
a7cfd51
            if status:
a7cfd51
                raise Exception("git archive failed")
a7cfd51
    finally:
a7cfd51
        os.chdir(cur_dir)
a7cfd51
a7cfd51
def get_rpmbuild_defines(results_dir):
a7cfd51
    results_dir = os.path.abspath(results_dir)
a7cfd51
    defines = []
a7cfd51
    defines += ["--define=_topdir %s" % results_dir]
a7cfd51
    return defines
a7cfd51
a7cfd51
def parse_opts():
a7cfd51
    parser = optparse.OptionParser()
a7cfd51
    parser.add_option("-s", "--source-dir", help="Location of the Condor git repo clone.", dest="source_dir", default="~/projects/condor")
a7cfd51
    parser.add_option("-b", "--branch", help="Name of the git branch to use for the condor build.", dest="branch", default="master")
a7cfd51
a7cfd51
    opts, args = parser.parse_args()
a7cfd51
a7cfd51
    opts.source_dir = os.path.expanduser(opts.source_dir)
a7cfd51
a7cfd51
    return args, opts
a7cfd51
a7cfd51
def main():
a7cfd51
a7cfd51
    args, opts = parse_opts()
a7cfd51
a7cfd51
    if len(args) != 2:
a7cfd51
        print "Usage: hcc_make_condor <action> <directory>"
a7cfd51
        print "Valid commands are 'build', 'prep', and 'srpm'"
a7cfd51
        print "<directory> should point at the fedpkg-condor-hcc clone."
a7cfd51
        return 1
a7cfd51
a7cfd51
    build_dir = os.path.join(args[1], "_build")
a7cfd51
a7cfd51
    prep_dirs(build_dir)
a7cfd51
    defines = get_rpmbuild_defines(build_dir)
a7cfd51
    prep_source(args[1])
a7cfd51
a7cfd51
    prepare_condor_tarball(build_dir, opts.source_dir, opts.branch)
a7cfd51
a7cfd51
    if args[0] == 'build':
a7cfd51
        os.execvp("rpmbuild", ["rpmbuild"] + defines + ["-ba", "condor.spec"])
a7cfd51
    elif args[0] == 'srpm':
a7cfd51
        os.execvp("rpmbuild", ["rpmbuild"] + defines + ["-bs", "condor.spec"])
a7cfd51
    elif args[0] == "prep":
a7cfd51
        os.execvp("rpmbuild", ["rpmbuild"] + defines + ["-bp", "condor.spec"])
a7cfd51
    else:
a7cfd51
        print "Unknown action: %s" % args[0]
a7cfd51
a7cfd51
    return 1
a7cfd51
        
a7cfd51
a7cfd51
if __name__ == '__main__':
a7cfd51
    sys.exit(main())