~jderose/+junk/scripts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/python3

DISTROS = ['precise', 'quantal', 'raring']
PPA = 'ppa:novacut/pre-stable'

import optparse
import os
from os import path
import shutil
import tempfile
from subprocess import check_call, CalledProcessError, check_output
import re
import time


MAINTAINER = check_output(['bzr', 'whoami']).decode('utf-8').strip()


changelog = """{name} ({version}-0~{distro}) {distro}; urgency=low

  * Upstream {version} release

 -- {maintainer}  {datetime}
"""


def rfc_2822():
	"""
	Returns the current UTC time as an RFC-2822 formatted string.
	"""
	return time.strftime(
		'%a, %d %b %Y %H:%M:%S +0000',
		time.gmtime()
	)


def build_deb(tarball, debian, **kw):
    tmp = tempfile.mkdtemp()
    orig = path.join(tmp, '{name}_{version}.orig.tar.tgz'.format(**kw))
    shutil.copy(tarball, orig)
    os.chdir(tmp)
    check_call(['tar', '-xzf', orig])
    tree = path.join(tmp, '{name}-{version}'.format(**kw))
    os.chdir(tree)
    shutil.copytree(debian, path.join(tree, 'debian'))
    open(path.join(tree, 'debian', 'changelog'), 'w').write(
        changelog.format(**kw)
    )
    check_call(['debuild', '-S', '-sa'])
    changes = path.join(tmp,
        '{name}_{version}-0~{distro}_source.changes'.format(**kw)
    )
    assert path.isfile(changes)
    try:
        check_call(['dput', PPA, changes])
    except Exception:
        pass


releases = path.join(os.environ['HOME'], 'Public', 'releases')

parser = optparse.OptionParser(
    usage='Usage: %prog [BRANCH]'
)
parser.add_option('--overwrite',
    action='store_true',
    default=False,
    help='overwrite previous build of this version of this project'
)
parser.add_option('-r',
    metavar='REV',
    help='branch as of revision REV (see `bzr help revisionspec`)',
)
parser.add_option('--dput',
    action='store_true',
    default=False,
    help='build source packages and dput to {}'.format(PPA),
)
(options, args) = parser.parse_args()

if len(args) > 1:
    raise SystemExit('ERROR: at most one non-option argument is allowed')
if len(args) == 1:
    branch = args[0]
else:
    branch = os.getcwd()
branch = path.abspath(branch)

tmp = tempfile.mkdtemp(prefix='release.')
trunk = path.join(tmp, 'trunk')
cmd = ['bzr', 'branch', branch, trunk]
if options.r:
    cmd.extend(['-r', options.r])
check_call(cmd)

tree = path.join(tmp, os.listdir(tmp)[0])
assert path.isdir(tree)
setup = path.join(tree, 'setup.py')
assert path.isfile(setup)
os.chdir(tree)
check_call([setup, 'sdist'])
check_call([setup, 'test'])

dist = path.join(tree, 'dist')
tarball = path.join(dist, os.listdir(dist)[0])
assert path.isfile(tarball)

fname = path.basename(tarball)
m = re.match(r'^([-\w]+)-([\d\.]+)\.tar\.gz$', fname)
name = m.group(1)
version = m.group(2)
project = path.join(releases, name)
release = path.join(project, version)
current = path.join(project, 'current')


# Test unpacked tarball
tmp2 = tempfile.mkdtemp(prefix='tarball-test.')
shutil.copy(tarball, tmp2)
os.chdir(tmp2)
check_call(['tar', '-xzf', path.join(tmp2, fname)])
tree2 = path.join(tmp2, '%s-%s' % (name, version))
os.chdir(tree2)
check_call([path.join(tree2, 'setup.py'), 'test'])
check_call([path.join(tree2, 'setup.py'), 'build'])  # do docs build?


# Create sums and sig:
os.chdir(dist)
checksum = tarball + '.sha384'
check_call(['sha384sum', fname], stdout=open(checksum, 'w'))
check_call(['sha384sum', '-c', checksum])

check_call(['gpg', '--sign', '--armor', '--detach-sig', fname])
check_call(['gpg', '--verify', fname + '.asc', fname])


# Create Debian packages:
if options.dput:
    kw = dict(
        name=name,
        version=version,
        maintainer=MAINTAINER,
        datetime=rfc_2822(),
    )
    for distro in DISTROS:
        kw['distro'] = distro
        build_deb(tarball, path.join(tree, 'debian'), **kw)

if not path.lexists(project):
    print('Creating directory: {!r}'.format(project))
    os.makedirs(project)
if path.lexists(release):
    if options.overwrite:
        shutil.rmtree(release)
    else:
        print('ERROR: not overwriting: {!r}'.format(release))
        print('Use --overwrite to force overwriting')
        raise SystemExit()
print('Copying result to {!r}'.format(release))
shutil.copytree(dist, release, symlinks=True)

if path.lexists(current) and path.islink(current):
    print('Deleting old symlink {!r}'.format(current))
    os.remove(current)
assert not path.lexists(current)
print('Symlinking {!r} to {!r}'.format(current, version))
os.symlink(version, current)

print('Tagging {!r} in {!r}'.format(version, branch))
os.chdir(branch)
cmd = ['bzr', 'tag', version]
if options.r:
    cmd.extend(['-r', options.r])
check_call(cmd)