#!/usr/bin/env python ############################################################################## # Copyright (c) 2016 Daniel Farrell and Others. All rights reserved. # # This program and the accompanying materials are made available under the # terms of the Eclipse Public License v1.0 which accompanies this distribution, # and is available at http://www.eclipse.org/legal/epl-v10.html ############################################################################## """Build OpenDaylight's RPMs using build description and Jinja2 templates.""" import os import shutil from string import Template import subprocess import cache.cache as cache import specs.build_specs as build_specs # Common paths used in this script # This file is assumed to be in the root of the RPM build logic's dir structure project_root = os.path.dirname(os.path.abspath(__file__)) cache_dir = os.path.join(project_root, "cache") specs_dir = os.path.join(project_root, "specs") rpmbuild_dir = os.path.join(os.path.expanduser("~"), "rpmbuild") src_in_dir = os.path.join(rpmbuild_dir, "SOURCES") spec_in_dir = os.path.join(rpmbuild_dir, "SPECS") srpm_out_dir = os.path.join(rpmbuild_dir, "SRPMS") rpm_out_dir = os.path.join(rpmbuild_dir, "RPMS", "noarch") # Templates that can be specialized into common artifact names per-build odl_template = Template("opendaylight-$version_major.$version_minor." "$version_patch-$pkg_version.tar.gz") specfile_template = Template("opendaylight-$version_major.$version_minor." "$version_patch-$pkg_version.spec") unitfile_tb_template = Template("opendaylight-$sysd_commit.service.tar.gz") rpm_template = Template("opendaylight-$version_major.$version_minor." "$version_patch-$pkg_version.el7.noarch.rpm") srpm_template = Template("opendaylight-$version_major.$version_minor." "$version_patch-$pkg_version.el7.src.rpm") def build_rpm(build): """Build the RPMs described by the given build description :param build: Description of an RPM build :type build: dict """ # Specialize a series of name templates for the given build odl_tarball = odl_template.substitute(build) odl_rpm = rpm_template.substitute(build) odl_srpm = srpm_template.substitute(build) odl_specfile = specfile_template.substitute(build) unitfile_tarball = unitfile_tb_template.substitute(build) # After building strings from the name templates, build their full path odl_tarball_path = os.path.join(cache_dir, odl_tarball) unitfile_tarball_path = os.path.join(cache_dir, unitfile_tarball) specfile_path = os.path.join(specs_dir, odl_specfile) spec_in_path = os.path.join(spec_in_dir, odl_specfile) rpm_out_path = os.path.join(rpm_out_dir, odl_rpm) srpm_out_path = os.path.join(srpm_out_dir, odl_srpm) # Call a helper function to cache the artifacts required for each build cache.cache_build(build) # Call helper script to build the required RPM .spec files build_specs.build_spec(build) # Clean up old rpmbuild dir structure if it exists if os.path.isdir(rpmbuild_dir): shutil.rmtree(rpmbuild_dir) # Create rpmbuild dir structure subprocess.call("rpmdev-setuptree") # Move unitfile, tarball and specfile to correct rpmbuild dirs shutil.copy(odl_tarball_path, src_in_dir) shutil.copy(unitfile_tarball_path, src_in_dir) shutil.copy(specfile_path, spec_in_dir) # Call rpmbuild, build both SRPMs/RPMs subprocess.call(["rpmbuild", "-ba", spec_in_path]) # Copy the RPMs/SRPMs from their output dir to the cache dir shutil.copy(rpm_out_path, cache_dir) shutil.copy(srpm_out_path, cache_dir)