From 097a4fc819781f899e4f7d6ee6cf47c7631f85db Mon Sep 17 00:00:00 2001 From: Renan Rodrigo Date: Mon, 15 Jan 2024 20:12:56 -0300 Subject: [PATCH] chore: use os.path.join instead of concatenating paths Removes trailing and connection slashes from paths, and everything reads more pythonic Fixes: #2799 Signed-off-by: Renan Rodrigo --- uaclient/defaults.py | 57 +++++++++++++++++++++++------------ uaclient/files/files.py | 2 +- uaclient/log.py | 14 ++++----- uaclient/system.py | 6 ++-- uaclient/tests/test_system.py | 2 +- 5 files changed, 50 insertions(+), 31 deletions(-) diff --git a/uaclient/defaults.py b/uaclient/defaults.py index 0b825bc553..dd73abf54a 100644 --- a/uaclient/defaults.py +++ b/uaclient/defaults.py @@ -5,35 +5,54 @@ any of our dependencies installed. """ -UAC_ETC_PATH = "/etc/ubuntu-advantage/" -UAC_RUN_PATH = "/run/ubuntu-advantage/" +import os + +# Base directories +UAC_ETC_PATH = "/etc/ubuntu-advantage" +UAC_RUN_PATH = "/run/ubuntu-advantage" DEFAULT_DATA_DIR = "/var/lib/ubuntu-advantage" +DEFAULT_LOG_DIR = "/var/log" + + +# Relative paths MACHINE_TOKEN_FILE = "machine-token.json" +CONFIG_FILE = "uaclient.conf" +USER_CONFIG_FILE = "user-config.json" +CANDIDATE_VERSION_FILE = "candidate-version" +DEFAULT_LOG_FILE_BASE_NAME = "ubuntu-advantage" PRIVATE_SUBDIR = "private" -DEFAULT_PRIVATE_MACHINE_TOKEN_PATH = ( - DEFAULT_DATA_DIR + "/" + PRIVATE_SUBDIR + "/" + MACHINE_TOKEN_FILE +MESSAGES_SUBDIR = "messages" +USER_CACHE_SUBDIR = "ubuntu-pro" +NOTICES_SUBDIR = "notices" +PRIVATE_ESM_CACHE_SUBDIR = "apt-esm" + +DEFAULT_PRIVATE_MACHINE_TOKEN_PATH = os.path.join( + DEFAULT_DATA_DIR, PRIVATE_SUBDIR, MACHINE_TOKEN_FILE +) +DEFAULT_PRIVATE_DATA_DIR = os.path.join(DEFAULT_DATA_DIR, PRIVATE_SUBDIR) +MESSAGES_DIR = os.path.join(DEFAULT_DATA_DIR, MESSAGES_SUBDIR) +DEFAULT_CONFIG_FILE = os.path.join(UAC_ETC_PATH, CONFIG_FILE) +CANDIDATE_CACHE_PATH = os.path.join(UAC_RUN_PATH, CANDIDATE_VERSION_FILE) +DEFAULT_USER_CONFIG_JSON_FILE = os.path.join( + DEFAULT_DATA_DIR, USER_CONFIG_FILE ) -DEFAULT_PRIVATE_DATA_DIR = DEFAULT_DATA_DIR + "/" + PRIVATE_SUBDIR -MESSAGES_SUBDIR = "/messages" -MESSAGES_DIR = DEFAULT_DATA_DIR + MESSAGES_SUBDIR -CANDIDATE_CACHE_PATH = UAC_RUN_PATH + "candidate-version" -DEFAULT_CONFIG_FILE = UAC_ETC_PATH + "uaclient.conf" -DEFAULT_USER_CONFIG_JSON_FILE = DEFAULT_DATA_DIR + "/user-config.json" -DEFAULT_UPGRADE_CONTRACT_FLAG_FILE = UAC_ETC_PATH + "request-update-contract" +DEFAULT_LOG_PREFIX = os.path.join(DEFAULT_LOG_DIR, DEFAULT_LOG_FILE_BASE_NAME) +ESM_APT_ROOTDIR = os.path.join(DEFAULT_DATA_DIR, PRIVATE_ESM_CACHE_SUBDIR) +NOTICES_PERMANENT_DIRECTORY = os.path.join(DEFAULT_DATA_DIR, NOTICES_SUBDIR) +NOTICES_TEMPORARY_DIRECTORY = os.path.join(UAC_RUN_PATH, NOTICES_SUBDIR) + + +# URLs BASE_CONTRACT_URL = "https://contracts.canonical.com" BASE_SECURITY_URL = "https://ubuntu.com/security" BASE_LIVEPATCH_URL = "https://livepatch.canonical.com" APT_NEWS_URL = "https://motd.ubuntu.com/aptnews.json" -CLOUD_BUILD_INFO = "/etc/cloud/build.info" -ESM_APT_ROOTDIR = DEFAULT_DATA_DIR + "/apt-esm/" PRINT_WRAP_WIDTH = 80 CONTRACT_EXPIRY_GRACE_PERIOD_DAYS = 14 CONTRACT_EXPIRY_PENDING_DAYS = 20 ATTACH_FAIL_DATE_FORMAT = "%B %d, %Y" -DEFAULT_LOG_DIR = "/var/log" -DEFAULT_LOG_FILE_BASE_NAME = "ubuntu-advantage" -DEFAULT_LOG_PREFIX = DEFAULT_LOG_DIR + "/" + DEFAULT_LOG_FILE_BASE_NAME + DEFAULT_LOG_FORMAT = ( "%(asctime)s - %(filename)s:(%(lineno)d) [%(levelname)s]: %(message)s" ) @@ -43,7 +62,7 @@ "security_url": BASE_SECURITY_URL, "data_dir": DEFAULT_DATA_DIR, "log_level": "debug", - "log_file": "/var/log/ubuntu-advantage.log", + "log_file": "{}.log".format(DEFAULT_LOG_PREFIX), } CONFIG_FIELD_ENVVAR_ALLOWLIST = [ @@ -55,10 +74,8 @@ ROOT_READABLE_MODE = 0o600 WORLD_READABLE_MODE = 0o644 -NOTICES_PERMANENT_DIRECTORY = DEFAULT_DATA_DIR + "/notices/" -NOTICES_TEMPORARY_DIRECTORY = UAC_RUN_PATH + "notices/" -USER_CACHE_SUBDIR = "ubuntu-pro" +CLOUD_BUILD_INFO = "/etc/cloud/build.info" SSL_CERTS_PATH = "/etc/ssl/certs/ca-certificates.crt" # used by apport, collect-logs, and tests diff --git a/uaclient/files/files.py b/uaclient/files/files.py index a35c065f5a..fb6b92a60f 100644 --- a/uaclient/files/files.py +++ b/uaclient/files/files.py @@ -79,7 +79,7 @@ def __init__( ): file_name = defaults.MACHINE_TOKEN_FILE self.private_file = UAFile( - file_name, directory + "/" + defaults.PRIVATE_SUBDIR + file_name, os.path.join(directory, defaults.PRIVATE_SUBDIR) ) self.public_file = UAFile(file_name, directory, False) self.machine_token_overlay_path = machine_token_overlay_path diff --git a/uaclient/log.py b/uaclient/log.py index 05e658af2b..e55a53cb95 100644 --- a/uaclient/log.py +++ b/uaclient/log.py @@ -63,7 +63,7 @@ def format(self, record: logging.LogRecord) -> str: def get_user_log_file() -> str: """Gets the correct user log_file storage location""" - return system.get_user_cache_dir() + "/ubuntu-pro.log" + return os.path.join(system.get_user_cache_dir(), "ubuntu-pro.log") def get_all_user_log_files() -> List[str]: @@ -74,12 +74,12 @@ def get_all_user_log_files() -> List[str]: user_directories = os.listdir("/home") log_files = [] for user_directory in user_directories: - user_path = ( - "/home/" - + user_directory - + "/.cache/" - + defaults.USER_CACHE_SUBDIR - + "/ubuntu-pro.log" + user_path = os.path.join( + "/home", + user_directory, + ".cache", + defaults.USER_CACHE_SUBDIR, + "ubuntu-pro.log", ) if os.path.isfile(user_path): log_files.append(user_path) diff --git a/uaclient/system.py b/uaclient/system.py index fe105f19b8..7ae2d3c641 100644 --- a/uaclient/system.py +++ b/uaclient/system.py @@ -753,9 +753,11 @@ def get_user_cache_dir() -> str: xdg_cache_home = os.environ.get("XDG_CACHE_HOME") if xdg_cache_home: - return xdg_cache_home + "/" + defaults.USER_CACHE_SUBDIR + return os.path.join(xdg_cache_home, defaults.USER_CACHE_SUBDIR) - return os.path.expanduser("~") + "/.cache/" + defaults.USER_CACHE_SUBDIR + return os.path.join( + os.path.expanduser("~"), ".cache", defaults.USER_CACHE_SUBDIR + ) def get_reboot_required_pkgs() -> Optional[RebootRequiredPkgs]: diff --git a/uaclient/tests/test_system.py b/uaclient/tests/test_system.py index 4e07683808..b8db4353b2 100644 --- a/uaclient/tests/test_system.py +++ b/uaclient/tests/test_system.py @@ -1418,7 +1418,7 @@ class TestGetUserCacheDir: "expected", ], ( - (True, None, None, "/run/ubuntu-advantage/"), + (True, None, None, "/run/ubuntu-advantage"), (False, None, "/home/user", "/home/user/.cache/ubuntu-pro"), (False, "/something", "/home/user", "/something/ubuntu-pro"), ),