/usr/lib/python3/dist-packages/lib389/dseldif.py is in python3-lib389 1.3.7.10-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 93 94 95 96 97 98 99 100 | # --- BEGIN COPYRIGHT BLOCK ---
# Copyright (C) 2017 Red Hat, Inc.
# All rights reserved.
#
# License: GPL (version 3 or any later version).
# See LICENSE for details.
# --- END COPYRIGHT BLOCK ---
#
import os
from lib389.paths import Paths
class DSEldif(object):
"""A class for working with dse.ldif file"""
def __init__(self, instance):
self._instance = instance
ds_paths = Paths(self._instance.serverid, self._instance)
self.path = os.path.join(ds_paths.config_dir, 'dse.ldif')
with open(self.path, 'r') as file_dse:
self._contents = file_dse.readlines()
def _update(self):
"""Update the dse.ldif with a new contents"""
with open(self.path, "w") as file_dse:
file_dse.write("".join(self._contents))
def _find_attr(self, entry_dn, attr):
"""Find all attribute values and indexes under a given entry
Returns entry dn index and attribute data dict:
relative attribute indexes and the attribute value
"""
entry_dn_i = self._contents.index("dn: {}\n".format(entry_dn))
attr_data = {}
# Find where the entry ends
try:
dn_end_i = self._contents[entry_dn_i:].index("\n")
except ValueError:
# We are in the end of the list
dn_end_i = len(self._contents)
entry_slice = self._contents[entry_dn_i:entry_dn_i + dn_end_i]
# Find the attribute
for line in entry_slice:
if line.startswith("{}:".format(attr)):
attr_value = line.split(" ", 1)[1][:-1]
attr_data.update({entry_slice.index(line): attr_value})
if not attr_data:
raise ValueError("Attribute {} wasn't found under dn: {}".format(attr, entry_dn))
return entry_dn_i, attr_data
def get(self, entry_dn, attr):
"""Return attribute values under a given entry"""
try:
_, attr_data = self._find_attr(entry_dn, attr)
except ValueError:
return None
return attr_data.values()
def add(self, entry_dn, attr, value):
"""Add an attribute under a given entry"""
entry_dn_i = self._contents.index("dn: {}\n".format(entry_dn))
self._contents.insert(entry_dn_i+1, "{}: {}\n".format(attr, value))
self._update()
def delete(self, entry_dn, attr, value=None):
"""Delete attributes under a given entry"""
entry_dn_i, attr_data = self._find_attr(entry_dn, attr)
if value is not None:
for attr_i, attr_value in attr_data.items():
if attr_value == value:
del self._contents[entry_dn_i + attr_i]
else:
for attr_i in sorted(attr_data.keys(), reverse=True):
del self._contents[entry_dn_i + attr_i]
self._update()
def replace(self, entry_dn, attr, value):
"""Replace attribute values with a new one under a given entry"""
try:
self.delete(entry_dn, attr)
except ValueError as e:
self._instance.log.debug("During replace operation: {}".format(e))
self.add(entry_dn, attr, value)
self._update()
|