|
a/migrate-3rdparty/allura_import.py |
|
b/migrate-3rdparty/allura_import.py |
|
... |
|
... |
18 |
optparser.add_option('-s', '--secret-key', dest='secret_key', help='Secret key')
|
18 |
optparser.add_option('-s', '--secret-key', dest='secret_key', help='Secret key')
|
19 |
optparser.add_option('-p', '--project', dest='project', help='Project to import to')
|
19 |
optparser.add_option('-p', '--project', dest='project', help='Project to import to')
|
20 |
optparser.add_option('-t', '--tracker', dest='tracker', help='Tracker to import to')
|
20 |
optparser.add_option('-t', '--tracker', dest='tracker', help='Tracker to import to')
|
21 |
optparser.add_option('-u', '--base-url', dest='base_url', default='https://sourceforge.net', help='Base Allura URL (%default)')
|
21 |
optparser.add_option('-u', '--base-url', dest='base_url', default='https://sourceforge.net', help='Base Allura URL (%default)')
|
22 |
optparser.add_option('-o', dest='import_opts', default=[], action='append', help='Specify import option(s)', metavar='opt=val')
|
22 |
optparser.add_option('-o', dest='import_opts', default=[], action='append', help='Specify import option(s)', metavar='opt=val')
|
23 |
optparser.add_option('-m', dest='user_map', default=[], action='append', help='Map users', metavar='import_user=allura_user')
|
23 |
optparser.add_option('--user-map', dest='user_map_file', help='Map original users to SF.net users', metavar='JSON_FILE')
|
24 |
optparser.add_option('--validate', dest='validate', action='store_true', help='Validate import data')
|
24 |
optparser.add_option('--validate', dest='validate', action='store_true', help='Validate import data')
|
25 |
optparser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Verbose operation')
|
25 |
optparser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Verbose operation')
|
26 |
options, args = optparser.parse_args()
|
26 |
options, args = optparser.parse_args()
|
27 |
if len(args) != 1:
|
27 |
if len(args) != 1:
|
28 |
optparser.error("Wrong number of arguments")
|
28 |
optparser.error("Wrong number of arguments")
|
29 |
if not options.api_key or not options.secret_key:
|
29 |
if not options.api_key or not options.secret_key:
|
30 |
optparser.error("Keys are required")
|
30 |
optparser.error("Keys are required")
|
31 |
if not options.project or not options.tracker:
|
31 |
if not options.project or not options.tracker:
|
32 |
optparser.error("Target project and tracker are required")
|
32 |
optparser.error("Target project and tracker are required")
|
33 |
return options, args
|
33 |
return optparser, options, args
|
34 |
|
34 |
|
35 |
|
35 |
|
36 |
class AlluraRestClient(object):
|
36 |
class AlluraRestClient(object):
|
37 |
|
37 |
|
38 |
def __init__(self, base_url, api_key, secret_key):
|
38 |
def __init__(self, base_url, api_key, secret_key):
|
|
... |
|
... |
61 |
e.msg += '. Error response:\n' + error_content
|
61 |
e.msg += '. Error response:\n' + error_content
|
62 |
raise e
|
62 |
raise e
|
63 |
|
63 |
|
64 |
|
64 |
|
65 |
if __name__ == '__main__':
|
65 |
if __name__ == '__main__':
|
66 |
options, args = parse_options()
|
66 |
optparser, options, args = parse_options()
|
67 |
url = '/rest/p/' + options.project + '/' + options.tracker
|
67 |
url = '/rest/p/' + options.project + '/' + options.tracker
|
68 |
if options.validate:
|
68 |
if options.validate:
|
69 |
url += '/validate_import'
|
69 |
url += '/validate_import'
|
70 |
else:
|
70 |
else:
|
71 |
url += '/perform_import'
|
71 |
url += '/perform_import'
|
|
... |
|
... |
74 |
for s in options.import_opts:
|
74 |
for s in options.import_opts:
|
75 |
k, v = s.split('=', 1)
|
75 |
k, v = s.split('=', 1)
|
76 |
if v == 'false':
|
76 |
if v == 'false':
|
77 |
v = False
|
77 |
v = False
|
78 |
import_options[k] = v
|
78 |
import_options[k] = v
|
|
|
79 |
|
|
|
80 |
user_map = {}
|
|
|
81 |
if options.user_map_file:
|
|
|
82 |
f = open(options.user_map_file)
|
79 |
|
83 |
try:
|
|
|
84 |
user_map = json.load(f)
|
|
|
85 |
if type(user_map) is not type({}):
|
|
|
86 |
raise ValueError
|
|
|
87 |
for k, v in user_map.iteritems():
|
|
|
88 |
print k, v
|
|
|
89 |
if not isinstance(k, basestring) or not isinstance(v, basestring):
|
|
|
90 |
raise ValueError
|
|
|
91 |
except ValueError:
|
|
|
92 |
optparser.error('--user-map should specify JSON file with format {"original_user": "sf_user", ...}')
|
|
|
93 |
finally:
|
|
|
94 |
f.close()
|
|
|
95 |
|
80 |
import_options['user_map'] = {}
|
96 |
import_options['user_map'] = user_map
|
81 |
for s in options.user_map:
|
|
|
82 |
k, v = s.split('=', 1)
|
|
|
83 |
import_options['user_map'][k] = v
|
|
|
84 |
|
97 |
|
85 |
cli = AlluraRestClient(options.base_url, options.api_key, options.secret_key)
|
98 |
cli = AlluraRestClient(options.base_url, options.api_key, options.secret_key)
|
86 |
print cli.call(url, doc=open(args[0]).read(), options=json.dumps(import_options))
|
99 |
print cli.call(url, doc=open(args[0]).read(), options=json.dumps(import_options))
|