Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.52 KB | None | 0 0
  1. import xmlrpclib
  2.  
  3. from flask import Flask, g, render_template, request, flash
  4.  
  5. app = Flask('wiki_copy')
  6.  
  7. ADMIN_URL = 'secret'
  8. PROD_URL = 'secret'
  9. ADMIN_SITE = 'secret'
  10. PROD_SITE = 'secret'
  11.  
  12. SOURCE_PAGE = 'production-wiki'
  13.  
  14. SEPERATOR = '{{=====BEGIN MAPPING HERE=====}}'
  15.  
  16. def get_proxies():
  17.    admin = xmlrpclib.ServerProxy(ADMIN_URL)
  18.    admin.users.get_me() # test to see if conn works
  19.    prod = xmlrpclib.ServerProxy(PROD_URL)
  20.    prod.users.get_me()
  21.    return admin, prod
  22.  
  23. def get_lines(admin):
  24.    try:
  25.       data = admin.pages.get_one({'site': ADMIN_SITE, 'page': SOURCE_PAGE})['content']
  26.    except:
  27.       flash('Error: unable to fetch the production-wiki page with mappings')
  28.       return
  29.    data = data.splitlines()
  30.    try:
  31.       idx = data.index(SEPERATOR)
  32.       data = data[idx+1:]
  33.    except:
  34.       flash('Error: malformed page mapping')
  35.       return
  36.    return data
  37.  
  38. def update_pictures():
  39.    try:
  40.       admin, prod = get_proxies()
  41.    except:
  42.       flash('Error: Unable to initialize XMLRPC connections')
  43.       return
  44.    data = get_lines(admin)
  45.    if not data:
  46.       return
  47.    for idx, line in enumerate(data):
  48.       if line.count('|') != 3:
  49.          continue
  50.       admin_page_url, admin_file_url, prod_page_url = line.split('|')
  51.       if not admin_page_url:
  52.          flash('Line {0}: Error: no admin page url'.format(idx))
  53.          return
  54.       if not admin_file_url:
  55.          flash('Line {0}: Error: no admin file url'.format(idx))
  56.          return
  57.       if not prod_page_url:
  58.          flash('Line {0}: Error: no prod page url'.format(idx))
  59.          return
  60.  
  61.       try:
  62.          file_ = admin.files.get_one(ADMIN_SITE, admin_page_url, admin_file_url)
  63.       except:
  64.          flash('Line {0]: Error: unable to fetch file {1}/{2}'.format(idx, admin_page_url, admin_file_url))
  65.          return
  66.  
  67.       kwargs = {'site': PROD_SITE,
  68.                 'page': prod_page_url,
  69.                 'file': admin_file_url,
  70.                 'content': file_['content'],
  71.                 'revision_comment': 'Updated by wiki_copy script'}
  72.  
  73.       if file_['comment']:
  74.          kwargs['comment'] = admin_page_content['comment']
  75.  
  76.       try:
  77.          prod.files.save_one(kwargs)
  78.       except:
  79.          flash('Line {0}: Error: unable to save page {1}/{2}->{3}/{2}'.format(idx, admin_page_url, admin_file_url, prod_page_url))
  80.          return
  81.       flash('Line {0}: Successful copy: {1}/{2}->{3}/{2}'.format(idx, admin_page_url, admin_file_url, prod_page_url))
  82.  
  83. def update_wiki():
  84.    try:
  85.       admin, prod = get_proxies()
  86.    except:
  87.       flash('Error: Unable to initialize XMLRPC connections')
  88.       return
  89.    data = get_lines(admin)
  90.    if not data:
  91.       return
  92.    for idx, line in enumerate(data):
  93.       if line.count('|') != 1:
  94.          continue
  95.       admin_page_url, prod_page_url = line.split('|')
  96.       if not admin_page_url:
  97.          flash('Line {0}: Error: no admin page url'.format(idx))
  98.          return
  99.       if not prod_page_url:
  100.          flash('Line {0}: Error: no prod page url'.format(idx))
  101.          return
  102.       try:
  103.          admin_page_content = admin.pages.get_one({'site': ADMIN_SITE, 'page': admin_page_url})
  104.       except:
  105.          flash('Line {0]: Error: unable to fetch page {1}'.format(idx, admin_page_url))
  106.          return
  107.  
  108.       kwargs = {'site': PROD_SITE,
  109.                 'page': prod_page_url,
  110.                 'revision_comment': 'Updated by wiki_copy script'}
  111.  
  112.       title = admin_page_content['title']
  113.       if not title:
  114.          title = prod_page_url
  115.       kwargs['title'] = title
  116.  
  117.       content = admin_page_content['content']
  118.       if not content:
  119.          flash('Line {0}: Error: page {1} has no content'.format(idx, admin_page_url))
  120.          return
  121.       kwargs['content'] = content
  122.  
  123.       if admin_page_content['tags']:
  124.          kwargs['tags'] = admin_page_content['tags']
  125.  
  126.       try:
  127.          prod.pages.save_one(kwargs)
  128.       except:
  129.          flash('Line {0}: Error: unable to save page {1}->{2}'.format(idx, admin_page_url, prod_page_url))
  130.          return
  131.       flash('Line {0}: Successful copy: {1}->{2}'.format(idx, admin_page_url, prod_page_url))
  132.    return
  133.  
  134. @app.route('/images', methods=('GET', 'POST'))
  135. def do_images():
  136.    update_pictures()
  137.    return render_template('index.html')
  138.  
  139. @app.route('/text', methods=('GET', 'POST'))
  140. def do_text():
  141.    update_wiki()
  142.    return render_template('index.html')
  143.  
  144. @app.route('/', methods=('GET', 'POST'))
  145. def front_page():
  146.    return render_template('index.html')
  147.  
  148. if __name__ == "__main__":
  149.    app.debug = True
  150.    app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement