From 8306f076b3a2d4c635eee058d8b2c04e7018599a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Sun, 22 Apr 2018 23:12:34 +0200 Subject: [PATCH] Move path creation into configpaths module --- gajim/common/check_paths.py | 50 ------------------------------------- gajim/common/configpaths.py | 24 ++++++++++++++++++ gajim/gajim.py | 3 +-- 3 files changed, 25 insertions(+), 52 deletions(-) delete mode 100644 gajim/common/check_paths.py diff --git a/gajim/common/check_paths.py b/gajim/common/check_paths.py deleted file mode 100644 index 2a4180898..000000000 --- a/gajim/common/check_paths.py +++ /dev/null @@ -1,50 +0,0 @@ -# -*- coding:utf-8 -*- -## src/common/check_paths.py -## -## Copyright (C) 2005-2006 Travis Shirk -## Nikos Kouremenos -## Copyright (C) 2005-2014 Yann Leboulanger -## Copyright (C) 2006 Dimitur Kirov -## Copyright (C) 2007 Tomasz Melcer -## Copyright (C) 2008 Jean-Marie Traissard -## -## This file is part of Gajim. -## -## Gajim 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; version 3 only. -## -## Gajim 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 Gajim. If not, see . -## - -import os -import sys - -from gajim.common import configpaths -from gajim.common.const import PathType - - -def check_and_possibly_create_paths(): - for path in configpaths.get_paths(PathType.FOLDER): - if not os.path.exists(path): - create_path(path) - elif os.path.isfile(path): - print(_('%s is a file but it should be a directory') % path) - print(_('Gajim will now exit')) - sys.exit() - - -def create_path(directory): - head, tail = os.path.split(directory) - if not os.path.exists(head): - create_path(head) - if os.path.exists(directory): - return - print(('creating %s directory') % directory) - os.mkdir(directory, 0o700) diff --git a/gajim/common/configpaths.py b/gajim/common/configpaths.py index 387145414..6613f9b90 100644 --- a/gajim/common/configpaths.py +++ b/gajim/common/configpaths.py @@ -24,7 +24,9 @@ ## import os +import sys import tempfile +from pathlib import Path from gajim.common.const import PathType, PathLocation @@ -57,6 +59,28 @@ def init(): _paths.init() +def create_paths(): + for path in get_paths(PathType.FOLDER): + if not isinstance(path, Path): + path = Path(path) + + if path.is_file(): + print(_('%s is a file but it should be a directory') % path) + print(_('Gajim will now exit')) + sys.exit() + + if not path.exists(): + for parent_path in reversed(path.parents): + # Create all parent folders + # dont use mkdir(parent=True), as it ignores `mode` + # when creating the parents + if not parent_path.exists(): + print(('creating %s directory') % parent_path) + parent_path.mkdir(mode=0o700) + print(('creating %s directory') % path) + path.mkdir(mode=0o700) + + class ConfigPaths: def __init__(self): self._paths = {} diff --git a/gajim/gajim.py b/gajim/gajim.py index 5014d8a67..c244bf87b 100644 --- a/gajim/gajim.py +++ b/gajim/gajim.py @@ -156,12 +156,11 @@ class GajimApplication(Gtk.Application): # Create and initialize Application Paths & Databases from gajim.common import app - from gajim.common import check_paths + configpaths.create_paths() from gajim.common import exceptions from gajim.common import logger from gajim.common import caps_cache try: - check_paths.check_and_possibly_create_paths() app.logger = logger.Logger() caps_cache.initialize(app.logger) except exceptions.DatabaseMalformed as error: