cardinal_pythonlib.platformfunc


Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Support for platform-specific problems.

cardinal_pythonlib.platformfunc.are_debian_packages_installed(packages: List[str]) Dict[str, bool][source]

Check which of a list of Debian packages are installed, via dpkg-query.

Parameters:

packages – list of Debian package names

Returns:

mapping from package name to boolean (“present?”)

Return type:

dict

cardinal_pythonlib.platformfunc.consume(iterator: Iterator[Any]) None[source]

Consume all remaining values of an iterator.

A reminder: iterable versus iterator: https://anandology.com/python-practice-book/iterators.html.

cardinal_pythonlib.platformfunc.contains_unquoted_ampersand_dangerous_to_windows(x: str) bool[source]

Under Windows, if an ampersand is in a path and is not quoted, it’ll break lots of things. See https://stackoverflow.com/questions/34124636. Simple example:

set RUBBISH=a & b           # 'b' is not recognizable as a... command
set RUBBISH='a & b'         # 'b'' is not recognizable as a... command
set RUBBISH="a & b"         # OK

… and you get similar knock-on effects, e.g. if you set RUBBISH using the Control Panel to the literal

a & dir

and then do

echo %RUBBISH%

it will (1) print “a” and then (2) print a directory listing as it RUNS “dir”! That’s pretty dreadful.

See also

https://www.thesecurityfactory.be/command-injection-windows.html

Anyway, this is a sanity check for that sort of thing.

cardinal_pythonlib.platformfunc.contains_unquoted_target(x: str, quote: str = '"', target: str = '&') bool[source]

Checks if target exists in x outside quotes (as defined by quote). Principal use: from contains_unquoted_ampersand_dangerous_to_windows().

cardinal_pythonlib.platformfunc.require_debian_packages(packages: List[str]) None[source]

Ensure specific packages are installed under Debian.

Parameters:

packages – list of packages

Raises:

ValueError – if any are missing

cardinal_pythonlib.platformfunc.test_windows_utf8_output() None[source]

Print a short string with unusual Unicode characters.

cardinal_pythonlib.platformfunc.validate_pair(ob: Any) bool[source]

Does the object have length 2?

cardinal_pythonlib.platformfunc.windows_get_environment_from_batch_command(env_cmd: str | List[str], initial_env: Dict[str, str] | None = None) Dict[str, str][source]

Take a command (either a single command or list of arguments) and return the environment created after running that command. Note that the command must be a batch (.bat) file or .cmd file, or the changes to the environment will not be captured.

If initial_env is supplied, it is used as the initial environment passed to the child process. (Otherwise, this process’s os.environ() will be used by default.)

From https://stackoverflow.com/questions/1214496/how-to-get-environment-from-a-subprocess-in-python, with decoding bug fixed for Python 3.

PURPOSE: under Windows, VCVARSALL.BAT sets up a lot of environment variables to compile for a specific target architecture. We want to be able to read them, not to replicate its work.

METHOD: create a composite command that executes the specified command, then echoes an unusual string tag, then prints the environment via SET; capture the output, work out what came from SET.

Parameters:
  • env_cmd – command, or list of command arguments

  • initial_env – optional dictionary containing initial environment

Returns:

environment created after running the command

Return type:

dict