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