#!/usr/bin/python -tt # # (C) 2006 Paul W. Frields. # This file is licensed under the GNU General Public License (GPL) v2. import os import sys import urllib import httplib from getpass import getpass from time import sleep """Take a list of Beats from the official Fedora Project wiki, download them, and convert to DocBook using a properly outfitted MoinMoin wiki.""" ### Globals myName = "beatconvert" debugging = True beatSite = "http://fedoraproject.org/wiki/" beatDir = "Docs/Beats/" beatUrl = beatSite + beatDir convSite = "http://fedora-test.fedoraproject.org/" convDir = "fedora-docs/Docs/Beats/" convUrl = convSite + convDir beatFolder = 'Beats' waitTime = 15 ###### def print_usage(): print "Usage:", myName, "" print "\t: list of Beats, one per line" if len(sys.argv) < 2: print_usage() sys.exit(1) print "Using", beatUrl, "->", convUrl try: beatFile = open(sys.argv[1], "r") except: print "Problem opening file", sys.argv[1] sys.exit(2) beatList = [beat.rstrip('\n') for beat in beatFile.readlines()] beatFile.close() beatData = {} for beat in beatList: inUrl = ''.join((beatUrl, beat)) outUrl = ''.join((convUrl, beat)) beatData[beat] = [inUrl, outUrl] print "Loaded", len(beatList), "beat names" loginName = raw_input('Enter your wiki login name on the DocBook Moin: ') password = getpass('Enter your password on the DocBook Moin: ') conn = httplib.HTTPConnection(convSite[7:][:-1]) # hack to remove http:// / # if debugging: conn.debuglevel = 1 print "Logging in to target wiki..." conn.request('POST', '/fedora-docs', urllib.urlencode({'action': 'login', 'login': 'Login', 'name': loginName, 'password': password})) print 'Cookie monster want cookie!' resp = conn.getresponse() try: cookie = resp.getheader('Set-Cookie') cookie = cookie[:cookie.find(';')] except: inp = raw_input("Couldn't get a cookie. You can proceed " + "anonymously, but results may not be\n" + "guaranteed. Continue? [y/N]") if inp not in ('y', 'Y'): sys.exit(0) # Make a folder to put the DocBook in. try: os.mkdir(beatFolder) except OSError: beatFolder = raw_input("Couldn't make a directory 'Beats' in your " + "current location.\nSpecify a new one: ") if beatFolder is '': print "Quitting then." sys.exit(0) try: os.mkdir(beatFolder) except: print "Failed, bailing." sys.exit(5) for beat, data in beatData.items(): print "Working on beat", beat inUrl = data[0] outUrl = data[1] data.append(''.join(urllib.urlopen(inUrl+'?action=raw').readlines())) print "Retrieved data for", beat savetext = data[2] submitStr = urllib.urlencode({'action': 'edit', 'editor': 'text', 'button_save': 'Save Changes', 'savetext': savetext}) print "Submitting beat", beat, "..." conn.request('POST', ''.join(('/', convDir, beat)), submitStr, {'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'beatconvert', 'Cookie': cookie}) resp = conn.getresponse() # Make sure things worked OK so we don't lock ourselves out... if resp.status != 200: print "Submission failed. If this were a better script,", print "there would be a fallback here." sys.exit(6) print "Submitted", beat # Get the goods! convStr = urllib.urlencode({'action': 'RenderAsDocbook'}) convUrl = data[1] print "Retrieving XML for", beat, "at", convUrl try: resp = ''.join(urllib.urlopen(''.join((convUrl, '?action=RenderAsDocbook'))).readlines()) except: print "Retrieval failed. If this were a better script,", print "there would be a fallback here." sys.exit(6) print "Retrieved", beat outXml = open(os.path.join(beatFolder, beat.replace('/', '')), "w") outXml.write(resp) outXml.close() print "Wrote XML for", beat print "Waiting for", waitTime, "seconds..." sleep(waitTime) conn.close() print "*** Finished! ***" print "Don't forget to rename and format the XML files with indenting."