def main(): if not pc_utils.is_windows(): test_show_controller_linux() else: test_show_controller_windows() return cmd_count
def get_home_dir(): if pc_utils.is_windows(): # running on windows home_dir = os.getenv('USERPROFILE') home_dir = home_dir.replace("\\", "/") else: home_dir = os.getenv('HOME') return home_dir
def __init__(self): self.box_is_windows = pc_utils.is_windows() self.psm_queue_path = os.path.expanduser(constants.PSM_QUEUE) self.psm_log_path = os.path.expanduser(constants.PSM_LOGDIR) self.cwd_path = os.path.expanduser(constants.CWD) # ensure our required dirs have been created if not os.path.exists(self.cwd_path): os.makedirs(self.cwd_path) if not os.path.exists(self.psm_queue_path): os.makedirs(self.psm_queue_path)
def run_cmd_in_new_console(cmd): if pc_utils.is_windows(): # windows os.system("start cmd /K " + cmd) else: # linux dest_dir = os.getcwd() fn_log = file_utils.make_tmp_dir("console_run") + "/new_console.log" start_async_run_detached(cmd, os.path.expanduser(dest_dir), fn_log, visible=True)
def can_create_console_window(): cc = False if pc_utils.has_gui(): if pc_utils.is_windows(): # windows cc = True else: # linux # TODO: how do we know if this linux can create a console window? cc = True return cc
def open_file_with_default_app(fn): import os import shutil if pc_utils.is_windows(): cmd = 'start' else: # priority order: # VISUAL (if VISUAL and DISPLAY are set) # EDITOR (if EDITOR is set) # vim or nano (if found in PATH) default = shutil.which('vim') or shutil.which('nano') cmd = os.environ.get('EDITOR', default) if 'DISPLAY' in os.environ: cmd = shutil.which(os.environ.get('VISUAL', cmd)) if cmd is None: console.warning('no editor found') return os.system(cmd + " " + fn)
def start_async_run_detached(cmd, working_dir, fn_stdout, visible=False, env_vars=None): DETACHED_PROCESS = 0x00000008 # if visible else 0 CREATE_NO_WINDOW = 0x08000000 shell = False is_windows = pc_utils.is_windows() if not visible and is_windows: # do NOT specify DETACHED_PROCESS when CREATE_WINDOW cflags = CREATE_NO_WINDOW elif visible and not is_windows: # If we are in a Linux environment then attempt to spawn # a gnome terminal and display output there. # note: this should only be called if process_utils.can_create_console_window() returns True shell = True cmd =\ ("bash -c 'if xset q &>/dev/null; then " "gnome-terminal -- bash -c \"{};bash\"; " "else bash -c \"{}\"; fi'").format(cmd, cmd) else: cflags = DETACHED_PROCESS with open(fn_stdout, 'w') as output: if is_windows: p = subprocess.Popen(cmd, cwd=working_dir, env=env_vars, stdout=output, stderr=subprocess.STDOUT, creationflags=cflags) else: p = subprocess.Popen(cmd, cwd=working_dir, env=env_vars, stdout=output, stderr=subprocess.STDOUT, shell=shell) return p
def _start_xt_cache_server(self): import subprocess DETACHED_PROCESS = 0x00000008 CREATE_NO_WINDOW = 0x08000000 # launch in visible window for debugging MAKE_SERVER_VISIBLE = False xtlib_dir = os.path.dirname(__file__) fn_script = "{}/cache_server.py".format(xtlib_dir) fn_log = os.path.expanduser("~/.xt/tmp/cache_server.log") file_utils.ensure_dir_exists(file=fn_log) if MAKE_SERVER_VISIBLE: #subprocess.Popen(parts, cwd=".", creationflags=DETACHED_PROCESS) cmd = "start python " + fn_script os.system(cmd) elif pc_utils.is_windows(): # run detached, hidden for WINDOWS parts = ["cmd", "/c", "python", fn_script] flags = CREATE_NO_WINDOW with open(fn_log, 'w') as output: subprocess.Popen(parts, cwd=".", creationflags=flags, stdout=output, stderr=subprocess.STDOUT) else: # run detached, hidden for LINUX parts = ["python", fn_script] with open(fn_log, 'w') as output: subprocess.Popen(parts, cwd=".", stdout=output, stderr=subprocess.STDOUT) # give it time to start-up and receive commands time.sleep(2)
def expand_system_symbols(self, text, section=None, prop_name=None): if text == "$vault": if not self.vault: # create on-demand self.create_vault_if_needed() assert section and prop_name text = self.vault.get_secret(section) if "$username" in text: ev_user = "******" if pc_utils.is_windows() else "USER" username = os.getenv(ev_user, os.getenv("XT_USERNAME")) username = username if username else "" text = text.replace("$username", username) if "$current_conda_env" in text: conda = os.getenv("CONDA_DEFAULT_ENV") if conda: text = text.replace("$current_conda_env", conda) return text
def fix_slashes(fn, is_linux=None): # cannot expand here - we don't know if it is local #fn = os.path.expanduser(fn) if is_linux is None: is_linux = not pc_utils.is_windows() if is_linux: fn = fn.replace("\\", "/") console.detail("fixed slashes for linux: {}".format(fn)) else: if "/run" in fn: # protect ws/runxxx entries which are legal XT cmds fn = fn.replace("/run", "$$run") fn = fn.replace("/", "\\") fn = fn.replace("$$run", "/run") else: fn = fn.replace("/", "\\") console.detail("fixed slashes for windows: {}".format(fn)) return fn
def compare_submit_logs(self, logs_dir): approve_prefix = "approved_" prefix_len = len(approve_prefix) approved_dir = approve_prefix + logs_dir if not os.path.exists(approved_dir): errors.general_error("missing approved_dir:", approved_dir) return paths = [os.path.join(approved_dir, fn) for fn in os.listdir(approved_dir)] approved_files = [fn for fn in paths if os.path.isfile(fn)] for fn in approved_files: if ((not pc_utils.is_windows() and fn.endswith(".bat")) or (pc.is_windows() and fn.endswith(".sh"))): continue self.file_count += 1 fnx = fn[prefix_len:] if not os.path.exists(fnx): errors.general_error("missing approved file in submitLogs dir: {}".format(fnx)) self.compare_log_files(fn, fnx)
def __init__(self): self.is_windows = pc_utils.is_windows()
# # Copyright (c) Microsoft Corporation. # Licensed under the MIT license. # # key_press_checker.py: handles checking for a key being pressed without blocking on Windows and Linux from xtlib import console from xtlib import pc_utils if pc_utils.is_windows(): import msvcrt else: import sys, select, tty, termios class KeyPressChecker: def __init__(self): self.is_windows = pc_utils.is_windows() def __enter__(self): if not self.is_windows: # save off current stdin settings (LINE mode) self.old_settings = termios.tcgetattr(sys.stdin) # put stdin into CHAR mode tty.setcbreak(sys.stdin.fileno()) return self def _get_windows_char(self, encoding): bb = msvcrt.getch()