Rebase to 3006.x

This commit is contained in:
Shane Lee 2024-01-10 15:34:39 -07:00 committed by Daniel Wozniak
parent d922f94e95
commit ba44de34fa
5 changed files with 49 additions and 87 deletions

1
changelog/65777.fixed.md Normal file
View file

@ -0,0 +1 @@
Fixes an issue when reading/modifying ini files that contain unicode characters

View file

@ -131,60 +131,16 @@ warnings.filterwarnings(
def __define_global_system_encoding_variable__():
import sys
# This is the most trustworthy source of the system encoding, though, if
# salt is being imported after being daemonized, this information is lost
# and reset to None
encoding = None
if not sys.platform.startswith("win") and sys.stdin is not None:
# On linux we can rely on sys.stdin for the encoding since it
# most commonly matches the filesystem encoding. This however
# does not apply to windows
encoding = sys.stdin.encoding
if not encoding:
# If the system is properly configured this should return a valid
# encoding. MS Windows has problems with this and reports the wrong
# encoding
import locale
try:
encoding = locale.getencoding()
except AttributeError:
# Python < 3.11
encoding = locale.getpreferredencoding(do_setlocale=True)
# This is now garbage collectable
del locale
if not encoding:
# This is most likely ascii which is not the best but we were
# unable to find a better encoding. If this fails, we fall all
# the way back to ascii
encoding = sys.getdefaultencoding()
if not encoding:
if sys.platform.startswith("darwin"):
# Mac OS X uses UTF-8
encoding = "utf-8"
elif sys.platform.startswith("win"):
# Windows uses a configurable encoding; on Windows, Python uses the name “mbcs”
# to refer to whatever the currently configured encoding is.
encoding = "mbcs"
else:
# On linux default to ascii as a last resort
encoding = "ascii"
import builtins
import sys
# Define the detected encoding as a built-in variable for ease of use
setattr(builtins, "__salt_system_encoding__", encoding)
setattr(builtins, "__salt_system_encoding__", sys.getdefaultencoding())
# This is now garbage collectable
del sys
del builtins
del encoding
del sys
__define_global_system_encoding_variable__()

View file

@ -12,7 +12,7 @@ Hives
This is the top level of the registry. They all begin with HKEY.
- HKEY_CLASSES_ROOT (HKCR)
- HKEY_CURRENT_USER(HKCU)
- HKEY_CURRENT_USER (HKCU)
- HKEY_LOCAL MACHINE (HKLM)
- HKEY_USER (HKU)
- HKEY_CURRENT_CONFIG
@ -139,7 +139,7 @@ def present(
A string value representing the full path of the key to include the
HIVE, Key, and all Subkeys. For example:
``HKEY_LOCAL_MACHINE\\SOFTWARE\\Salt``
``HKEY_LOCAL_MACHINE\SOFTWARE\Salt``
Valid hive values include:
@ -298,24 +298,24 @@ def present(
Example:
The following example will set the ``(Default)`` value for the
``SOFTWARE\\Salt`` key in the ``HKEY_CURRENT_USER`` hive to
``SOFTWARE\Salt`` key in the ``HKEY_CURRENT_USER`` hive to
``2016.3.1``:
.. code-block:: yaml
HKEY_CURRENT_USER\\SOFTWARE\\Salt:
HKEY_CURRENT_USER\SOFTWARE\Salt:
reg.present:
- vdata: 2016.3.1
Example:
The following example will set the value for the ``version`` entry under
the ``SOFTWARE\\Salt`` key in the ``HKEY_CURRENT_USER`` hive to
the ``SOFTWARE\Salt`` key in the ``HKEY_CURRENT_USER`` hive to
``2016.3.1``. The value will be reflected in ``Wow6432Node``:
.. code-block:: yaml
HKEY_CURRENT_USER\\SOFTWARE\\Salt:
HKEY_CURRENT_USER\SOFTWARE\Salt:
reg.present:
- vname: version
- vdata: 2016.3.1
@ -323,7 +323,7 @@ def present(
In the above example the path is interpreted as follows:
- ``HKEY_CURRENT_USER`` is the hive
- ``SOFTWARE\\Salt`` is the key
- ``SOFTWARE\Salt`` is the key
- ``vname`` is the value name ('version') that will be created under the key
- ``vdata`` is the data that will be assigned to 'version'
@ -423,7 +423,7 @@ def present(
)
add_change = {
"Key": r"{}\{}".format(hive, key),
"Key": rf"{hive}\{key}",
"Entry": "{}".format(
salt.utils.stringutils.to_unicode(vname, "utf-8") if vname else "(Default)"
),
@ -451,10 +451,10 @@ def present(
if not ret["result"]:
ret["changes"] = {}
ret["comment"] = r"Failed to add {} to {}\{}".format(vname, hive, key)
ret["comment"] = rf"Failed to add {vname} to {hive}\{key}"
else:
ret["changes"] = {"reg": {"Added": add_change}}
ret["comment"] = r"Added {} to {}\{}".format(vname, hive, key)
ret["comment"] = rf"Added {vname} to {hive}\{key}"
if ret["result"]:
ret = __utils__["dacl.check_perms"](
@ -480,7 +480,7 @@ def absent(name, vname=None, use_32bit_registry=False):
A string value representing the full path of the key to include the
HIVE, Key, and all Subkeys. For example:
``HKEY_LOCAL_MACHINE\\SOFTWARE\\Salt``
``HKEY_LOCAL_MACHINE\SOFTWARE\Salt``
Valid hive values include:
@ -504,7 +504,7 @@ def absent(name, vname=None, use_32bit_registry=False):
.. code-block:: yaml
'HKEY_CURRENT_USER\\SOFTWARE\\Salt':
'HKEY_CURRENT_USER\SOFTWARE\Salt':
reg.absent
- vname: version
@ -521,11 +521,11 @@ def absent(name, vname=None, use_32bit_registry=False):
hive=hive, key=key, vname=vname, use_32bit_registry=use_32bit_registry
)
if not reg_check["success"] or reg_check["vdata"] == "(value not set)":
ret["comment"] = "{} is already absent".format(name)
ret["comment"] = f"{name} is already absent"
return ret
remove_change = {
"Key": r"{}\{}".format(hive, key),
"Key": rf"{hive}\{key}",
"Entry": "{}".format(vname if vname else "(Default)"),
}
@ -541,10 +541,10 @@ def absent(name, vname=None, use_32bit_registry=False):
)
if not ret["result"]:
ret["changes"] = {}
ret["comment"] = r"Failed to remove {} from {}".format(key, hive)
ret["comment"] = rf"Failed to remove {key} from {hive}"
else:
ret["changes"] = {"reg": {"Removed": remove_change}}
ret["comment"] = r"Removed {} from {}".format(key, hive)
ret["comment"] = rf"Removed {key} from {hive}"
return ret
@ -598,10 +598,10 @@ def key_absent(name, use_32bit_registry=False):
if not __utils__["reg.read_value"](
hive=hive, key=key, use_32bit_registry=use_32bit_registry
)["success"]:
ret["comment"] = "{} is already absent".format(name)
ret["comment"] = f"{name} is already absent"
return ret
ret["changes"] = {"reg": {"Removed": {"Key": r"{}\{}".format(hive, key)}}}
ret["changes"] = {"reg": {"Removed": {"Key": rf"{hive}\{key}"}}}
# Check for test option
if __opts__["test"]:
@ -617,6 +617,6 @@ def key_absent(name, use_32bit_registry=False):
)["success"]:
ret["result"] = False
ret["changes"] = {}
ret["comment"] = "Failed to remove registry key {}".format(name)
ret["comment"] = f"Failed to remove registry key {name}"
return ret

View file

@ -23,9 +23,8 @@ Values/Entries are name/data pairs. There can be many values in a key. The
(Default) value corresponds to the Key itself, the rest are their own name/value
pairs.
:depends: - PyWin32
:depends: PyWin32
"""
# When production windows installer is using Python 3, Python 2 code can be removed
import logging
@ -73,7 +72,7 @@ def __virtual__():
def _to_mbcs(vdata):
"""
Converts unicode to to current users character encoding. Use this for values
Converts unicode to current users character encoding. Use this for values
returned by reg functions
"""
return salt.utils.stringutils.to_unicode(vdata, "mbcs")
@ -186,7 +185,7 @@ def key_exists(hive, key, use_32bit_registry=False):
try:
hkey = registry.hkeys[local_hive]
except KeyError:
raise CommandExecutionError("Invalid Hive: {}".format(local_hive))
raise CommandExecutionError(f"Invalid Hive: {local_hive}")
access_mask = registry.registry_32[use_32bit_registry]
handle = None
@ -241,7 +240,7 @@ def value_exists(hive, key, vname, use_32bit_registry=False):
try:
hkey = registry.hkeys[local_hive]
except KeyError:
raise CommandExecutionError("Invalid Hive: {}".format(local_hive))
raise CommandExecutionError(f"Invalid Hive: {local_hive}")
access_mask = registry.registry_32[use_32bit_registry]
try:
@ -340,7 +339,7 @@ def list_keys(hive, key=None, use_32bit_registry=False):
try:
hkey = registry.hkeys[local_hive]
except KeyError:
raise CommandExecutionError("Invalid Hive: {}".format(local_hive))
raise CommandExecutionError(f"Invalid Hive: {local_hive}")
access_mask = registry.registry_32[use_32bit_registry]
subkeys = []
@ -355,10 +354,10 @@ def list_keys(hive, key=None, use_32bit_registry=False):
except win32api.error as exc:
if exc.winerror == 2:
log.debug(r"Cannot find key: %s\%s", hive, key, exc_info=True)
return False, r"Cannot find key: {}\{}".format(hive, key)
return False, rf"Cannot find key: {hive}\{key}"
if exc.winerror == 5:
log.debug(r"Access is denied: %s\%s", hive, key, exc_info=True)
return False, r"Access is denied: {}\{}".format(hive, key)
return False, rf"Access is denied: {hive}\{key}"
raise
finally:
@ -412,7 +411,7 @@ def list_values(hive, key=None, use_32bit_registry=False):
try:
hkey = registry.hkeys[local_hive]
except KeyError:
raise CommandExecutionError("Invalid Hive: {}".format(local_hive))
raise CommandExecutionError(f"Invalid Hive: {local_hive}")
access_mask = registry.registry_32[use_32bit_registry]
handle = None
values = list()
@ -445,10 +444,10 @@ def list_values(hive, key=None, use_32bit_registry=False):
except win32api.error as exc:
if exc.winerror == 2:
log.debug(r"Cannot find key: %s\%s", hive, key)
return False, r"Cannot find key: {}\{}".format(hive, key)
return False, rf"Cannot find key: {hive}\{key}"
elif exc.winerror == 5:
log.debug(r"Access is denied: %s\%s", hive, key)
return False, r"Access is denied: {}\{}".format(hive, key)
return False, rf"Access is denied: {hive}\{key}"
raise
finally:
@ -538,7 +537,7 @@ def read_value(hive, key, vname=None, use_32bit_registry=False):
try:
hkey = registry.hkeys[local_hive]
except KeyError:
raise CommandExecutionError("Invalid Hive: {}".format(local_hive))
raise CommandExecutionError(f"Invalid Hive: {local_hive}")
access_mask = registry.registry_32[use_32bit_registry]
handle = None
@ -574,13 +573,13 @@ def read_value(hive, key, vname=None, use_32bit_registry=False):
raise
except win32api.error as exc:
if exc.winerror == 2:
msg = "Cannot find key: {}\\{}".format(local_hive, local_key)
msg = f"Cannot find key: {local_hive}\\{local_key}"
log.trace(exc)
log.trace(msg)
ret["comment"] = msg
ret["success"] = False
elif exc.winerror == 5:
msg = "Access is denied: {}\\{}".format(local_hive, local_key)
msg = f"Access is denied: {local_hive}\\{local_key}"
log.trace(exc)
log.trace(msg)
ret["comment"] = msg
@ -733,7 +732,7 @@ def set_value(
try:
hkey = registry.hkeys[local_hive]
except KeyError:
raise CommandExecutionError("Invalid Hive: {}".format(local_hive))
raise CommandExecutionError(f"Invalid Hive: {local_hive}")
vtype_value = registry.vtype[local_vtype]
access_mask = registry.registry_32[use_32bit_registry] | win32con.KEY_ALL_ACCESS
@ -886,7 +885,7 @@ def delete_key_recursive(hive, key, use_32bit_registry=False):
try:
hkey = registry.hkeys[local_hive]
except KeyError:
raise CommandExecutionError("Invalid Hive: {}".format(local_hive))
raise CommandExecutionError(f"Invalid Hive: {local_hive}")
key_path = local_key
access_mask = registry.registry_32[use_32bit_registry] | win32con.KEY_ALL_ACCESS
@ -918,7 +917,7 @@ def delete_key_recursive(hive, key, use_32bit_registry=False):
"""
_key = win32api.RegOpenKeyEx(_hkey, _keypath, 0, _access_mask)
for subkeyname in _subkeys(_key):
subkeypath = "{}\\{}".format(_keypath, subkeyname)
subkeypath = f"{_keypath}\\{subkeyname}"
_ret = _traverse_registry_tree(_hkey, subkeypath, _ret, access_mask)
_ret.append(subkeypath)
return _ret
@ -938,13 +937,13 @@ def delete_key_recursive(hive, key, use_32bit_registry=False):
key_handle = win32api.RegOpenKeyEx(hkey, sub_key_path, 0, access_mask)
try:
win32api.RegDeleteKey(key_handle, "")
ret["Deleted"].append(r"{}\{}".format(hive, sub_key_path))
ret["Deleted"].append(rf"{hive}\{sub_key_path}")
except OSError as exc:
log.error(exc, exc_info=True)
ret["Failed"].append(r"{}\{} {}".format(hive, sub_key_path, exc))
ret["Failed"].append(rf"{hive}\{sub_key_path} {exc}")
except win32api.error as exc:
log.error(exc, exc_info=True)
ret["Failed"].append(r"{}\{} {}".format(hive, sub_key_path, exc.strerror))
ret["Failed"].append(rf"{hive}\{sub_key_path} {exc.strerror}")
finally:
if key_handle:
win32api.RegCloseKey(key_handle)
@ -998,7 +997,7 @@ def delete_value(hive, key, vname=None, use_32bit_registry=False):
try:
hkey = registry.hkeys[local_hive]
except KeyError:
raise CommandExecutionError("Invalid Hive: {}".format(local_hive))
raise CommandExecutionError(f"Invalid Hive: {local_hive}")
access_mask = registry.registry_32[use_32bit_registry] | win32con.KEY_ALL_ACCESS
handle = None

View file

@ -0,0 +1,6 @@
import sys
def test_salt_system_encoding():
encoding = sys.getdefaultencoding()
assert __salt_system_encoding__ == encoding