aboutsummaryrefslogtreecommitdiffstats
path: root/MgaRepo/cgi/submit.py
blob: e06ae07a8f3acb8638451467df43b759641b55bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/python
from MgaRepo import Error, config
from MgaRepo.rpmutil import get_srpm
from MgaRepo.cgiutil import CgiError, get_targets
import cgi
import sys
import os

TEMPLATE = """\
<html>
<head>
<title>Repository package submission system</title>
</head>
<body bgcolor="white">
<table cellspacing=0 cellpadding=0 border=0 width="100%%">
  <tr bgcolor="#020264"><td align="left" valign="middle"><img src="http://qa.mandriva.com/mandriva.png" hspace=0 border=0 alt=""></td></tr>
</table>
<br>
<hr>
<center>
<b>%(message)s</b>
<br><br>
<form method="POST" action="">
<table><tr><td valign="top">
  Package URL:<br>
  <input name="packageurl" size="60" value="svn+ssh://cvs.mandriva.com/svn/mdv/cooker/"><br>
  <small>Ex. svn+ssh://cvs.mandriva.com/svn/mdv/cooker/pkgname</small><br>
  </td><td valign="top">
  Revision:<br>
  <input name="packagerev" size="10" value=""><br>
  </td></tr></table>
  <br>
  Package target:<br>
  <select name="target" size=5>
  %(targetoptions)s
  </select><br>
  <br>
  <input type="submit" value="Submit package">
</form>
</center>
<hr/>
</body>
</html>
"""

def get_targetoptions():
    s = ""
    selected = " selected"
    for target in get_targets():
        s += '<option value="%s"%s>%s</option>' \
             % (target.name, selected, target.name)
        selected = ""
    return s
 
def show(msg="", error=0):
    if error:
        msg = '<font color="red">%s</font>' % msg
    print(TEMPLATE % {"message":msg, "targetoptions":get_targetoptions()})

def submit_packages(packager):
    form = cgi.FieldStorage()
    packageurl = form.getfirst("packageurl", "").strip()
    packagerev = form.getfirst("packagerev", "").strip()
    if not packageurl:
        show()
    elif not packagerev:
        raise CgiError("No revision provided!")
    else:
        targetname = form.getfirst("target")
        if not targetname:
            raise CgiError("No target selected!")
        for target in get_targets():
            if target.name == targetname:
                break
        else:
            raise CgiError("Target not found!")
        try:
            tmp = int(packagerev)
        except ValueError:
            raise CgiError("Invalid revision provided!")
        for allowed in target.allowed:
            if packageurl.startswith(allowed):
                break
        else:
            raise CgiError("%s is not allowed for this target!" % packageurl)
        get_srpm(packageurl,
                 revision=packagerev,
                 targetdirs=target.target,
                 packager=packager,
                 revname=1,
                 svnlog=1,
                 scripts=target.scripts)
        show("Package submitted!")

def main():
    if 'REQUEST_METHOD' not in os.environ:
        sys.stderr.write("error: this program is meant to be used as a cgi\n")
        sys.exit(1)
    print("Content-type: text/html\n\n")
    try:
        username = os.environ.get("REMOTE_USER")
        method = os.environ.get("REQUEST_METHOD")
        if not username or method != "POST":
            show()
        else:
            useremail = config.get("users", username)
            if not useremail:
                raise CgiError("Your email was not found. Contact the administrator!")
            submit_packages(useremail)
    except CgiError as e:
        show(str(e), error=1)
    except Error as e:
        error = str(e)
        show(error[0].upper()+error[1:], error=1)
    except:
        cgi.print_exception()

# vim:et:ts=4:sw=4