You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2013/09/12 00:19:37 UTC

[1/2] git commit: [#5966] add wiki-copy.py script

Updated Branches:
  refs/heads/db/5966 [created] c30981768


[#5966] add wiki-copy.py script


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/ac49b04d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/ac49b04d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/ac49b04d

Branch: refs/heads/db/5966
Commit: ac49b04d98c1f1597f6ed967f7352a334a22daa3
Parents: 8e5201d
Author: Dave Brondsema <db...@slashdotmedia.com>
Authored: Wed Sep 11 19:54:24 2013 +0000
Committer: Dave Brondsema <db...@slashdotmedia.com>
Committed: Wed Sep 11 19:54:24 2013 +0000

----------------------------------------------------------------------
 scripts/wiki-copy.py | 136 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 136 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/ac49b04d/scripts/wiki-copy.py
----------------------------------------------------------------------
diff --git a/scripts/wiki-copy.py b/scripts/wiki-copy.py
new file mode 100644
index 0000000..bca726d
--- /dev/null
+++ b/scripts/wiki-copy.py
@@ -0,0 +1,136 @@
+#!/usr/bin/env python
+
+#       Licensed to the Apache Software Foundation (ASF) under one
+#       or more contributor license agreements.  See the NOTICE file
+#       distributed with this work for additional information
+#       regarding copyright ownership.  The ASF licenses this file
+#       to you under the Apache License, Version 2.0 (the
+#       "License"); you may not use this file except in compliance
+#       with the License.  You may obtain a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#       Unless required by applicable law or agreed to in writing,
+#       software distributed under the License is distributed on an
+#       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#       KIND, either express or implied.  See the License for the
+#       specific language governing permissions and limitations
+#       under the License.
+
+import re
+import os
+import sys
+import urllib
+import urlparse
+from optparse import OptionParser
+import json
+
+from ConfigParser import ConfigParser, NoOptionError
+import webbrowser
+import oauth2 as oauth
+
+
+def main():
+    cp = ConfigParser()
+    cp.read('/var/local/allura/Allura/production.ini')
+
+    op = OptionParser(usage='usage: %prog [options]')
+    op.add_option('-u', '--base-url', action='store', dest='base_url',
+            default=cp.get('app:main', 'base_url'), help='URL of API to upload to [default: %default]')
+    op.add_option('-D', '--debug', action='store_true', dest='debug', default=False)
+    op.add_option('-f', '--from-wiki', action='store', dest='from_wiki',
+            help='URL of wiki to copy from like http://fromserver.com/rest/p/test/wiki/')
+    op.add_option('-t', '--to-wiki', action='store', dest='to_wiki',
+            help='URL of wiki to copy to like http://toserver.com/rest/p/test/wiki/')
+    (options, args) = op.parse_args(sys.argv[1:])
+
+    oauth_client = make_oauth_client(options.base_url)
+
+    wiki_data = urllib.urlopen(options.from_wiki).read()
+    wiki_json = json.loads(wiki_data)['pages']
+    for p in wiki_json:
+        from_url = options.from_wiki+urllib.quote(p)
+        to_url = options.to_wiki+urllib.quote(p)
+        try:
+            page_data = urllib.urlopen(from_url).read()
+            page_json = json.loads(page_data)
+            if options.debug:
+                print page_json['text']
+                break
+            resp = oauth_client.request(to_url, 'POST', body=urllib.urlencode(dict(text=page_json['text'].encode('utf-8'))))
+            if resp[0]['status'] == '200':
+                print "Posted {0} to {1}".format(page_json['title'], to_url)
+            else:
+                print "Error posting {0} to {1}: {2} (project may not exist)".format(page_json['title'], to_url, resp[0]['status'])
+                break
+        except:
+            print "Error processing " + p
+            raise
+
+def make_oauth_client(base_url):
+    """
+    Build an oauth.Client with which callers can query Allura.
+
+    Based on Allura, sfpy, and sfx push scripts
+    """
+
+    cp = ConfigParser()
+    cp.read(os.path.join(os.environ['HOME'], '.convertrc'))
+
+    # https://sourceforge.net/p/forge/documentation/API%20-%20Beta/
+    REQUEST_TOKEN_URL = base_url+'/rest/oauth/request_token'
+    AUTHORIZE_URL =     base_url+'/rest/oauth/authorize'
+    ACCESS_TOKEN_URL =  base_url+'/rest/oauth/access_token'
+    oauth_key = option(cp, base_url, 'oauth_key', 'Forge API OAuth Key (%s/auth/oauth/): ' % base_url)
+    oauth_secret = option(cp, base_url, 'oauth_secret', 'Forge API Oauth Secret: ')
+    consumer = oauth.Consumer(oauth_key, oauth_secret)
+
+    try:
+        oauth_token = cp.get(base_url, 'oauth_token')
+        oauth_token_secret = cp.get(base_url, 'oauth_token_secret')
+    except NoOptionError:
+        client = oauth.Client(consumer)
+        resp, content = client.request(REQUEST_TOKEN_URL, 'GET')
+        assert resp['status'] == '200', resp
+
+        request_token = dict(urlparse.parse_qsl(content))
+        pin_url = "%s?oauth_token=%s" % (AUTHORIZE_URL, request_token['oauth_token'])
+        if getattr(webbrowser.get(), 'name', '') == 'links':
+            # sandboxes
+            print("Go to %s" % pin_url)
+        else:
+            webbrowser.open(pin_url)
+        oauth_verifier = raw_input('What is the PIN? ')
+
+        token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
+        token.set_verifier(oauth_verifier)
+        client = oauth.Client(consumer, token)
+        resp, content = client.request(ACCESS_TOKEN_URL, "GET")
+        access_token = dict(urlparse.parse_qsl(content))
+        oauth_token = access_token['oauth_token']
+        oauth_token_secret = access_token['oauth_token_secret']
+
+        cp.set(base_url, 'oauth_token', oauth_token)
+        cp.set(base_url, 'oauth_token_secret', oauth_token_secret)
+
+    # save oauth token for later use
+    cp.write(open(os.path.join(os.environ['HOME'], '.convertrc'), 'w'))
+
+    access_token = oauth.Token(oauth_token, oauth_token_secret)
+    oauth_client = oauth.Client(consumer, access_token)
+    return oauth_client
+
+def option(cp, section, key, prompt=None):
+    """ shared (copy/paste) between Allura & sfpy """
+    if not cp.has_section(section):
+        cp.add_section(section)
+    if cp.has_option(section, key):
+        value = cp.get(section, key)
+    else:
+        value = raw_input(prompt or ('%s: ' % key))
+        cp.set(section, key, value)
+    return value
+
+
+if __name__ == '__main__':
+    main()


[2/2] git commit: [#5966] remove SF-internal references; PEP-8 cleanup; determine base_url automatically

Posted by br...@apache.org.
[#5966] remove SF-internal references; PEP-8 cleanup; determine base_url automatically


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/c3098176
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/c3098176
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/c3098176

Branch: refs/heads/db/5966
Commit: c30981768b1c1669dbcc29b0fed39ac86469b10e
Parents: ac49b04
Author: Dave Brondsema <db...@slashdotmedia.com>
Authored: Wed Sep 11 22:12:59 2013 +0000
Committer: Dave Brondsema <db...@slashdotmedia.com>
Committed: Wed Sep 11 22:12:59 2013 +0000

----------------------------------------------------------------------
 scripts/wiki-copy.py | 33 ++++++++++++++-------------------
 1 file changed, 14 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c3098176/scripts/wiki-copy.py
----------------------------------------------------------------------
diff --git a/scripts/wiki-copy.py b/scripts/wiki-copy.py
index bca726d..35e0077 100644
--- a/scripts/wiki-copy.py
+++ b/scripts/wiki-copy.py
@@ -17,7 +17,6 @@
 #       specific language governing permissions and limitations
 #       under the License.
 
-import re
 import os
 import sys
 import urllib
@@ -31,20 +30,16 @@ import oauth2 as oauth
 
 
 def main():
-    cp = ConfigParser()
-    cp.read('/var/local/allura/Allura/production.ini')
-
     op = OptionParser(usage='usage: %prog [options]')
-    op.add_option('-u', '--base-url', action='store', dest='base_url',
-            default=cp.get('app:main', 'base_url'), help='URL of API to upload to [default: %default]')
-    op.add_option('-D', '--debug', action='store_true', dest='debug', default=False)
     op.add_option('-f', '--from-wiki', action='store', dest='from_wiki',
-            help='URL of wiki to copy from like http://fromserver.com/rest/p/test/wiki/')
+                  help='URL of wiki API to copy from like http://fromserver.com/rest/p/test/wiki/')
     op.add_option('-t', '--to-wiki', action='store', dest='to_wiki',
-            help='URL of wiki to copy to like http://toserver.com/rest/p/test/wiki/')
+                  help='URL of wiki API to copy to like http://toserver.com/rest/p/test/wiki/')
+    op.add_option('-D', '--debug', action='store_true', dest='debug', default=False)
     (options, args) = op.parse_args(sys.argv[1:])
 
-    oauth_client = make_oauth_client(options.base_url)
+    base_url = options.to_wiki.split('/rest/')[0]
+    oauth_client = make_oauth_client(base_url)
 
     wiki_data = urllib.urlopen(options.from_wiki).read()
     wiki_json = json.loads(wiki_data)['pages']
@@ -67,20 +62,18 @@ def main():
             print "Error processing " + p
             raise
 
+
 def make_oauth_client(base_url):
     """
     Build an oauth.Client with which callers can query Allura.
-
-    Based on Allura, sfpy, and sfx push scripts
     """
-
+    config_file = os.path.join(os.environ['HOME'], '.allurarc')
     cp = ConfigParser()
-    cp.read(os.path.join(os.environ['HOME'], '.convertrc'))
+    cp.read(config_file)
 
-    # https://sourceforge.net/p/forge/documentation/API%20-%20Beta/
     REQUEST_TOKEN_URL = base_url+'/rest/oauth/request_token'
-    AUTHORIZE_URL =     base_url+'/rest/oauth/authorize'
-    ACCESS_TOKEN_URL =  base_url+'/rest/oauth/access_token'
+    AUTHORIZE_URL = base_url+'/rest/oauth/authorize'
+    ACCESS_TOKEN_URL = base_url+'/rest/oauth/access_token'
     oauth_key = option(cp, base_url, 'oauth_key', 'Forge API OAuth Key (%s/auth/oauth/): ' % base_url)
     oauth_secret = option(cp, base_url, 'oauth_secret', 'Forge API Oauth Secret: ')
     consumer = oauth.Consumer(oauth_key, oauth_secret)
@@ -114,14 +107,16 @@ def make_oauth_client(base_url):
         cp.set(base_url, 'oauth_token_secret', oauth_token_secret)
 
     # save oauth token for later use
-    cp.write(open(os.path.join(os.environ['HOME'], '.convertrc'), 'w'))
+    cp.write(open(config_file, 'w'))
+    print 'Saving oauth tokens in {} for later re-use'.format(config_file)
+    print
 
     access_token = oauth.Token(oauth_token, oauth_token_secret)
     oauth_client = oauth.Client(consumer, access_token)
     return oauth_client
 
+
 def option(cp, section, key, prompt=None):
-    """ shared (copy/paste) between Allura & sfpy """
     if not cp.has_section(section):
         cp.add_section(section)
     if cp.has_option(section, key):