Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _init_children(self, parent):
"""
Initialise each node's children - essentially build the tree.
"""
for dir_name in self.get_children_paths(parent.full_path):
child = Node(name=dir_name, parent=parent)
parent.children.append(child)
self._init_children(child)
def delete_cgroup(self, name):
"""
Delete a cgroup by name and detach it from this node.
Raises OSError if the cgroup is not empty.
"""
name = name.encode()
fp = os.path.join(self.full_path, name)
if os.path.exists(fp):
os.rmdir(fp)
node = Node(name, parent=self)
try:
self.children.remove(node)
except ValueError:
return
def create_cgroup(self, name):
"""
Create a cgroup by name and attach it under this node.
"""
node = Node(name, parent=self)
if node in self.children:
raise RuntimeError('Node {} already exists under {}'.format(name, self.path))
name = name.encode()
fp = os.path.join(self.full_path, name)
os.mkdir(fp)
self.children.append(node)
return node
def _build_tree(self):
"""
Build a full or a partial tree, depending on the groups/sub-groups specified.
"""
groups = self._groups or self.get_children_paths(self.root_path)
for group in groups:
node = Node(name=group, parent=self.root)
self.root.children.append(node)
self._init_sub_groups(node)
def _init_sub_groups(self, parent):
"""
Initialise sub-groups, and create any that do not already exist.
"""
if self._sub_groups:
for sub_group in self._sub_groups:
for component in split_path_components(sub_group):
fp = os.path.join(parent.full_path, component)
if os.path.exists(fp):
node = Node(name=component, parent=parent)
parent.children.append(node)
else:
node = parent.create_cgroup(component)
parent = node
self._init_children(node)
else:
self._init_children(parent)
def bootstrap(root_path=b"/sys/fs/cgroup/", subsystems=None):
if not os.path.ismount(root_path):
if not os.path.isdir(root_path):
os.makedirs(root_path)
try:
mount(b"cgroup_root", root_path, b"tmpfs")
except RuntimeError:
os.rmdir(root_path)
raise
if subsystems is None:
subsystems = Node.CONTROLLERS.keys()
for subsystem in subsystems:
path = root_path+subsystem
if not os.path.ismount(path):
if not os.path.isdir(path):
os.makedirs(path)
try:
mount(subsystem, path, b"cgroup", subsystem)
except RuntimeError:
os.rmdir(path)
raise
def __init__(self, root_path="/sys/fs/cgroup/", groups=None, sub_groups=None):
self.root_path = root_path
self._groups = groups or []
self._sub_groups = sub_groups or []
self.root = Node(root_path)
self._build_tree()