#!/usr/bin/env python3 import datetime import re import cgi import cgitb; cgitb.enable() # for troubleshooting # The master list of data and how we tranform it into the xml we need. # 0 - the form fieldname # 1 - the xml tag name to use # 2 - attribute name if applicable # 3 - whether it's a URL, 1 = yes (append https:// if rqd), 0 = no # 4 - printf format string to use for data # formData = ( # 0:fieldname 1:xmltag 2:attname 3:URL? 4:Format [ 'name', 'name', '', 0, 'Apache %s' ], [ 'homepage', 'homepage', 'rdf:resource', 1 ], [ 'pmc', 'asfext:pmc', 'rdf:resource', 0, 'https://%s.apache.org' ], [ 'sdesc', 'shortdesc' ], [ 'ldesc', 'description' ], [ 'bugdb', 'bug-database', 'rdf:resource', 1 ], [ 'mail', 'mailing-list', 'rdf:resource', 1 ], [ 'dl', 'download-page', 'rdf:resource', 1 ], [ 'lang', 'programming-language' ], [ 'cat', 'category', 'rdf:resource', 0, 'https://projects.apache.org/category/%s' ], ) def addData(val, line): fields = len(line) print(" <%s" % line[1], end='') if fields > 2 and line[2]: # there is an attribute name print(" %s=\"" % line[2], end='') else: print(">", end='') if fields > 3 and line[3] == 1: val = makeURL(val) if fields > 4 and line[4]: # have format print(line[4] % val, end='') else: print(val, end='') if fields > 2 and line[2]: # there is an attribute name print("\" />") else: print("" % line[1]) def makeURL(s): if s.startswith('http') : return s return 'https://' + s dt = '{:%Y-%m-%d}'.format(datetime.datetime.now()) form = cgi.FieldStorage() projName = form.getvalue("name", "projectName") # projName is used in the response headers; ensure the name is safe as a file name projName = re.sub(r"[^\w-]", '_', projName) + ".rdf" thehomepage = makeURL(form.getvalue('homepage','www.apache.org')) print("""Content-type: application/rdf+xml Content-Disposition: attachment; filename=doap_%(projName)s %(dt)s """ % {'projName' : projName, 'dt': dt, 'thehomepage': thehomepage} ) for line in formData: field = line[0] val = form.getvalue(field) if val: if type(val) is list: for v in val: addData(v, line) else: addData(val, line) if form.getvalue('relname'): print(""" %s %s %s """ % (form.getvalue('relname'), form.getvalue('reldt'), form.getvalue('relvers'))) if form.getvalue('svnurl'): print(""" """ % (makeURL(form.getvalue('svnurl')), makeURL(form.getvalue('svnhttp')))) if form.getvalue('giturl'): print(""" """ % (makeURL(form.getvalue('giturl')), makeURL(form.getvalue('githttp')))) if form.getvalue('std_title'): print(""" %s %s %s """ % (form.getvalue('std_title'), form.getvalue('std_body'), form.getvalue('std_id'), makeURL(form.getvalue('std_url')))) print(""" """)