config_reader.py 5.5 KB
Newer Older
1
#
Penom Nom's avatar
Penom Nom committed
2
# Copyright (C) 2015 INRA
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os
Celine Noirot's avatar
Celine Noirot committed
19
import re
20
import sys
21
import inspect
Penom Nom's avatar
Penom Nom committed
22
import logging
23

Penom Nom's avatar
Penom Nom committed
24
from configparser import RawConfigParser, NoOptionError
25

26
from jflow.utils import which, display_error_message
27

28
29
30
31
class JFlowConfigReader(object):
    """
    """
    
Jerome Mariette's avatar
Jerome Mariette committed
32
    CONFIG_FILE_PATH = "../../application.properties"
Celine Noirot's avatar
Celine Noirot committed
33
    USER_PATTERN = re.compile("###USER###")
34
35
36
    def __init__(self):
        """ 
        """
Penom Nom's avatar
Penom Nom committed
37
        self.reader = RawConfigParser()
38
39
40
        self.reader.read(os.path.join(os.path.dirname(inspect.getfile(self.__class__)), self.CONFIG_FILE_PATH))

    def get_tmp_directory(self):
Celine Noirot's avatar
Celine Noirot committed
41
42
43
44
45
46
47
        tmp_dir=self.reader.get("storage", "tmp_directory")
        if self.USER_PATTERN.search(self.reader.get("storage", "tmp_directory")) is not None :
            tmp_dir=tmp_dir.replace("###USER###",os.getenv("USER"))
        if not os.path.isdir(tmp_dir):
            os.makedirs(tmp_dir, 0o751)
        return tmp_dir

48
    def get_work_directory(self):
Celine Noirot's avatar
Celine Noirot committed
49
50
51
52
        if self.USER_PATTERN.search(self.reader.get("storage", "work_directory")) is None :
            return self.reader.get("storage", "work_directory")
        else:
            return self.reader.get("storage", "work_directory").replace("###USER###",os.getenv("USER"))
53
54
55
    
    def get_exec(self, software):
        try:
56
            return self.reader.get("softwares", software)
Penom Nom's avatar
Penom Nom committed
57
        except NoOptionError:
58
            return None
59

60
61
62
    def get_resource(self, resource):
        return self.reader.get("resources", resource)

63
64
65
66
67
68
    def get_log_file_path(self):
        """
        return the log file path
          @return: the path to the log file
        """
        try:
Celine Noirot's avatar
Celine Noirot committed
69
70
71
72
            if self.USER_PATTERN.search(self.reader.get("storage", "log_file")) is None :
                return self.reader.get('storage', 'log_file')
            else :
                return self.reader.get('storage', 'log_file').replace("###USER###",os.getenv("USER"))
73
        except :
Penom Nom's avatar
Penom Nom committed
74
            raise NoOptionError("Failed when parsing the config file, no section logging found!")
Celine Noirot's avatar
Celine Noirot committed
75

76
77
78
        
    def get_makeflow_path(self):
        try:
79
            exec_path = self.reader.get("global", "makeflow")
Penom Nom's avatar
Penom Nom committed
80
        except NoOptionError:
81
            exec_path = None
Jerome Mariette's avatar
Jerome Mariette committed
82
        if exec_path is None: exec_path = "makeflow"
83
        if which(exec_path) == None:
84
85
            logging.getLogger("jflow").exception("'makeflow' path connot be retrieved either in the PATH and in the application.properties file!")
            raise Exception("'makeflow' path connot be retrieved either in the PATH and in the application.properties file!")
86
        return exec_path
Penom Nom's avatar
Penom Nom committed
87
88
89
90
91

    def get_date_format(self):
        try:
            date_format = self.reader.get("global", "date_format")
        except:
Penom Nom's avatar
Penom Nom committed
92
            raise NoOptionError("Failed when parsing the config file, no parameter date_format!")
Penom Nom's avatar
Penom Nom committed
93
94
        return date_format

95
96
    def get_batch(self):
        try:
97
98
            type = self.reader.get("global", "batch_system_type")
            options = self.reader.get("global", "batch_options")
99
            limit_submission = self.reader.get("global", "limit_submission")
Penom Nom's avatar
Penom Nom committed
100
            return [type, options, limit_submission]
Penom Nom's avatar
Penom Nom committed
101
        except NoOptionError:
102
            return None
103
          
104
105
    def get_socket_options(self):
        try:
Jerome Mariette's avatar
Jerome Mariette committed
106
            return [self.reader.get("global", "server_socket_host"), int(self.reader.get("global", "server_socket_port"))]
107
        except:
Jerome Mariette's avatar
Jerome Mariette committed
108
            return ["127.0.0.1", 8080]
Penom Nom's avatar
Penom Nom committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
        
    def get_email_options(self):
        try: smtps = self.reader.get("email", "smtp_server")
        except: smtps = None
        try: smtpp = self.reader.get("email", "smtp_port")
        except: smtpp = None
        try: froma = self.reader.get("email", "from_address")
        except: froma = None
        try: fromp = self.reader.get("email", "from_password")
        except: fromp = None
        try: toa = self.reader.get("email", "to_address")
        except: toa = None
        try: subject = self.reader.get("email", "subject")
        except: subject = None
        try: message = self.reader.get("email", "message")
        except: message = None
        return [smtps, smtpp, froma, fromp, toa, subject, message]
    
127
128
129
130
131
    def get_component_batch_options(self, component_class):
        try:
            return self.reader.get("components", component_class+".batch_options")
        except:
            return ""
Celine Noirot's avatar
Celine Noirot committed
132
133
134
135
136
137

    def get_component_modules(self, component_class):
        try:
            return self.reader.get("components", component_class+".modules").split(",")
        except:
            return []
Penom Nom's avatar
Penom Nom committed
138
139
140
141
142
143
    
    def get_workflow_group(self, workflow_class):
        try:
            return self.reader.get("workflows", workflow_class+".group")
        except:
            return ""
Celine Noirot's avatar
Celine Noirot committed
144
145
146
147
148
149
150
151
152

    def get_browse_root_dir(self):
        return self.reader.get("storage", "browse_root_dir")

    def get_debug(self):
        try:
            return self.reader.get("global", "debug") == "True"
        except NoOptionError:
            return False