Passed
Push — master ( d71f34...c856ab )
by Oleksandr
01:12
created

utils.setup_utils.main()   A

Complexity

Conditions 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 21
rs 9.75
c 0
b 0
f 0
cc 4
nop 3
1
import configparser
2
from pathlib import Path
3
import getpass
4
import sys
5
from tabpy_tools.client import Client
6
7
8
def get_default_config_file_path():
9
    config_file_path = str(Path(__file__).resolve().parent.parent.parent
10
                           / 'tabpy-server/tabpy_server/common/default.conf')
11
    return config_file_path
12
13
14
def parse_config(config_file_path):
15
    config = configparser.ConfigParser()
16
    config.read(config_file_path)
17
    tabpy_config = config['TabPy']
18
    port = tabpy_config['TABPY_PORT']
19
    auth_on = 'TABPY_PWD_FILE' in tabpy_config
20
    ssl_on = 'TABPY_TRANSFER_PROTOCOL' in tabpy_config and \
21
             'TABPY_CERTIFICATE_FILE' in tabpy_config and \
22
             'TABPY_KEY_FILE' in tabpy_config
23
    prefix = "https" if ssl_on else "http"
24
    return port, auth_on, prefix
25
26
27
def get_creds():
28
    if sys.stdin.isatty():
29
        user = input("Username: ")
30
        passwd = getpass.getpass("Password: ")
31
    else:
32
        user = sys.stdin.readline().rstrip()
33
        passwd = sys.stdin.readline().rstrip()
34
    return [user, passwd]
35
36
37
def main(funcName, func, funcDescription):
38
    # running from setup.py
39
    if len(sys.argv) > 1:
40
        config_file_path = sys.argv[1]
41
    else:
42
        config_file_path = get_default_config_file_path()
43
    port, auth_on, prefix = parse_config(config_file_path)
44
45
    connection = Client(f'{prefix}://localhost:{port}/')
46
47
    if auth_on:
48
        # credentials are passed in from setup.py
49
        if len(sys.argv) == 4:
50
            user, passwd = sys.argv[2], sys.argv[3]
51
        # running Sentiment Analysis independently
52
        else:
53
            user, passwd = get_creds()
54
        connection.set_credentials(user, passwd)
55
56
    connection.deploy(funcName, func, funcDescription, override=True)
57
    print(f'Successfully deployed {funcName}')
58