Switch to side-by-side view

--- a/scripts/allura_import.py
+++ b/scripts/allura_import.py
@@ -1,3 +1,4 @@
+import os
 import sys
 import urllib
 import urllib2
@@ -9,34 +10,10 @@
 from pprint import pprint
 from datetime import datetime
 
-from allura_import_api import AlluraImportApiClient
+from allura.lib.import_api import AlluraImportApiClient
 
 
-def parse_options():
-    optparser = OptionParser(usage='''%prog [options] <JSON dump>
-
-Import project data dump in JSON format into an Allura project.''')
-    optparser.add_option('-a', '--api-ticket', dest='api_key', help='API ticket')
-    optparser.add_option('-s', '--secret-key', dest='secret_key', help='Secret key')
-    optparser.add_option('-p', '--project', dest='project', help='Project to import to')
-    optparser.add_option('-t', '--tracker', dest='tracker', help='Tracker to import to')
-    optparser.add_option('-u', '--base-url', dest='base_url', default='https://sourceforge.net', help='Base Allura URL (%default)')
-    optparser.add_option('-o', dest='import_opts', default=[], action='append', help='Specify import option(s)', metavar='opt=val')
-    optparser.add_option('--user-map', dest='user_map_file', help='Map original users to SF.net users', metavar='JSON_FILE')
-    optparser.add_option('--validate', dest='validate', action='store_true', help='Validate import data')
-    optparser.add_option('-c', '--continue', dest='cont', action='store_true', help='Continue import (skip existing tickets)')
-    optparser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Verbose operation')
-    options, args = optparser.parse_args()
-    if len(args) != 1:
-        optparser.error("Wrong number of arguments")
-    if not options.api_key or not options.secret_key:
-        optparser.error("Keys are required")
-    if not options.project or not options.tracker:
-        optparser.error("Target project and tracker are required")
-    return optparser, options, args
-
-
-if __name__ == '__main__':
+def main():
     optparser, options, args = parse_options()
 
     import_options = {}
@@ -65,9 +42,19 @@
     import_options['user_map'] = user_map
 
     cli = AlluraImportApiClient(options.base_url, options.api_key, options.secret_key, options.verbose)
-    url = '/rest/p/' + options.project + '/' + options.tracker
     doc_txt = open(args[0]).read()
-    if options.validate:
+
+    # import the tracker (if any)
+    if options.tracker:
+        import_tracker(cli, options.project, options.tracker, import_options, doc_txt,
+                       validate=options.validate,
+                       verbose=options.verbose)
+    elif options.forum:
+        import_forum(cli, options.project, options.forum, user_map, doc_txt, validate=options.validate)
+
+def import_tracker(cli, project, tool, import_options, doc_txt, validate=True, verbose=False):
+    url = '/rest/p/' + project + '/' + tool
+    if validate:
         url += '/validate_import'
     else:
         url += '/perform_import'
@@ -96,7 +83,40 @@
                 if res['warnings']:
                     print "Ticket id %s warnings: %s" % (ticket_in['id'], res['warnings'])
             else:
-                if res['warnings']:
-                    print "Imported ticket id %s, warnings: %s" % (ticket_in['id'], res['warnings'])
-                else:
-                    print "Imported ticket id %s" % (ticket_in['id'])
+                print "Imported ticket id %s" % (ticket_in['id'])
+
+def import_forum(cli, project, tool, user_map, doc_txt, validate=True):
+    url = '/rest/p/' + project + '/' + tool
+    if validate:
+        url += '/validate_import'
+        print cli.call(url, doc=doc_txt, user_map=json.dumps(user_map))
+    else:
+        url += '/perform_import'
+        print cli.call(url, doc=doc_txt, user_map=json.dumps(user_map))
+
+def parse_options():
+    optparser = OptionParser(usage='''%prog [options] <JSON dump>
+
+Import project data dump in JSON format into an Allura project.''')
+    optparser.add_option('-a', '--api-ticket', dest='api_key', help='API ticket')
+    optparser.add_option('-s', '--secret-key', dest='secret_key', help='Secret key')
+    optparser.add_option('-p', '--project', dest='project', help='Project to import to')
+    optparser.add_option('-t', '--tracker', dest='tracker', help='Tracker to import to')
+    optparser.add_option('-f', '--forum', dest='forum', help='Forum tool to import to')
+    optparser.add_option('-u', '--base-url', dest='base_url', default='https://sourceforge.net', help='Base Allura URL (%default)')
+    optparser.add_option('-o', dest='import_opts', default=[], action='append', help='Specify import option(s)', metavar='opt=val')
+    optparser.add_option('--user-map', dest='user_map_file', help='Map original users to SF.net users', metavar='JSON_FILE')
+    optparser.add_option('--validate', dest='validate', action='store_true', help='Validate import data')
+    optparser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Verbose operation')
+    options, args = optparser.parse_args()
+    if len(args) != 1:
+        optparser.error("Wrong number of arguments")
+    if not options.api_key or not options.secret_key:
+        optparser.error("Keys are required")
+    if not options.project:
+        optparser.error("Target project is required")
+    return optparser, options, args
+
+
+if __name__ == '__main__':
+    main()