Files
deskflow/scripts/lib/linux.py
Chris Rizzitello ed1217e9cc Use Deskflow Name (#7519)
* Use Deskflow Name

* Remove business-oriented options from issue templates

* Remove business-oriented workflow

* Bump version to 3.0.0 (to avoid confusion with previously used version numbers 1.x & 2.x)

* Update readme to reflect new project name and goals

* Found some more "synergy" to rename

* Rename `synlib` to `app`

* Rename `syntool` to `deskflow-legacy`

* Rename `synwinhk` to `dfwhook`

* Rename dirs from synergy to deskflow

* Rename more "Synergy" files

* Rename app bundle ID

* Fixed copyright typo

* Rename only title in serial key dialog (to be moved downstream later)

* Preserve original serial key window for moving downstream

* Restore dialogs ready for moving downstream

* Rename `QDeskflowApplication` to `DeskflowApplication` (the Q is confusing)

* Restore Volker's original project name

* Fixed mimetype

* Fixed weird grammar

* Fixed (more) weird grammar

* Broken link, restoring (but we should move all links out of source)

* Broken link, restoring (but we should move all links out of source)

* Add write permission to valgrind-analysis.yml

* Restore AUR conflicts

* Apply Clang format

* Update ChangeLog

* Back out version change

---------

Co-authored-by: Nick Bolton <nick@symless.com>
2024-09-17 20:00:25 +01:00

207 lines
6.5 KiB
Python

# Deskflow -- mouse and keyboard sharing utility
# Copyright (C) 2024 Symless Ltd.
#
# This package is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# found in the file LICENSE that should have accompanied this file.
#
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty 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, see <http://www.gnu.org/licenses/>.
import os, shutil, glob, sys
import lib.cmd_utils as cmd_utils
import lib.env as env
from enum import Enum, auto
class PackageType(Enum):
DISTRO = auto()
TGZ = auto()
dist_dir = "dist"
build_dir = "build"
package_name = "deskflow"
test_cmd = "deskflows --version"
def run_command(command, check=True):
has_sudo = cmd_utils.has_command("sudo")
if "sudo" in command and not has_sudo:
# assume we're running as root if sudo is not found (common on older distros).
# a space char is intentionally added after "sudo" for intentionality.
# possible limitation with stripping "sudo" is that if any packages with "sudo" in the
# name are added to the list (probably very unlikely), this will have undefined behavior.
print("The 'sudo' command was not found, stripping sudo from command")
command = command.replace("sudo ", "").strip()
cmd_utils.run(command, check, shell=True, print_cmd=True)
def package(filename_base, package_type: PackageType, leave_test_installed=False):
extension, cmd = get_package_info(package_type)
run_package_cmd(cmd)
package_filename = get_package_filename(extension)
target_file = f"{filename_base}.{extension}"
target_path = copy_to_dist_dir(package_filename, target_file)
if package_type == PackageType.DISTRO:
test_install(target_path, remove_test=not leave_test_installed)
def get_package_info(package_type: PackageType):
command = None
cpack_generator = None
file_extension = None
if package_type == PackageType.TGZ:
cpack_generator = "TGZ"
file_extension = "tar.gz"
elif package_type == PackageType.DISTRO:
distro, distro_like, _distro_version = env.get_linux_distro()
if not distro_like:
distro_like = distro
if "debian" in distro_like:
cpack_generator = "DEB"
file_extension = "deb"
elif "fedora" in distro_like or "opensuse" in distro_like:
cpack_generator = "RPM"
file_extension = "rpm"
elif "arch" in distro_like:
command = ["makepkg", "-s"]
file_extension = "pkg.tar.zst"
else:
raise RuntimeError(f"Linux distro not yet supported: {distro}")
if not cpack_generator and not command:
raise RuntimeError("No package generator or command found")
if cpack_generator:
command = ["cpack", "-G", cpack_generator]
return file_extension, command
def run_package_cmd(command):
package_user = env.get_env("LINUX_PACKAGE_USER", required=False)
if package_user:
cmd_utils.run(
["sudo", "chown", "-R", package_user, "build"], check=True, print_cmd=True
)
command = ["sudo", "-u", package_user] + command
cwd = os.getcwd()
try:
os.chdir("build")
cmd_utils.run(command, check=True, print_cmd=True)
finally:
os.chdir(cwd)
def get_package_filename(extension):
files = glob.glob(f"build/*.{extension}")
if not files:
raise ValueError(f"No .{extension} file found in build directory")
return files[0]
def copy_to_dist_dir(source_file, target_file):
os.makedirs(dist_dir, exist_ok=True)
target_path = f"{dist_dir}/{target_file}"
print(f"Copying to: {target_path}")
shutil.copy(source_file, target_path)
return target_path
def test_install(package_file, remove_test=True):
distro, distro_like, _distro_version = env.get_linux_distro()
if not distro_like:
distro_like = distro
install_base = None
list_cmd = None
remove_base = None
if "debian" in distro_like:
install_base = ["apt", "install", "-f", "-y"]
remove_base = ["apt", "remove", "-y"]
list_cmd = ["dpkg", "-L", "deskflow"]
elif "fedora" in distro_like:
install_base = ["dnf", "install", "-y"]
remove_base = ["dnf", "remove", "-y"]
list_cmd = ["rpm", "-ql", "deskflow"]
elif "opensuse" in distro_like:
install_base = ["zypper", "--no-gpg-checks", "install", "-y"]
remove_base = ["zypper", "remove", "-y"]
list_cmd = ["rpm", "-ql", "deskflow"]
elif "arch" in distro_like:
install_base = ["pacman", "-U", "--noconfirm"]
remove_base = ["pacman", "-R", "--noconfirm"]
list_cmd = ["pacman", "-Ql", "deskflow"]
else:
raise RuntimeError(f"Linux distro not yet supported: {distro}")
has_sudo = cmd_utils.has_command("sudo")
sudo = ["sudo"] if has_sudo else []
print("Testing installation...")
cmd_utils.run(
sudo + install_base + [f"./{package_file}"],
check=True,
print_cmd=True,
)
print("Listing installed files...")
cmd_utils.run(sudo + list_cmd, check=True, print_cmd=True)
try:
cmd_utils.run(test_cmd, shell=True, check=True, print_cmd=True)
except Exception:
raise RuntimeError("Unable to verify version")
finally:
if remove_test:
cmd_utils.run(
sudo + remove_base + [package_name], check=True, print_cmd=True
)
else:
print("Leaving test package installed")
print("Installation test passed")
def is_package_available(package):
distro, distro_like, _distro_version = env.get_linux_distro()
if not distro_like:
distro_like = distro
if "debian" in distro_like:
command = ["apt-cache", "show", package]
elif "fedora" in distro_like:
command = ["dnf", "info", package]
elif "opensuse" in distro_like:
command = ["zypper", "info", package]
elif "arch" in distro_like:
command = ["pacman", "-Si", package]
else:
raise RuntimeError(f"Linux distro not yet supported: {distro}")
result = cmd_utils.run(command, check=False, print_cmd=True, get_output=True)
if result.stderr:
print(result.stderr, file=sys.stderr)
return result.returncode == 0