|
Lines 94-104
class ConfigRegistry(dict):
Link Here
|
| 94 |
ConfigRegistry.BASES[reg]) |
94 |
ConfigRegistry.BASES[reg]) |
| 95 |
return _ConfigRegistry(filename=filename) |
95 |
return _ConfigRegistry(filename=filename) |
| 96 |
|
96 |
|
| 97 |
def load(self): |
97 |
def load(self, writable=False): |
| 98 |
"""Load registry from file.""" |
98 |
"""Load registry from file.""" |
| 99 |
for reg in self._registry: |
99 |
for reg in self._registry: |
| 100 |
if isinstance(reg, _ConfigRegistry): |
100 |
if isinstance(reg, _ConfigRegistry): |
| 101 |
reg.load() |
101 |
reg.load(writable and reg is self._write_registry) |
| 102 |
strict = self.is_true('ucr/encoding/strict') |
102 |
strict = self.is_true('ucr/encoding/strict') |
| 103 |
for reg in self._registry: |
103 |
for reg in self._registry: |
| 104 |
if isinstance(reg, _ConfigRegistry): |
104 |
if isinstance(reg, _ConfigRegistry): |
|
Lines 108-121
class ConfigRegistry(dict):
Link Here
|
| 108 |
"""Save registry to file.""" |
108 |
"""Save registry to file.""" |
| 109 |
self._write_registry.save() |
109 |
self._write_registry.save() |
| 110 |
|
110 |
|
| 111 |
def lock(self): |
|
|
| 112 |
"""Lock registry file.""" |
| 113 |
self._write_registry.lock() |
| 114 |
|
| 115 |
def unlock(self): |
| 116 |
"""Un-lock registry file.""" |
| 117 |
self._write_registry.unlock() |
| 118 |
|
| 119 |
def __delitem__(self, key): |
111 |
def __delitem__(self, key): |
| 120 |
"""Delete registry key.""" |
112 |
"""Delete registry key.""" |
| 121 |
del self._write_registry[key] |
113 |
del self._write_registry[key] |
|
Lines 241-252
class _ConfigRegistry(dict):
Link Here
|
| 241 |
self.file = filename or '/etc/univention/base.conf' |
233 |
self.file = filename or '/etc/univention/base.conf' |
| 242 |
self.__create_base_conf() |
234 |
self.__create_base_conf() |
| 243 |
self.backup_file = self.file + '.bak' |
235 |
self.backup_file = self.file + '.bak' |
| 244 |
self.lock_filename = self.file + '.lock' |
|
|
| 245 |
# will be set by <ConfigRegistry> for each <_ConfigRegistry> - <True> |
236 |
# will be set by <ConfigRegistry> for each <_ConfigRegistry> - <True> |
| 246 |
# means the backend files are valid UTF-8 and should stay that way --> |
237 |
# means the backend files are valid UTF-8 and should stay that way --> |
| 247 |
# only accept valid UTF-8 |
238 |
# only accept valid UTF-8 |
| 248 |
self.strict_encoding = False |
239 |
self.strict_encoding = False |
| 249 |
self.lock_file = None |
240 |
self.reg_file = None |
| 250 |
|
241 |
|
| 251 |
def __create_base_conf(self): |
242 |
def __create_base_conf(self): |
| 252 |
"""Create sub registry file.""" |
243 |
"""Create sub registry file.""" |
|
Lines 262-318
class _ConfigRegistry(dict):
Link Here
|
| 262 |
def __load_file(self, reg_file): |
253 |
def __load_file(self, reg_file): |
| 263 |
"""Load sub registry from opened file.""" |
254 |
"""Load sub registry from opened file.""" |
| 264 |
self.clear() |
255 |
self.clear() |
| 265 |
for line in reg_file.readlines(): |
256 |
nr = -1 |
|
|
257 |
for nr, line in enumerate(reg_file): |
| 266 |
line = re.sub(r'^[^:]*#.*$', "", line) |
258 |
line = re.sub(r'^[^:]*#.*$', "", line) |
| 267 |
if line == '': |
259 |
if not line: |
| 268 |
continue |
260 |
continue |
| 269 |
if line.find(': ') == -1: |
261 |
try: |
|
|
262 |
key, value = line.split(': ', 1) |
| 263 |
except ValueError: |
| 270 |
continue |
264 |
continue |
|
|
265 |
else: |
| 266 |
self[key] = value.strip() |
| 267 |
return nr >= 2 |
| 271 |
|
268 |
|
| 272 |
key, value = line.split(': ', 1) |
269 |
def load(self, writable=False): |
| 273 |
value = value.strip() |
|
|
| 274 |
if len(value) == 0: # if variable was set without an value |
| 275 |
value = '' |
| 276 |
|
| 277 |
self[key] = value |
| 278 |
|
| 279 |
def load(self): |
| 280 |
"""Load sub registry from file.""" |
270 |
"""Load sub registry from file.""" |
| 281 |
import_failed = False |
|
|
| 282 |
try: |
271 |
try: |
| 283 |
reg_file = open(self.file, 'r') |
272 |
if writable: |
|
|
273 |
self.reg_file = reg_file = os.fdopen(os.open(self.file, os.O_RDWR | os.O_CREAT, 0644), 'w') |
| 274 |
fcntl.flock(self.reg_file.fileno(), fcntl.LOCK_EX) |
| 275 |
if self.__load_file(reg_file): |
| 276 |
return |
| 277 |
else: |
| 278 |
reg_file = open(self.file, 'r') |
| 279 |
try: |
| 280 |
fcntl.flock(reg_file.fileno(), fcntl.LOCK_SH) |
| 281 |
if self.__load_file(reg_file): |
| 282 |
return |
| 283 |
finally: |
| 284 |
reg_file.close() |
| 284 |
except EnvironmentError: |
285 |
except EnvironmentError: |
| 285 |
import_failed = True |
286 |
pass |
| 286 |
else: |
|
|
| 287 |
if len(reg_file.readlines()) < 3: # comment or nothing |
| 288 |
import_failed = True |
| 289 |
|
| 290 |
if import_failed: |
| 291 |
try: |
| 292 |
reg_file = open(self.backup_file, 'r') |
| 293 |
except EnvironmentError: |
| 294 |
return |
| 295 |
|
287 |
|
| 296 |
reg_file.seek(0) |
288 |
# open or loading failed, try to load backup |
| 297 |
self.__load_file(reg_file) |
289 |
try: |
| 298 |
reg_file.close() |
290 |
bak_file = open(self.backup_file, 'r') |
|
|
291 |
fcntl.flock(bak_file.fileno(), fcntl.LOCK_SH) |
| 292 |
self.__load_file(bak_file) |
| 293 |
bak_file.close() |
| 294 |
except EnvironmentError: |
| 295 |
return |
| 299 |
|
296 |
|
| 300 |
if import_failed: |
297 |
# Try to write main file |
| 301 |
self.__save_file(self.file) |
298 |
try: |
|
|
299 |
if writable: |
| 300 |
self.__save_file(self.reg_file) |
| 301 |
else: |
| 302 |
reg_file = os.fdopen(os.open(self.file, os.O_RDWR | os.O_CREAT, 0644), 'w') |
| 303 |
try: |
| 304 |
fcntl.flock(reg_file.fileno(), fcntl.LOCK_EX) |
| 305 |
self.__save_file(reg_file) |
| 306 |
finally: |
| 307 |
reg_file.close() |
| 308 |
except EnvironmentError: |
| 309 |
pass |
| 302 |
|
310 |
|
| 303 |
def __save_file(self, filename): |
311 |
def __save_file(self, reg_file): |
| 304 |
"""Save sub registry to file.""" |
312 |
"""Save sub registry to file.""" |
| 305 |
try: |
313 |
try: |
| 306 |
# open temporary file for writing |
314 |
reg_file.seek(0) |
| 307 |
reg_file = open(filename, 'w') |
|
|
| 308 |
# write data to file |
315 |
# write data to file |
| 309 |
reg_file.write('# univention_ base.conf\n\n') |
316 |
reg_file.write('# univention_ base.conf\n\n') |
| 310 |
reg_file.write(self.__str__()) |
317 |
reg_file.write(self.__str__()) |
| 311 |
# flush (meta)data |
318 |
# flush (meta)data |
| 312 |
reg_file.flush() |
319 |
reg_file.flush() |
|
|
320 |
reg_file.truncate() |
| 313 |
os.fsync(reg_file.fileno()) |
321 |
os.fsync(reg_file.fileno()) |
| 314 |
# close fd |
|
|
| 315 |
reg_file.close() |
| 316 |
except EnvironmentError, ex: |
322 |
except EnvironmentError, ex: |
| 317 |
# suppress certain errors |
323 |
# suppress certain errors |
| 318 |
if ex.errno != errno.EACCES: |
324 |
if ex.errno != errno.EACCES: |
|
Lines 320-336
class _ConfigRegistry(dict):
Link Here
|
| 320 |
|
326 |
|
| 321 |
def save(self): |
327 |
def save(self): |
| 322 |
"""Save sub registry to file.""" |
328 |
"""Save sub registry to file.""" |
| 323 |
for filename in (self.backup_file, self.file): |
329 |
assert self.reg_file |
| 324 |
self.__save_file(filename) |
330 |
try: |
| 325 |
|
331 |
bak_file = os.fdopen(os.open(self.backup_file, os.O_RDWR | os.O_CREAT, 0644), 'w') |
| 326 |
def lock(self): |
332 |
try: |
| 327 |
"""Lock sub registry file.""" |
333 |
fcntl.flock(bak_file.fileno(), fcntl.LOCK_EX) |
| 328 |
self.lock_file = open(self.lock_filename, "a+") |
334 |
self.__save_file(bak_file) |
| 329 |
fcntl.flock(self.lock_file.fileno(), fcntl.LOCK_EX) |
335 |
finally: |
|
|
336 |
bak_file.close() |
| 330 |
|
337 |
|
| 331 |
def unlock(self): |
338 |
try: |
| 332 |
"""Un-lock sub registry file.""" |
339 |
self.__save_file(self.reg_file) |
| 333 |
self.lock_file.close() |
340 |
finally: |
|
|
341 |
self.reg_file.close() |
| 342 |
self.reg_file = None |
| 343 |
except EnvironmentError, ex: |
| 344 |
# suppress certain errors |
| 345 |
if ex.errno != errno.EACCES: |
| 346 |
raise |
| 334 |
|
347 |
|
| 335 |
def __getitem__(self, key): |
348 |
def __getitem__(self, key): |
| 336 |
"""Return value from sub registry.""" |
349 |
"""Return value from sub registry.""" |