Skip to content
Snippets Groups Projects
Verified Commit 234ea151 authored by Nicolas KAROLAK's avatar Nicolas KAROLAK
Browse files

use pathlib instead of os

parent 5d4ec482
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,6 @@
"""EnvSetup utilities."""
from collections import OrderedDict
import os
from pathlib import Path
import re
import subprocess
......@@ -84,7 +83,7 @@ def get_dir(file_path: str) -> str:
:rtype: str
"""
return os.path.dirname(os.path.abspath(os.path.expanduser(file_path)))
return str(Path(file_path).expanduser().resolve().parent)
def exec_cmd(cmd: Any, log_output: bool = True, get_output: bool = True) -> tuple:
......@@ -153,14 +152,14 @@ def load_conf() -> dict:
conf = {}
base_dir = get_dir(__file__)
files = (
(os.path.join(base_dir, DEFAULT_CONF_PATH), True),
(os.path.join(base_dir, AUTO_CONF_PATH), False),
(os.path.join(base_dir, CONF_PATH), False),
(str(Path(base_dir, DEFAULT_CONF_PATH)), True),
(str(Path(base_dir, AUTO_CONF_PATH)), False),
(str(Path(base_dir, CONF_PATH)), False),
)
only_default = True
override = OrderedDict()
for path, is_default in files:
if not os.path.exists(path):
if not Path(path).exists():
if is_default:
log(
"The configuration file '{}' does not exist.".format(path),
......@@ -190,7 +189,7 @@ def load_conf() -> dict:
log("\033[93mWarning:\033[0m")
log("The configuration is using only default values.")
log("Perhaps you forget to change the configuration.")
log("Path of configuration file: %s" % os.path.join(base_dir, CONF_PATH))
log("Path of configuration file: %s" % str(Path(base_dir, CONF_PATH)))
log("Perhaps you want to quit this script to change the configuration?\n")
return conf
......@@ -279,15 +278,15 @@ def run_commands(cmds: list):
raise Exception("No target file to write in.")
if (
cmd.get("backup") and
os.path.exists(cmd["target"]) and
not os.path.exists(cmd["target"] + ".back")
Path(cmd["target"]).exists() and
not Path(cmd["target"], ".back").exists()
):
os.rename(cmd["target"], cmd["target"] + ".back")
Path(cmd["target"]).rename(Path(cmd["target"], ".back"))
log("A backup file has been created for:\n%s" % cmd["target"])
# Load content from template if any
content = cmd.get("content", "")
if cmd.get("template"):
if not os.path.exists(cmd["template"]):
if not Path(cmd["template"]).exists():
raise Exception(
"Template file does not exist: %s." % cmd["template"]
)
......@@ -303,8 +302,8 @@ def run_commands(cmds: list):
elif cmd["line"] == "backup":
if not cmd.get("target"):
raise Exception("No target file to backup.")
if not os.path.exists(cmd["target"] + ".back"):
os.rename(cmd["target"], cmd["target"] + ".back")
if not Path(cmd["target"], ".back").exists():
Path(cmd["target"]).rename(Path(cmd["target"], ".back"))
log("A backup file has been created for:\n%s" % cmd["target"])
else:
log("A backup file already exist for:\n%s" % cmd["target"])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment