| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import collections |
| 4 |
import datetime |
| 5 |
import importlib.machinery |
| 6 |
import importlib.util |
| 7 |
import os |
| 8 |
import re |
| 9 |
import subprocess |
| 10 |
import sys |
| 11 |
|
| 12 |
import click |
| 13 |
from jinja2 import Environment |
| 14 |
|
| 15 |
# Import system hackery to import conf.py.in without copying it to |
| 16 |
# have a .py suffix. This is entirely so that we can run from a git |
| 17 |
# checkout without any user intervention. |
| 18 |
|
| 19 |
loader = importlib.machinery.SourceFileLoader('meld.conf', 'meld/conf.py.in') |
| 20 |
spec = importlib.util.spec_from_loader(loader.name, loader) |
| 21 |
mod = importlib.util.module_from_spec(spec) |
| 22 |
loader.exec_module(mod) |
| 23 |
|
| 24 |
import meld # noqa: E402 isort:skip |
| 25 |
meld.conf = mod |
| 26 |
sys.modules['meld.conf'] = mod |
| 27 |
import meld.conf # noqa: E402 isort:skip |
| 28 |
|
| 29 |
|
| 30 |
PO_DIR = "po" |
| 31 |
HELP_DIR = "help" |
| 32 |
RELEASE_BRANCH_RE = r'%s-\d+-\d+' % meld.conf.__package__ |
| 33 |
VERSION_RE = r'__version__\s*=\s*"(?P<version>.*)"' |
| 34 |
|
| 35 |
NEWS_TEMPLATE = """ |
| 36 |
{{ [date, app, version]|join(' ') }} |
| 37 |
{{ '=' * [date, app, version]|join(' ')|length }} |
| 38 |
|
| 39 |
Features: |
| 40 |
|
| 41 |
|
| 42 |
Fixes: |
| 43 |
{% for commit in commits%} |
| 44 |
* {{ commit }} |
| 45 |
{%- endfor %} |
| 46 |
|
| 47 |
Translations: |
| 48 |
{% for translator in translator_langs|sort %} |
| 49 |
* {{ translator }} ({{translator_langs[translator]|sort|join(', ')}}) |
| 50 |
{%- endfor %} |
| 51 |
|
| 52 |
""" |
| 53 |
|
| 54 |
APPDATA_TEMPLATE = """ |
| 55 |
<release date="{{ utcdate }}" version="{{ version }}"> |
| 56 |
<description> |
| 57 |
<p> |
| 58 |
{%- if stable_release %} |
| 59 |
This is a stable release in the {{ release_series }} series. |
| 60 |
{%- else %} |
| 61 |
This is an unstable release in the {{ release_series }} development series. |
| 62 |
{%- endif %} |
| 63 |
</p> |
| 64 |
<ul> |
| 65 |
<li></li> |
| 66 |
{%- if translator_langs %} |
| 67 |
<li>Updated translations</li> |
| 68 |
{%- endif %} |
| 69 |
</ul> |
| 70 |
</description> |
| 71 |
</release> |
| 72 |
""" |
| 73 |
|
| 74 |
|
| 75 |
def get_last_release_tag(): |
| 76 |
cmd = ['git', 'describe', '--abbrev=0', '--tags'] |
| 77 |
tag_name = subprocess.check_output(cmd).strip().decode('utf-8') |
| 78 |
try: |
| 79 |
version = [int(v) for v in tag_name.split('.')] |
| 80 |
if len(version) != 3: |
| 81 |
raise ValueError() |
| 82 |
except ValueError: |
| 83 |
raise ValueError("Couldn't parse tag name %s" % tag_name) |
| 84 |
return tag_name |
| 85 |
|
| 86 |
|
| 87 |
def get_translation_commits(folder): |
| 88 |
last_release = get_last_release_tag() |
| 89 |
revspec = "%s..HEAD" % last_release |
| 90 |
cmd = ['git', 'log', '--pretty=format:%an', '--name-only', revspec, |
| 91 |
'--', folder] |
| 92 |
name_files = subprocess.check_output(cmd).strip().decode('utf-8') |
| 93 |
if not name_files: |
| 94 |
return [] |
| 95 |
commits = name_files.split('\n\n') |
| 96 |
commits = [(c.split('\n')[0], c.split('\n')[1:]) for c in commits] |
| 97 |
return commits |
| 98 |
|
| 99 |
|
| 100 |
def get_translator_langs(folders=[PO_DIR, HELP_DIR]): |
| 101 |
|
| 102 |
def get_lang(path): |
| 103 |
filename = os.path.basename(path) |
| 104 |
if not filename.endswith('.po'): |
| 105 |
return None |
| 106 |
return filename[:-3] |
| 107 |
|
| 108 |
translation_commits = [] |
| 109 |
for folder in folders: |
| 110 |
translation_commits.extend(get_translation_commits(folder)) |
| 111 |
|
| 112 |
author_map = collections.defaultdict(set) |
| 113 |
for author, langs in translation_commits: |
| 114 |
langs = [get_lang(lang) for lang in langs if get_lang(lang)] |
| 115 |
author_map[author] |= set(langs) |
| 116 |
|
| 117 |
return author_map |
| 118 |
|
| 119 |
|
| 120 |
def get_non_translation_commits(): |
| 121 |
last_release = get_last_release_tag() |
| 122 |
revspec = "%s..HEAD" % last_release |
| 123 |
cmd = [ |
| 124 |
'git', 'log', '--pretty=format:%s (%an)', revspec, |
| 125 |
# Exclude commits that only cover the po/ or help/ folders, |
| 126 |
# except commits in help/C. Basically, we want to separate |
| 127 |
# translation-only commits from everything else. |
| 128 |
'--', '.', ":!po/", ':(glob,exclude)help/[!C]*/**', |
| 129 |
] |
| 130 |
commits = subprocess.check_output(cmd).strip().splitlines() |
| 131 |
return [c.decode('utf-8') for c in commits] |
| 132 |
|
| 133 |
|
| 134 |
def get_last_news_entry(): |
| 135 |
cmd = ['git', 'log', '--pretty=format:', '-p', '-1', 'NEWS'] |
| 136 |
lines = subprocess.check_output(cmd).strip().decode('utf-8').splitlines() |
| 137 |
lines = [ |
| 138 |
line[1:] |
| 139 |
for line in lines |
| 140 |
if (line and line[0] in ('+', '-')) |
| 141 |
and (len(line) < 2 or line[1] not in ('+', '-')) |
| 142 |
] |
| 143 |
return "\n".join(lines) |
| 144 |
|
| 145 |
|
| 146 |
def parse_news_entry(news): |
| 147 |
features, fixes, translators = [], [], [] |
| 148 |
section = None |
| 149 |
sections = { |
| 150 |
'Features': features, |
| 151 |
'Fixes': fixes, |
| 152 |
'Translations': translators, |
| 153 |
} |
| 154 |
for line in news.splitlines(): |
| 155 |
if line.strip(' :') in sections: |
| 156 |
section = line.strip(' :') |
| 157 |
continue |
| 158 |
if not section or not line.strip(): |
| 159 |
continue |
| 160 |
sections[section].append(line) |
| 161 |
|
| 162 |
def reformat(section): |
| 163 |
if not section: |
| 164 |
return section |
| 165 |
|
| 166 |
def space_prefix(s): |
| 167 |
for i in range(1, len(s)): |
| 168 |
if not s[:i].isspace(): |
| 169 |
break |
| 170 |
return i - 1 |
| 171 |
|
| 172 |
indent = min(space_prefix(line) for line in section) |
| 173 |
return [line[indent:] for line in section] |
| 174 |
|
| 175 |
return reformat(features), reformat(fixes), reformat(translators) |
| 176 |
|
| 177 |
|
| 178 |
def make_env(): |
| 179 |
|
| 180 |
def minor_version(version): |
| 181 |
return '.'.join(version.split('.')[:2]) |
| 182 |
|
| 183 |
jinja_env = Environment() |
| 184 |
jinja_env.filters['minor_version'] = minor_version |
| 185 |
return jinja_env |
| 186 |
|
| 187 |
|
| 188 |
def get_tokens(): |
| 189 |
news = get_last_news_entry() |
| 190 |
features, fixes, translators = parse_news_entry(news) |
| 191 |
version = meld.conf.__version__ |
| 192 |
major, minor, *release = version.split('.') |
| 193 |
stable_release = int(minor) % 2 == 0 |
| 194 |
release_series = '{}.{}'.format(major, minor) |
| 195 |
|
| 196 |
return { |
| 197 |
'date': datetime.date.today().isoformat(), |
| 198 |
# Appstream appears to expect release dates in UTC. |
| 199 |
'utcdate': datetime.datetime.now(datetime.UTC).date().isoformat(), |
| 200 |
'app': meld.conf.__package__, |
| 201 |
'version': version, |
| 202 |
'release_series': release_series, |
| 203 |
'stable_release': stable_release, |
| 204 |
'translator_langs': get_translator_langs(), |
| 205 |
'features': features, |
| 206 |
'fixes': fixes, |
| 207 |
'translators': translators, |
| 208 |
'commits': get_non_translation_commits(), |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
def render_template(template): |
| 213 |
tokens = get_tokens() |
| 214 |
jinja_env = make_env() |
| 215 |
template = jinja_env.from_string(template) |
| 216 |
return(template.render(tokens)) |
| 217 |
|
| 218 |
|
| 219 |
def call_with_output( |
| 220 |
cmd, stdin_text=None, echo_stdout=True, abort_on_fail=True, |
| 221 |
timeout=30): |
| 222 |
pipe = subprocess.PIPE |
| 223 |
with subprocess.Popen(cmd, stdin=pipe, stdout=pipe, stderr=pipe) as proc: |
| 224 |
stdout, stderr = proc.communicate(stdin_text, timeout=timeout) |
| 225 |
if stdout and echo_stdout: |
| 226 |
click.echo('\n' + stdout.decode('utf-8')) |
| 227 |
if stderr or proc.returncode: |
| 228 |
click.secho('\n' + stderr.decode('utf-8'), fg='red') |
| 229 |
if abort_on_fail and proc.returncode: |
| 230 |
raise click.Abort() |
| 231 |
return proc.returncode |
| 232 |
|
| 233 |
|
| 234 |
def check_release_branch(): |
| 235 |
cmd = ['git', 'rev-parse', '--abbrev-ref', 'HEAD'] |
| 236 |
branch = subprocess.check_output(cmd).strip().decode('utf-8') |
| 237 |
if branch != 'main' and not re.match(RELEASE_BRANCH_RE, branch): |
| 238 |
click.echo( |
| 239 |
'\nBranch "%s" doesn\'t appear to be a release branch.\n' % branch) |
| 240 |
click.confirm('Are you sure you wish to continue?', abort=True) |
| 241 |
return branch |
| 242 |
|
| 243 |
|
| 244 |
def pull(): |
| 245 |
check_release_branch() |
| 246 |
cmd = ['git', 'pull', '--rebase'] |
| 247 |
call_with_output(cmd, timeout=None) |
| 248 |
|
| 249 |
|
| 250 |
def commit(message=None): |
| 251 |
cmd = ['git', 'diff', 'HEAD'] |
| 252 |
call_with_output(cmd, echo_stdout=True) |
| 253 |
confirm = click.confirm('\nCommit this change?', default=True) |
| 254 |
if not confirm: |
| 255 |
return |
| 256 |
|
| 257 |
cmd = ['git', 'commit', '-a'] |
| 258 |
if message: |
| 259 |
cmd.append('-m') |
| 260 |
cmd.append(message) |
| 261 |
call_with_output(cmd, timeout=None) |
| 262 |
|
| 263 |
|
| 264 |
def push(): |
| 265 |
branch = check_release_branch() |
| 266 |
cmd = ['git', 'log', 'origin/%s..%s' % (branch, branch)] |
| 267 |
call_with_output(cmd, echo_stdout=True) |
| 268 |
|
| 269 |
confirm = click.confirm('\nPush these commits?', default=True) |
| 270 |
if not confirm: |
| 271 |
return |
| 272 |
|
| 273 |
cmd = ['git', 'push'] |
| 274 |
call_with_output(cmd, echo_stdout=True) |
| 275 |
|
| 276 |
|
| 277 |
@click.group() |
| 278 |
def cli(): |
| 279 |
pass |
| 280 |
|
| 281 |
|
| 282 |
@cli.command() |
| 283 |
def news(): |
| 284 |
rendered = render_template(NEWS_TEMPLATE) |
| 285 |
with open('NEWS', 'r') as f: |
| 286 |
current_news = f.read() |
| 287 |
|
| 288 |
new_news = rendered + current_news |
| 289 |
with open('NEWS', 'w') as f: |
| 290 |
f.write(new_news) |
| 291 |
|
| 292 |
message = click.edit(filename='NEWS') |
| 293 |
return message |
| 294 |
|
| 295 |
|
| 296 |
@cli.command() |
| 297 |
def metainfo(): |
| 298 |
filename = 'data/org.gnome.Meld.metainfo.xml.in.in' |
| 299 |
|
| 300 |
rendered = render_template(APPDATA_TEMPLATE) |
| 301 |
with open(filename, 'r') as f: |
| 302 |
metainfo = f.read() |
| 303 |
|
| 304 |
insert_tag = '<releases>' |
| 305 |
insert_idx = metainfo.find(insert_tag) |
| 306 |
if insert_idx == -1: |
| 307 |
click.echo('Failed to find <releases> tag in metainfo') |
| 308 |
raise click.Abort() |
| 309 |
|
| 310 |
insert_idx += len(insert_tag) |
| 311 |
|
| 312 |
new_metainfo = metainfo[:insert_idx] + rendered + metainfo[insert_idx:] |
| 313 |
|
| 314 |
with open(filename, 'w') as f: |
| 315 |
f.write(new_metainfo) |
| 316 |
|
| 317 |
message = click.edit(filename=filename) |
| 318 |
return message |
| 319 |
|
| 320 |
|
| 321 |
def write_somewhere(filename, output): |
| 322 |
if filename and os.path.exists(filename): |
| 323 |
overwrite = click.confirm( |
| 324 |
'File "%s" already exists. Overwrite?' % filename, abort=True) |
| 325 |
if not overwrite: |
| 326 |
raise click.Abort() |
| 327 |
if filename: |
| 328 |
with open(filename, 'w') as f: |
| 329 |
f.write(output) |
| 330 |
click.echo('Wrote %s' % filename) |
| 331 |
else: |
| 332 |
click.echo(output) |
| 333 |
|
| 334 |
|
| 335 |
@cli.command() |
| 336 |
def dist(): |
| 337 |
build_dir = '_build' |
| 338 |
archive = '%s-%s.tar.xz' % (meld.conf.__package__, meld.conf.__version__) |
| 339 |
dist_archive_path = os.path.abspath( |
| 340 |
os.path.join(build_dir, 'meson-dist', archive)) |
| 341 |
|
| 342 |
click.echo('Running meson...') |
| 343 |
cmd = ['meson', build_dir] |
| 344 |
call_with_output(cmd, echo_stdout=False) |
| 345 |
|
| 346 |
click.echo('Creating distribution...') |
| 347 |
cmd = ['meson', 'dist', '-C', build_dir] |
| 348 |
call_with_output(cmd, echo_stdout=True) |
| 349 |
|
| 350 |
if not os.path.exists(dist_archive_path): |
| 351 |
click.echo('Failed to create archive file %s' % dist_archive_path) |
| 352 |
raise click.Abort() |
| 353 |
return dist_archive_path |
| 354 |
|
| 355 |
|
| 356 |
@cli.command() |
| 357 |
def tag(): |
| 358 |
last_release = get_last_release_tag() |
| 359 |
click.echo('\nLast release tag was: ', nl=False) |
| 360 |
click.secho(last_release, fg='green', bold=True) |
| 361 |
click.echo('New release tag will be: ', nl=False) |
| 362 |
click.secho(meld.conf.__version__, fg='green', bold=True) |
| 363 |
click.confirm('\nTag this release?', default=True, abort=True) |
| 364 |
|
| 365 |
news_text = get_last_news_entry().encode('utf-8') |
| 366 |
# FIXME: Should be signing tags |
| 367 |
cmd = ['git', 'tag', '-a', '--file=-', meld.conf.__version__] |
| 368 |
call_with_output(cmd, news_text) |
| 369 |
click.echo('Tagged %s' % meld.conf.__version__) |
| 370 |
|
| 371 |
cmd = ['git', 'show', '-s', meld.conf.__version__] |
| 372 |
call_with_output(cmd, echo_stdout=True) |
| 373 |
confirm = click.confirm('\nPush this tag?', default=True) |
| 374 |
if not confirm: |
| 375 |
return |
| 376 |
|
| 377 |
cmd = ['git', 'push', 'origin', meld.conf.__version__] |
| 378 |
call_with_output(cmd, echo_stdout=True) |
| 379 |
|
| 380 |
|
| 381 |
@cli.command('version-bump') |
| 382 |
def version_bump(): |
| 383 |
with open(meld.conf.__file__) as f: |
| 384 |
conf_data = f.read().splitlines() |
| 385 |
|
| 386 |
for i, line in enumerate(conf_data): |
| 387 |
if line.startswith('__version__'): |
| 388 |
match = re.match(VERSION_RE, line) |
| 389 |
version = match.group('version') |
| 390 |
if version != meld.conf.__version__: |
| 391 |
continue |
| 392 |
version_line = i |
| 393 |
break |
| 394 |
else: |
| 395 |
click.echo('Couldn\'t determine version from %s' % meld.conf.__file__) |
| 396 |
raise click.Abort() |
| 397 |
|
| 398 |
click.echo('Current version is: %s' % meld.conf.__version__) |
| 399 |
default_version = meld.conf.__version__.split('.') |
| 400 |
default_version[-1] = str(int(default_version[-1]) + 1) |
| 401 |
default_version = '.'.join(default_version) |
| 402 |
new_version = click.prompt('Enter new version', default=default_version) |
| 403 |
|
| 404 |
conf_data[version_line] = '__version__ = "%s"' % new_version |
| 405 |
with open(meld.conf.__file__, 'w') as f: |
| 406 |
f.write('\n'.join(conf_data) + '\n') |
| 407 |
|
| 408 |
cmd = ["meson", "rewrite", "kwargs", "set", "project", "/", "version", new_version] |
| 409 |
call_with_output(cmd, timeout=120) |
| 410 |
|
| 411 |
|
| 412 |
@cli.command('release') |
| 413 |
@click.option( |
| 414 |
"--skip-news", |
| 415 |
is_flag=True, |
| 416 |
default=False, |
| 417 |
help=( |
| 418 |
"Skip the NEWS + metainfo templating step (but not the commit). Use " |
| 419 |
"this if you have already written the NEWS and metainfo updates and " |
| 420 |
"want to skip the templating helpers." |
| 421 |
), |
| 422 |
) |
| 423 |
@click.option( |
| 424 |
"--skip-news-commit", |
| 425 |
is_flag=True, |
| 426 |
default=False, |
| 427 |
help="Skip the NEWS + metainfo commit step", |
| 428 |
) |
| 429 |
@click.pass_context |
| 430 |
def make_release(ctx, skip_news, skip_news_commit): |
| 431 |
if not skip_news: |
| 432 |
pull() |
| 433 |
ctx.invoke(news) |
| 434 |
ctx.invoke(metainfo) |
| 435 |
if not skip_news_commit: |
| 436 |
commit(message='Update NEWS + metainfo') |
| 437 |
push() |
| 438 |
# Run dist as a check; we don't need the actual tarball |
| 439 |
ctx.invoke(dist) |
| 440 |
ctx.invoke(tag) |
| 441 |
ctx.invoke(version_bump) |
| 442 |
commit(message='Post-release version bump') |
| 443 |
push() |
| 444 |
|
| 445 |
|
| 446 |
if __name__ == '__main__': |
| 447 |
# FIXME: Should include sanity check that we're at the top level of the |
| 448 |
# project |
| 449 |
cli() |