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 @@ ...@@ -3,7 +3,6 @@
"""EnvSetup utilities.""" """EnvSetup utilities."""
from collections import OrderedDict from collections import OrderedDict
import os
from pathlib import Path from pathlib import Path
import re import re
import subprocess import subprocess
...@@ -84,7 +83,7 @@ def get_dir(file_path: str) -> str: ...@@ -84,7 +83,7 @@ def get_dir(file_path: str) -> str:
:rtype: 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: def exec_cmd(cmd: Any, log_output: bool = True, get_output: bool = True) -> tuple:
...@@ -153,14 +152,14 @@ def load_conf() -> dict: ...@@ -153,14 +152,14 @@ def load_conf() -> dict:
conf = {} conf = {}
base_dir = get_dir(__file__) base_dir = get_dir(__file__)
files = ( files = (
(os.path.join(base_dir, DEFAULT_CONF_PATH), True), (str(Path(base_dir, DEFAULT_CONF_PATH)), True),
(os.path.join(base_dir, AUTO_CONF_PATH), False), (str(Path(base_dir, AUTO_CONF_PATH)), False),
(os.path.join(base_dir, CONF_PATH), False), (str(Path(base_dir, CONF_PATH)), False),
) )
only_default = True only_default = True
override = OrderedDict() override = OrderedDict()
for path, is_default in files: for path, is_default in files:
if not os.path.exists(path): if not Path(path).exists():
if is_default: if is_default:
log( log(
"The configuration file '{}' does not exist.".format(path), "The configuration file '{}' does not exist.".format(path),
...@@ -190,7 +189,7 @@ def load_conf() -> dict: ...@@ -190,7 +189,7 @@ def load_conf() -> dict:
log("\033[93mWarning:\033[0m") log("\033[93mWarning:\033[0m")
log("The configuration is using only default values.") log("The configuration is using only default values.")
log("Perhaps you forget to change the configuration.") 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") log("Perhaps you want to quit this script to change the configuration?\n")
return conf return conf
...@@ -279,15 +278,15 @@ def run_commands(cmds: list): ...@@ -279,15 +278,15 @@ def run_commands(cmds: list):
raise Exception("No target file to write in.") raise Exception("No target file to write in.")
if ( if (
cmd.get("backup") and cmd.get("backup") and
os.path.exists(cmd["target"]) and Path(cmd["target"]).exists() and
not os.path.exists(cmd["target"] + ".back") 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"]) log("A backup file has been created for:\n%s" % cmd["target"])
# Load content from template if any # Load content from template if any
content = cmd.get("content", "") content = cmd.get("content", "")
if cmd.get("template"): if cmd.get("template"):
if not os.path.exists(cmd["template"]): if not Path(cmd["template"]).exists():
raise Exception( raise Exception(
"Template file does not exist: %s." % cmd["template"] "Template file does not exist: %s." % cmd["template"]
) )
...@@ -303,8 +302,8 @@ def run_commands(cmds: list): ...@@ -303,8 +302,8 @@ def run_commands(cmds: list):
elif cmd["line"] == "backup": elif cmd["line"] == "backup":
if not cmd.get("target"): if not cmd.get("target"):
raise Exception("No target file to backup.") raise Exception("No target file to backup.")
if not os.path.exists(cmd["target"] + ".back"): if 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"]) log("A backup file has been created for:\n%s" % cmd["target"])
else: else:
log("A backup file already exist for:\n%s" % cmd["target"]) 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