mirror of
https://github.com/altlinux/gpupdate.git
synced 2025-03-22 02:50:32 +03:00
Merge pull request #75 from altlinux/folder_applier
Applier for Folders.xml
This commit is contained in:
commit
400ea0aced
72
gpoa/frontend/appliers/folder.py
Normal file
72
gpoa/frontend/appliers/folder.py
Normal file
@ -0,0 +1,72 @@
|
||||
#
|
||||
# GPOA - GPO Applier for Linux
|
||||
#
|
||||
# Copyright (C) 2019-2020 BaseALT Ltd.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
from gpt.folders import (
|
||||
FileAction
|
||||
, action_letter2enum
|
||||
)
|
||||
|
||||
def remove_dir_tree(path, delete_files=False, delete_folder=False, delete_sub_folders=False):
|
||||
for entry in path.iterdir():
|
||||
if entry.is_file():
|
||||
entry.unlink()
|
||||
if entry.is_dir():
|
||||
if delete_sub_folders:
|
||||
remove_dir_tree(entry,
|
||||
delete_files,
|
||||
delete_folder,
|
||||
delete_sub_folders)
|
||||
|
||||
if delete_folder:
|
||||
path.rmdir()
|
||||
|
||||
def str2bool(boolstr):
|
||||
if boolstr.lower in ['true', 'yes', '1']:
|
||||
return True
|
||||
return False
|
||||
|
||||
class Folder:
|
||||
def __init__(self, folder_object):
|
||||
self.folder_path = Path(folder_object.path)
|
||||
self.action = action_letter2enum(folder_object.action)
|
||||
self.delete_files = str2bool(folder_object.delete_files)
|
||||
self.delete_folder = str2bool(folder_object.delete_folder)
|
||||
self.delete_sub_folders = str2bool(folder_object.delete_sub_folders)
|
||||
|
||||
def _create_action(self):
|
||||
self.folder_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def _delete_action(self):
|
||||
remove_dir_tree(self.folder_path,
|
||||
self.delete_files,
|
||||
self.delete_folders,
|
||||
self.delete_sub_folders)
|
||||
|
||||
def action(self):
|
||||
if self.action == FileAction.CREATE:
|
||||
self._create_action()
|
||||
if self.action == FileAction.DELETE:
|
||||
self._delete_action()
|
||||
if self.action == FileAction.REPLACE:
|
||||
self._delete_action()
|
||||
self._create_action()
|
||||
|
55
gpoa/frontend/folder_applier.py
Normal file
55
gpoa/frontend/folder_applier.py
Normal file
@ -0,0 +1,55 @@
|
||||
#
|
||||
# GPOA - GPO Applier for Linux
|
||||
#
|
||||
# Copyright (C) 2019-2020 BaseALT Ltd.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from .applier_frontend import applier_frontend
|
||||
from .appliers.folder import Folder
|
||||
from util.logging import slogm
|
||||
|
||||
import logging
|
||||
|
||||
class folder_applier(applier_frontend):
|
||||
def __init__(self, storage, sid):
|
||||
self.storage = storage
|
||||
self.sid = sid
|
||||
self.folders = self.storage.get_folders(self.sid)
|
||||
|
||||
def apply(self):
|
||||
for directory_obj in self.folders:
|
||||
fld = Folder(directory_obj)
|
||||
fld.action()
|
||||
|
||||
class folder_applier_user(applier_frontend):
|
||||
def __init__(self, storage, sid, username):
|
||||
self.storage = storage
|
||||
self.sid = sid
|
||||
self.username = username
|
||||
self.folders = self.storage.get_folders(self.sid)
|
||||
|
||||
def admin_context_apply(self):
|
||||
for directory_obj in self.folders:
|
||||
fld = Folder(directory_obj)
|
||||
fld.action()
|
||||
|
||||
def user_context_apply(self):
|
||||
print('folder:user:user')
|
||||
for directory_obj in self.folders:
|
||||
fld = Folder(directory_obj)
|
||||
fld.action()
|
||||
|
@ -33,6 +33,10 @@ from .gsettings_applier import (
|
||||
gsettings_applier,
|
||||
gsettings_applier_user
|
||||
)
|
||||
from .folder_applier import (
|
||||
folder_applier
|
||||
, folder_applier_user
|
||||
)
|
||||
from .cifs_applier import cifs_applier_user
|
||||
from util.windows import get_sid
|
||||
from util.users import (
|
||||
@ -88,6 +92,7 @@ class frontend_manager:
|
||||
, 'shortcuts': shortcut_applier(self.storage)
|
||||
, 'gsettings': gsettings_applier(self.storage)
|
||||
, 'cups': cups_applier(self.storage)
|
||||
, 'folders': folder_applier(self.storage, self.sid)
|
||||
#, 'package': package_applier(self.storage)
|
||||
})
|
||||
|
||||
@ -95,6 +100,7 @@ class frontend_manager:
|
||||
# files and settings, mostly in $HOME.
|
||||
self.user_appliers = dict({
|
||||
'shortcuts': shortcut_applier_user(self.storage, self.sid, self.username),
|
||||
'folders': folder_applier_user(self.storage, self.sid, self.username),
|
||||
'gsettings': gsettings_applier_user(self.storage, self.sid, self.username),
|
||||
'cifs': cifs_applier_user(self.storage, self.sid, self.username)
|
||||
})
|
||||
@ -118,15 +124,18 @@ class frontend_manager:
|
||||
if is_root():
|
||||
logging.debug(slogm('Running user appliers from administrator context'))
|
||||
self.user_appliers['shortcuts'].admin_context_apply()
|
||||
self.user_appliers['folders'].admin_context_apply()
|
||||
self.user_appliers['gsettings'].admin_context_apply()
|
||||
self.user_appliers['cifs'].admin_context_apply()
|
||||
|
||||
logging.debug(slogm('Running user appliers for user context'))
|
||||
with_privileges(self.username, self.user_appliers['shortcuts'].user_context_apply)
|
||||
with_privileges(self.username, self.user_appliers['folders'].user_context_apply)
|
||||
with_privileges(self.username, self.user_appliers['gsettings'].user_context_apply)
|
||||
else:
|
||||
logging.debug(slogm('Running user appliers from user context'))
|
||||
self.user_appliers['shortcuts'].user_context_apply()
|
||||
self.user_appliers['folders'].user_context_apply()
|
||||
self.user_appliers['gsettings'].user_context_apply()
|
||||
|
||||
def apply_parameters(self):
|
||||
|
@ -26,12 +26,16 @@ from util.xml import get_xml_root
|
||||
class FileAction(Enum):
|
||||
CREATE = 'C'
|
||||
REPLACE = 'R'
|
||||
UPDATE = 'U'
|
||||
DELETE = 'D'
|
||||
|
||||
|
||||
def action_letter2enum(letter):
|
||||
if letter in ['C', 'R']:
|
||||
if letter in ['C', 'R', 'U', 'D']:
|
||||
if letter == 'C': return FileAction.CREATE
|
||||
if letter == 'R': return FileAction.REPLACE
|
||||
if letter == 'U': return FileAction.UPDATE
|
||||
if letter == 'D': return FileAction.DELETE
|
||||
|
||||
return FileAction.CREATE
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user