summaryrefslogtreecommitdiffstats
path: root/tests/src/lib/config.py
blob: fcd5fdd935ea9e540c7cf53221a46058cfa9801c (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
# Copyright (C) 2020 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

import configparser
import os

class ReplicantConfig(object):
    def __init__(self):
        # This should implement the XDG Base Directory Specification which is
        # available here:
        # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html

        config_file = None
        xdg_config_home = ''
        try:
            xdg_config_home = os.environ['XDG_CONFIG_HOME']
        except KeyError:
            pass

        if xdg_config_home == '':
            try:
                xdg_config_home = os.environ['HOME'] + os.sep + '.config'
            except KeyError:
                # This follow strictly the specification
                xdg_config_home = os.sep + '.config'

        xdg_config_dirs = ''
        try:
            xdg_config_dirs = os.environ['XDG_CONFIG_DIRS']
        except KeyError:
            pass
        if xdg_config_dirs == '':
            xdg_config_dirs = os.sep + 'etc' + os.sep + 'xdg'

        for base in [xdg_config_home] + xdg_config_dirs.split(os.pathsep):
            config_path = base + os.sep + 'replicant' + os.sep \
                + 'replicant_tests.conf'
            if not os.path.isfile(config_path):
                continue
            try:
                # Silently skip the file in case of issues as per the
                # specification
                config_file = open(config_path, 'r')
            except:
                pass

            if config_file:
                break

        if config_file is None:
            # TODO: raise some error
            print("Configuration file not found")
            assert(False)

        self.config = configparser.ConfigParser()
        self.config.read_file(config_file)

    # The config parameter corresponds to the sections in the configuration
    # file. At the time of writing we have the following sections:
    # - replicant-builder
    # - replicant-installer
    # - fdroid-installer
    def get(self, section):
        if section not in ['replicant-builder',
                          'replicant-installer',
                          'fdroid-installer']:
            # TODO
            assert(False)

        results = {}

        replicant_installer_config = self.config[section]
        for k, v in replicant_installer_config.items():
            if v and v.startswith('~'):
                results[k] = os.environ['HOME'] + v[1:]
            else:
                results[k] = v

        return results