|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright 2019 - Swiss Data Science Center (SDSC) |
| 4 | +# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and |
| 5 | +# Eidgenössische Technische Hochschule Zürich (ETHZ). |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +"""Client for handling a configuration.""" |
| 19 | +import configparser |
| 20 | +import fcntl |
| 21 | +import os |
| 22 | +from pathlib import Path |
| 23 | + |
| 24 | +import attr |
| 25 | +import click |
| 26 | + |
| 27 | +APP_NAME = 'Renku' |
| 28 | +"""Application name for storing configuration.""" |
| 29 | + |
| 30 | +RENKU_HOME = '.renku' |
| 31 | +"""Project directory name.""" |
| 32 | + |
| 33 | + |
| 34 | +def print_app_config_path(ctx, param, value): |
| 35 | + """Print application config path.""" |
| 36 | + if not value or ctx.resilient_parsing: |
| 37 | + return |
| 38 | + click.echo(ConfigManagerMixin().config_path) |
| 39 | + ctx.exit() |
| 40 | + |
| 41 | + |
| 42 | +def default_config_dir(): |
| 43 | + """Return default config directory.""" |
| 44 | + return click.get_app_dir(APP_NAME, force_posix=True) |
| 45 | + |
| 46 | + |
| 47 | +@attr.s |
| 48 | +class ConfigManagerMixin: |
| 49 | + """Client for handling global configuration.""" |
| 50 | + |
| 51 | + config_dir = attr.ib(default=default_config_dir(), converter=str) |
| 52 | + config_name = attr.ib(default='renku.ini', converter=str) |
| 53 | + |
| 54 | + _lock = attr.ib(default=None) |
| 55 | + |
| 56 | + def __enter__(self): |
| 57 | + """Acquire a lock file.""" |
| 58 | + lock_name = '{0}/{1}.lock'.format(self.config_dir, self.config_name) |
| 59 | + locked_file_descriptor = open(lock_name, 'w+') |
| 60 | + fcntl.lockf(locked_file_descriptor, fcntl.LOCK_EX) |
| 61 | + self._lock = locked_file_descriptor |
| 62 | + |
| 63 | + def __exit__(self, type, value, traceback): |
| 64 | + """Release lock file.""" |
| 65 | + self._lock.close() |
| 66 | + |
| 67 | + @property |
| 68 | + def config_path(self): |
| 69 | + """Renku config path.""" |
| 70 | + config = Path(self.config_dir) |
| 71 | + if not config.exists(): |
| 72 | + config.mkdir() |
| 73 | + |
| 74 | + return config / Path(self.config_name) |
| 75 | + |
| 76 | + def load_config(self): |
| 77 | + """Loads global configuration object.""" |
| 78 | + config = configparser.ConfigParser() |
| 79 | + config.read(str(self.config_path)) |
| 80 | + return config |
| 81 | + |
| 82 | + def store_config(self, config): |
| 83 | + """Persists global configuration object.""" |
| 84 | + os.umask(0) |
| 85 | + fd = os.open(str(self.config_path), os.O_CREAT | os.O_WRONLY, 0o600) |
| 86 | + with open(fd, 'w') as file: |
| 87 | + config.write(file) |
| 88 | + |
| 89 | + def get_value(self, section, key): |
| 90 | + """Get value from specified section and key.""" |
| 91 | + config = self.load_config() |
| 92 | + return config.get(section, key, fallback=None) |
| 93 | + |
| 94 | + def set_value(self, section, key, value): |
| 95 | + """Set value to specified section and key.""" |
| 96 | + config = self.load_config() |
| 97 | + if section in config: |
| 98 | + config[section][key] = value |
| 99 | + else: |
| 100 | + config[section] = {key: value} |
| 101 | + |
| 102 | + self.store_config(config) |
| 103 | + return config |
| 104 | + |
| 105 | + |
| 106 | +def get_config(client, write_op, is_global): |
| 107 | + """Get configuration object.""" |
| 108 | + if is_global: |
| 109 | + return client |
| 110 | + |
| 111 | + if write_op: |
| 112 | + return client.repo.config_writer() |
| 113 | + return client.repo.config_reader() |
0 commit comments