diff --git a/tools/python-kde-release/kde_release/appstream.py b/tools/python-kde-release/kde_release/appstream.py index f6a00bc..6a071b8 100644 --- a/tools/python-kde-release/kde_release/appstream.py +++ b/tools/python-kde-release/kde_release/appstream.py @@ -1,79 +1,80 @@ # Copyright 2015, 2019 Jonathan Riddell # Copyright 2017, 2019 Adrian Chaves # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . from glob import glob from os import chdir, path -from shutil import rmtree from subprocess import run from click import echo, ClickException -from git import GitCommandError, Repo -from git.exc import InvalidGitRepositoryError, NoSuchPathError -from kde_release.cmake import project_version from kde_release.configuration import CONFIGURATION from kde_release.environment import KDE_RELEASE_DIR from kde_release.modules import update_modules APPSTREAM_UPDATER = CONFIGURATION['DEFAULT']['APPSTREAM_UPDATER'] -def add_version_to_appstream(source_folder, version, date): - appstream_files = glob(source_folder + '/**/*appdata.xml', recursive=True) - appstream_files += glob(source_folder + '/**/*metainfo.xml', recursive=True) +def add_version_to_appstream(directory, version, date): + appstream_files = glob(directory + '/**/*appdata.xml', recursive=True) + appstream_files += glob(directory + '/**/*metainfo.xml', recursive=True) for appstream_file in appstream_files: date_string = date.strftime('%A, %-d %B %Y') - result = run([APPSTREAM_UPDATER, "--version", version, "--datestring", date_string, "--releases-to-show" , "4", appstream_file]) + result = run([APPSTREAM_UPDATER, + "--version", version, + "--datestring", date_string, + "--releases-to-show", "4", + appstream_file]) result.check_returncode() if len(appstream_files) > 0: - chdir(source_folder) - result = run(['git', 'commit', '-a', '-m', 'Update Appstream for new release']) + chdir(directory) + result = run(['git', 'commit', '-a', + '-m', 'Update Appstream for new release']) result.check_returncode() result = run(['git', 'push']) result.check_returncode() def add_versions(srcdir, date, verbose, dry, clone, hide_skipped): with (KDE_RELEASE_DIR / 'version').open() as version_file: global_version = version_file.readline().rstrip() if int(global_version.split('.')[2]) > 50: raise ClickException( f'Version number {global_version} indicates this is a testing ' f'release so not adding versions to Appstream files') if not path.isfile(APPSTREAM_UPDATER): raise ClickException( f'Can not find appstream-metainfo-release-update executable file ' f'at {APPSTREAM_UPDATER}.\nGet it from git@invent.kde.org:jriddell' f'/appstream-metainfo-release-update.git') kwargs = {'clone': clone, 'log_missing_versions': not hide_skipped} for directory, product, version in update_modules(srcdir, **kwargs): if dry: echo(f'{product}\n' f'\t(would have added {version})') continue try: add_version_to_appstream(directory, version, date) except Exception as error: echo(f'{product}\n' f'\t(would have added {version})\n' f'\t\t(error: {repr(error)})') else: echo(f'{product}\n' f'\t(added {version})') diff --git a/tools/python-kde-release/kde_release/bugzilla.py b/tools/python-kde-release/kde_release/bugzilla.py index 2540092..64e6439 100644 --- a/tools/python-kde-release/kde_release/bugzilla.py +++ b/tools/python-kde-release/kde_release/bugzilla.py @@ -1,96 +1,95 @@ # Copyright 2015, 2019 Jonathan Riddell # Copyright 2017, 2019 Adrian Chaves # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import re -from os import path from bs4 import BeautifulSoup from click import echo from requests import RequestException, Session from kde_release.modules import update_modules URL = 'https://bugs.kde.org' EDIT_VERSION_URL = '{}/editversions.cgi'.format(URL) def response_error(response): html = response.text soup = BeautifulSoup(html, 'html.parser') error_message = soup.find('div', {'id': 'error_msg'}) if not error_message: return None return re.sub(r'\s+', ' ', error_message.get_text()).strip() def log_in(session, email, password): data = {'Bugzilla_login': email, 'Bugzilla_password': password} headers = {'Referer': 'https://bugs.kde.org/'} try: response = session.post(URL, data=data, headers=headers) error = response_error(response) if error: echo(error) exit(1) except RequestException: echo('Unexpected login error. Please, contact the maintainers of this ' 'script.') exit(1) def bugzilla_csrf_token(session, product): params = {'action': 'add', 'product': product} response = session.get(EDIT_VERSION_URL, params=params) soup = BeautifulSoup(response.text, 'html.parser') try: return soup.find('input', {'name': 'token'})['value'] except Exception: raise RuntimeError('Could not parse token from \'{}\''.format( response.url)) def add_version_to_bugzilla_project(session, product, version): params = {'version': version, 'action': 'new', 'product': product, 'token': bugzilla_csrf_token(session, product)} response = session.get(EDIT_VERSION_URL, params=params) response.raise_for_status() error = response_error(response) if error: raise RuntimeError(error) def add_versions(srcdir, email, password, dry, clone, hide_skipped): session = Session() if not dry: log_in(session, email, password) kwargs = {'clone': clone, 'log_missing_versions': not hide_skipped} for directory, product, version in update_modules(srcdir, **kwargs): if dry: echo(f'{product}\n' f'\t(would have added {version})') continue try: add_version_to_bugzilla_project(session, product, version) except Exception as error: echo(f'{product}\n' f'\t(would have added {version})\n' f'\t\t(error: {repr(error)})') else: echo(f'{product}\n' f'\t(added {version})') diff --git a/tools/python-kde-release/kde_release/cli.py b/tools/python-kde-release/kde_release/cli.py index fbe15f4..a0a054a 100644 --- a/tools/python-kde-release/kde_release/cli.py +++ b/tools/python-kde-release/kde_release/cli.py @@ -1,97 +1,97 @@ # Copyright 2015, 2019 Jonathan Riddell # Copyright 2017, 2019 Adrian Chaves # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from click import command, option, prompt, ClickException, DateTime +from click import command, echo, option, prompt, ClickException, DateTime from kde_release.appstream import add_versions as _add_appstream_versions from kde_release.bugzilla import add_versions as _add_bugzilla_versions def handle_all_exceptions(function): def wrapper(*args, **kwargs): try: function(*args, **kwargs) except ClickException: raise except Exception as error: raise ClickException(repr(error)) return wrapper @command() @option('-s', '--srcdir', prompt='Source folder', help='Folder containing local clones of the Git repositories from ' '../modules.git') @option('--date', prompt='Release date', type=DateTime(), help='Planned release date to record in AppStream files') @option('-v', '--verbose', is_flag=True, help='Provide additional details about errors') @option('-d', '--dry', is_flag=True, help='Do not change AppStream files in repositories') @option('-c', '--clone', is_flag=True, help='Clone missing Git folders.') @option('--hide-skipped', is_flag=True, help='Do not print lines for skipped products') @handle_all_exceptions def add_appstream_versions(srcdir, date, verbose, dry, clone, hide_skipped): """Adds new project versions to AppStream metainfo files. Speficically, for each project listed in modules.git: 1. CMake is used to determine the project version, as defined by CMake’s project command. If the version is not found, the project is skipped. 2. A version is added to the AppStream metainfo file of the target project. """ _add_appstream_versions(srcdir, date, verbose, dry, clone, hide_skipped) @command() @option('-s', '--srcdir', prompt='Source folder', help='Folder containing local clones of the Git repositories from ' 'modules.git') @option('-e', '--email', help='Email of your Bugzilla account') @option('-p', '--password', help='Password of your Bugzilla account') @option('-v', '--verbose', is_flag=True, hidden=True) @option('-d', '--dry', is_flag=True, help='Do not submit anything to Bugzilla. Note: Local Git clones may ' 'be modified.') @option('-c', '--clone', is_flag=True, help='Clone missing Git folders.') @option('--hide-skipped', is_flag=True, help='Do not print lines for skipped products') @handle_all_exceptions def add_bugzilla_versions(srcdir, email, password, verbose, dry, clone, hide_skipped): """Adds new project versions to the KDE Bugtracking System. Speficically, for each project listed in modules.git: 1. CMake is used to determine the project version, as defined by CMake’s project command. If the version is not found, the project is skipped. 2. A version is added to the target project in the KDE Bugtracking System. """ if not dry: if email is None: email = prompt('Email') if password is None: password = prompt('Password', hide_input=True) if verbose: echo('WARNING: The -v/--verbose option is deprecated') _add_bugzilla_versions(srcdir, email, password, dry, clone, hide_skipped)