| 1 |
#!/usr/bin/env 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 |
# Adapted to upload all the file types of Armagetron Advanced, not just tarballs |
| 21 |
|
| 22 |
# Based on the version shipping with Ubuntu 24.04. |
| 23 |
# Modified for Armagetron Advanced to support series and different file types. |
| 24 |
|
| 25 |
'''Upload a release tarball to a Launchpad project.''' |
| 26 |
|
| 27 |
import datetime |
| 28 |
import os |
| 29 |
import sys |
| 30 |
import tempfile |
| 31 |
import os.path |
| 32 |
|
| 33 |
from launchpadlib.errors import HTTPError |
| 34 |
from launchpadlib.launchpad import Launchpad |
| 35 |
from launchpadlib.credentials import UnencryptedFileCredentialStore |
| 36 |
|
| 37 |
import subprocess |
| 38 |
|
| 39 |
# from lptools import config |
| 40 |
|
| 41 |
def create_release(project, series_name, version): |
| 42 |
'''Create new release and milestone for LP project.''' |
| 43 |
|
| 44 |
series = None |
| 45 |
for ser in project.series: |
| 46 |
if ser.display_name == series_name: |
| 47 |
series = ser |
| 48 |
break |
| 49 |
if not series: |
| 50 |
print('Series %s not found, available:' % series_name, file=sys.stderr) |
| 51 |
for s in proj.series.entries: |
| 52 |
print('%s: %s' % (s['display_name'], s['summary']), file=sys.stderr) |
| 53 |
sys.exit(3) |
| 54 |
|
| 55 |
release_date = datetime.date.today().strftime('%Y-%m-%d') |
| 56 |
|
| 57 |
milestone = series.newMilestone(name=version, |
| 58 |
date_targeted=release_date) |
| 59 |
return milestone.createProductRelease(date_released=release_date) |
| 60 |
|
| 61 |
def edit_file(prefix, description): |
| 62 |
(fd, f) = tempfile.mkstemp(prefix=prefix+'.') |
| 63 |
os.write(fd, b'\n\n#------\n# Please enter the %s here. ' |
| 64 |
b'Lines which start with "#" are ignored.\n' % description.encode()) |
| 65 |
os.close(fd) |
| 66 |
subprocess.call(['sensible-editor', f]) |
| 67 |
return cat_file(f) |
| 68 |
|
| 69 |
def cat_file(f): |
| 70 |
content = '' |
| 71 |
for line in open(f): |
| 72 |
if line.startswith('#'): |
| 73 |
continue |
| 74 |
content += line |
| 75 |
return content.strip() |
| 76 |
|
| 77 |
def main(): |
| 78 |
if len(sys.argv) < 5 or len(sys.argv) > 7: |
| 79 |
print('''Upload a release tarball to a Launchpad project. |
| 80 |
|
| 81 |
Usage: %s <project name> <series> <version> <tarball> [changelog file] [releasenotes file]''' % sys.argv[0], file=sys.stderr) |
| 82 |
sys.exit(1) |
| 83 |
|
| 84 |
new_milestone = None |
| 85 |
changelog_file = None |
| 86 |
releasenotes_file = None |
| 87 |
if len(sys.argv) == 5: |
| 88 |
(project, series, version, tarball) = sys.argv[1:] |
| 89 |
elif len(sys.argv) == 6: |
| 90 |
(project, series, version, tarball, changelog_file) = sys.argv[1:] |
| 91 |
elif len(sys.argv) == 7: |
| 92 |
(project, series, version, tarball, changelog_file, releasenotes_file) = sys.argv[1:] |
| 93 |
|
| 94 |
#print("login") |
| 95 |
|
| 96 |
# Armagetronad: Looks like we also used custom login procedure, seems to work, but we provide credentials via |
| 97 |
# LP_CREDENTIALS_FILE environment variable now, which simplifies things |
| 98 |
|
| 99 |
#scriptPath = sys.argv[0] #executable |
| 100 |
#secretFile = os.path.join(os.path.dirname(scriptPath),"secrets","lp-credentials") |
| 101 |
# print secretFile |
| 102 |
#launchpad = Launchpad.login_with("lptools-project-upload","production",credential_store=UnencryptedFileCredentialStore(secretFile)) |
| 103 |
# end old method |
| 104 |
|
| 105 |
launchpad = Launchpad.login_with("lptools-project-upload","production") |
| 106 |
|
| 107 |
# the canonical way would be this, requires 'import lptools', which is not universally available |
| 108 |
# launchpad = config.get_launchpad("project-upload") |
| 109 |
|
| 110 |
#print("login done") |
| 111 |
|
| 112 |
try: |
| 113 |
# Look up the project using the Launchpad instance. |
| 114 |
proj = launchpad.projects[project] |
| 115 |
|
| 116 |
# Get the file contents. |
| 117 |
with open(tarball, 'rb') as f: |
| 118 |
file_content = f.read() |
| 119 |
# Get the signature, if available. |
| 120 |
signature = tarball + '.asc' |
| 121 |
if not os.path.exists(signature): |
| 122 |
print('Calling GPG to create tarball signature...') |
| 123 |
cmd = ['gpg', '--armor', '--sign', '--detach-sig', tarball] |
| 124 |
if subprocess.call(cmd) != 0: |
| 125 |
print('gpg failed, aborting', file=sys.stderr) |
| 126 |
|
| 127 |
if os.path.exists(signature): |
| 128 |
with open(signature, 'rb') as f: |
| 129 |
signature_content = f.read() |
| 130 |
else: |
| 131 |
signature_content = None |
| 132 |
|
| 133 |
# Create a new product release file. |
| 134 |
filename = os.path.basename(tarball) |
| 135 |
|
| 136 |
# default types |
| 137 |
mime_type="application/octet-stream" |
| 138 |
file_type="Installer file" |
| 139 |
description="Unknown File" |
| 140 |
|
| 141 |
basename, extension = os.path.splitext(filename) |
| 142 |
basename, secondextension = os.path.splitext(basename) |
| 143 |
basename, thirdextension = os.path.splitext(basename) |
| 144 |
fn = filename.replace("-", "_") |
| 145 |
# print basename, thirdextension, secondextension, extension |
| 146 |
|
| 147 |
if extension == ".tbz": |
| 148 |
mime_type="application/bzip2" |
| 149 |
if "_32_" in fn: |
| 150 |
description="Client Linux 32 Bit Tarball for Zero Install" |
| 151 |
elif "_64_" in fn: |
| 152 |
description="Client Linux 64 Bit Tarball for Zero Install" |
| 153 |
else: |
| 154 |
file_type="Code Release Tarball" |
| 155 |
description="Source" |
| 156 |
else: |
| 157 |
if fn == "CHANGELOG.txt": |
| 158 |
file_type="Release Notes" |
| 159 |
description="Patch Notes" |
| 160 |
if fn == "CHANGELOG.md": |
| 161 |
file_type="ChangeLog File" |
| 162 |
description="Changelog" |
| 163 |
if extension == ".AppImage": |
| 164 |
description="Client AppImage for Linux" |
| 165 |
Linux=True |
| 166 |
if extension == ".deb": |
| 167 |
description="Client Debian Package" |
| 168 |
Linux=True |
| 169 |
if extension == ".dmg": |
| 170 |
description="Client OSX Disk Image" |
| 171 |
mime_type="application/x-apple-diskimage" |
| 172 |
Mac=True |
| 173 |
if extension == ".exe": |
| 174 |
description="Client Windows Installer" |
| 175 |
mime_type="application/exe" |
| 176 |
Windows=True |
| 177 |
if extension == ".zip": |
| 178 |
if "source" in filename: |
| 179 |
description="Windows Source Zip" |
| 180 |
elif ".macOS." in filename: |
| 181 |
description="Client macOS Zip" |
| 182 |
else: |
| 183 |
description="Client Windows Binary Zip for Zero Install" |
| 184 |
mime_type="application/zip" |
| 185 |
Windows=True |
| 186 |
if "AppImage" in description: |
| 187 |
Linux=True |
| 188 |
if "_32bit" in fn: |
| 189 |
description = description.replace("Linux", "32 bit Linux") |
| 190 |
else: |
| 191 |
description = description.replace("Linux", "64 bit Linux") |
| 192 |
if "edicated" in filename or "server" in filename: |
| 193 |
description=description.replace("Client ", "Server ") |
| 194 |
|
| 195 |
print (filename,description,mime_type,file_type) |
| 196 |
|
| 197 |
# Find the release in the project's releases collection. |
| 198 |
release = None |
| 199 |
for rel in proj.releases: |
| 200 |
if rel.version == version: |
| 201 |
release = rel |
| 202 |
break |
| 203 |
if not release: |
| 204 |
for milestone in proj.all_milestones: |
| 205 |
if milestone.name == version: |
| 206 |
today = datetime.date.today().strftime('%Y-%m-%d') |
| 207 |
release = milestone.createProductRelease(date_released=today) |
| 208 |
if not release: |
| 209 |
release = create_release(proj, series, version) |
| 210 |
|
| 211 |
#print("release created") |
| 212 |
|
| 213 |
for file in release.files: |
| 214 |
#print(file.self_link) |
| 215 |
#print(os.path.basename(file.self_link)) |
| 216 |
if os.path.basename(file.self_link) == filename and file.description == description: |
| 217 |
print("file already released") |
| 218 |
return |
| 219 |
|
| 220 |
release.add_file(filename=filename, description=description, |
| 221 |
file_content=file_content, content_type=mime_type, |
| 222 |
file_type=file_type, signature_filename=signature, |
| 223 |
signature_content=signature_content) |
| 224 |
|
| 225 |
print("file added") |
| 226 |
|
| 227 |
changelog = None |
| 228 |
if changelog_file is not None: |
| 229 |
changelog = cat_file(changelog_file) |
| 230 |
if changelog is not None: |
| 231 |
release.changelog = changelog |
| 232 |
|
| 233 |
release_notes = None |
| 234 |
if releasenotes_file is not None: |
| 235 |
release_notes = cat_file(releasenotes_file) |
| 236 |
if release_notes is not None: |
| 237 |
release.release_notes = release_notes |
| 238 |
|
| 239 |
release.lp_save() |
| 240 |
|
| 241 |
# print("release saved") |
| 242 |
|
| 243 |
return |
| 244 |
|
| 245 |
# Create a new milestone if requested |
| 246 |
if new_milestone is not None: |
| 247 |
mil = release.milestone |
| 248 |
for series in proj.series: |
| 249 |
if mil.name in [milestone.name for milestone in series.all_milestones]: |
| 250 |
series.newMilestone(name=new_milestone) |
| 251 |
|
| 252 |
except HTTPError as error: |
| 253 |
print('An error happened in the upload:', error.content) |
| 254 |
sys.exit(1) |
| 255 |
|
| 256 |
if __name__ == '__main__': |
| 257 |
main() |