summaryrefslogtreecommitdiffstats
path: root/tests/src/lib/config.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/src/lib/config.py')
-rw-r--r--tests/src/lib/config.py92
1 files changed, 92 insertions, 0 deletions
diff --git a/tests/src/lib/config.py b/tests/src/lib/config.py
new file mode 100644
index 0000000..fcd5fdd
--- /dev/null
+++ b/tests/src/lib/config.py
@@ -0,0 +1,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