3
# Copyright (c) 2009 Canonical Ltd.
5
# This program is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by the
7
# Free Software Foundation; either version 2, or (at your option) any
10
# lp-set-dup is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
# General Public License for more details.
16
# Martin Pitt <martin.pitt@ubuntu.com>, based on
17
# http://blog.launchpad.net/api/recipe-for-uploading-files-via-the-api
19
'''Upload a release tarball to a Launchpad project.'''
21
import sys, datetime, os.path, subprocess, tempfile, os
23
from ubuntutools.lp.libsupport import get_launchpad
24
from launchpadlib.errors import HTTPError
26
def create_release(project, version):
27
'''Create new release and milestone for LP project.'''
29
print 'Release %s could not be found for project. Create it? (Y/n)' % version
30
answer = sys.stdin.readline().strip()
31
if answer.startswith('n'):
33
if len(proj.series) != 1:
34
print >> sys.stderr, 'Does not support creating releases if more than one series exists.'
36
release_date = datetime.date.today().strftime('%Y-%m-%d')
37
series = proj.series[0]
38
milestone = series.newMilestone(name=version,
39
date_targeted=release_date)
40
return milestone.createProductRelease(date_released=release_date)
42
def edit_file(prefix, description):
43
(fd, f) = tempfile.mkstemp(prefix=prefix+'.')
44
os.write(fd, '\n\n#------\n# Please enter the %s here. Lines which start with "#" are ignored.\n' %
47
subprocess.call(['sensible-editor', f])
54
return content.strip()
60
if len(sys.argv) != 4:
61
print >> sys.stderr, '''Upload a release tarball to a Launchpad project.
63
Usage: %s <project name> <version> <tarball>''' % sys.argv[0]
66
(project, version, tarball) = sys.argv[1:]
69
lp = get_launchpad('ubuntu-dev-tools')
71
print >> sys.stderr, 'Could not connect to Launchpad:', str(e)
75
# Look up the project using the Launchpad instance.
76
proj = lp.projects[project]
77
# Find the release in the project's releases collection.
79
for rel in proj.releases:
80
if rel.version == version:
84
release = create_release(proj, version)
86
# Get the file contents.
87
file_content = open(tarball, 'r').read()
88
# Get the signature, if available.
89
signature = tarball + '.asc'
90
if not os.path.exists(signature):
91
print 'Calling GPG to create tarball signature...'
92
if subprocess.call(['gpg', '--armor', '--sign', '--detach-sig', tarball]) != 0:
93
print >> sys.stderr, 'gpg failed, aborting'
95
if os.path.exists(signature):
96
signature_content = open(signature, 'r').read()
98
signature_content = None
100
# Create a new product release file.
101
release.add_file(filename=tarball, description='release tarball',
102
file_content=file_content, content_type='appplication/x-gzip',
103
file_type='Code Release Tarball', signature_filename=signature,
104
signature_content=signature_content)
106
changelog = edit_file('changelog', 'changelog')
108
release.changelog = changelog
109
release_notes = edit_file('releasenotes', 'release notes')
111
release.release_notes = release_notes
116
print 'An error happened in the upload:', e.content