Skip to content

Commit

Permalink
apt: use a decorator to ensure apt_pkg is initialized
Browse files Browse the repository at this point in the history
Instead of performing the check at the module level, we decorate the
functions that use the module without instantiating a Cache object (in
the Cache case, we initialize apt_pkg separately)

Signed-off-by: Renan Rodrigo <[email protected]>
  • Loading branch information
renanrodrigo committed Oct 2, 2023
1 parent 32f3ea1 commit bf7671b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
27 changes: 19 additions & 8 deletions uaclient/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import subprocess
import sys
import tempfile
from functools import lru_cache
from functools import lru_cache, wraps
from typing import Dict, Iterable, List, NamedTuple, Optional, Set, Union

import apt_pkg # type: ignore
Expand Down Expand Up @@ -83,12 +83,6 @@
event = event_logger.get_event_logger()
LOG = logging.getLogger(util.replace_top_level_logger_name(__name__))

# Most parts of the apt_pkg functionality needs the module to be initialized.
# This call is checking for the 'Dir' configuration - which needs to be there
# for apt_pkg to be ready - and if it is empty we initialize.
if apt_pkg.config.get("Dir") == "":
apt_pkg.init()


@enum.unique
class AptProxyScope(enum.Enum):
Expand All @@ -100,7 +94,24 @@ class AptProxyScope(enum.Enum):
"InstalledAptPackage", [("name", str), ("version", str), ("arch", str)]
)

version_compare = apt_pkg.version_compare

def ensure_apt_pkg_init(f):
"""Decorator ensuring apt_pkg is initialized."""

@wraps(f)
def new_f(*args, **kwargs):
# This call is checking for the 'Dir' configuration - which needs to be
# there for apt_pkg to be ready - and if it is empty we initialize.
if apt_pkg.config.get("Dir") == "":
apt_pkg.init()
return f(*args, **kwargs)

return new_f


@ensure_apt_pkg_init
def version_compare(a: str, b: str):
return apt_pkg.version_compare(a, b)


def assert_valid_apt_credentials(repo_url, username, password):
Expand Down
3 changes: 2 additions & 1 deletion uaclient/apt_news.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from uaclient import defaults, messages, system, util
from uaclient.api.u.pro.status.is_attached.v1 import _is_attached
from uaclient.apt import ensure_apt_pkg_init
from uaclient.clouds.identity import get_cloud_type
from uaclient.config import UAConfig
from uaclient.contract import ContractExpiryStatus, get_contract_expiry_status
Expand Down Expand Up @@ -149,6 +150,7 @@ def select_message(
return None


@ensure_apt_pkg_init
def fetch_aptnews_json(cfg: UAConfig):
os.makedirs(defaults.UAC_RUN_PATH, exist_ok=True)
acq = apt_pkg.Acquire()
Expand Down Expand Up @@ -214,7 +216,6 @@ def update_apt_news(cfg: UAConfig):
try:
news = local_apt_news(cfg)
if not news:
apt_pkg.init()
news = fetch_and_process_apt_news(cfg)
if news:
state_files.apt_news_raw_file.write(news)
Expand Down

0 comments on commit bf7671b

Please sign in to comment.