# -*- coding: utf-8 -*-## This file is subject to the terms and conditions defined in# file 'LICENSE.txt', which is part of this source code package.#fromtypingimportUnionimportos
[docs]defWhich(program:str)->str:"""Check if an executable is reachable and return the real "executable" (with the .exe or .bat ) inspiration from: https://stackoverflow.com/questions/377017/test-if-executable-exists-in-python Parameters ---------- program : str the name of the program to find, in windows the user can omit the .exe or the .bat extension Returns ------- str return the string of the real executable or and empty string if the executable is not found """defis_exe(fpath:str)->bool:"""Helper function the check if a file is exist and has the execution rights Parameters ---------- fpath : str absolute path to a file Returns ------- bool true if exists, is a file , has executable permission """returnos.path.isfile(fpath)andos.access(fpath,os.X_OK)fpath=os.path.dirname(program)iffpath:# we have a path so we search for the exact filenameifis_exe(program):returnprogramelse:forpathinos.environ["PATH"].split(os.pathsep):path=path.strip('"')exe_file=os.path.join(path,program)ifis_exe(exe_file):# pragma: no coverage (check the user path, not possible to test this)returnexe_fileifos.name=="nt":# only nt coveragetry:fromwin32apiimportFindExecutable,GetLongPathName_,executable=FindExecutable(program)except:fromMuscat.Helpers.LoggerimportWarningWarning("module win32api is missing, you executable may not be found")else:ifos.path.isfile(executable):returnexecutablereturn""
[docs]defCheckIntegrity(GUI:bool=False)->str:fromMuscat.Helpers.IO.WhichimportWhichifos.name=="nt":# only nt coverage# print(Which('explorer'))print(Which('explorer'),'C:\\WINDOWS\\explorer.exe')print(Which("explorer.exe"),'C:\\WINDOWS\\explorer.exe')print(Which("C:\\WINDOWS\\explorer.exe"),'C:\\WINDOWS\\explorer.exe')assert(Which('None_Existent_executable_like_ls')=="")else:# only posix coverageprint(Which('ls'))assert(Which('ls')=="/usr/bin/ls")assert(Which('/usr/bin/ls')=="/usr/bin/ls")assert(Which('explorer')=="")return"OK"
if__name__=='__main__':# pragma: no coverprint(CheckIntegrity(True))