|
| 1 | +from pathlib import Path |
| 2 | +from flask import Blueprint, current_app, request, jsonify |
| 3 | +from typing import Tuple, Iterable, Dict, Any, List |
| 4 | + |
| 5 | +from ..common import pull_all |
| 6 | +from ..metadata import Netkan |
| 7 | +from ..repos import NetkanRepo |
| 8 | + |
| 9 | + |
| 10 | +spacedock_metapackage = Blueprint('spacedock_metapackage', __name__) # pylint: disable=invalid-name |
| 11 | + |
| 12 | + |
| 13 | +# For metapackage hook on SpaceDock |
| 14 | +# Handles: https://netkan.ksp-ckan.space/sd/metapackage |
| 15 | +# POST form parameters: |
| 16 | +# mod_ids: The mods' ID numbers on SpaceDock |
| 17 | +@spacedock_metapackage.route('/metapackage', methods=['POST']) |
| 18 | +def metapackage_hook() -> Tuple[str, int]: |
| 19 | + # Make sure our NetKAN and CKAN-meta repos are up to date |
| 20 | + pull_all(current_app.config['repos']) |
| 21 | + # Get the relevant netkans |
| 22 | + sd_ids = request.form.getlist('mod_ids') |
| 23 | + nks = find_netkans(current_app.config['nk_repo'], sd_ids) |
| 24 | + if nks: |
| 25 | + return jsonify({ |
| 26 | + 'missing': not_found(sd_ids, nks), |
| 27 | + 'metapackage': mk_metapackage(nks, request.form.get('name', ''), request.form.get('abstract', '')), |
| 28 | + }), 204 |
| 29 | + return 'No such modules', 404 |
| 30 | + |
| 31 | + |
| 32 | +def mk_metapackage(netkans: Iterable[Netkan], name: str, abstract: str) -> Dict[str, Any]: |
| 33 | + return { |
| 34 | + 'spec_version': 'v1.4', |
| 35 | + 'identifier': 'spacedock_metapackage', |
| 36 | + 'name': name, |
| 37 | + 'abstract': abstract, |
| 38 | + 'kind': 'metapackage', |
| 39 | + 'version': '1.0', |
| 40 | + 'license': 'unknown', |
| 41 | + 'depends': [{'name': nk.identifier} for nk in netkans] |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | +def find_netkans(nk_repo: NetkanRepo, sd_ids: List[str]) -> List[Netkan]: |
| 46 | + return [nk for nk in nk_repo.netkans() |
| 47 | + if nk.kref_src == 'spacedock' and nk.kref_id in sd_ids] |
| 48 | + |
| 49 | + |
| 50 | +def not_found(sd_ids: Iterable[str], netkans: List[Netkan]) -> List[str]: |
| 51 | + nk_ids = {nk.kref_id for nk in netkans} |
| 52 | + return [i for i in sd_ids if i not in nk_ids] |
0 commit comments