| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
# Copyright (c) 2009 Canonical Ltd. |
| 4 |
# |
| 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 |
| 8 |
# later version. |
| 9 |
# |
| 10 |
# lp-project-upload 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. |
| 14 |
|
| 15 |
# Authors: |
| 16 |
# Martin Pitt <[email protected]>, based on |
| 17 |
# http://blog.launchpad.net/api/recipe-for-uploading-files-via-the-api |
| 18 |
# Dustin Kirkland <[email protected]> |
| 19 |
# - support files for changelog and release notes |
| 20 |
|
| 21 |
'''Upload a release tarball to a Launchpad project.''' |
| 22 |
|
| 23 |
import datetime |
| 24 |
import os |
| 25 |
import sys |
| 26 |
import tempfile |
| 27 |
|
| 28 |
from launchpadlib.errors import HTTPError |
| 29 |
|
| 30 |
import subprocess |
| 31 |
|
| 32 |
from lptools import config |
| 33 |
|
| 34 |
def create_release(project, version): |
| 35 |
'''Create new release and milestone for LP project.''' |
| 36 |
|
| 37 |
print('Release %s could not be found for project. Create it? (Y/n)' % \ |
| 38 |
version) |
| 39 |
answer = sys.stdin.readline().strip() |
| 40 |
if answer.startswith('n'): |
| 41 |
sys.exit(0) |
| 42 |
|
| 43 |
n_series = len(project.series) |
| 44 |
if n_series == 1: |
| 45 |
series = project.series[0] |
| 46 |
elif n_series > 1: |
| 47 |
msg = 'More than one series exist. Which one would you like to ' \ |
| 48 |
'upload to? Possible series are (listed as index, name):' |
| 49 |
print(msg) |
| 50 |
for idx, serie in enumerate(project.series): |
| 51 |
print('\t%i - %s' % (idx, serie.name)) |
| 52 |
print('Enter series index: ') |
| 53 |
answer = sys.stdin.readline().strip() |
| 54 |
try: |
| 55 |
series = project.series[int(answer)] |
| 56 |
except (ValueError, IndexError): |
| 57 |
print('The series index is invalid (%s).' % answer, file=sys.stderr) |
| 58 |
sys.exit(3) |
| 59 |
else: |
| 60 |
print("Using series named '%s'" % series.name) |
| 61 |
else: |
| 62 |
print(('Does not support creating releases if no ' |
| 63 |
'series exists.'), file=sys.stderr) |
| 64 |
sys.exit(3) |
| 65 |
|
| 66 |
release_date = datetime.date.today().strftime('%Y-%m-%d') |
| 67 |
milestone = series.newMilestone(name=version, |
| 68 |
date_targeted=release_date) |
| 69 |
return milestone.createProductRelease(date_released=release_date) |
| 70 |
|
| 71 |
def edit_file(prefix, description): |
| 72 |
(fd, f) = tempfile.mkstemp(prefix=prefix+'.') |
| 73 |
os.write(fd, b'\n\n#------\n# Please enter the %s here. ' |
| 74 |
b'Lines which start with "#" are ignored.\n' % description.encode()) |
| 75 |
os.close(fd) |
| 76 |
subprocess.call(['sensible-editor', f]) |
| 77 |
return cat_file(f) |
| 78 |
|
| 79 |
def cat_file(f): |
| 80 |
content = '' |
| 81 |
for line in open(f): |
| 82 |
if line.startswith('#'): |
| 83 |
continue |
| 84 |
content += line |
| 85 |
return content.strip() |
| 86 |
|
| 87 |
def main(): |
| 88 |
if len(sys.argv) < 4 or len(sys.argv) > 7: |
| 89 |
print('''Upload a release tarball to a Launchpad project. |
| 90 |
|
| 91 |
Usage: %s <project name> <version> <tarball> [new milestone] [changelog file] [releasenotes file]''' % sys.argv[0], file=sys.stderr) |
| 92 |
sys.exit(1) |
| 93 |
|
| 94 |
new_milestone = None |
| 95 |
changelog_file = None |
| 96 |
releasenotes_file = None |
| 97 |
if len(sys.argv) == 4: |
| 98 |
(project, version, tarball) = sys.argv[1:] |
| 99 |
elif len(sys.argv) == 5: |
| 100 |
(project, version, tarball, new_milestone) = sys.argv[1:] |
| 101 |
elif len(sys.argv) == 6: |
| 102 |
(project, version, tarball, new_milestone, changelog_file) = sys.argv[1:] |
| 103 |
elif len(sys.argv) == 7: |
| 104 |
(project, version, tarball, new_milestone, changelog_file, releasenotes_file) = sys.argv[1:] |
| 105 |
|
| 106 |
launchpad = config.get_launchpad("project-upload") |
| 107 |
|
| 108 |
try: |
| 109 |
# Look up the project using the Launchpad instance. |
| 110 |
proj = launchpad.projects[project] |
| 111 |
# Find the release in the project's releases collection. |
| 112 |
release = None |
| 113 |
for rel in proj.releases: |
| 114 |
if rel.version == version: |
| 115 |
release = rel |
| 116 |
break |
| 117 |
if not release: |
| 118 |
for milestone in proj.all_milestones: |
| 119 |
if milestone.name == version: |
| 120 |
today = datetime.date.today().strftime('%Y-%m-%d') |
| 121 |
release = milestone.createProductRelease(date_released=today) |
| 122 |
if not release: |
| 123 |
release = create_release(proj, version) |
| 124 |
|
| 125 |
# Get the file contents. |
| 126 |
with open(tarball, 'rb') as f: |
| 127 |
file_content = f.read() |
| 128 |
# Get the signature, if available. |
| 129 |
signature = tarball + '.asc' |
| 130 |
if not os.path.exists(signature): |
| 131 |
print('Calling GPG to create tarball signature...') |
| 132 |
cmd = ['gpg', '--armor', '--sign', '--detach-sig', tarball] |
| 133 |
if subprocess.call(cmd) != 0: |
| 134 |
print('gpg failed, aborting', file=sys.stderr) |
| 135 |
|
| 136 |
if os.path.exists(signature): |
| 137 |
with open(signature, 'rb') as f: |
| 138 |
signature_content = f.read() |
| 139 |
else: |
| 140 |
signature_content = None |
| 141 |
|
| 142 |
# Create a new product release file. |
| 143 |
filename = os.path.basename(tarball) |
| 144 |
release.add_file(filename=filename, description='release tarball', |
| 145 |
file_content=file_content, content_type='application/x-gzip', |
| 146 |
file_type='Code Release Tarball', signature_filename=signature, |
| 147 |
signature_content=signature_content) |
| 148 |
|
| 149 |
if changelog_file is not None: |
| 150 |
changelog = cat_file(changelog_file) |
| 151 |
else: |
| 152 |
changelog = edit_file('changelog', 'changelog') |
| 153 |
if changelog: |
| 154 |
release.changelog = changelog |
| 155 |
|
| 156 |
if releasenotes_file is not None: |
| 157 |
release_notes = cat_file(releasenotes_file) |
| 158 |
else: |
| 159 |
release_notes = edit_file('releasenotes', 'release notes') |
| 160 |
if release_notes: |
| 161 |
release.release_notes = release_notes |
| 162 |
|
| 163 |
release.lp_save() |
| 164 |
|
| 165 |
# Create a new milestone if requested |
| 166 |
if new_milestone is not None: |
| 167 |
mil = release.milestone |
| 168 |
for series in proj.series: |
| 169 |
if mil.name in [milestone.name for milestone in series.all_milestones]: |
| 170 |
series.newMilestone(name=new_milestone) |
| 171 |
|
| 172 |
except HTTPError as error: |
| 173 |
print('An error happened in the upload:', error.content) |
| 174 |
sys.exit(1) |
| 175 |
|
| 176 |
if __name__ == '__main__': |
| 177 |
main() |