Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move product.py to core #5002

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions pyanaconda/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,18 @@

# Used for digits, ascii_letters, punctuation constants
import string
from enum import Enum

from pyanaconda.core.i18n import N_
from pyanaconda.core.product import get_product_name, get_product_version

from enum import Enum

# Use -1 to indicate that the selinux configuration is unset
SELINUX_DEFAULT = -1

# where to look for 3rd party addons
ADDON_PATHS = ["/usr/share/anaconda/addons"]

# common string needs to be easy to change
from pyanaconda import product
productName = product.productName
isFinal = product.isFinal
shortProductName = product.shortProductName

# The default virtio port.
VIRTIO_PORT = "/dev/virtio-ports/org.fedoraproject.anaconda.log.0"

Expand All @@ -56,7 +52,7 @@

# Get list of repo names witch should be used as base repo
DEFAULT_REPOS = [
product.productName.split('-')[0].lower(), # pylint: disable=no-member
get_product_name().split('-')[0].lower(), # pylint: disable=no-member
"fedora-modular-server",
"rawhide",
"BaseOS", # Used by RHEL
Expand Down Expand Up @@ -101,7 +97,7 @@
NETWORK_CONNECTED_CHECK_INTERVAL = 0.1 # in seconds

# Anaconda user agent
USER_AGENT = "%s (anaconda)/%s" % (product.productName, product.productVersion)
USER_AGENT = "%s (anaconda)/%s" % (get_product_name(), get_product_version())

# Thread names
THREAD_EXECUTE_STORAGE = "AnaExecuteStorageThread"
Expand Down
116 changes: 116 additions & 0 deletions pyanaconda/core/product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#
# Copyright (C) 2023 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties 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 this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
import os
import configparser
from functools import cache
from collections import namedtuple


ProductData = namedtuple("ProductData", [
"is_final_release",
"name",
"version",
"short_name",
])


def trim_product_version_for_ui(version):
"""Trim off parts of version that should not be displayed in UI.

Example: 8.0.1 -> 8.0

:param str version: Version as read from the system
:return str: Shortened version
"""
if version.count('.') >= 2:
version = '.'.join(version.split('.')[:2])

# Correctly report Rawhide
if version == "development":
version = "rawhide"

return version


def shorten_product_name(long_name):
"""Shorten a product name.

This is used in device names. eg. "fedora", "rhel".

:param str long_name: Name of the product, as read from the system
:return str: Short name for the product
"""
product_short_name = long_name.lower() # pylint: disable=no-member

if long_name.count(" "): # pylint: disable=no-member
product_short_name = ''.join(s[0] for s in product_short_name.split())

return product_short_name


@cache
def get_product_values():
"""Provide product data based on available inputs.

Order of precedence for the values is:
1) Buildstamp file specified by the PRODBUILDPATH environment variable
2) Buildstamp file /.buildstamp
3) Environment variable ANACONDA_ISFINAL
4) In absence of any data, fall back to "false"

:return: Data about product
:rtype: ProductData
"""

# First, load in the defaults. In order of precedence: contents of
# .buildstamp, environment, stupid last ditch hardcoded defaults.
config = configparser.ConfigParser()
config.add_section("Main")
config.set("Main", "IsFinal", os.environ.get("ANACONDA_ISFINAL", "false"))
config.set("Main", "Product", os.environ.get("ANACONDA_PRODUCTNAME", "anaconda"))
config.set("Main", "Version", os.environ.get("ANACONDA_PRODUCTVERSION", "bluesky"))

# Now read in the .buildstamp file, wherever it may be.
config.read(["/.buildstamp", os.environ.get("PRODBUILDPATH", "")])

# Set up some variables we import throughout, applying a couple transforms as necessary.
is_final_release = config.getboolean("Main", "IsFinal")
product_name = config.get("Main", "Product")
product_version = trim_product_version_for_ui(config.get("Main", "Version"))

# for use in device names, eg: "fedora", "rhel"
product_short_name = shorten_product_name(product_name)

result = ProductData(is_final_release, product_name, product_version, product_short_name)
return result


def get_product_is_final_release():
return get_product_values().is_final_release


def get_product_name():
return get_product_values().name


def get_product_short_name():
return get_product_values().short_name


def get_product_version():
return get_product_values().version
4 changes: 2 additions & 2 deletions pyanaconda/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@

from pyanaconda import kickstart
from pyanaconda.core import util
from pyanaconda import product
from pyanaconda.core.async_utils import run_in_loop
from pyanaconda.core.configuration.anaconda import conf
from pyanaconda.core.constants import THREAD_EXCEPTION_HANDLING_TEST, IPMI_FAILED
from pyanaconda.core.product import get_product_is_final_release
from pyanaconda.errors import NonInteractiveError
from pyanaconda.core.i18n import _
from pyanaconda.modules.common.errors.storage import UnusableStorageError
Expand Down Expand Up @@ -326,7 +326,7 @@ def initExceptionHandling(anaconda):
# anaconda-tb file
config.register_callback("journalctl", journalctl_callback, attchmnt_only=False)

if not product.isFinal:
if not get_product_is_final_release():
config.register_callback("release_type", lambda: "pre-release", attchmnt_only=True)

handler = AnacondaExceptionHandler(config, anaconda.intf.meh_interface,
Expand Down
6 changes: 3 additions & 3 deletions pyanaconda/modules/payloads/payload/dnf/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

from pyanaconda.anaconda_loggers import get_module_logger
from pyanaconda.core.configuration.anaconda import conf
from pyanaconda.core.constants import isFinal as is_final_release, REPO_ORIGIN_TREEINFO, \
REPO_ORIGIN_SYSTEM
from pyanaconda.core.constants import REPO_ORIGIN_TREEINFO, REPO_ORIGIN_SYSTEM
from pyanaconda.core.i18n import _
from pyanaconda.core.path import join_paths
from pyanaconda.core.product import get_product_is_final_release
from pyanaconda.core.util import execWithRedirect
from pyanaconda.modules.common.errors.payload import UnknownRepositoryError, SourceSetupError
from pyanaconda.modules.common.structures.payload import RepoConfigurationData
Expand Down Expand Up @@ -186,7 +186,7 @@ def disable_default_repositories(dnf_manager):
log.debug("Disable repositories based on the Anaconda configuration file.")
enable_matching_repositories(dnf_manager, conf.payload.disabled_repositories, False)

if not is_final_release:
if not get_product_is_final_release():
return

log.debug("Disable rawhide repositories.")
Expand Down
6 changes: 3 additions & 3 deletions pyanaconda/modules/payloads/payload/dnf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from pyanaconda.modules.common.constants.services import STORAGE
from pyanaconda.modules.common.structures.packages import PackagesSelectionData
from pyanaconda.modules.payloads.constants import SourceType
from pyanaconda.product import productName, productVersion
from pyanaconda.core.product import get_product_name, get_product_version
from pyanaconda.modules.payloads.base.utils import sort_kernel_version_list

log = get_module_logger(__name__)
Expand Down Expand Up @@ -89,11 +89,11 @@ def get_product_release_version():
:return: a string with the release version
"""
try:
release_version = VERSION_DIGITS.match(productVersion).group(1)
release_version = VERSION_DIGITS.match(get_product_version()).group(1)
except AttributeError:
release_version = "rawhide"

log.debug("Release version of %s is %s.", productName, release_version)
log.debug("Release version of %s is %s.", get_product_name(), release_version)
return release_version


Expand Down
4 changes: 2 additions & 2 deletions pyanaconda/modules/runtime/user_interface/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pyanaconda.core.constants import PASSWORD_POLICY_LUKS, PASSWORD_POLICY_ROOT, \
PASSWORD_POLICY_USER
from pyanaconda.core.dbus import DBus
from pyanaconda.core.product import get_product_is_final_release
from pyanaconda.core.signal import Signal
from pyanaconda.modules.runtime.user_interface.ui_interface import UIInterface
from pyanaconda.modules.common.base import KickstartBaseModule
Expand Down Expand Up @@ -85,5 +86,4 @@ def is_final(self):

:return bool: final or not
"""
from pyanaconda import product
return product.isFinal
return get_product_is_final_release()
6 changes: 3 additions & 3 deletions pyanaconda/modules/storage/bootloader/efi.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from pyanaconda.core.configuration.anaconda import conf
from pyanaconda.core.kernel import kernel_arguments
from pyanaconda.core.path import join_paths
from pyanaconda.product import productName
from pyanaconda.core.product import get_product_name

from pyanaconda.anaconda_loggers import get_module_logger
log = get_module_logger(__name__)
Expand Down Expand Up @@ -85,7 +85,7 @@ def _add_single_efi_boot_target(self, partition):
create_method = "-C" if self.keep_boot_order else "-c" # pylint: disable=no-member

rc = self.efibootmgr(
create_method, "-w", "-L", productName.split("-")[0], # pylint: disable=no-member
create_method, "-w", "-L", get_product_name().split("-")[0], # pylint: disable=no-member
"-d", boot_disk.path, "-p", boot_part_num,
"-l", self.efi_dir_as_efifs_dir + self._efi_binary, # pylint: disable=no-member
root=conf.target.system_root
Expand All @@ -111,7 +111,7 @@ def remove_efi_boot_target(self):
except ValueError:
continue

if _product == productName.split("-")[0]: # pylint: disable=no-member
if _product == get_product_name().split("-")[0]: # pylint: disable=no-member
slot_id = slot[4:8]
# slot_id is hex, we can't use .isint and use this regex:
if not re.match("^[0-9a-fA-F]+$", slot_id):
Expand Down
4 changes: 2 additions & 2 deletions pyanaconda/modules/storage/bootloader/extlinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
BootLoaderError
from pyanaconda.core.util import execWithRedirect
from pyanaconda.core.configuration.anaconda import conf
from pyanaconda.product import productName
from pyanaconda.core.product import get_product_name

from pyanaconda.anaconda_loggers import get_module_logger
log = get_module_logger(__name__)
Expand Down Expand Up @@ -96,7 +96,7 @@ def write_config_header(self, config):
"menu hidden\n\n"
"timeout %(timeout)d\n"
"#totaltimeout 9000\n\n"
% {"productName": productName, "timeout": self.timeout * 10})
% {"productName": get_product_name(), "timeout": self.timeout * 10})
config.write(header)
if self.default is not None:
config.write("default %(default)s\n\n" % {"default": self.image_label(self.default).replace(" ", "")})
Expand Down
6 changes: 3 additions & 3 deletions pyanaconda/modules/storage/bootloader/grub2.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from pyanaconda.core.configuration.anaconda import conf
from pyanaconda.core.i18n import _
from pyanaconda.core.path import open_with_perm
from pyanaconda.product import productName
from pyanaconda.core.product import get_product_name

from pyanaconda.anaconda_loggers import get_module_logger
log = get_module_logger(__name__)
Expand Down Expand Up @@ -184,7 +184,7 @@ def write_config_images(self, config):

@property
def stage2_format_types(self):
if productName.startswith("Red Hat "): # pylint: disable=no-member
if get_product_name().startswith("Red Hat "): # pylint: disable=no-member
return ["xfs", "ext4", "ext3", "ext2"]
else:
return ["ext4", "ext3", "ext2", "btrfs", "xfs"]
Expand Down Expand Up @@ -350,7 +350,7 @@ def write_config(self):
root=conf.target.system_root
)
if rc:
log.error("failed to set default menu entry to %s", productName)
log.error("failed to set default menu entry to %s", get_product_name())

# set menu_auto_hide grubenv variable if we should enable menu_auto_hide
# set boot_success so that the menu is hidden on the boot after install
Expand Down
16 changes: 11 additions & 5 deletions pyanaconda/modules/storage/bootloader/systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from pyanaconda.core.configuration.anaconda import conf
from pyanaconda.core.i18n import _
from pyanaconda.core.path import join_paths
from pyanaconda.product import productName
from pyanaconda.core.product import get_product_name

from pyanaconda.anaconda_loggers import get_module_logger
log = get_module_logger(__name__)
Expand Down Expand Up @@ -134,10 +134,16 @@ def install(self, args=None):
log.info("systemd.py: install systemd boot install (root=%s)", conf.target.system_root)

# the --esp-path= isn't strictly required, but we want to be explicit about it.
rc = util.execWithRedirect("bootctl", [ "install", "--esp-path=/boot/efi",
"--efi-boot-option-description=" + productName.split("-")[0] ],
root=conf.target.system_root,
env_prune=['MALLOC_PERTURB_'])
rc = util.execWithRedirect(
"bootctl",
[
"install",
"--esp-path=/boot/efi",
"--efi-boot-option-description=" + get_product_name().split("-")[0]
],
root=conf.target.system_root,
env_prune=['MALLOC_PERTURB_']
)
if rc:
raise BootLoaderError(_("bootctl failed to install UEFI boot loader. "
"More information may be found in the log files stored in /tmp"))
Expand Down
4 changes: 2 additions & 2 deletions pyanaconda/modules/storage/bootloader/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pyanaconda.modules.storage.bootloader.image import LinuxBootLoaderImage
from pyanaconda.core.configuration.anaconda import conf
from pyanaconda.core.util import execWithRedirect
from pyanaconda.product import productName
from pyanaconda.core.product import get_product_name

from pyanaconda.anaconda_loggers import get_module_logger
log = get_module_logger(__name__)
Expand Down Expand Up @@ -122,7 +122,7 @@ def _collect_os_images(storage, kernel_versions):
log.debug("Collecting the OS images for: %s", ", ".join(kernel_versions))

# all the linux images' labels are based on the default image's
base_label = productName
base_label = get_product_name()

# The first one is the default kernel. Update the bootloader's default
# entry to reflect the details of the default kernel.
Expand Down
4 changes: 2 additions & 2 deletions pyanaconda/modules/storage/bootloader/zipl.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
BootLoaderError
from pyanaconda.core import util
from pyanaconda.core.configuration.anaconda import conf
from pyanaconda.product import productName
from pyanaconda.core.product import get_product_name

from pyanaconda.anaconda_loggers import get_module_logger
log = get_module_logger(__name__)
Expand All @@ -42,7 +42,7 @@ class ZIPL(BootLoader):

@property
def stage2_format_types(self):
if productName.startswith("Red Hat "): # pylint: disable=no-member
if get_product_name().startswith("Red Hat "): # pylint: disable=no-member
return ["xfs", "ext4", "ext3", "ext2"]
else:
return ["ext4", "ext3", "ext2", "xfs"]
Expand Down
Loading