release: Revamp to use subprocess instead of os.system

Signed-off-by: James McCoy <jamessan@users.sourceforge.net>
This commit is contained in:
James McCoy 2012-06-17 10:30:17 -04:00
parent fefacf5b2f
commit e6d361fc92

View File

@ -4,6 +4,7 @@ import os
import re import re
import sys import sys
import shutil import shutil
import subprocess
from optparse import OptionParser from optparse import OptionParser
@ -13,6 +14,7 @@ def firstLines(filename, n):
while n: while n:
n -= 1 n -= 1
lines.append(fd.readline().rstrip('\r\n')) lines.append(fd.readline().rstrip('\r\n'))
fd.close()
return lines return lines
def firstLine(filename): def firstLine(filename):
@ -22,23 +24,29 @@ def error(s):
sys.stderr.write(s+'\n') sys.stderr.write(s+'\n')
sys.exit(-1) sys.exit(-1)
def system(sh, errmsg=None): def system(sh, errmsg=None, **kwargs):
if errmsg is None: if errmsg is None:
errmsg = repr(sh) if isinstance(sh, basestring):
ret = os.system(sh) errmsg = repr(sh)
else:
errmsg = repr(' '.join(sh))
ret = subprocess.call(sh, **kwargs)
if ret: if ret:
error(errmsg + ' (error code: %s)' % ret) error(errmsg + ' (error code: %s)' % ret)
def checkGitRepo(): def checkGitRepo():
system('test "$(git rev-parse --is-inside-work-tree)" = "true"', system('test "$(git rev-parse --is-inside-work-tree)" = "true"',
'Must be run from a git checkout.') 'Must be run from a git checkout.',
shell=True)
system('test "$(git rev-parse --show-cdup >/dev/null)" = ""', system('test "$(git rev-parse --show-cdup >/dev/null)" = ""',
'Must be run from the top-level directory of the git checkout.') 'Must be run from the top-level directory of the git checkout.',
shell=True)
system('git rev-parse --verify HEAD >/dev/null ' system('git rev-parse --verify HEAD >/dev/null '
'&& git update-index --refresh' '&& git update-index --refresh'
'&& git diff-files --quiet' '&& git diff-files --quiet'
'&& git diff-index --cached --quiet HEAD --', '&& git diff-index --cached --quiet HEAD --',
'Your tree is unclean. Can\'t run from here.') 'Your tree is unclean. Can\'t run from here.',
shell=True)
if __name__ == '__main__': if __name__ == '__main__':
usage = 'usage: %prog [options] <username> <version>' usage = 'usage: %prog [options] <username> <version>'
@ -72,8 +80,8 @@ if __name__ == '__main__':
' Change to an appropriate directory or remove the supybot ' ' Change to an appropriate directory or remove the supybot '
'directory to continue.') 'directory to continue.')
print 'Checking out fresh tree from git.' print 'Checking out fresh tree from git.'
system('git clone -b %s git+ssh://%s@supybot.git.sourceforge.net/gitroot/supybot' system(['git', 'clone', '-b', branch,
% (branch, u)) 'git+ssh://%s@supybot.git.sourceforge.net/gitroot/supybot' % u])
os.chdir('supybot') os.chdir('supybot')
print 'Checking RELNOTES version line.' print 'Checking RELNOTES version line.'
@ -90,36 +98,45 @@ if __name__ == '__main__':
print 'Updating version in version files.' print 'Updating version in version files.'
versionFiles = ['src/version.py'] versionFiles = ['src/version.py']
for fn in versionFiles: for fn in versionFiles:
sh = 'perl -pi -e "s/^version\s*=.*/version = \'%s\'/" %s' % (v, fn) sh = ['perl', '-pi', '-e', 's/^version\s*=.*/version = \'%s\'/' % v, fn]
system(sh, 'Error changing version in %s' % fn) system(sh, 'Error changing version in %s' % fn)
system('git commit %s -m \'Updated to %s.\' %s' commit = ['git', 'commit']
% (sign, v, ' '.join(versionFiles))) if sign:
commit.append('-s')
system(commit + ['-m', 'Updated to %s.' % v] + versionFiles)
print 'Tagging release.' print 'Tagging release.'
system('git tag %s -m "Release %s" %s' % (sign or '-a', v, v)) tag = ['git', 'tag']
if sign:
tag.append('-s')
system(tag + ['-m', "Release %s" % v, 'v%s' % v])
print 'Committing %s+git to version files.' % v print 'Committing %s+git to version files.' % v
for fn in versionFiles: for fn in versionFiles:
sh = 'perl -pi -e "s/^version\s*=.*/version = \'%s\'/" %s' % \ system(['perl', '-pi', '-e',
(v + '+git', fn) 's/^version\s*=.*/version = \'%s+git\'/' % v, fn],
system(sh, 'Error changing version in %s' % fn) 'Error changing version in %s' % fn)
system('git commit %s -m \'Updated to %s+git.\' %s' system(commit + ['-m', 'Updated to %s+git.' % v] + versionFiles)
% (sign, v, ' '.join(versionFiles)))
if not dryrun: if not dryrun:
print 'Pushing commits and tag.' print 'Pushing commits and tag.'
system('git push origin %s' % branch) system(['git', 'push', 'origin', branch])
system('git push --tags') system(['git', 'push', '--tags'])
archive = ['git', 'archive', '--prefix=Supybot-%s/' % v]
print 'Creating tarball (gzip).' print 'Creating tarball (gzip).'
system('git archive --prefix=Supybot-%s/ --format=tar %s ' system(archive + ['-o', '../Supybot-%s.tar.gz' % v,
'| gzip -c >../Supybot-%s.tar.gz' % (v, v, v)) '--format=tgz', 'v%s' % v])
system(['git', 'config', 'tar.bz2.command', 'bzip2 -c'])
print 'Creating tarball (bzip2).' print 'Creating tarball (bzip2).'
system('git archive --prefix=Supybot-%s/ --format=tar %s ' system(archive + ['-o', '../Supybot-%s.tar.bz2' % v,
'| bzip2 -c >../Supybot-%s.tar.bz2' % (v, v, v)) '--format=bz2', 'v%s' % v])
print 'Creating zip.' print 'Creating zip.'
system('git archive --prefix=Supybot-%s/ --format=zip %s ' system(archive + ['-o', '../Supybot-%s.zip' % v,
'>../Supybot-%s.zip' % (v, v, v)) '--format=zip', 'v%s' % v])
os.chdir('..') os.chdir('..')
shutil.rmtree('supybot') shutil.rmtree('supybot')