* Reorganize CMake Packaging module * Match if statements to function order * Cleanup root, res, and doc dirs * Move deps to requirements.txt file * Reorganize and format CMake files * Rename changelog lint * Add reccomended extension * Workflow to lint CMake files * Move CMake lint to script * Try lighter dep * Use venv * Add --format arg * Format all CMake files * Convert bash script to Python * Set CMake line ending format * Restore formatting * Add pyyaml dep * Remove unused arg * Rename config file * Remove comment * Repair copyrights (broken by defualt cmake-format) * Restore 3rd party copyright * Break up libs config into smaller macros * Better macro name * Load config after venv * Make intentional noop clearer * Only use upload step if required (make skip clearer) * Use CPack for deb and rpm packaging * Add upload step for Linux * Remove cpack dep, doesn't exist * Roll back presets version * Fixed distro like match * Update ChangeLog * Legacy checkout for some distros * All distros support v4 * Trying out newer Linux distros * Install Git on Docker images * Install without actions (not available before checkout) * Delete useless action * Install Python * Support for Arch and OpenSUSE * Add Arch and OpenSUSE to deps * Name steps * Full OpenSUSE names * Mark Git dir safe * Add pkgconf * Legacy CMake for Debian 11 * Add OpenSSL to OpenSUSE * Drop OpenSUSE Leap (no C++20 support) * Skip packaging for Arch and OpenSUSE (for now) * Shorten Arch/OpenSUSE names * Clearer step name * SImpler bootstrap * Shell not needed * Update apt * Don't check return code * Simplify python deps commands * Add STGZ/.sh package type * Prevent input prompt * Only config git safe dir when needed * Try cache v4 * Safe dir for Ubuntu * Safe dir for Arch * All Docker images seem to need safe dir config * Refactor env var getters * Make Ubuntu build extra packages * Condense bootstep to single step * Fixed var name * Fixed bootstrap logic * Simplify logic for upload condition (Windows and macOS) * Make package/upload condition easier to understand * Add Manjaro * Generic names for Linux .tar.gz and .sh packages * Add Manjaro deps * Swap macOS matrix entries * Add Red Hat UBI * Remove RHEL subscription manager * Throw on unsupported package distro * Conditionally install pip and venv * Remove extra pip arg * Add config for RHEL * Install EPEL for RHEL * Back-out RHEL as EPEL requires subscription * Restore Python deps logic * Fixed bug: Packacking run twice * Testing arm32v7 and arm64v8 * Revert "Testing arm32v7 and arm64v8" This reverts commit cb3caf188d2b79ed083a62fc091de295f9889f3d. * Re-add icon and shortcut file for Linux to package * Support OpenSUSE RPM build * Check return code * Add `rpm-build` for OpenSUSE * Reorg packages * Remove busybox-which * Add --non-interactive * Move --non-interactive to correct position * Experiment with makepkg * Check and print package commands * Make distro version optional * Use 8 cores to build * Default to distro name only * Fixed bad PKGBUILD filename * Use 4-part version for Arch * Remove comma from conflicts * Use .tar.gz from cwd * Generate checksum for Arch * Fixed file extension * Use shell to print output * Don't use shell * Gaurd against bad cmd_utils.run * Fixed bad import * Use list command * Fixed unable to run list commands * Use source file name * Simplify PKGBUILD to use make install * Change install prefix * Use DESTDIR * Copy .desktop and .png to build dir * Restore original `install(FILES...` * Improving comments * Fixed: makepkg runs from `src` by default * Move error after command print * Remove shell arg * Package as a user instead of root (makepkg can't run as root) * Fixed codesign runs in shell * Allow list commands in shell on windows * Don't use sudo on arch * Install sudo on Arch * Fixed typo * Fix ownership of build directory for package user * Improve example .env * Change to depend on libstdc++6 * Add TODO * Fixed Fedora version * Remove libstdc++ deps (names vary between distros) * Roll back to Fedora 40 and 39 * Improve comment * Remove unneccesary default
128 lines
4.5 KiB
Python
128 lines
4.5 KiB
Python
import subprocess
|
|
import sys
|
|
import lib.env as env
|
|
|
|
|
|
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):
|
|
"""
|
|
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.
|
|
When a YAML file is parsed, this becomes "\\ " (without a new line char), so this character
|
|
sequence must be removed before running the command.
|
|
This doesn't seem to be an issue on Windows, since the \\ path separator is rarely followed
|
|
by a space.
|
|
"""
|
|
cmd_continuation = "\\ "
|
|
|
|
if isinstance(command, list):
|
|
return [c.replace(cmd_continuation, "") for c in command]
|
|
else:
|
|
return command.replace(cmd_continuation, "")
|
|
|
|
|
|
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}") from None
|
|
|
|
if result.returncode != 0:
|
|
print(
|
|
f"Command exited with code {result.returncode}: {command_str}",
|
|
file=sys.stderr,
|
|
)
|
|
|
|
return result
|