Files
deskflow/scripts/lib/cmd_utils.py
Nick Bolton 4e844bf307 Wayland support (port Red Hat libei and libportal impl) (#7449)
* Add libei and libportal

* Port libei and libportal code by Peter Hutterer and Olivier Fourdan

* Add Peter Hutterer and Olivier Fourdan to important devs list

* Improve error handling for libei and libportal builds

* Checkout libportal  tags/0.7.1

* Hack out the gi-docgen clone

* Remove new submodules in favor of using ExternalProject_Add

* Remove submodule dirs

* Use ExternalProject_Add instead of submodules

* Fixed namespace

* Hack to work around type libportal causing type conflicts

* Add log helper functions

* Use original log functions

* Switch to FetchContent, use libportal a1530a9 (unreleased) and use camelCase member names for consistency

* Restore a few events (much more work required)

* Add TODOs for supporting multiple lib versions

* Revert "Switch to FetchContent, use libportal a1530a9 (unreleased) and use camelCase member names for consistency"

This reverts commit 610cebb5b6a08282eee68f4424fcdbe9eaab4bf9.

* Simplify cmake config by removing builds for libei and libportal (will do this in `install_deps.py` instead)

* Remove submodules

* Remove .gitmodules

* Use meson to build subprojects

* Update copyright with Peter Hutterer's guidance

* Use meson for installing deps

* Fixed typo in tag name

* Remove submodules

* Remove old submodules

* Fixed libei name

* Defaults for pugixml and gtest depending on whether source exists in subprojects

* Ignore some subproject dirs

* Make deps OS-specific

* Move python deps to pyproject

* Only compile and install on Linux with Meson

* Revert "Move python deps to pyproject"

This reverts commit 92c8a287b8376a4d166058c85f1d6081f6fdb423.

* Add ninja to brewfile

* Add python3-attr

* Restore original coverage config

* Add ninja for meson

* Include meson in same try-except

* Fixed ninja dep name

* Move libs to correct oS

* Fixed include for wintoast

* Disable docs for libportal

* More options for libei and libportal

* Fixed option for libei

* Use ninja directly to install

* Revert "Use ninja directly to install"

This reverts commit c926d78ba483406a55acd10fb157c39e13f90b71.

* Meson install verbose

* Prints stdout/stderr

* Remove `from None`

* Remove submodules that somehow crept back in?!

* Prepend sudo if exists

* Add libei deps for all distros

* Fixed Fedora package name

* Add more deps for other distros

* Add more libs (including pugixml)

* Fix lib name

* Drop -u from pacman

* Add vala to rhel

* Make libportal optional

* Make portal link optional

* Remove example code

* Always use system pugixml

* Disable interactive apt through install_deps.py

* Revert "Disable interactive apt through install_deps.py"

This reverts commit 5bbc8fd689182447c79b81db16c961b98361a292.

* Set DEBIAN_FRONTEND in workflows

* Set DEBIAN_FRONTEND in CodeQL workflow

* Add gtest dep

* Add bundled libei dep

* Add libei dep to Arch

* Use `googletest` on openSUSE

* Add gmock dep

* Remove gtest dep from openSUSE

* Add libei on Fedora

* Bundle libei for older Linux distros

* Disable libei dep for RPM

* Also bundle symlink to .so

* Use ${CMAKE_INSTALL_LIBDIR}

* Rename libei to fix openSUSE

* List installed files

* Add libei-devel to openSUSE

* Add googletest-devel to openSUSE

* Remove manual deps (probably resolved automatically)

* Remove googletest from openSUSE (doesn't provide google mock)

* Only add Portal* if libportal found

* WIP - Partial work on using old events system :'(

* Add deps install commands for subprojects

* Solved more compile issues related to events system, threads, etc

* Fixed bad config for adding x, ei, portal sources

* Remove redundant deps

* Remove (another) redundant dep

* Fixed pacman command

* Add Ubuntu and Linux Mint libei deps

* Fixed Ubuntu and Linux Mint libei deps aliases

* Only iterate subprojects if not None

* Add rhel, rocky, and alma for libei

* Make rhel-like deps same as fedora again

* Build libportal on rhel-like

* Re-enable meson rhel-like for libportal

* Remove dbus-python

* Make libportal optional (for rhel-like)

* Handle ei event queue results

* Re-introduce libportal

* Print libei and libportal versions

* Add ei/portal args and client screen

* Switch --use args to --no

* Don't build libei/libportal on older distros as it's pointless

* Make libei and libportal optional

* Add Debian 13 runner

* Make some packages optional

* Remove subprojects

* Improve comment

* Add comment for libportal

* Improve comment

* Add Debian 13 runner

* Make optional... optional

* Change continuation stripper to remove newline and continuation char

* Make command strip more uniform

* Fixed help var syntax

* Fixed libei linking

* Ensure `kHelpNoWayland` is always defined

* Improve help message

* Fixed Debian 13 runner name

* Include sstream and use const var

* Update ChangeLog

* Remove Wayland block

* Return new timer

* Make tray icon logging verbose

* Fixed arg parser for wayland args

* Fixed init of EI screen

* Fixed lint issues

* Update README to indicate Wayland support in GNOME 46

* Add missing word

* Fixed comment positions

* Automate CI env

* Tone down debug log messages

* Add missing comma

* Remove redundant log line
2024-08-30 15:53:25 +01:00

142 lines
4.7 KiB
Python

import subprocess
import sys
import lib.env as env
try:
import colorama # type: ignore
from colorama import Fore # type: ignore
colorama.init()
except ImportError:
class Fore:
RESET = ""
YELLOW = ""
def has_command(command):
platform = sys.platform
if platform == "win32":
cmd = f"where {command}"
else:
cmd = f"which {command}"
try:
subprocess.check_output(cmd, shell=True)
return True
except subprocess.CalledProcessError:
return False
def strip_continuation_sequences(command, strip_newlines=True):
"""
Remove the continuation sequences (\\) from a command.
To spread strings over multiple lines in YAML files, like in bash, a backslash is used at
the end of each line as continuation character.
"""
if isinstance(command, list):
raise ValueError("List commands are not supported")
cmd_continuation = " \\"
command = command.replace(cmd_continuation, "")
# Some versions of pyyaml will remove the newlines already, so always stripping
# makes the output more consistent.
if strip_newlines:
command = command.replace("\n", " ")
return command
def run(
command,
check=True, # true by default to fail fast
shell=False, # false by default for security
get_output=False,
print_cmd=False, # false by default for security
):
"""
Convenience wrapper around `subprocess.run` to:
- print the command before running it (if `print_cmd` is True)
This differs to `subprocess.run` in that by default it:
- checks the return code by default
- prints list commands as a readable string on failure
This is the same as `subprocess.run` in that it:
- does not use shell by default for security (shell is less secure)
Args:
command (str or list): The command to run.
check (bool): Raise an exception if the command fails.
shell (bool): Run the command in a shell (false by default for security)
get_output (bool): Return the output of the command.
print_cmd (bool): Print the command before running it (false by default for security)
"""
is_list_cmd = isinstance(command, list)
# create string version of list command, only for debugging purposes
command_str = command
if is_list_cmd:
command_str = " ".join(command)
if print_cmd:
print(f"Running: {command_str}")
else:
print("Running command...")
command_str = "***"
# TODO: You can definitely use a list command with shell=True on Windows,
# but can you use a string command with shell=False on Windows?
#
# The `subprocess.run` function has a little gotcha:
# - a string command must be used when `shell=True`
# - a list command must be used when shell isn't or `shell=False`
# however, it allows you to pass a string command when shell isn't used or `shell=False`
# then fails with a vague error message. same problem with list commands and `shell=True`
if not env.is_windows() and is_list_cmd and shell:
raise ValueError("List commands cannot be used when shell=True on Unix systems")
elif not is_list_cmd and not shell:
raise ValueError("String commands cannot be used when shell=False or not set")
# Flush the output to ensure the command is printed before the output of the command,
# which seems to happen in the GitHub runner logs.
sys.stdout.flush()
sys.stderr.flush()
try:
if get_output:
result = subprocess.run(
command,
shell=shell,
check=check,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
else:
result = subprocess.run(command, check=check, shell=shell)
except subprocess.CalledProcessError as e:
# Take control of how failed commands are printed:
# - if `print_cmd` is false, it will print `***` instead of the command
# - if the command was a list, the command is printed as a readable string
raise RuntimeError(
f"Command exited with code {e.returncode}: {command_str}"
) from None
except Exception:
# Take control of how failed commands are printed:
# - if `print_cmd` is false, it will print `***` instead of the command
# - if the command was a list, the command is printed as a readable string
raise RuntimeError(f"Command failed: {command_str}")
if result.returncode != 0:
print(
f"{Fore.YELLOW}Command exited with code {result.returncode}:{Fore.RESET} {command_str}",
file=sys.stderr,
)
return result