ef93f33cf7f3a8e5456ff2a3b1ae2059bb12d9c3
[releng.git] / utils / test / result_collection_api / update / utils.py
1 ##############################################################################
2 # Copyright (c) 2016 ZTE Corporation
3 # feng.xiaowei@zte.com.cn
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 import os
10 import urlparse
11 import subprocess
12
13
14 def get_abspath(path):
15     assert os.path.isdir(path), 'Path %s can\'t be found.' % path
16     return os.path.abspath(path)
17
18
19 def url_parse(url):
20     url = urlparse.urlparse(url)
21     assert url.scheme == 'mongodb', 'URL must be a MongoDB URL'
22
23     return url.username, url.password, url.hostname, url.port
24
25
26 def execute(cmd, args):
27     (username, password, hostname, port) = url_parse(args.url)
28     cmd.extend(['--host', '%s' % hostname, '--port', '%s' % port])
29     db = args.db
30     if db is not None:
31         cmd.extend(['--db', '%s' % db])
32     if username is not None:
33         cmd.extend(['-u', '%s' % username, '-p', '%s' % password])
34     print('execute: %s' % cmd)
35     execute_output = subprocess.check_output(cmd)
36     print(execute_output)
37
38
39 def main(method, parser):
40     args = parser.parse_args()
41     try:
42         method(args)
43     except AssertionError, msg:
44         print(msg)