-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Webhook to generate metapackages for SpaceDock
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from pathlib import Path | ||
from flask import Blueprint, current_app, request, jsonify | ||
|
||
from ..common import pull_all | ||
|
||
|
||
spacedock_metapackage = Blueprint('spacedock_metapackage', __name__) # pylint: disable=invalid-name | ||
|
||
|
||
# For metapackage hook on SpaceDock | ||
# Handles: https://netkan.ksp-ckan.space/sd/metapackage | ||
# POST form parameters: | ||
# mod_ids: The mods' ID numbers on SpaceDock | ||
@spacedock_metapackage.route('/metapackage', methods=['POST']) | ||
def metapackage_hook(): | ||
# Make sure our NetKAN and CKAN-meta repos are up to date | ||
pull_all(current_app.config['repos']) | ||
# Get the relevant netkans | ||
sd_ids = request.form.getlist('mod_ids') | ||
nks = find_netkans(current_app.config['nk_repo'], sd_ids) | ||
if nks: | ||
return jsonify({ | ||
'missing': not_found(sd_ids, nks), | ||
'metapackage': mk_metapackage(nks, request.form.get('name'), request.form.get('abstract')), | ||
}), 204 | ||
return 'No such modules', 404 | ||
|
||
|
||
def mk_metapackage(netkans, name, abstract): | ||
return { | ||
'spec_version': 'v1.4', | ||
'identifier': 'spacedock_metapackage', | ||
'name': name, | ||
'abstract': abstract, | ||
'kind': 'metapackage', | ||
'version': '1.0', | ||
'license': 'unknown', | ||
'depends': [{'name': nk.identifier} for nk in netkans] | ||
} | ||
|
||
|
||
def find_netkans(nk_repo, sd_ids): | ||
return [nk for nk in nk_repo.netkans() | ||
if nk.kref_src == 'spacedock' and nk.kref_id in sd_ids] | ||
|
||
|
||
def not_found(sd_ids, netkans): | ||
nk_ids = {nk.kref_id for nk in netkans} | ||
return [i for i in sd_ids if i not in nk_ids] |