diff options
Diffstat (limited to 'buildscripts/packager.py')
-rwxr-xr-x | buildscripts/packager.py | 101 |
1 files changed, 46 insertions, 55 deletions
diff --git a/buildscripts/packager.py b/buildscripts/packager.py index c1da01b552a..4587182b105 100755 --- a/buildscripts/packager.py +++ b/buildscripts/packager.py @@ -248,36 +248,44 @@ class Distro(object): else: return re.sub(r'^rh(el\d).*$', r'\1', build_os) -def main(argv): +def get_args(distros): - # --distros must be "build_os" value, not actual distro name ("ubuntu1404" vs. "ubuntu") - # - distros=[Distro(distro) for distro in DISTROS] DISTRO_CHOICES=[] for distro in distros: DISTRO_CHOICES.extend(distro.build_os()) parser = argparse.ArgumentParser(description='Build MongoDB Packages') - parser.add_argument("-s", "--server-version", help="Server version to build (e.g. 2.7.8-rc0)") + parser.add_argument("-s", "--server-version", help="Server version to build (e.g. 2.7.8-rc0)", required=True) parser.add_argument("-m", "--metadata-gitspec", help="Gitspec to use for package metadata files", required=False) parser.add_argument("-r", "--release-number", help="RPM release number base", type=int, required=False) parser.add_argument("-d", "--distros", help="Distros to build for", choices=DISTRO_CHOICES, required=False, default=[], action='append') + parser.add_argument("-p", "--prefix", help="Directory to build into", required=False) parser.add_argument("-a", "--arches", help="Architecture to build", choices=DEFAULT_ARCHES, default=DEFAULT_ARCHES, required=False, action='append') parser.add_argument("-t", "--tarball", help="Local tarball to package instead of downloading (only valid with one distro/arch combination)", required=False, type=lambda x: is_valid_file(parser, x)) + args = parser.parse_args() if len(args.distros) * len(args.arches) > 1 and args.tarball: parser.error("Can only specify local tarball with one distro/arch combination") + return args + +def main(argv): + + distros=[Distro(distro) for distro in DISTROS] + + args = get_args(distros) spec = Spec(args.server_version, args.metadata_gitspec, args.release_number) oldcwd=os.getcwd() srcdir=oldcwd+"/../" - # We do all our work in a randomly-created directory. You can set - # TEMPDIR to influence where this program will do stuff. - prefix=tempfile.mkdtemp() + # Where to do all of our work. Use a randomly-created directory if one + # is not passed in. + prefix = args.prefix + if prefix is None: + prefix = tempfile.mkdtemp() print "Working in directory %s" % prefix os.chdir(prefix) @@ -360,11 +368,8 @@ def httpget(url, filename): resp, content = h.request(url, "GET") t=filename+'.TMP' if resp.status==200: - f = open(t, 'w') - try: + with open(t, 'w') as f: f.write(content) - finally: - f.close() else: raise Exception("HTTP error %d" % resp.status) os.rename(t, filename) @@ -463,7 +468,7 @@ def make_deb(distro, build_os, arch, spec, srcdir): oldcwd=os.getcwd() try: os.chdir(sdir) - sysassert(["dpkg-buildpackage", "-a"+distro_arch, "-k Richard Kreuter <richard@10gen.com>"]) + sysassert(["dpkg-buildpackage", "-uc", "-us", "-a" + distro_arch]) finally: os.chdir(oldcwd) r=distro.repodir(arch, build_os, spec) @@ -482,17 +487,11 @@ def make_deb_repo(repo, distro, build_os, spec): dirs=set([os.path.dirname(deb)[2:] for deb in backtick(["find", ".", "-name", "*.deb"]).split()]) for d in dirs: s=backtick(["dpkg-scanpackages", d, "/dev/null"]) - f=open(d+"/Packages", "w") - try: + with open(d+"/Packages", "w") as f: f.write(s) - finally: - f.close() b=backtick(["gzip", "-9c", d+"/Packages"]) - f=open(d+"/Packages.gz", "wb") - try: + with open(d+"/Packages.gz", "wb") as f: f.write(b) - finally: - f.close() finally: os.chdir(oldpwd) # Notes: the Release{,.gpg} files must live in a special place, @@ -514,22 +513,9 @@ Description: MongoDB packages os.chdir(repo+"../../") s2=backtick(["apt-ftparchive", "release", "."]) try: - f=open("Release", 'w') - try: + with open("Release", 'w') as f: f.write(s) f.write(s2) - finally: - f.close() - - arg=None - for line in backtick(["gpg", "--list-keys"]).split("\n"): - tokens=line.split() - if len(tokens)>0 and tokens[0] == "uid": - arg=tokens[-1] - break - # Note: for some reason, I think --no-tty might be needed - # here, but maybe not. - sysassert(["gpg", "-r", arg, "--no-secmem-warning", "-abs", "--output", "Release.gpg", "Release"]) finally: os.chdir(oldpwd) @@ -606,7 +592,6 @@ def write_debian_changelog(path, spec, srcdir): s=preamble+backtick(["sh", "-c", "git archive %s debian/changelog | tar xOf -" % spec.metadata_gitspec()]) finally: os.chdir(oldcwd) - f=open(path, 'w') lines=s.split("\n") # If the first line starts with "mongodb", it's not a revision # preamble, and so frob the version number. @@ -615,10 +600,8 @@ def write_debian_changelog(path, spec, srcdir): lines=[re.sub("^mongodb ", "mongodb%s " % (spec.suffix()), l) for l in lines] lines=[re.sub("^ --", " --", l) for l in lines] s="\n".join(lines) - try: + with open(path, 'w') as f: f.write(s) - finally: - f.close() def make_rpm(distro, build_os, arch, spec, srcdir): # Create the specfile. @@ -650,8 +633,11 @@ def make_rpm(distro, build_os, arch, spec, srcdir): # that "macrofiles" setting doesn't do anything for newer RPM # versions, where you have to use the --macros flag instead. And # all of this is to let us do our work with some guarantee that - # we're not clobbering anything that doesn't belong to us. Why is - # RPM so braindamaged? + # we're not clobbering anything that doesn't belong to us. + # + # On RHEL systems, --rcfile will generally be used and + # --macros will be used in Ubuntu. + # macrofiles=[l for l in backtick(["rpm", "--showrc"]).split("\n") if l.startswith("macrofiles")] flags=[] macropath=os.getcwd()+"/macros" @@ -661,16 +647,24 @@ def make_rpm(distro, build_os, arch, spec, srcdir): macrofiles=macrofiles[0]+":"+macropath rcfile=os.getcwd()+"/rpmrc" write_rpmrc_file(rcfile, macrofiles) - flags=["--rpmrc", rcfile] + flags=["--rcfile", rcfile] else: # This hard-coded hooey came from some box running RPM # 4.4.2.3. It may not work over time, but RPM isn't sanely # configurable. flags=["--macros", "/usr/lib/rpm/macros:/usr/lib/rpm/%s-linux/macros:/etc/rpm/macros.*:/etc/rpm/macros:/etc/rpm/%s-linux/macros:~/.rpmmacros:%s" % (distro_arch, distro_arch, macropath)] # Put the specfile and the tar'd up binaries and stuff in - # place. FIXME: see if shutil.copyfile can do this without too - # much hassle. - sysassert(["cp", "-v", specfile, topdir+"SPECS/"]) + # place. + # + # The version of rpm and rpm tools in RHEL 5.5 can't interpolate the + # %{dynamic_version} macro, so do it manually + with open(specfile, "r") as spec_source: + with open(topdir+"SPECS/" + os.path.basename(specfile), "w") as spec_dest: + for line in spec_source: + line = line.replace('%{dynamic_version}', spec.pversion(distro)) + line = line.replace('%{dynamic_release}', spec.prelease()) + spec_dest.write(line) + oldcwd=os.getcwd() os.chdir(sdir+"/../") try: @@ -678,7 +672,8 @@ def make_rpm(distro, build_os, arch, spec, srcdir): finally: os.chdir(oldcwd) # Do the build. - flags.extend(["-D", "dynamic_version " + spec.pversion(distro), "-D", "dynamic_release " + spec.prelease()]) + + flags.extend(["-D", "dynamic_version " + spec.pversion(distro), "-D", "dynamic_release " + spec.prelease(), "-D", "_topdir " + topdir]) sysassert(["rpmbuild", "-ba", "--target", distro_arch] + flags + ["%s/SPECS/mongodb%s.spec" % (topdir, suffix)]) r=distro.repodir(arch, build_os, spec) ensure_dir(r) @@ -697,20 +692,14 @@ def make_rpm_repo(repo): def write_rpmrc_file(path, string): - f=open(path, 'w') - try: + with open(path, 'w') as f: f.write(string) - finally: - f.close() def write_rpm_macros_file(path, topdir, release_dist): - f=open(path, 'w') - try: + with open(path, 'w') as f: f.write("%%_topdir %s\n" % topdir) f.write("%%dist .%s\n" % release_dist) f.write("%_use_internal_dependency_generator 0\n") - finally: - f.close() def ensure_dir(filename): """Make sure that the directory that's the dirname part of @@ -729,9 +718,11 @@ def ensure_dir(filename): def is_valid_file(parser, filename): """Check if file exists, and return the filename""" if not os.path.exists(filename): - parser.error("The file %s does not exist!" % arg) + parser.error("The file %s does not exist!" % filename) else: return filename if __name__ == "__main__": main(sys.argv) + + |