diff --git a/scripts/wk/hw/ddrescue.py b/scripts/wk/hw/ddrescue.py index 3f0ca7ba..e999bd75 100644 --- a/scripts/wk/hw/ddrescue.py +++ b/scripts/wk/hw/ddrescue.py @@ -6,7 +6,6 @@ import atexit import logging import os import pathlib -import platform import plistlib import re import subprocess @@ -46,6 +45,7 @@ PANE_RATIOS = ( 22, # ddrescue progress 4, # Journal (kernel messages) ) +PLATFORM = std.PLATFORM SETTING_PRESETS = ( 'Default', 'Fast', diff --git a/scripts/wk/hw/diags.py b/scripts/wk/hw/diags.py index 0c0c642a..32264b08 100644 --- a/scripts/wk/hw/diags.py +++ b/scripts/wk/hw/diags.py @@ -6,7 +6,6 @@ import atexit import logging import os import pathlib -import platform import re import subprocess import time @@ -75,6 +74,7 @@ MENU_SETS = { MENU_TOGGLES = ( 'Skip USB Benchmarks', ) +PLATFORM = std.PLATFORM STATUS_COLORS = { 'Passed': 'GREEN', 'Aborted': 'YELLOW', @@ -374,7 +374,7 @@ class State(): # Functions def audio_test(): """Run an OS-specific audio test.""" - if platform.system() == 'Linux': + if PLATFORM == 'Linux': audio_test_linux() # TODO: Add tests for other OS @@ -423,10 +423,10 @@ def build_menu(cli_mode=False, quick_mode=False): menu.add_action('Power Off') # Compatibility checks - if platform.system() != 'Linux': + if PLATFORM != 'Linux': for name in ('Audio Test', 'Keyboard Test', 'Network Test'): menu.actions[name]['Disabled'] = True - if platform.system() not in ('Darwin', 'Linux'): + if PLATFORM not in ('Darwin', 'Linux'): for name in ('Matrix', 'Tubes'): menu.actions[name]['Disabled'] = True @@ -661,10 +661,10 @@ def cpu_mprime_test(state, test_objects): state.update_progress_pane() state.panes['Prime95'] = tmux.split_window( lines=10, vertical=True, watch_file=prime_log) - if platform.system() == 'Darwin': + if PLATFORM == 'Darwin': state.panes['Temps'] = tmux.split_window( behind=True, percent=80, vertical=True, cmd='./hw-sensors') - elif platform.system() == 'Linux': + elif PLATFORM == 'Linux': state.panes['Temps'] = tmux.split_window( behind=True, percent=80, vertical=True, watch_file=sensors_out) tmux.resize_pane(height=3) @@ -750,7 +750,7 @@ def disk_io_benchmark(state, test_objects, skip_usb=True): def _run_io_benchmark(test_obj, log_path): """Run I/O benchmark and handle exceptions.""" dev_path = test_obj.dev.path - if platform.system() == 'Darwin': + if PLATFORM == 'Darwin': # Use "RAW" disks under macOS dev_path = dev_path.with_name(f'r{dev_path.name}') offset = 0 @@ -783,7 +783,7 @@ def disk_io_benchmark(state, test_objects, skip_usb=True): f'if={dev_path}', 'of=/dev/null', ] - if platform.system() == 'Linux': + if PLATFORM == 'Linux': cmd.append('iflag=direct') # Run and get read rate @@ -1270,7 +1270,7 @@ def screensaver(name): cmd = ['cmatrix', '-abs'] elif name == 'pipes': cmd = [ - 'pipes' if platform.system() == 'Linux' else 'pipes.sh', + 'pipes' if PLATFORM == 'Linux' else 'pipes.sh', '-t', '0', '-t', '1', '-t', '2', @@ -1294,7 +1294,7 @@ def set_apple_fan_speed(speed): raise RuntimeError(f'Invalid speed {speed}') # Set cmd - if platform.system() == 'Linux': + if PLATFORM == 'Linux': cmd = ['apple-fans', speed] #TODO: Add method for use under macOS diff --git a/scripts/wk/hw/obj.py b/scripts/wk/hw/obj.py index 177f34bf..933bc610 100644 --- a/scripts/wk/hw/obj.py +++ b/scripts/wk/hw/obj.py @@ -3,7 +3,6 @@ import logging import pathlib -import platform import plistlib import re @@ -20,7 +19,13 @@ from wk.cfg.hw import ( ) from wk.cfg.main import KIT_NAME_SHORT from wk.exe import get_json_from_command, run_program -from wk.std import bytes_to_string, color_string, sleep, string_to_bytes +from wk.std import ( + PLATFORM, + bytes_to_string, + color_string, + sleep, + string_to_bytes, + ) # STATIC VARIABLES @@ -94,11 +99,11 @@ class CpuRam(BaseObj): def get_cpu_details(self): """Get CPU details using OS specific methods.""" - if platform.system() == 'Darwin': + if PLATFORM == 'Darwin': cmd = 'sysctl -n machdep.cpu.brand_string'.split() proc = run_program(cmd, check=False) self.description = re.sub(r'\s+', ' ', proc.stdout.strip()) - elif platform.system() == 'Linux': + elif PLATFORM == 'Linux': cmd = ['lscpu', '--json'] json_data = get_json_from_command(cmd) for line in json_data.get('lscpu', [{}]): @@ -117,9 +122,9 @@ class CpuRam(BaseObj): def get_ram_details(self): """Get RAM details using OS specific methods.""" - if platform.system() == 'Darwin': + if PLATFORM == 'Darwin': dimm_list = get_ram_list_macos() - elif platform.system() == 'Linux': + elif PLATFORM == 'Linux': dimm_list = get_ram_list_linux() details = {'Total': 0} @@ -299,9 +304,9 @@ class Disk(BaseObj): Required details default to generic descriptions and are converted to the correct type. """ - if platform.system() == 'Darwin': + if PLATFORM == 'Darwin': self.details = get_disk_details_macos(self.path) - elif platform.system() == 'Linux': + elif PLATFORM == 'Linux': self.details = get_disk_details_linux(self.path) # Set necessary details @@ -362,9 +367,9 @@ class Disk(BaseObj): def is_4k_aligned(self): """Check that all disk partitions are aligned, returns bool.""" aligned = True - if platform.system() == 'Darwin': + if PLATFORM == 'Darwin': aligned = is_4k_aligned_macos(self.details) - elif platform.system() == 'Linux': + elif PLATFORM == 'Linux': aligned = is_4k_aligned_linux(self.path, self.details['phy-sec']) #TODO: Add checks for other OS @@ -640,9 +645,9 @@ def get_disk_serial_macos(path): def get_disks(skip_kits=False): """Get disks using OS-specific methods, returns list.""" disks = [] - if platform.system() == 'Darwin': + if PLATFORM == 'Darwin': disks = get_disks_macos() - elif platform.system() == 'Linux': + elif PLATFORM == 'Linux': disks = get_disks_linux() # Skip WK disks diff --git a/scripts/wk/hw/sensors.py b/scripts/wk/hw/sensors.py index a23a8575..77cbcfa3 100644 --- a/scripts/wk/hw/sensors.py +++ b/scripts/wk/hw/sensors.py @@ -4,14 +4,13 @@ import json import logging import pathlib -import platform import re from subprocess import CalledProcessError from wk.cfg.hw import CPU_CRITICAL_TEMP, SMC_IDS, TEMP_COLORS from wk.exe import run_program, start_thread -from wk.std import color_string, sleep +from wk.std import PLATFORM, color_string, sleep # STATIC VARIABLES @@ -23,7 +22,7 @@ SMC_REGEX = re.compile( r'\s+(?P.*?)' r'\s*\(bytes (?P.*)\)$' ) -SENSOR_SOURCE_WIDTH = 25 if platform.system() == 'Darwin' else 20 +SENSOR_SOURCE_WIDTH = 25 if PLATFORM == 'Darwin' else 20 # Error Classes @@ -188,9 +187,9 @@ class Sensors(): def update_sensor_data(self, exit_on_thermal_limit=True): """Update sensor data via OS-specific means.""" - if platform.system() == 'Darwin': + if PLATFORM == 'Darwin': self.update_sensor_data_macos(exit_on_thermal_limit) - elif platform.system() == 'Linux': + elif PLATFORM == 'Linux': self.update_sensor_data_linux(exit_on_thermal_limit) def update_sensor_data_linux(self, exit_on_thermal_limit=True): @@ -262,9 +261,9 @@ def fix_sensor_name(name): def get_sensor_data(): """Get sensor data via OS-specific means, returns dict.""" sensor_data = {} - if platform.system() == 'Darwin': + if PLATFORM == 'Darwin': sensor_data = get_sensor_data_macos() - elif platform.system() == 'Linux': + elif PLATFORM == 'Linux': sensor_data = get_sensor_data_linux() return sensor_data diff --git a/scripts/wk/net.py b/scripts/wk/net.py index c0e967ef..8b930c3b 100644 --- a/scripts/wk/net.py +++ b/scripts/wk/net.py @@ -3,13 +3,12 @@ import os import pathlib -import platform import re import psutil from wk.exe import get_json_from_command, run_program -from wk.std import GenericError, show_data +from wk.std import PLATFORM, GenericError, show_data from wk.cfg.net import BACKUP_SERVERS @@ -62,7 +61,7 @@ def mount_backup_shares(read_write=False): mount_str = f'{name} (//{details["Address"]}/{details["Share"]})' # Prep mount point - if platform.system() in ('Darwin', 'Linux'): + if PLATFORM in ('Darwin', 'Linux'): mount_point = pathlib.Path(f'/Backups/{name}') try: if not mount_point.exists(): @@ -107,7 +106,7 @@ def mount_network_share(details, mount_point=None, read_write=False): raise RuntimeError('Not connected to a network') # Build OS-specific command - if platform.system() == 'Darwin': + if PLATFORM == 'Darwin': cmd = [ 'sudo', 'mount', @@ -116,7 +115,7 @@ def mount_network_share(details, mount_point=None, read_write=False): f'//{username}:{password}@{address}/{share}', mount_point, ] - elif platform.system() == 'Linux': + elif PLATFORM == 'Linux': cmd = [ 'sudo', 'mount', @@ -131,7 +130,7 @@ def mount_network_share(details, mount_point=None, read_write=False): f'//{address}/{share}', mount_point ] - elif platform.system() == 'Windows': + elif PLATFORM == 'Windows': cmd = ['net', 'use'] if mount_point: cmd.append(f'{mount_point}:') @@ -158,14 +157,14 @@ def share_is_mounted(details): """Check if dev/share/etc is mounted, returns bool.""" mounted = False - if platform.system() == 'Darwin': + if PLATFORM == 'Darwin': # Weak and naive text search proc = run_program(['mount'], check=False) for line in proc.stdout.splitlines(): if f'{details["Address"]}/{details["Share"]}' in line: mounted = True break - elif platform.system() == 'Linux': + elif PLATFORM == 'Linux': cmd = [ 'findmnt', '--list', @@ -183,7 +182,7 @@ def share_is_mounted(details): mounted = True break #TODO: Check mount status under Windows - #elif platform.system() == 'Windows': + #elif PLATFORM == 'Windows': # Done return mounted @@ -222,9 +221,9 @@ def unmount_backup_shares(): continue # Build OS specific kwargs - if platform.system() in ('Darwin', 'Linux'): + if PLATFORM in ('Darwin', 'Linux'): kwargs['mount_point'] = f'/Backups/{name}' - elif platform.system() == 'Windows': + elif PLATFORM == 'Windows': kwargs['details'] = details # Unmount and add to report @@ -243,9 +242,9 @@ def unmount_network_share(details=None, mount_point=None): cmd = [] # Build OS specific command - if platform.system() in ('Darwin', 'Linux'): + if PLATFORM in ('Darwin', 'Linux'): cmd = ['sudo', 'umount', mount_point] - elif platform.system() == 'Windows': + elif PLATFORM == 'Windows': cmd = ['net', 'use'] if mount_point: cmd.append(f'{mount_point}:') diff --git a/scripts/wk/std.py b/scripts/wk/std.py index cd5e9dc1..f8254ea0 100644 --- a/scripts/wk/std.py +++ b/scripts/wk/std.py @@ -50,6 +50,7 @@ COLORS = { 'CYAN': '\033[36m', } LOG = logging.getLogger(__name__) +PLATFORM = platform.system() REGEX_SIZE_STRING = re.compile( r'(?P\-?\d+\.?\d*)\s*(?P[PTGMKB])(?PI?)B?' ) @@ -138,7 +139,7 @@ class Menu(): # Display item in YELLOW disabled = True checkmark = '*' - if 'DISPLAY' in os.environ or platform.system() == 'Darwin': + if 'DISPLAY' in os.environ or PLATFORM == 'Darwin': checkmark = '✓' display_name = f'{index if index else name[:1].upper()}: ' if not (index and index >= 10): diff --git a/scripts/wk/tmux.py b/scripts/wk/tmux.py index 478a81d7..2f5b32e7 100644 --- a/scripts/wk/tmux.py +++ b/scripts/wk/tmux.py @@ -3,9 +3,9 @@ import logging import pathlib -import platform from wk.exe import run_program +from wk.std import PLATFORM # STATIC_VARIABLES @@ -149,7 +149,7 @@ def prep_action( elif text: # Display text echo_cmd = ['echo'] - if platform.system() == 'Linux': + if PLATFORM == 'Linux': echo_cmd.append('-e') action_cmd.extend([ 'watch',