actev_get_system.py 3.17 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
    """Private entry point.

    Downloads a credentialed, web-accessible content into src

    Args:
        url (str): Url to get the system
Baptiste CHOCOT's avatar
Baptiste CHOCOT committed
23
        system_type (str): `git` or `archive`
24
25
26
27
        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
28
        install_cli (bool, optional): Information to know whether CLI has to be
29
            installed
Baptiste CHOCOT's avatar
Baptiste CHOCOT committed
30
        sha (str, optional): commit SHA or tag to checkout after cloning
Baptiste CHOCOT's avatar
Baptiste CHOCOT committed
31
        name (str, optional): Directory in which the system will be saved
Baptiste CHOCOT's avatar
Baptiste CHOCOT committed
32

33
34
    """
    try:
Alexandre Boyer's avatar
Alexandre Boyer committed
35
36
        command = system_types[system_type]()
        script = command.entry_point
Baptiste CHOCOT's avatar
Baptiste CHOCOT committed
37
    except KeyError:
38
        raise Exception("Unknown system type")
Alexandre Boyer's avatar
Alexandre Boyer committed
39

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

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

    # 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
53
54
    if not location:
        location = "None"
55
56
57
58
59

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

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

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

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

    # SHA
    if not sha:
        sha = "None"

84
85
    if not name:
        name = ""
Alexandre Boyer's avatar
Alexandre Boyer committed
86

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

    # 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))