]>
Commit | Line | Data |
---|---|---|
47a4cb89 MT |
1 | #!/usr/bin/python |
2 | ||
3 | import logging | |
4 | import os | |
5 | ||
6 | from ConfigParser import ConfigParser | |
7 | ||
8 | import base | |
9 | ||
10 | from constants import * | |
11 | ||
12 | class Config(object): | |
13 | def __init__(self, pakfire): | |
14 | self.pakfire = pakfire | |
15 | ||
16 | self._config = { | |
6174d54a | 17 | "debug" : False, |
47a4cb89 MT |
18 | "logfile" : "/var/log/pakfire.log", |
19 | "source_download_url" : SOURCE_DOWNLOAD_URL, | |
0ec833c6 | 20 | "local_build_repo_path" : LOCAL_BUILD_REPO_PATH, |
47a4cb89 MT |
21 | } |
22 | ||
23 | self._config_repos = {} | |
24 | self._distro = {} | |
25 | self._files = [] | |
26 | ||
27 | # Read default configuration files | |
28 | for file in self.config_files: | |
29 | self.read(file) | |
30 | ||
31 | def dump(self): | |
32 | logging.debug("Configuration:") | |
33 | for k, v in self._config.items(): | |
34 | logging.debug(" %s : %s" % (k, v)) | |
35 | ||
36 | logging.debug("Loaded from files:") | |
37 | for f in self._files: | |
38 | logging.debug(" %s" % f) | |
39 | ||
40 | def read(self, filename): | |
41 | # If filename does not exist we return silently | |
afd1400d | 42 | if not filename or not os.path.exists(filename): |
47a4cb89 MT |
43 | return |
44 | ||
45 | filename = os.path.abspath(filename) | |
46 | ||
47 | # If the file was already loaded, we return silently, too | |
48 | if filename in self._files: | |
49 | return | |
50 | ||
51 | logging.debug("Reading configuration file: %s" % filename) | |
52 | ||
53 | config = ConfigParser() | |
54 | config.read(filename) | |
55 | ||
56 | # Read the main section from the file if any | |
57 | if "main" in config.sections(): | |
58 | for k,v in config.items("main"): | |
59 | self._config[k] = v | |
60 | config.remove_section("main") | |
61 | ||
62 | # Read distribution information from the file | |
63 | if "distro" in config.sections(): | |
64 | for k,v in config.items("distro"): | |
65 | self._distro[k] = v | |
66 | config.remove_section("distro") | |
67 | ||
68 | # Read repository definitions | |
69 | for section in config.sections(): | |
70 | if not self._config_repos.has_key(section): | |
71 | self._config_repos[section] = {} | |
72 | ||
73 | options = {} | |
74 | for option in config.options(section): | |
75 | options[option] = config.get(section, option) | |
76 | ||
77 | self._config_repos[section].update(options) | |
78 | ||
79 | self._files.append(filename) | |
80 | ||
81 | def get(self, key, default=None): | |
82 | return self._config.get(key, default) | |
83 | ||
84 | def set(self, key, val): | |
85 | self._config[key] = val | |
86 | ||
87 | def get_repos(self): | |
88 | return self._config_repos.items() | |
89 | ||
90 | @property | |
91 | def config_files(self): | |
92 | files = [] | |
93 | ||
94 | if self.pakfire.builder: | |
95 | path = os.getcwd() | |
96 | ||
97 | while not path == "/": | |
98 | _path = os.path.join(path, "config") | |
99 | if os.path.exists(_path): | |
100 | break | |
101 | ||
102 | _path = None | |
103 | path = os.path.dirname(path) | |
104 | ||
105 | if _path: | |
106 | files.append(os.path.join(_path, "pakfire.conf")) | |
107 | files.append(os.path.join(_path, "default.conf")) | |
108 | ||
109 | # Remove non-existant files | |
110 | for f in files: | |
111 | if not os.path.exists(f): | |
112 | files.remove(f) | |
113 | ||
114 | if not files: | |
115 | # Return system configuration files | |
116 | files += [CONFIG_FILE] | |
117 | files += [os.path.join(CONFIG_DIR, f) for f in os.listdir(CONFIG_DIR)] | |
118 | ||
119 | return files | |
120 |