From b5a2fba896de0978be033acea01298b5948a2a36 Mon Sep 17 00:00:00 2001 From: derrod Date: Wed, 29 Dec 2021 12:54:49 +0100 Subject: [PATCH] [utils] Add helpers for CrossOver on macOS --- legendary/utils/crossover.py | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 legendary/utils/crossover.py diff --git a/legendary/utils/crossover.py b/legendary/utils/crossover.py new file mode 100644 index 0000000..114dd4a --- /dev/null +++ b/legendary/utils/crossover.py @@ -0,0 +1,50 @@ +import logging +import plistlib +import os +import subprocess + +logger = logging.getLogger('CXHelpers') + + +def mac_get_crossover_version(app_path): + try: + plist = plistlib.load(open(os.path.join(app_path, 'Contents', 'Info.plist'), 'rb')) + return plist['CFBundleShortVersionString'] + except Exception as e: + logger.debug(f'Failed to load plist for "{app_path}" with {e!r}') + return None + + +def mac_find_crossover_apps(): + paths = ['/Applications/CrossOver.app'] + try: + out = subprocess.check_output(['mdfind', 'kMDItemCFBundleIdentifier="com.codeweavers.CrossOver"']) + paths.extend(out.decode('utf-8', 'replace').strip().split('\n')) + except Exception as e: + logger.warning(f'Trying to find CrossOver installs via mdfind failed: {e!r}') + + valid = [p for p in paths if os.path.exists(os.path.join(p, 'Contents', 'Info.plist'))] + found_tuples = set() + + for path in valid: + version = mac_get_crossover_version(path) + if not version: + continue + logger.debug(f'Found Crossover {version} at "{path}"') + found_tuples.add((version, path)) + + return sorted(found_tuples, reverse=True) + + +def mac_get_crossover_bottles(): + bottles_path = os.path.expanduser('~/Library/Application Support/CrossOver/Bottles') + if not os.path.exists(bottles_path): + return [] + return sorted(p for p in os.listdir(bottles_path) if + os.path.isdir(os.path.join(bottles_path, p)) and + os.path.exists(os.path.join(bottles_path, p, 'cxbottle.conf'))) + + +def mac_is_valid_bottle(bottle_name): + bottles_path = os.path.expanduser('~/Library/Application Support/CrossOver/Bottles') + return os.path.exists(os.path.join(bottles_path, bottle_name, 'cxbottle.conf'))