#!/usr/bin/python
-# Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+# Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
import datetime
import subprocess
import re
+import copy
+import inspect
+
+TEMPORARY_DIRECTORY = '/tmp'
+
+class Error(Exception):
+ def __init__(self, value):
+ self.value = value
+ def __str__(self):
+ return '\x1b[31m%s\x1b[0m' % repr(self.value)
+ def __repr__(self):
+ return str(self)
#
# Configuration
#
+class Option(object):
+ def __init__(self, key):
+ self.key = key
+ self.value = None
+
+ def offer(self, key, value):
+ if key == self.key:
+ self.value = value
+
+class BoolOption(object):
+ def __init__(self, key):
+ self.key = key
+ self.value = False
+
+ def offer(self, key, value):
+ if key == self.key:
+ self.value = (value == 'yes' or value == '1' or value == 'true')
+
class Config:
def __init__(self):
- self.keys = ['linux_dir_in_chroot',
- 'linux_chroot_prefix',
- 'windows_environment_prefix',
- 'mingw_prefix',
- 'git_prefix',
- 'osx_build_host',
- 'osx_dir_in_host',
- 'osx_environment_prefix',
- 'osx_sdk_prefix',
- 'osx_sdk']
-
- self.dict = dict()
+ self.options = [ Option('linux_chroot_prefix'),
+ Option('windows_environment_prefix'),
+ Option('mingw_prefix'),
+ Option('git_prefix'),
+ Option('osx_build_host'),
+ Option('osx_environment_prefix'),
+ Option('osx_sdk_prefix'),
+ Option('osx_sdk') ]
try:
f = open('%s/.config/cdist' % os.path.expanduser('~'), 'r')
s = l.strip().split()
if len(s) == 2:
- for k in self.keys:
- if k == s[0]:
- self.dict[k] = s[1]
+ for k in self.options:
+ k.offer(s[0], s[1])
except:
raise
def get(self, k):
- if k in self.dict:
- return self.dict[k]
+ for o in self.options:
+ if o.key == k:
+ return o.value
- print >>sys.stderr,'Required setting %s not found' % k
- sys.exit(1)
+ raise Error('Required setting %s not found' % k)
config = Config()
if not args.quiet:
print '\x1b[33m* %s\x1b[0m' % m
-def error(e):
- print '\x1b[31mError: %s\x1b[0m' % e
- sys.exit(1)
-
def copytree(a, b):
log('copy %s -> %s' % (a, b))
shutil.copytree(a, b)
log(c)
r = os.system(c)
if (r >> 8) and not can_fail:
- error('command %s failed' % c)
+ raise Error('command %s failed' % c)
def command_and_read(c):
log(c)
class Version:
def __init__(self, s):
- self.pre = False
- self.beta = None
+ self.devel = False
if s.startswith("'"):
s = s[1:]
if s.endswith("'"):
s = s[0:-1]
+ if s.endswith('devel'):
+ s = s[0:-5]
+ self.devel = True
+
if s.endswith('pre'):
s = s[0:-3]
- self.pre = True
-
- b = s.find("beta")
- if b != -1:
- self.beta = int(s[b+4:])
- s = s[0:b]
p = s.split('.')
self.major = int(p[0])
self.minor = int(p[1])
+ if len(p) == 3:
+ self.micro = int(p[2])
+ else:
+ self.micro = 0
- def bump(self):
+ def bump_minor(self):
self.minor += 1
- self.pre = False
- self.beta = None
+ self.micro = 0
- def to_pre(self):
- self.pre = True
- self.beta = None
+ def bump_micro(self):
+ self.micro += 1
- def bump_and_to_pre(self):
- self.bump()
- self.pre = True
- self.beta = None
+ def to_devel(self):
+ self.devel = True
def to_release(self):
- self.pre = False
- self.beta = None
-
- def bump_beta(self):
- if self.pre:
- self.pre = False
- self.beta = 1
- elif self.beta is not None:
- self.beta += 1
- elif self.beta is None:
- self.beta = 1
+ self.devel = False
def __str__(self):
- s = '%d.%02d' % (self.major, self.minor)
- if self.beta is not None:
- s += 'beta%d' % self.beta
- elif self.pre:
- s += 'pre'
+ s = '%d.%d.%d' % (self.major, self.minor, self.micro)
+ if self.devel:
+ s += 'devel'
return s
#
class Target(object):
- def __init__(self, platform, parallel):
+ # @param directory directory to work in; if None we will use a temporary directory
+ # Temporary directories will be removed after use; specified directories will not
+ def __init__(self, platform, parallel, directory=None):
self.platform = platform
self.parallel = parallel
+
+ if directory is None:
+ self.directory = tempfile.mkdtemp('', 'tmp', TEMPORARY_DIRECTORY)
+ self.rmdir = True
+ else:
+ self.directory = directory
+ self.rmdir = False
+
+ print 'Working in %s' % self.directory
+
# Environment variables that we will use when we call cscripts
self.variables = {}
self.debug = False
def build_dependencies(self, project):
cwd = os.getcwd()
if 'dependencies' in project.cscript:
- print project.cscript['dependencies'](self)
for d in project.cscript['dependencies'](self):
log('Building dependency %s %s of %s' % (d[0], d[1], project.name))
dep = Project(d[0], '.', d[1])
# Make the options to pass in from the option_defaults of the thing
# we are building and any options specified by the parent.
- # The presence of option_defaults() is taken to mean that this
- # cscript understands and expects options
options = {}
if 'option_defaults' in dep.cscript:
options = dep.cscript['option_defaults']()
for k, v in d[2].iteritems():
options[k] = v
- self.build(dep, options)
- else:
- # Backwards compatibility
- self.build(dep)
+ self.build(dep, options)
os.chdir(cwd)
def build(self, project, options=None):
- if options is not None:
+ variables = copy.copy(self.variables)
+ if len(inspect.getargspec(project.cscript['build']).args) == 2:
project.cscript['build'](self, options)
else:
project.cscript['build'](self)
+ self.variables = variables
def package(self, project):
project.checkout(self)
self.build_dependencies(project)
- project.cscript['build'](self)
+ self.build(project)
return project.cscript['package'](self, project.version)
def test(self, project):
project.checkout(self)
self.build_dependencies(project)
- project.cscript['build'](self)
+ self.build(project)
project.cscript['test'](self)
def set(self, a, b):
def get(self, a):
return self.variables[a]
+ def append_with_space(self, k, v):
+ if not k in self.variables:
+ self.variables[k] = v
+ else:
+ self.variables[k] = '%s %s' % (self.variables[k], v)
+
def variables_string(self, escaped_quotes=False):
e = ''
for k, v in self.variables.iteritems():
return e
def cleanup(self):
- pass
+ if self.rmdir:
+ rmtree(self.directory)
#
# Windows
#
class WindowsTarget(Target):
- # @param directory directory to work in; if None, we will use a temporary directory
def __init__(self, bits, directory=None):
- super(WindowsTarget, self).__init__('windows', 2)
+ super(WindowsTarget, self).__init__('windows', 2, directory)
self.bits = bits
- if directory is None:
- self.directory = tempfile.mkdtemp()
- self.rmdir = True
- else:
- self.directory = directory
- self.rmdir = False
-
+
self.windows_prefix = '%s/%d' % (config.get('windows_environment_prefix'), self.bits)
if not os.path.exists(self.windows_prefix):
- error('windows prefix %s does not exist' % self.windows_prefix)
+ raise Error('windows prefix %s does not exist' % self.windows_prefix)
if self.bits == 32:
self.mingw_name = 'i686'
self.mingw_prefixes = ['/%s/%d' % (config.get('mingw_prefix'), self.bits), '%s/%d/%s-w64-mingw32' % (config.get('mingw_prefix'), bits, self.mingw_name)]
self.set('PKG_CONFIG_LIBDIR', '%s/lib/pkgconfig' % self.windows_prefix)
- self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/bin/pkgconfig' % (self.work_dir_cscript(), self.work_dir_cscript()))
+ self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/bin/pkgconfig' % (self.directory, self.directory))
self.set('PATH', '%s/bin:%s:%s' % (self.windows_prefix, mingw_path, os.environ['PATH']))
self.set('CC', '%s-w64-mingw32-gcc' % self.mingw_name)
self.set('CXX', '%s-w64-mingw32-g++' % self.mingw_name)
self.set('LD', '%s-w64-mingw32-ld' % self.mingw_name)
self.set('RANLIB', '%s-w64-mingw32-ranlib' % self.mingw_name)
self.set('WINRC', '%s-w64-mingw32-windres' % self.mingw_name)
- cxx = '-I%s/include -I%s/include' % (self.windows_prefix, self.work_dir_cscript())
- link = '-L%s/lib -L%s/lib' % (self.windows_prefix, self.work_dir_cscript())
+ cxx = '-I%s/include -I%s/include' % (self.windows_prefix, self.directory)
+ link = '-L%s/lib -L%s/lib' % (self.windows_prefix, self.directory)
for p in self.mingw_prefixes:
cxx += ' -I%s/include' % p
link += ' -L%s/lib' % p
self.set('CXXFLAGS', '"%s"' % cxx)
self.set('LINKFLAGS', '"%s"' % link)
- def work_dir_cdist(self):
- return '%s/%d' % (self.directory, self.bits)
-
- def work_dir_cscript(self):
- return '%s/%d' % (self.directory, self.bits)
-
def command(self, c):
log('host -> %s' % c)
command('%s %s' % (self.variables_string(), c))
- def cleanup(self):
- if self.rmdir:
- rmtree(self.directory)
-
#
# Linux
#
class LinuxTarget(Target):
def __init__(self, distro, version, bits, directory=None):
- "directory -- directory to work in; if None, we will use the configured linux_dir_in_chroot"
- super(LinuxTarget, self).__init__('linux', 2)
+ super(LinuxTarget, self).__init__('linux', 2, directory)
self.distro = distro
self.version = version
self.bits = bits
+ # e.g. ubuntu-14.04-64
self.chroot = '%s-%s-%d' % (self.distro, self.version, self.bits)
- if directory is None:
- self.dir_in_chroot = config.get('linux_dir_in_chroot')
- else:
- self.dir_in_chroot = directory
-
- for g in glob.glob('%s/*' % self.work_dir_cdist()):
- rmtree(g)
+ # e.g. /home/carl/Environments/ubuntu-14.04-64
+ self.chroot_prefix = '%s/%s' % (config.get('linux_chroot_prefix'), self.chroot)
- self.set('CXXFLAGS', '-I%s/include' % self.work_dir_cscript())
- self.set('LINKFLAGS', '-L%s/lib' % self.work_dir_cscript())
- self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig' % self.work_dir_cscript())
+ self.set('CXXFLAGS', '-I%s/include' % self.directory)
+ self.set('LINKFLAGS', '-L%s/lib' % self.directory)
+ self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:/usr/local/lib/pkgconfig' % self.directory)
self.set('PATH', '%s:/usr/local/bin' % (os.environ['PATH']))
- def work_dir_cdist(self):
- return '%s/%s%s' % (config.get('linux_chroot_prefix'), self.chroot, self.dir_in_chroot)
-
- def work_dir_cscript(self):
- return self.dir_in_chroot
-
def command(self, c):
- # Work out the cwd for the chrooted command
- cwd = os.getcwd()
- prefix = '%s/%s' % (config.get('linux_chroot_prefix'), self.chroot)
- assert(cwd.startswith(prefix))
- cwd = cwd[len(prefix):]
-
- log('schroot [%s] -> %s' % (cwd, c))
- command('%s schroot -c %s -d %s -p -- %s' % (self.variables_string(), self.chroot, cwd, c))
-
- def cleanup(self):
- for g in glob.glob('%s/*' % self.work_dir_cdist()):
- rmtree(g)
+ command('%s schroot -c %s -p -- %s' % (self.variables_string(), self.chroot, c))
#
# OS X
class OSXTarget(Target):
def __init__(self, directory=None):
- "directory -- directory to work in; if None, we will use the configured osx_dir_in_host"
- super(OSXTarget, self).__init__('osx', 4)
-
- if directory is None:
- self.dir_in_host = config.get('osx_dir_in_host')
- else:
- self.dir_in_host = directory
-
- for g in glob.glob('%s/*' % self.dir_in_host):
- rmtree(g)
+ super(OSXTarget, self).__init__('osx', 4, directory)
def command(self, c):
command('%s %s' % (self.variables_string(False), c))
enviro = '%s/%d' % (config.get('osx_environment_prefix'), bits)
# Environment variables
- self.set('CFLAGS', '"-I%s/include -I%s/include %s"' % (self.work_dir_cscript(), enviro, flags))
- self.set('CXXFLAGS', '"-I%s/include -I%s/include %s"' % (self.work_dir_cscript(), enviro, flags))
- self.set('LDFLAGS', '"-L%s/lib -L%s/lib %s"' % (self.work_dir_cscript(), enviro, flags))
- self.set('LINKFLAGS', '"-L%s/lib -L%s/lib %s"' % (self.work_dir_cscript(), enviro, flags))
- self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/lib/pkgconfig:/usr/lib/pkgconfig' % (self.work_dir_cscript(), enviro))
+ self.set('CFLAGS', '"-I%s/include -I%s/include %s"' % (self.directory, enviro, flags))
+ self.set('CXXFLAGS', '"-I%s/include -I%s/include %s"' % (self.directory, enviro, flags))
+ self.set('LDFLAGS', '"-L%s/lib -L%s/lib %s"' % (self.directory, enviro, flags))
+ self.set('LINKFLAGS', '"-L%s/lib -L%s/lib %s"' % (self.directory, enviro, flags))
+ self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/lib/pkgconfig:/usr/lib/pkgconfig' % (self.directory, enviro))
self.set('PATH', '$PATH:/usr/bin:/sbin:/usr/local/bin:%s/bin' % enviro)
self.set('MACOSX_DEPLOYMENT_TARGET', config.get('osx_sdk'))
- def work_dir_cdist(self):
- return self.work_dir_cscript()
-
- def work_dir_cscript(self):
- return '%s/%d' % (self.dir_in_host, self.bits)
-
def package(self, project):
- error('cannot package non-universal OS X versions')
+ raise Error('cannot package non-universal OS X versions')
class OSXUniversalTarget(OSXTarget):
def __init__(self, directory=None):
super(OSXUniversalTarget, self).__init__(directory)
self.parts = []
- self.parts.append(OSXSingleTarget(32, directory))
- self.parts.append(OSXSingleTarget(64, directory))
-
- def work_dir_cscript(self):
- return self.dir_in_host
+ self.parts.append(OSXSingleTarget(32, os.path.join(self.directory, '32')))
+ self.parts.append(OSXSingleTarget(64, os.path.join(self.directory, '64')))
def package(self, project):
for p in self.parts:
class SourceTarget(Target):
def __init__(self):
super(SourceTarget, self).__init__('source', 2)
- self.directory = tempfile.mkdtemp()
-
- def work_dir_cdist(self):
- return self.directory
-
- def work_dir_cscript(self):
- return self.directory
def command(self, c):
log('host -> %s' % c)
# windows-{32,64}
# or ubuntu-version-{32,64}
# or debian-version-{32,64}
+# or centos-version-{32,64}
# or osx-{32,64}
# or source
# @param debug True to build with debugging symbols (where possible)
target = None
if s.startswith('windows-'):
target = WindowsTarget(int(s.split('-')[1]), work)
- elif s.startswith('ubuntu-') or s.startswith('debian-'):
+ elif s.startswith('ubuntu-') or s.startswith('debian-') or s.startswith('centos-'):
p = s.split('-')
if len(p) != 3:
print >>sys.stderr,"Bad Linux target name `%s'; must be something like ubuntu-12.04-32 (i.e. distro-version-bits)" % s
self.directory = directory
self.version = None
self.specifier = specifier
+ self.git_commit = None
if self.specifier is None:
self.specifier = 'master'
if args.quiet:
flags = '-q'
redirect = '>/dev/null'
- command('git clone %s %s/%s.git %s/src/%s' % (flags, config.get('git_prefix'), self.name, target.work_dir_cdist(), self.name))
- os.chdir('%s/src/%s' % (target.work_dir_cdist(), self.name))
+ command('git clone %s %s/%s.git %s/src/%s' % (flags, config.get('git_prefix'), self.name, target.directory, self.name))
+ os.chdir('%s/src/%s' % (target.directory, self.name))
command('git checkout %s %s %s' % (flags, self.specifier, redirect))
- command('git submodule init')
- command('git submodule update')
+ self.git_commit = command_and_read('git rev-parse --short=7 HEAD').readline().strip()
+ command('git submodule init --quiet')
+ command('git submodule update --quiet')
os.chdir(self.directory)
- proj = '%s/src/%s/%s' % (target.work_dir_cdist(), self.name, self.directory)
+ proj = '%s/src/%s/%s' % (target.directory, self.name, self.directory)
self.read_cscript('%s/cscript' % proj)
command('dch -b -v %s-1 "New upstream release."' % version)
+def devel_to_git(project, filename):
+ if project.git_commit is not None:
+ filename = filename.replace('devel', '-%s' % project.git_commit)
+ return filename
+
#
# Command-line parser
#
parser.add_argument('command')
parser.add_argument('-p', '--project', help='project name')
parser.add_argument('-d', '--directory', help='directory within project repo', default='.')
-parser.add_argument('--beta', help='beta release', action='store_true')
-parser.add_argument('--full', help='full release', action='store_true')
+parser.add_argument('--minor', help='minor version number bump', action='store_true')
+parser.add_argument('--micro', help='micro version number bump', action='store_true')
parser.add_argument('-c', '--checkout', help='string to pass to git for checkout')
parser.add_argument('-o', '--output', help='output directory', default='.')
parser.add_argument('-q', '--quiet', help='be quiet', action='store_true')
args.work = os.path.abspath(args.work)
if args.project is None and args.command != 'shell':
- error('you must specify -p or --project')
+ raise Error('you must specify -p or --project')
project = Project(args.project, args.directory, args.checkout)
if args.command == 'build':
if args.target is None:
- error('you must specify -t or --target')
+ raise Error('you must specify -t or --target')
target = target_factory(args.target, args.debug, args.work)
project.checkout(target)
elif args.command == 'package':
if args.target is None:
- error('you must specify -t or --target')
+ raise Error('you must specify -t or --target')
target = target_factory(args.target, args.debug, args.work)
packages = [packages]
if target.platform == 'linux':
- out = '%s/%s-%d' % (args.output, target.version, target.bits)
+ out = '%s/%s-%s-%d' % (args.output, target.distro, target.version, target.bits)
try:
os.makedirs(out)
except:
pass
for p in packages:
- copyfile(p, '%s/%s' % (out, os.path.basename(p)))
+ copyfile(p, '%s/%s' % (out, os.path.basename(devel_to_git(project, p))))
else:
for p in packages:
- copyfile(p, '%s/%s' % (args.output, os.path.basename(p)))
+ copyfile(p, '%s/%s' % (args.output, os.path.basename(devel_to_git(project, p))))
- target.cleanup()
+ if not args.keep:
+ target.cleanup()
elif args.command == 'release':
- if args.full is False and args.beta is False:
- error('you must specify --full or --beta')
+ if args.minor is False and args.micro is False:
+ raise Error('you must specify --minor or --micro')
target = SourceTarget()
project.checkout(target)
version = project.version
- if args.full:
- version.to_release()
+ version.to_release()
+ if args.minor:
+ version.bump_minor()
else:
- version.bump_beta()
+ version.bump_micro()
set_version_in_wscript(version)
append_version_to_changelog(version)
command('git commit -a -m "Bump version"')
command('git tag -m "v%s" v%s' % (version, version))
- if args.full:
- version.bump_and_to_pre()
- set_version_in_wscript(version)
- command('git commit -a -m "Bump version"')
-
+ version.to_devel()
+ set_version_in_wscript(version)
+ command('git commit -a -m "Bump version"')
command('git push')
command('git push --tags')
if len(l) > 0 and l[0] == "\t":
s = l.split()
if len(s) == 4 and s[1] == "Version" and s[3] == "released.":
- if not "beta" in s[2]:
+ v = Version(s[2])
+ if v.micro == 0:
if last is not None and len(changes) > 0:
print >>html,"<h2>Changes between version %s and %s</h2>" % (s[2], last)
print >>html,"<ul>"
if m:
tags = m.group(1).split(', ')
for t in tags:
+ s = t.split()
+ if len(s) > 1:
+ t = s[1]
if len(t) > 0 and t[0] == 'v':
latest = t[1:]
elif args.command == 'test':
if args.target is None:
- error('you must specify -t or --target')
+ raise Error('you must specify -t or --target')
- target = target_factory(args.target, args.debug, args.work)
- target.test(project)
+ target = None
+ try:
+ target = target_factory(args.target, args.debug, args.work)
+ target.test(project)
+ except Error as e:
+ if target is not None:
+ target.cleanup()
+ raise
+
+ if target is not None:
+ target.cleanup()
elif args.command == 'shell':
if args.target is None:
- error('you must specify -t or --target')
+ raise Error('you must specify -t or --target')
target = target_factory(args.target, args.debug, args.work)
target.command('bash')
else:
- error('invalid command %s' % args.command)
+ raise Error('invalid command %s' % args.command)