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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
|
#!/usr/bin/env python3
import collections
import datetime
import importlib.machinery
import importlib.util
import os
import re
import subprocess
import sys
import urllib.parse
import click
import keyring
import requests
from jinja2 import Environment
# Import system hackery to import conf.py.in without copying it to
# have a .py suffix. This is entirely so that we can run from a git
# checkout without any user intervention.
loader = importlib.machinery.SourceFileLoader('meld.conf', 'meld/conf.py.in')
spec = importlib.util.spec_from_loader(loader.name, loader)
mod = importlib.util.module_from_spec(spec)
loader.exec_module(mod)
import meld # noqa: E402 isort:skip
meld.conf = mod
sys.modules['meld.conf'] = mod
import meld.conf # noqa: E402 isort:skip
PO_DIR = "po"
HELP_DIR = "help"
RELEASE_BRANCH_RE = r'%s-\d+-\d+' % meld.conf.__package__
VERSION_RE = r'__version__\s*=\s*"(?P<version>.*)"'
GITLAB_API_BASE = 'https://gitlab.gnome.org/api/v4'
GITLAB_PROJECT_ID = 'GNOME/meld'
NEWS_TEMPLATE = """
{{ [date, app, version]|join(' ') }}
{{ '=' * [date, app, version]|join(' ')|length }}
Features:
Fixes:
{% for commit in commits%}
* {{ commit }}
{%- endfor %}
Translations:
{% for translator in translator_langs|sort %}
* {{ translator }} ({{translator_langs[translator]|sort|join(', ')}})
{%- endfor %}
"""
APPDATA_TEMPLATE = """
<release date="{{ utcdate }}" version="{{ version }}">
<description>
<p>
{%- if stable_release %}
This is a stable release in the {{ release_series }} series.
{%- else %}
This is an unstable release in the {{ release_series }} development series.
{%- endif %}
</p>
<ul>
<li></li>
{%- if translator_langs %}
<li>Updated translations</li>
{%- endif %}
</ul>
</description>
</release>
"""
def get_last_release_tag():
cmd = ['git', 'describe', '--abbrev=0', '--tags']
tag_name = subprocess.check_output(cmd).strip().decode('utf-8')
try:
version = [int(v) for v in tag_name.split('.')]
if len(version) != 3:
raise ValueError()
except ValueError:
raise ValueError("Couldn't parse tag name %s" % tag_name)
return tag_name
def get_translation_commits(folder):
last_release = get_last_release_tag()
revspec = "%s..HEAD" % last_release
cmd = ['git', 'log', '--pretty=format:%an', '--name-only', revspec,
'--', folder]
name_files = subprocess.check_output(cmd).strip().decode('utf-8')
if not name_files:
return []
commits = name_files.split('\n\n')
commits = [(c.split('\n')[0], c.split('\n')[1:]) for c in commits]
return commits
def get_translator_langs(folders=[PO_DIR, HELP_DIR]):
def get_lang(path):
filename = os.path.basename(path)
if not filename.endswith('.po'):
return None
return filename[:-3]
translation_commits = []
for folder in folders:
translation_commits.extend(get_translation_commits(folder))
author_map = collections.defaultdict(set)
for author, langs in translation_commits:
langs = [get_lang(lang) for lang in langs if get_lang(lang)]
author_map[author] |= set(langs)
return author_map
def get_non_translation_commits():
last_release = get_last_release_tag()
revspec = "%s..HEAD" % last_release
cmd = [
'git', 'log', '--pretty=format:%s (%an)', revspec,
# Exclude commits that only cover the po/ or help/ folders,
# except commits in help/C. Basically, we want to separate
# translation-only commits from everything else.
'--', '.', ":!po/", ':(glob,exclude)help/[!C]*/**',
]
commits = subprocess.check_output(cmd).strip().splitlines()
return [c.decode('utf-8') for c in commits]
def get_last_news_entry():
cmd = ['git', 'log', '--pretty=format:', '-p', '-1', 'NEWS']
lines = subprocess.check_output(cmd).strip().decode('utf-8').splitlines()
lines = [l[1:] for l in lines if (l and l[0] in ('+', '-')) and
(len(l) < 2 or l[1] not in ('+', '-'))]
return "\n".join(lines)
def parse_news_entry(news):
features, fixes, translators = [], [], []
section = None
sections = {
'Features': features,
'Fixes': fixes,
'Translations': translators,
}
for line in news.splitlines():
if line.strip(' :') in sections:
section = line.strip(' :')
continue
if not section or not line.strip():
continue
sections[section].append(line)
def reformat(section):
if not section:
return section
def space_prefix(s):
for i in range(1, len(s)):
if not s[:i].isspace():
break
return i - 1
indent = min(space_prefix(l) for l in section)
return [l[indent:] for l in section]
return reformat(features), reformat(fixes), reformat(translators)
def make_env():
def minor_version(version):
return '.'.join(version.split('.')[:2])
jinja_env = Environment()
jinja_env.filters['minor_version'] = minor_version
return jinja_env
def get_tokens():
news = get_last_news_entry()
features, fixes, translators = parse_news_entry(news)
version = meld.conf.__version__
major, minor, *release = version.split('.')
stable_release = int(minor) % 2 == 0
release_series = '{}.{}'.format(major, minor)
return {
'date': datetime.date.today().isoformat(),
# Appstream appears to expect release dates in UTC.
'utcdate': datetime.datetime.utcnow().date().isoformat(),
'app': meld.conf.__package__,
'version': version,
'release_series': release_series,
'stable_release': stable_release,
'translator_langs': get_translator_langs(),
'features': features,
'fixes': fixes,
'translators': translators,
'commits': get_non_translation_commits(),
}
def render_template(template):
tokens = get_tokens()
jinja_env = make_env()
template = jinja_env.from_string(template)
return(template.render(tokens))
def call_with_output(
cmd, stdin_text=None, echo_stdout=True, abort_on_fail=True,
timeout=30):
pipe = subprocess.PIPE
with subprocess.Popen(cmd, stdin=pipe, stdout=pipe, stderr=pipe) as proc:
stdout, stderr = proc.communicate(stdin_text, timeout=timeout)
if stdout and echo_stdout:
click.echo('\n' + stdout.decode('utf-8'))
if stderr or proc.returncode:
click.secho('\n' + stderr.decode('utf-8'), fg='red')
if abort_on_fail and proc.returncode:
raise click.Abort()
return proc.returncode
def check_release_branch():
cmd = ['git', 'rev-parse', '--abbrev-ref', 'HEAD']
branch = subprocess.check_output(cmd).strip().decode('utf-8')
if branch != "main" and not re.match(RELEASE_BRANCH_RE, branch):
click.echo(
'\nBranch "%s" doesn\'t appear to be a release branch.\n' % branch)
click.confirm('Are you sure you wish to continue?', abort=True)
return branch
def pull():
check_release_branch()
cmd = ['git', 'pull', '--rebase']
call_with_output(cmd, timeout=None)
def commit(message=None):
cmd = ['git', 'diff', 'HEAD']
call_with_output(cmd, echo_stdout=True)
confirm = click.confirm('\nCommit this change?', default=True)
if not confirm:
return
cmd = ['git', 'commit', '-a']
if message:
cmd.append('-m')
cmd.append(message)
call_with_output(cmd, timeout=None)
def push():
branch = check_release_branch()
cmd = ['git', 'log', 'origin/%s..%s' % (branch, branch)]
call_with_output(cmd, echo_stdout=True)
confirm = click.confirm('\nPush these commits?', default=True)
if not confirm:
return
cmd = ['git', 'push']
call_with_output(cmd, echo_stdout=True)
def get_gitlab_token():
auth_token = keyring.get_password('gitlab-api', GITLAB_PROJECT_ID)
if not auth_token:
raise click.ClickException(
"No password in keychain for {id}\n\n"
"Set password using the Python `keyring` package. "
"From the command line:\n"
" $ keyring set gitlab-api {id}\n"
"and enter your secret token from the Gitlab UI.\n".format(
id=GITLAB_PROJECT_ID)
)
return auth_token
def gitlab_release_tag(tag):
auth_token = get_gitlab_token()
cmd = ['git', 'tag', '-l', "--format=%(contents)", tag]
description = subprocess.check_output(cmd).decode('utf-8')
endpoint = '{base}/projects/{id}/releases'
release_url = endpoint.format(
base=GITLAB_API_BASE,
id=urllib.parse.quote_plus(GITLAB_PROJECT_ID),
)
release_data = {
"tag_name": tag,
"description": description
}
# TODO: Should probably sanity-check that it's not already a
# release tag.
response = requests.post(
release_url,
json=release_data,
headers={
'Private-Token': auth_token,
},
)
try:
response.raise_for_status()
except Exception as e:
click.secho(
'Error making release: {}\n{}'.format(e, response.text), fg='red')
@click.group()
def cli():
pass
@cli.command()
def test():
cmd = ['py.test-3', 'test/']
call_with_output(cmd, echo_stdout=True)
@cli.command()
def news():
rendered = render_template(NEWS_TEMPLATE)
with open('NEWS', 'r') as f:
current_news = f.read()
new_news = rendered + current_news
with open('NEWS', 'w') as f:
f.write(new_news)
message = click.edit(filename='NEWS')
return message
@cli.command()
def appdata():
filename = 'data/org.gnome.meld.appdata.xml.in.in'
rendered = render_template(APPDATA_TEMPLATE)
with open(filename, 'r') as f:
appdata = f.read()
insert_tag = '<releases>'
insert_idx = appdata.find(insert_tag)
if insert_idx == -1:
click.echo('Failed to find <releases> tag in appdata')
raise click.Abort()
insert_idx += len(insert_tag)
new_appdata = appdata[:insert_idx] + rendered + appdata[insert_idx:]
with open(filename, 'w') as f:
f.write(new_appdata)
message = click.edit(filename=filename)
return message
def write_somewhere(filename, output):
if filename and os.path.exists(filename):
overwrite = click.confirm(
'File "%s" already exists. Overwrite?' % filename, abort=True)
if not overwrite:
raise click.Abort()
if filename:
with open(filename, 'w') as f:
f.write(output)
click.echo('Wrote %s' % filename)
else:
click.echo(output)
@cli.command()
def dist():
build_dir = '_build'
archive = '%s-%s.tar.xz' % (meld.conf.__package__, meld.conf.__version__)
dist_archive_path = os.path.abspath(
os.path.join(build_dir, 'meson-dist', archive))
click.echo('Running meson...')
cmd = ['meson', build_dir]
call_with_output(cmd, echo_stdout=False)
click.echo('Creating distribution...')
cmd = ['meson', 'dist', '-C', build_dir]
call_with_output(cmd, echo_stdout=False)
if not os.path.exists(dist_archive_path):
click.echo('Failed to create archive file %s' % dist_archive_path)
raise click.Abort()
return dist_archive_path
@cli.command()
def tag():
last_release = get_last_release_tag()
click.echo('\nLast release tag was: ', nl=False)
click.secho(last_release, fg='green', bold=True)
click.echo('New release tag will be: ', nl=False)
click.secho(meld.conf.__version__, fg='green', bold=True)
click.confirm('\nTag this release?', default=True, abort=True)
news_text = get_last_news_entry().encode('utf-8')
# FIXME: Should be signing tags
cmd = ['git', 'tag', '-a', '--file=-', meld.conf.__version__]
call_with_output(cmd, news_text)
click.echo('Tagged %s' % meld.conf.__version__)
cmd = ['git', 'show', '-s', meld.conf.__version__]
call_with_output(cmd, echo_stdout=True)
confirm = click.confirm('\nPush this tag?', default=True)
if not confirm:
return
cmd = ['git', 'push', 'origin', meld.conf.__version__]
call_with_output(cmd, echo_stdout=True)
@cli.command('gitlab-release')
def gitlab_release():
last_release = get_last_release_tag()
confirm = click.confirm(
'Annotate {} as a Gitlab release?'.format(last_release), default=True)
if not confirm:
return
gitlab_release_tag(last_release)
@cli.command('version-bump')
def version_bump():
with open(meld.conf.__file__) as f:
conf_data = f.read().splitlines()
for i, line in enumerate(conf_data):
if line.startswith('__version__'):
match = re.match(VERSION_RE, line)
version = match.group('version')
if version != meld.conf.__version__:
continue
version_line = i
break
else:
click.echo('Couldn\'t determine version from %s' % meld.conf.__file__)
raise click.Abort()
click.echo('Current version is: %s' % meld.conf.__version__)
default_version = meld.conf.__version__.split('.')
default_version[-1] = str(int(default_version[-1]) + 1)
default_version = '.'.join(default_version)
new_version = click.prompt('Enter new version', default=default_version)
conf_data[version_line] = '__version__ = "%s"' % new_version
with open(meld.conf.__file__, 'w') as f:
f.write('\n'.join(conf_data) + '\n')
cmd = ["meson", "rewrite", "kwargs", "set", "project", "/", "version", new_version]
call_with_output(cmd, timeout=120)
@cli.command('release')
@click.pass_context
def make_release(ctx):
# Check that we have the necessary local auth before proceeding
get_gitlab_token()
pull()
ctx.forward(news)
ctx.forward(appdata)
commit(message='Update NEWS + appdata')
push()
# Run dist as a check; we don't need the actual tarball
ctx.forward(dist)
ctx.forward(tag)
ctx.forward(gitlab_release)
ctx.forward(version_bump)
commit(message='Post-release version bump')
push()
if __name__ == '__main__':
# FIXME: Should include sanity check that we're at the top level of the
# project
cli()
|