actev_get_system.py 3.14 KB
Newer Older
1
"""Entry point module: get-system
2
3
4
5

This file should not be modified.
"""
import os
6

7
from diva_evaluation_cli.bin.private_src.implementation.utils.\
8
9
    actev_cli_environment import init_environment, export_variable, \
    activate_environment
10

11
12
from diva_evaluation_cli.bin.private_src.implementation.get_system.\
    system_types_definition import system_types
13

14
15
16

def entry_point(url, system_type, location=None, user=None, password=None,
                token=None, install_cli=False, sha=None, name=False):
17
18
19
20
21
22
23
24
25
26
    """Private entry point.

    Downloads a credentialed, web-accessible content into src

    Args:
        url (str): Url to get the system
        location (str, optional): Path to store the system
        user (str, optional): Username to access the url
        password (str, optional): Password to access the url
        token (str, optional): Token to access the url
Baptiste CHOCOT's avatar
Baptiste CHOCOT committed
27
        install_cli (bool, optional): Information to know whether CLI has to be
28
            installed
Baptiste CHOCOT's avatar
Baptiste CHOCOT committed
29
        sha (str, optional): commit SHA or tag to checkout after cloning
Baptiste CHOCOT's avatar
Baptiste CHOCOT committed
30
        name (str, optional): Directory in which the system will be saved
31
32
    """
    try:
Alexandre Boyer's avatar
Alexandre Boyer committed
33
34
        command = system_types[system_type]()
        script = command.entry_point
Baptiste CHOCOT's avatar
Baptiste CHOCOT committed
35
    except KeyError:
36
        raise Exception("Unknown system type")
Alexandre Boyer's avatar
Alexandre Boyer committed
37

38
39
40
41
    # if it exists, loads the scorer conda environment
    activate_environment()
    scorer_env = os.getenv("SCORER_CONDA_ENV")

42
43
    # Init the actev_cli_environemnt variables
    init_environment()
44
45
    if scorer_env:
        export_variable("SCORER_CONDA_ENV", scorer_env)
46
47
48
49
50

    # Add variables into the actev_cli_environemnt
    export_variable('ACTEV_GET_SYSTEM_URL', url)

    # Destination of the software
Alexandre Boyer's avatar
Alexandre Boyer committed
51
52
    if not location:
        location = "None"
53
54
55
56
57

    # Username
    if user:
        export_variable('ACTEV_GET_SYSTEM_USER', user)
    else:
Alexandre Boyer's avatar
Alexandre Boyer committed
58
        user = "None"
59
60
61
62
63

    # Password
    if password:
        export_variable('ACTEV_GET_SYSTEM_PASSWORD', password)
    else:
Alexandre Boyer's avatar
Alexandre Boyer committed
64
        password = "None"
65
66
67
68
69

    # Token
    if token:
        export_variable('ACTEV_GET_SYSTEM_TOKEN', token)
    else:
Alexandre Boyer's avatar
Alexandre Boyer committed
70
        token = "None"
71
72

    # Whether installing the CLI
73
74
75
76
    if install_cli:
        install_cli = "True"
    else:
        install_cli = "False"
Baptiste CHOCOT's avatar
Baptiste CHOCOT committed
77
78
79
80
81

    # SHA
    if not sha:
        sha = "None"

82
83
    if not name:
        name = ""
Alexandre Boyer's avatar
Alexandre Boyer committed
84

85
86
    # go into the right directory to execute the script
    path = os.path.dirname(__file__)
87
    script = os.path.join(path, '../implementation/get_system/get/' + script)
88
89
90
    script += " " + url + \
              " " + location + \
              " " + user + \
Baptiste CHOCOT's avatar
Baptiste CHOCOT committed
91
              " " + password.replace('#', '\\#') + \
92
              " " + token + \
93
              " " + install_cli + \
Baptiste CHOCOT's avatar
Baptiste CHOCOT committed
94
              " " + sha + \
95
              " " + name
96
97
98
99
100
101
102
103
104
105
106
107
108

    # status is a 16-bit long integer
    # The 8 strongest bits are exit_code
    # The 8 weakest bits are signal_number
    status = os.system(script)
    exit_code = status >> 8
    signal_number = status & (2**8 - 1)
    if status != 0:
        if signal_number == 2:  # SIGINT
            raise KeyboardInterrupt
        else:
            raise Exception("Error occured in %s (error code: %d)" % (
                command.entry_point, exit_code))