Commit 55775645 authored by Vangelis Koukis's avatar Vangelis Koukis
Browse files

Import devflow code

Import devflow code by
Christos Stavrakakis <cstavr@grnet.gr>,
with minor modifications.
parent 1ff00aa6
*.db
*.pyc
*~
*.*.swp
bin/
share/
build/
include/
*.pt.py
*.installed.cfg
*.sqlite
.Python
.idea
.DS_Store
selenium-server-standalone-2.0b2.jar
.project
.pydevproject
.settings/
settings.d/*-local.conf
*.egg-info
dist
_build
# version module created automatically from setup.py
/devflow/version.py
# Copyright 2012 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
import git
import os
import sys
from sh import mktemp, cd, rm, git_dch, python
from optparse import OptionParser
try:
from colors import red, green
except ImportError:
red = lambda x: x
green = lambda x: x
print_red = lambda x: sys.stdout.write(red(x) + "\n")
print_green = lambda x: sys.stdout.write(green(x) + "\n")
AVAILABLE_MODES = ["release", "snapshot"]
# TODO: Make the PACKAGES tuple configurable,
# read it from autopkg.conf in the git toplevel directory.
PACKAGES = ("devflow", )
def main():
from devflow.version import __version__
parser = OptionParser(usage="usage: %prog [options] mode",
version="%prog - devflow %s" % __version__)
parser.add_option("-k", "--keep-repo",
action="store_true",
dest="keep_repo",
default=False,
help="Do not delete the cloned repository")
parser.add_option("-b", "--build-dir",
dest="build_dir",
default=None,
help="Directory to store created pacakges")
parser.add_option("-r", "--repo-dir",
dest="repo_dir",
default=None,
help="Directory to clone repository")
parser.add_option("-d", "--dirty",
dest="force_dirty",
default=False,
action="store_true",
help="Do not check if working directory is dirty")
(options, args) = parser.parse_args()
mode = args[0]
if mode not in AVAILABLE_MODES:
raise ValueError(red("Invalid argument! Mode must be one: %s"
% ", ".join(AVAILABLE_MODES)))
# Do not prompt for merge message. Required for some Git versions
os.environ["GITFLOW_BUILD_MODE"] = mode
try:
original_repo = git.Repo(".")
except git.git.InvalidGitRepositoryError:
raise RuntimeError(red("Current directory is not git repository."))
if original_repo.is_dirty() and not options.force_dirty:
toplevel = original_repo.working_dir
raise RuntimeError(red("Repository %s is dirty." % toplevel))
repo_dir = options.repo_dir
if not repo_dir:
repo_dir = mktemp("-d", "/tmp/devflow-build-repo-XXX").stdout.strip()
print_green("Created temporary directory '%s' for the cloned repo."
% repo_dir)
repo = original_repo.clone(repo_dir)
print_green("Cloned current repository to '%s'." % repo_dir)
reflog_hexsha = repo.head.log()[-1].newhexsha
print "Latest Reflog entry is %s" % reflog_hexsha
branch = repo.head.reference.name
if branch == "master":
debian_branch = "debian"
else:
debian_branch = "debian-" + branch
try:
repo.references[debian_branch]
except IndexError:
# Branch does not exist
# FIXME: remove hard-coded strings..
if branch == "debian":
repo.git.branch("--track", debian_branch, "origin/debian")
else:
repo.git.branch("--track", debian_branch, "origin/debian-develop")
repo.git.checkout(debian_branch)
print_green("Changed to branch '%s'" % debian_branch)
repo.git.merge(branch)
print_green("Merged branch '%s' into '%s'" % (branch, debian_branch))
cd(repo_dir)
version = python(repo_dir + "/devflow/version.py", "debian").strip()
print_green("The new debian version will be: '%s'" % version)
dch = git_dch("--debian-branch=%s" % debian_branch,
"--git-author",
"--ignore-regex=\".*\"",
"--multimaint-merge",
"--since=HEAD",
"--new-version=%s" % version)
print_green("Successfully ran '%s'" % " ".join(dch.cmd))
os.system("vim debian/changelog")
repo.git.add("debian/changelog")
if mode == "release":
repo.git.commit("-s", "-a", "-m", "Bump new upstream version")
if branch == "master":
repo.git.tag("debian/" + version)
for package in PACKAGES:
# python setup.py should run in its directory
cd(package)
package_dir = repo_dir + "/" + package
res = python(package_dir + "/setup.py", "sdist", _out=sys.stdout)
cd("../")
print res.stdout
# Add version.py files to repo
os.system("grep \"__version_vcs\" -r . -l -I | xargs git add -f")
build_dir = options.build_dir
if not options.build_dir:
build_dir = mktemp("-d", "/tmp/devflow-build-XXX").stdout.strip()
print_green("Created directory '%s' to store the .deb files." %
build_dir)
os.system("git-buildpackage --git-export-dir=%s --git-upstream-branch=%s"
" --git-debian-branch=%s --git-export=INDEX --git-ignore-new -sa"
% (build_dir, branch, debian_branch))
if not options.keep_repo:
print_green("Removing cloned repo '%s'." % repo_dir)
rm("-r", repo_dir)
else:
print_green("Repository dir '%s'" % repo_dir)
print_green("Completed. Version '%s', build area: '%s'"
% (version, build_dir))
if __name__ == "__main__":
sys.exit(main())
#!/usr/bin/env python
#
# Copyright 2012 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
#
#
"""Unit Tests for devflow.versioning
Provides unit tests for module devflow.versioning,
for automatic generation of version strings.
"""
import os
import unittest
from pkg_resources import parse_version
from versioning import debian_version_from_python_version
class DebianVersionObject(object):
"""Object representing a Debian Version."""
def __init__(self, pyver):
self.version = debian_version_from_python_version(pyver)
def __str__(self):
return self.version
def debian_compare_versions(a, op, b):
i = os.system("dpkg --compare-versions %s %s %s" % (a, op, b))
return i == 0
# Set ordering between DebianVersionObject objects, by adding
# debian_compare_versions
for op in ["lt", "le", "eq", "ne", "gt", "ge"]:
def gen(op):
def operator_func(self, other):
return debian_compare_versions(self.version, op, other.version)
return operator_func
setattr(DebianVersionObject, "__%s__" % op, gen(op))
def _random_commit():
import random
import string
return "".join(random.choice(string.hexdigits) for n in xrange(8)).lower()
# Add a random commit number at the end of snapshot versions
def version_with_commit(parse_func, v):
if "_" in v:
return parse_func(v + "_" + _random_commit())
else:
return parse_func(v)
V = lambda v: version_with_commit(parse_version, v)
D = lambda v: version_with_commit(DebianVersionObject, v)
class TestVersionFunctions(unittest.TestCase):
def setUp(self):
self.version_orderings = (
("0.14next", ">", "0.14"),
("0.14next", ">", "0.14rc7"),
("0.14next", "<", "0.14.1"),
("0.14rc6", "<", "0.14"),
("0.14.2rc6", ">", "0.14.1"),
("0.14next_150", "<", "0.14next"),
("0.14.1next_150", "<", "0.14.1next"),
("0.14.1_149", "<", "0.14.1"),
("0.14.1_149", "<", "0.14.1_150"),
("0.13next_102", "<", "0.13next"),
("0.13next", "<", "0.14rc5_120"),
("0.14rc3_120", "<", "0.14rc3"),
# The following test fails, but versioning.python_version
# will never try to produce such a version:
# ("0.14rc3", "<", "0.14_1"),
("0.14_120", "<", "0.14"),
("0.14", "<", "0.14next_20"),
("0.14next_20", "<", "0.14next"),
)
def test_python_versions(self):
for a, op, b in self.version_orderings:
res = compare(V, a, op, b)
self.assertTrue(res, "Python version: %s %s %s"
" is not True" % (a, op, b))
def test_debian_versions(self):
for a, op, b in self.version_orderings:
res = compare(D, a, op, b)
self.assertTrue(res, "Debian version %s %s %s"
" is not True" % (a, op, b))
def compare(function, a, op, b):
import operator
str_to_op = {"<": operator.lt,
"<=": operator.le,
"==": operator.eq,
">": operator.gt,
">=": operator.ge}
try:
return str_to_op[op](function(a), function(b))
except KeyError:
raise ValueError("Unknown operator '%s'" % op)
if __name__ == '__main__':
unittest.main()
#!/usr/bin/env python
#
# Copyright (C) 2010, 2011, 2012 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
import os
import re
import sys
import pprint
import subprocess
import git
from distutils import log
from collections import namedtuple
# Branch types:
# builds_snapshot: Whether the branch can produce snapshot builds
# builds_release: Whether the branch can produce release builds
# versioned: Whether the name of the branch defines a specific version
# allowed_version_re: A regular expression describing allowed values for
# base_version in this branch
branch_type = namedtuple("branch_type", ["builds_snapshot", "builds_release",
"versioned", "allowed_version_re"])
VERSION_RE = "[0-9]+\.[0-9]+"
BRANCH_TYPES = {
"feature": branch_type(True, False, False, "^%snext$" % VERSION_RE),
"develop": branch_type(True, False, False, "^%snext$" % VERSION_RE),
"release": branch_type(True, True, True,
"^(?P<bverstr>%s)rc[1-9][0-9]*$" % VERSION_RE),
"master": branch_type(False, True, False,
"^%s$" % VERSION_RE),
"hotfix": branch_type(True, True, True,
"^(?P<bverstr>^%s\.[1-9][0-9]*)$" % VERSION_RE)}
BASE_VERSION_FILE = "version"
def get_commit_id(commit, current_branch):
"""Return the commit ID
If the commit is a 'merge' commit, and one of the parents is a
debian branch we return a compination of the parents commits.
"""
def short_id(commit):
return commit.hexsha[0:7]
parents = commit.parents
cur_br_name = current_branch.name
if len(parents) == 1:
return short_id(commit)
elif len(parents) == 2:
if cur_br_name.startswith("debian-") or cur_br_name == "debian":
pr1, pr2 = parents
return short_id(pr1) + "~" + short_id(pr2)
else:
return short_id(commit)
else:
raise RuntimeError("Commit %s has more than 2 parents!" % commit)
def vcs_info():
"""
Return current git HEAD commit information.
Returns a tuple containing
- branch name
- commit id
- commit count
- git describe output
- path of git toplevel directory
"""
try:
repo = git.Repo(".")
branch = repo.head.reference
revid = get_commit_id(branch.commit, branch)
revno = len(list(repo.iter_commits()))
desc = repo.git.describe("--tags")
toplevel = repo.working_dir
except git.InvalidGitRepositoryError:
log.error("Could not retrieve git information. " +
"Current directory not a git repository?")
return None
info = namedtuple("vcs_info", ["branch", "revid", "revno",
"desc", "toplevel"])
return info(branch=branch.name, revid=revid, revno=revno, desc=desc,
toplevel=toplevel)
def base_version(vcs_info):
"""Determine the base version from a file in the repository"""
f = open(os.path.join(vcs_info.toplevel, BASE_VERSION_FILE))
lines = [l.strip() for l in f.readlines()]
l = [l for l in lines if not l.startswith("#")]
if len(l) != 1:
raise ValueError("File '%s' should contain a single non-comment line.")
return l[0]
def build_mode():
"""Determine the build mode from the value of $GITFLOW_BUILD_MODE"""
try:
mode = os.environ["GITFLOW_BUILD_MODE"]
assert mode == "release" or mode == "snapshot"
except (KeyError, AssertionError):
raise ValueError("GITFLOW_BUILD_MODE environment variable must be "
"'release' or 'snapshot'")
return mode
def python_version(base_version, vcs_info, mode):
"""Generate a Python distribution version following devtools conventions.
This helper generates a Python distribution version from a repository
commit, following devtools conventions. The input data are:
* base_version: a base version number, presumably stored in text file
inside the repository, e.g., /version
* vcs_info: vcs information: current branch name and revision no
* mode: "snapshot", or "release"
This helper assumes a git branching model following:
http://nvie.com/posts/a-successful-git-branching-model/
with 'master', 'develop', 'release-X', 'hotfix-X' and 'feature-X' branches.
General rules:
a) any repository commit can get as a Python version
b) a version is generated either in 'release' or in 'snapshot' mode
c) the choice of mode depends on the branch, see following table.
A python version is of the form A_NNN,
where A: X.Y.Z{,next,rcW} and NNN: a revision number for the commit,
as returned by vcs_info().
For every combination of branch and mode, releases are numbered as follows:
BRANCH: / MODE: snapshot release
-------- ------------------------------
feature 0.14next_150 N/A
develop 0.14next_151 N/A
release 0.14rc2_249 0.14rc2
master N/A 0.14
hotfix 0.14.1rc6_121 0.14.1rc6
N/A 0.14.1
The suffix 'next' in a version name is used to denote the upcoming version,
the one being under development in the develop and release branches.
Version '0.14next' is the version following 0.14, and only lives on the
develop and feature branches.
The suffix 'rc' is used to denote release candidates. 'rc' versions live
only in release and hotfix branches.
Suffixes 'next' and 'rc' have been chosen to ensure proper ordering
according to setuptools rules:
http://www.python.org/dev/peps/pep-0386/#setuptools
Every branch uses a value for A so that all releases are ordered based
on the branch they came from, so:
So
0.13next < 0.14rcW < 0.14 < 0.14next < 0.14.1
and
>>> V("0.14next") > V("0.14")
True
>>> V("0.14next") > V("0.14rc7")
True
>>> V("0.14next") > V("0.14.1")
False
>>> V("0.14rc6") > V("0.14")
False
>>> V("0.14.2rc6") > V("0.14.1")
True
The value for _NNN is chosen based of the revision number of the specific
commit. It is used to ensure ascending ordering of consecutive releases
from the same branch. Every version of the form A_NNN comes *before*
than A: All snapshots are ordered so they come before the corresponding
release.
So
0.14next_* < 0.14
0.14.1_* < 0.14.1
etc
and
>>> V("0.14next_150") < V("0.14next")
True
>>> V("0.14.1next_150") < V("0.14.1next")
True
>>> V("0.14.1_149") < V("0.14.1")
True
>>> V("0.14.1_149") < V("0.14.1_150")
True
Combining both of the above, we get
0.13next_* < 0.13next < 0.14rcW_* < 0.14rcW < 0.14_* < 0.14
< 0.14next_* < 0.14next < 0.14.1_* < 0.14.1
and
>>> V("0.13next_102") < V("0.13next")
True
>>> V("0.13next") < V("0.14rc5_120")
True
>>> V("0.14rc3_120") < V("0.14rc3")
True
>>> V("0.14rc3") < V("0.14_1")
True
>>> V("0.14_120") < V("0.14")
True
>>> V("0.14") < V("0.14next_20")
True
>>> V("0.14next_20") < V("0.14next")
True
Note: one of the tests above fails because of constraints in the way
setuptools parses version numbers. It does not affect us because the
specific version format that triggers the problem is not contained in the
table showing allowed branch / mode combinations, above.
"""
branch = vcs_info.branch
# If it's a debian branch, ignore starting "debian-"
brnorm = branch
if brnorm == "debian":
brnorm = "debian-master"
if brnorm.startswith("debian-"):
brnorm = brnorm.replace("debian-", "", 1)
# Sanity checks
if "-" in brnorm: