Skip to content

Commit

Permalink
Merge pull request #12684 from stevekuznetsov/skuznets/refactor-tito
Browse files Browse the repository at this point in the history
Merged by openshift-bot
  • Loading branch information
OpenShift Bot committed Jan 31, 2017
2 parents 805d0d9 + ed01c7b commit 488e6f1
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 298 deletions.
93 changes: 11 additions & 82 deletions .tito/lib/origin/builder/__init__.py
@@ -1,33 +1,26 @@
"""
Code for building Origin
"""

import sys
import json

from tito.common import (
get_latest_commit,
get_latest_tagged_version,
check_tag_exists,
run_command,
find_spec_file,
get_spec_version_and_release,
munge_specfile
)

from tito.common import get_latest_commit, run_command
from tito.builder import Builder

from ..common import inject_os_git_vars

class OriginBuilder(Builder):
"""
builder which defines 'commit' as the git hash prior to building
Used For:
- Packages that want to know the commit in all situations
"""
def _get_tag_for_version(self, version):
return "v{}".format(version)

def _get_rpmbuild_dir_options(self):
git_hash = get_latest_commit()
cmd = '. ./hack/common.sh ; OS_ROOT=$(pwd) ; echo $(os::build::ldflags)'
cmd = 'source ./hack/lib/init.sh; os::build::ldflags'
ldflags = run_command("bash -c '{0}'".format(cmd))

return ('--define "_topdir %s" --define "_sourcedir %s" --define "_builddir %s" '
Expand All @@ -40,46 +33,12 @@ def _get_rpmbuild_dir_options(self):

def _setup_test_specfile(self):
if self.test and not self.ran_setup_test_specfile:
# If making a test rpm we need to get a little crazy with the spec
# file we're building off. (note that this is a temp copy of the
# spec) Swap out the actual release for one that includes the git
# SHA1 we're building for our test package:
sha = self.git_commit_id[:7]
fullname = "{0}-{1}".format(self.project_name, self.display_version)
munge_specfile(
self.spec_file,
sha,
self.commit_count,
fullname,
self.tgz_filename,
)
# Custom Openshift v3 stuff follows, everything above is the standard
# builder
super(OriginBuilder, self)._setup_test_specfile()

## Fixup os_git_vars
cmd = '. ./hack/common.sh ; OS_ROOT=$(pwd) ; os::build::os_version_vars ; echo ${OS_GIT_COMMIT}'
os_git_commit = run_command("bash -c '{0}'".format(cmd))
cmd = '. ./hack/common.sh ; OS_ROOT=$(pwd) ; os::build::os_version_vars ; echo ${OS_GIT_VERSION}'
os_git_version = run_command("bash -c '{0}'".format(cmd))
os_git_version = os_git_version.replace('-dirty', '')
cmd = '. ./hack/common.sh ; OS_ROOT=$(pwd) ; os::build::os_version_vars ; echo ${OS_GIT_MAJOR}'
os_git_major = run_command("bash -c '{0}'".format(cmd))
cmd = '. ./hack/common.sh ; OS_ROOT=$(pwd) ; os::build::os_version_vars ; echo ${OS_GIT_MINOR}'
os_git_minor = run_command("bash -c '{0}'".format(cmd))
print("OS_GIT_COMMIT::{0}".format(os_git_commit))
print("OS_GIT_VERSION::{0}".format(os_git_version))
print("OS_GIT_MAJOR::{0}".format(os_git_major))
print("OS_GIT_MINOR::{0}".format(os_git_minor))
update_os_git_vars = \
"sed -i 's|^%global os_git_vars .*$|%global os_git_vars OS_GIT_TREE_STATE='clean' OS_GIT_VERSION={0} OS_GIT_COMMIT={1} OS_GIT_MAJOR={2} OS_GIT_MINOR={3}|' {4}".format(
os_git_version,
os_git_commit,
os_git_major,
os_git_minor,
self.spec_file
)
output = run_command(update_os_git_vars)
inject_os_git_vars(self.spec_file)
self._inject_bundled_deps()

def _inject_bundled_deps(self):
# Add bundled deps for Fedora Guidelines as per:
# https://fedoraproject.org/wiki/Packaging:Guidelines#Bundling_and_Duplication_of_system_libraries
provides_list = []
Expand Down Expand Up @@ -113,34 +72,4 @@ def _setup_test_specfile(self):
else:
spec_file_f.write(line)

self.build_version += ".git." + \
str(self.commit_count) + \
"." + \
str(self.git_commit_id[:7])
self.ran_setup_test_specfile = True

def _get_build_version(self):
"""
Figure out the git tag and version-release we're building.
"""
# Determine which package version we should build:
build_version = None
if self.build_tag:
build_version = self.build_tag[len(self.project_name + "-"):]
else:
build_version = get_latest_tagged_version(self.project_name)
if build_version is None:
if not self.test:
error_out(["Unable to lookup latest package info.",
"Perhaps you need to tag first?"])
sys.stderr.write("WARNING: unable to lookup latest package "
"tag, building untagged test project\n")
build_version = get_spec_version_and_release(self.start_dir,
find_spec_file(in_dir=self.start_dir))
self.build_tag = "v{0}".format(build_version)

if not self.test:
check_tag_exists(self.build_tag, offline=self.offline)
return build_version

# vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=python:textwidth=0:
# vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=python:textwidth=0:
60 changes: 60 additions & 0 deletions .tito/lib/origin/common.py
@@ -0,0 +1,60 @@
from tito.common import run_command, get_latest_commit

def inject_os_git_vars(spec_file):
"""
Determine the OpenShift version variables as dictated by the Origin
shell utilities and overwrite the specfile to reflect them. A line
with the following syntax is expected in the specfile:
%global os_git_vars
This line will be overwritten to add the git tree state, the full
"git version", the last source commit in the release, and the major
and minor versions of the current product release.
"""
os_git_vars = get_os_git_vars()
for var_name in os_git_vars:
print("{}::{}".format(var_name, os_git_vars[var_name]))

update_os_git_vars = \
"sed -i 's|^%global os_git_vars .*$|%global os_git_vars {}|' {}".format(
" ".join(["{}={}".format(key, value) for key, value in os_git_vars.items()]),
spec_file
)
output = run_command(update_os_git_vars)

def get_os_git_vars():
"""
Determine the OpenShift version variables as dictated by the Origin
shell utilities. The git tree state is spoofed.
"""
git_vars = {}
for var in ["COMMIT", "VERSION", "MAJOR", "MINOR"]:
var_name = "OS_GIT_{}".format(var)
git_vars[var_name] = run_command(
"bash -c 'source ./hack/lib/init.sh; os::build::os_version_vars; echo ${}'".format(var_name)
)

# we hard-code this to a clean state as tito will have dirtied up the tree
# but that will not have changed any of the source used for the product
# release and we therefore don't want that reflected in the release version
git_vars["OS_GIT_TREE_STATE"] = "clean"
git_vars["OS_GIT_VERSION"] = git_vars["OS_GIT_VERSION"].replace("-dirty", "")
return git_vars

def update_global_hash(spec_file):
"""
Update the specfile to reflect the latest commit. A line
with the following syntax is expected in the specfile:
%global commit
This line will be overwritten to add the git commit.
"""
git_hash = get_latest_commit()
update_commit = \
"sed -i 's/^%global commit .*$/%global commit {0}/' {1}".format(
git_hash,
spec_file
)
output = run_command(update_commit)

0 comments on commit 488e6f1

Please sign in to comment.