Switch to unified view

a/scripts/allura_import.py b/scripts/allura_import.py
1
import os
1
import sys
2
import sys
2
import urllib
3
import urllib
3
import urllib2
4
import urllib2
4
import urlparse
5
import urlparse
5
import hmac
6
import hmac
...
...
7
import json
8
import json
8
from optparse import OptionParser
9
from optparse import OptionParser
9
from pprint import pprint
10
from pprint import pprint
10
from datetime import datetime
11
from datetime import datetime
11
12
12
from allura_import_api import AlluraImportApiClient
13
from allura.lib.import_api import AlluraImportApiClient
13
14
14
15
15
def parse_options():
16
def main():
16
    optparser = OptionParser(usage='''%prog [options] <JSON dump>
17
18
Import project data dump in JSON format into an Allura project.''')
19
    optparser.add_option('-a', '--api-ticket', dest='api_key', help='API ticket')
20
    optparser.add_option('-s', '--secret-key', dest='secret_key', help='Secret key')
21
    optparser.add_option('-p', '--project', dest='project', help='Project to import to')
22
    optparser.add_option('-t', '--tracker', dest='tracker', help='Tracker to import to')
23
    optparser.add_option('-u', '--base-url', dest='base_url', default='https://sourceforge.net', help='Base Allura URL (%default)')
24
    optparser.add_option('-o', dest='import_opts', default=[], action='append', help='Specify import option(s)', metavar='opt=val')
25
    optparser.add_option('--user-map', dest='user_map_file', help='Map original users to SF.net users', metavar='JSON_FILE')
26
    optparser.add_option('--validate', dest='validate', action='store_true', help='Validate import data')
27
    optparser.add_option('-c', '--continue', dest='cont', action='store_true', help='Continue import (skip existing tickets)')
28
    optparser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Verbose operation')
29
    options, args = optparser.parse_args()
30
    if len(args) != 1:
31
        optparser.error("Wrong number of arguments")
32
    if not options.api_key or not options.secret_key:
33
        optparser.error("Keys are required")
34
    if not options.project or not options.tracker:
35
        optparser.error("Target project and tracker are required")
36
    return optparser, options, args
37
38
39
if __name__ == '__main__':
40
    optparser, options, args = parse_options()
17
    optparser, options, args = parse_options()
41
18
42
    import_options = {}
19
    import_options = {}
43
    for s in options.import_opts:
20
    for s in options.import_opts:
44
        k, v = s.split('=', 1)
21
        k, v = s.split('=', 1)
...
...
63
            f.close()
40
            f.close()
64
41
65
    import_options['user_map'] = user_map
42
    import_options['user_map'] = user_map
66
43
67
    cli = AlluraImportApiClient(options.base_url, options.api_key, options.secret_key, options.verbose)
44
    cli = AlluraImportApiClient(options.base_url, options.api_key, options.secret_key, options.verbose)
68
    url = '/rest/p/' + options.project + '/' + options.tracker
69
    doc_txt = open(args[0]).read()
45
    doc_txt = open(args[0]).read()
46
47
    # import the tracker (if any)
48
    if options.tracker:
49
        import_tracker(cli, options.project, options.tracker, import_options, doc_txt,
50
                       validate=options.validate,
51
                       verbose=options.verbose)
52
    elif options.forum:
53
        import_forum(cli, options.project, options.forum, user_map, doc_txt, validate=options.validate)
54
55
def import_tracker(cli, project, tool, import_options, doc_txt, validate=True, verbose=False):
56
    url = '/rest/p/' + project + '/' + tool
70
    if options.validate:
57
    if validate:
71
        url += '/validate_import'
58
        url += '/validate_import'
72
    else:
59
    else:
73
        url += '/perform_import'
60
        url += '/perform_import'
74
61
75
    existing_map = {}
62
    existing_map = {}
...
...
94
            assert res['status'] and not res['errors']
81
            assert res['status'] and not res['errors']
95
            if options.validate:
82
            if options.validate:
96
                if res['warnings']:
83
                if res['warnings']:
97
                    print "Ticket id %s warnings: %s" % (ticket_in['id'], res['warnings'])
84
                    print "Ticket id %s warnings: %s" % (ticket_in['id'], res['warnings'])
98
            else:
85
            else:
99
                if res['warnings']:
100
                    print "Imported ticket id %s, warnings: %s" % (ticket_in['id'], res['warnings'])
101
                else:
102
                    print "Imported ticket id %s" % (ticket_in['id'])
86
                print "Imported ticket id %s" % (ticket_in['id'])
87
88
def import_forum(cli, project, tool, user_map, doc_txt, validate=True):
89
    url = '/rest/p/' + project + '/' + tool
90
    if validate:
91
        url += '/validate_import'
92
        print cli.call(url, doc=doc_txt, user_map=json.dumps(user_map))
93
    else:
94
        url += '/perform_import'
95
        print cli.call(url, doc=doc_txt, user_map=json.dumps(user_map))
96
97
def parse_options():
98
    optparser = OptionParser(usage='''%prog [options] <JSON dump>
99
100
Import project data dump in JSON format into an Allura project.''')
101
    optparser.add_option('-a', '--api-ticket', dest='api_key', help='API ticket')
102
    optparser.add_option('-s', '--secret-key', dest='secret_key', help='Secret key')
103
    optparser.add_option('-p', '--project', dest='project', help='Project to import to')
104
    optparser.add_option('-t', '--tracker', dest='tracker', help='Tracker to import to')
105
    optparser.add_option('-f', '--forum', dest='forum', help='Forum tool to import to')
106
    optparser.add_option('-u', '--base-url', dest='base_url', default='https://sourceforge.net', help='Base Allura URL (%default)')
107
    optparser.add_option('-o', dest='import_opts', default=[], action='append', help='Specify import option(s)', metavar='opt=val')
108
    optparser.add_option('--user-map', dest='user_map_file', help='Map original users to SF.net users', metavar='JSON_FILE')
109
    optparser.add_option('--validate', dest='validate', action='store_true', help='Validate import data')
110
    optparser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Verbose operation')
111
    options, args = optparser.parse_args()
112
    if len(args) != 1:
113
        optparser.error("Wrong number of arguments")
114
    if not options.api_key or not options.secret_key:
115
        optparser.error("Keys are required")
116
    if not options.project:
117
        optparser.error("Target project is required")
118
    return optparser, options, args
119
120
121
if __name__ == '__main__':
122
    main()