Package coprs :: Package views :: Package apiv3_ns :: Module apiv3_project_chroots
[hide private]
[frames] | no frames]

Source Code for Module coprs.views.apiv3_ns.apiv3_project_chroots

  1  import flask 
  2  from . import query_params, get_copr, file_upload, GET, PUT 
  3  from .json2form import get_form_compatible_data 
  4  from coprs.helpers import generate_additional_repos, generate_build_config 
  5  from coprs.views.misc import api_login_required 
  6  from coprs.views.apiv3_ns import apiv3_ns 
  7  from coprs.logic.complex_logic import ComplexLogic 
  8  from coprs.exceptions import ObjectNotFound, BadRequest 
  9  from coprs import db, forms 
 10  from coprs.logic.coprs_logic import CoprChrootsLogic 
11 12 13 -def to_dict(project_chroot):
14 return { 15 "mock_chroot": project_chroot.mock_chroot.name, 16 "projectname": project_chroot.copr.name, 17 "ownername": project_chroot.copr.owner_name, 18 "comps_name": project_chroot.comps_name, 19 "additional_repos": project_chroot.repos_list, 20 "additional_packages": project_chroot.buildroot_pkgs_list, 21 "with_opts": str_to_list(project_chroot.with_opts), 22 "without_opts": str_to_list(project_chroot.without_opts), 23 "delete_after_days": project_chroot.delete_after_days, 24 }
25
26 27 -def to_build_config_dict(project_chroot):
28 config = generate_build_config(project_chroot.copr, project_chroot.name) 29 return { 30 "chroot": project_chroot.name, 31 "repos": config["repos"], 32 "additional_repos": generate_additional_repos(project_chroot), 33 "additional_packages": (project_chroot.buildroot_pkgs or "").split(), 34 "use_bootstrap_container": project_chroot.copr.use_bootstrap_container, 35 "enable_net": project_chroot.copr.enable_net, 36 "with_opts": str_to_list(project_chroot.with_opts), 37 "without_opts": str_to_list(project_chroot.without_opts), 38 }
39
40 41 -def rename_fields(input):
42 replace = { 43 "additional_repos": "repos", 44 "additional_packages": "buildroot_pkgs", 45 } 46 output = input.copy() 47 for from_name, to_name in replace.items(): 48 if from_name not in output: 49 continue 50 output[to_name] = output.pop(from_name) 51 return output
52
53 54 -def str_to_list(value):
55 return (value or "").split()
56
57 58 @apiv3_ns.route("/project-chroot", methods=GET) 59 @query_params() 60 -def get_project_chroot(ownername, projectname, chrootname):
61 copr = get_copr(ownername, projectname) 62 chroot = ComplexLogic.get_copr_chroot_safe(copr, chrootname) 63 return flask.jsonify(to_dict(chroot))
64
65 66 @apiv3_ns.route("/project-chroot/build-config", methods=GET) 67 @query_params() 68 -def get_build_config(ownername, projectname, chrootname):
69 copr = get_copr(ownername, projectname) 70 chroot = ComplexLogic.get_copr_chroot_safe(copr, chrootname) 71 if not chroot: 72 raise ObjectNotFound('Chroot not found.') 73 config = to_build_config_dict(chroot) 74 return flask.jsonify(config)
75
76 77 @apiv3_ns.route("/project-chroot/edit/<ownername>/<projectname>/<chrootname>", methods=PUT) 78 @file_upload() 79 @api_login_required 80 -def edit_project_chroot(ownername, projectname, chrootname):
81 copr = get_copr(ownername, projectname) 82 data = rename_fields(get_form_compatible_data()) 83 form = forms.ModifyChrootForm(data, meta={'csrf': False}) 84 chroot = ComplexLogic.get_copr_chroot_safe(copr, chrootname) 85 86 if not form.validate_on_submit(): 87 raise BadRequest(form.errors) 88 89 buildroot_pkgs = repos = comps_xml = comps_name = with_opts = without_opts = None 90 if "buildroot_pkgs" in data: 91 buildroot_pkgs = form.buildroot_pkgs.data 92 if "repos" in data: 93 repos = form.repos.data 94 if "with_opts" in data: 95 with_opts = form.with_opts.data 96 if "without_opts" in data: 97 without_opts = form.without_opts.data 98 if form.upload_comps.has_file(): 99 comps_xml = form.upload_comps.data.stream.read() 100 comps_name = form.upload_comps.data.filename 101 if form.delete_comps.data: 102 CoprChrootsLogic.remove_comps(flask.g.user, chroot) 103 CoprChrootsLogic.update_chroot( 104 flask.g.user, chroot, buildroot_pkgs, repos, comps=comps_xml, comps_name=comps_name, 105 with_opts=with_opts, without_opts=without_opts) 106 db.session.commit() 107 return flask.jsonify(to_dict(chroot))
108