aboutsummaryrefslogtreecommitdiff
path: root/wqflask/utility/tools.py
blob: c3c9b29245d753f5bd3b49939dc2323733776bb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Tools/paths finder resolves external paths from settings and/or environment
# variables
#
# Currently supported:
#
#   PYLMM_PATH finds the root of the git repository of the pylmm_gn2 tool 

import os
import sys
from wqflask import app

def get_setting(command_id,guess=None):
    """Resolve a setting from the environment or the global settings in
    app.config, with get_valid_path is a function checking whether the
    path points to an expected directory and returns the full path to
    the binary command

      guess = os.environ.get('HOME')+'/pylmm'
      get_setting('PYLMM_PATH',guess)

    first tries the environment variable in +id+, next gets the Flask
    app setting for the same +id+ and finally does an educated
    +guess+.

    In all, the environment overrides the others, next is the flask
    setting, then the guess. A valid path to the binary command is
    returned. If none is resolved an exception is thrown.

    Note that we do not use the system path. This is on purpose
    because it will mess up controlled (reproducible) deployment. The
    proper way is to either use the GNU Guix defaults as listed in
    etc/default_settings.py or override them yourself by creating a
    different settings.py file (or setting the environment).

    """
    def valid(command):
        if command:
            sys.stderr.write("Found value "+command+"\n")
            return command
        else:
            return None
    
    # ---- Check whether environment exists
    sys.stderr.write("Looking for "+command_id+"\n")
    command = valid(os.environ.get(command_id))
    if not command:
        # ---- Check whether setting exists in app
        command = valid(app.config.get(command_id))
        if not command:
            command = valid(guess)
            if not command:
                raise Exception(command_id+' path unknown or faulty (update settings.py?). '+command_id+' should point to the path')
    return command

def pylmm_command(guess=None):
    return get_setting("PYLMM_RUN",guess)

def gemma_command(guess=None):
    return get_setting("GEMMA_RUN",guess)

def plink_command(guess=None):
    return get_setting("PLINK_RUN",guess)

def flat_files(subdir=None):
    base = get_setting("GENENETWORK_FILES")
    if subdir:
        return base+"/"+subdir
    return base