ensuring ownership of all created files and folders

This commit is contained in:
sezanzeb 2020-11-29 18:24:50 +01:00
parent 58b7d16a3f
commit 27b47e475d
5 changed files with 39 additions and 18 deletions

View File

@ -27,7 +27,7 @@ import json
import shutil import shutil
import copy import copy
from keymapper.paths import CONFIG from keymapper.paths import CONFIG, touch
from keymapper.logger import logger from keymapper.logger import logger
@ -98,10 +98,7 @@ class _Config:
def save_config(self): def save_config(self):
"""Save the config to the file system.""" """Save the config to the file system."""
if not os.path.exists(CONFIG_PATH): touch(CONFIG_PATH)
logger.debug('Creating "%s"', CONFIG_PATH)
os.makedirs(os.path.dirname(CONFIG_PATH), exist_ok=True)
os.mknod(CONFIG_PATH)
with open(CONFIG_PATH, 'w') as file: with open(CONFIG_PATH, 'w') as file:
json.dump(self._config, file, indent=4) json.dump(self._config, file, indent=4)

View File

@ -113,8 +113,7 @@ def add_filehandler(path='~/.log/key-mapper'):
log_path = os.path.expanduser(path) log_path = os.path.expanduser(path)
log_file = os.path.join(log_path, 'log') log_file = os.path.join(log_path, 'log')
if not os.path.exists(log_path): os.makedirs(log_path, exist_ok=True)
os.makedirs(log_path)
if os.path.exists(log_file): if os.path.exists(log_file):
# keep the log path small, start from scratch each time # keep the log path small, start from scratch each time

View File

@ -24,11 +24,10 @@
import os import os
import json import json
import shutil
import copy import copy
from keymapper.logger import logger from keymapper.logger import logger
from keymapper.paths import get_config_path from keymapper.paths import get_config_path, touch
def keep_reverse_mapping_intact(func): def keep_reverse_mapping_intact(func):
@ -171,12 +170,7 @@ class Mapping:
path = get_config_path(device, preset) path = get_config_path(device, preset)
logger.info('Saving preset to %s', path) logger.info('Saving preset to %s', path)
if not os.path.exists(path): touch(path)
logger.debug('Creating "%s"', path)
os.makedirs(os.path.dirname(path), exist_ok=True)
os.mknod(path)
# if this is done with sudo rights, give the file to the user
shutil.chown(path, os.getlogin(), os.getlogin())
with open(path, 'w') as file: with open(path, 'w') as file:
# make sure to keep the option to add metadata if ever needed, # make sure to keep the option to add metadata if ever needed,

View File

@ -23,11 +23,43 @@
import os import os
import shutil
from keymapper.logger import logger
CONFIG = os.path.join('/home', os.getlogin(), '.config/key-mapper') CONFIG = os.path.join('/home', os.getlogin(), '.config/key-mapper')
def touch(path):
"""Create an empty file and all its parent dirs, give it to the user."""
if os.path.exists(path):
return
logger.info('Creating file "%s"', path)
folder = os.path.dirname(path)
if not os.path.exists(folder):
os.makedirs(folder, exist_ok=True)
# in case this is running as sudo
shutil.chown(folder, os.getlogin(), os.getlogin())
os.mknod(path)
shutil.chown(path, os.getlogin(), os.getlogin())
def mkdir(path):
"""Create a folder, give it to the user."""
if os.path.exists(path):
return
logger.info('Creating dir "%s"', path)
os.makedirs(path, exist_ok=True)
# in case this is running as sudo
shutil.chown(path, os.getlogin(), os.getlogin())
def get_config_path(device=None, preset=None): def get_config_path(device=None, preset=None):
"""Get a path to the stored preset, or to store a preset to.""" """Get a path to the stored preset, or to store a preset to."""
if device is None: if device is None:

View File

@ -26,7 +26,7 @@ import os
import time import time
import glob import glob
from keymapper.paths import get_config_path from keymapper.paths import get_config_path, mkdir
from keymapper.logger import logger from keymapper.logger import logger
from keymapper.getdevices import get_devices from keymapper.getdevices import get_devices
@ -53,8 +53,7 @@ def get_presets(device):
device : string device : string
""" """
device_folder = get_config_path(device) device_folder = get_config_path(device)
if not os.path.exists(device_folder): mkdir(device_folder)
os.makedirs(device_folder)
paths = glob.glob(os.path.join(device_folder, '*.json')) paths = glob.glob(os.path.join(device_folder, '*.json'))
presets = [ presets = [