def get_magicdb_location(_cache=[]): """ Return the location of the magicdb loaded from either: - an environment variable ``TYPECODE_LIBMAGIC_DB_PATH``, - a plugin-provided path, - the system PATH. Trigger a warning if no magicdb file is found. """ if _cache: return _cache[0] from plugincode.location_provider import get_location # try the environment first magicdb_loc = os.environ.get(TYPECODE_LIBMAGIC_DB_PATH_ENVVAR) if TRACE and magicdb_loc: logger_debug('get_magicdb_location:', 'got environ magicdb location:', magicdb_loc) # try a plugin-provided path second if not magicdb_loc: magicdb_loc = get_location(TYPECODE_LIBMAGIC_DB) if TRACE and magicdb_loc: logger_debug('get_magicdb_location:', 'got plugin magicdb location:', magicdb_loc) # try the PATH if not magicdb_loc: db = 'magic.mgc' magicdb_loc = command.find_in_path(db) if magicdb_loc: warnings.warn( 'magicdb found in the PATH. ' 'Install instead a typecode-libmagic plugin for best support.\n' f'OR set the {TYPECODE_LIBMAGIC_DB_PATH_ENVVAR} environment variable.' ) if TRACE and magicdb_loc: logger_debug('get_magicdb_location:', 'got path magicdb location:', magicdb_loc) if not magicdb_loc: warnings.warn( 'Libmagic magic database not found. ' 'A default will be used if possible. ' 'Install instead a typecode-libmagic plugin for best support.\n' f'OR set the {TYPECODE_LIBMAGIC_DB_PATH_ENVVAR} environment variable.' ) return _cache.append(magicdb_loc) return magicdb_loc
def load_lib(): """ Return the libmagic shared library object loaded from either: - an environment variable ``TYPECODE_LIBMAGIC_PATH`` - a plugin-provided path, - the system PATH. Raise an NoMagicLibError if no libmagic can be found. """ from plugincode.location_provider import get_location # try the environment first dll_loc = os.environ.get(TYPECODE_LIBMAGIC_PATH_ENVVAR) if TRACE and dll_loc: logger_debug('load_lib:', 'got environ magic location:', dll_loc) # try a plugin-provided path second if not dll_loc: dll_loc = get_location(TYPECODE_LIBMAGIC_DLL) if TRACE and dll_loc: logger_debug('load_lib:', 'got plugin magic location:', dll_loc) # try well known locations if not dll_loc: failover_lib = load_lib_failover() if failover_lib: warnings.warn( 'System libmagic found in typical location is used. ' 'Install instead a typecode-libmagic plugin for best support.') return failover_lib # try the PATH if not dll_loc: dll = 'libmagic.dll' if on_windows else 'libmagic.so' dll_loc = command.find_in_path(dll) if dll_loc: warnings.warn( 'libmagic found in the PATH. ' 'Install instead a typecode-libmagic plugin for best support.') if TRACE and dll_loc: logger_debug('load_lib:', 'got path magic location:', dll_loc) if not dll_loc or not os.path.isfile(dll_loc): raise NoMagicLibError( 'CRITICAL: libmagic DLL and its magic database are not installed. ' 'Unable to continue: you need to install a valid typecode-libmagic ' 'plugin with a valid and proper libmagic and magic DB available.\n' f'OR set the {TYPECODE_LIBMAGIC_PATH_ENVVAR} and ' f'{TYPECODE_LIBMAGIC_DB_PATH_ENVVAR} environment variables.\n' f'OR install libmagic in typical common locations.\n' f'OR have a libmagic in the system PATH.\n') return command.load_shared_library(dll_loc)
def test_find_in_path(self): d1 = self.get_temp_dir('foo') d2 = self.get_temp_dir('bar') filename = 'baz' assert None == command.find_in_path(filename, searchable_paths=( d1, d2, )) f2 = os.path.join(d2, filename) with open(f2, 'w') as o: o.write('some') assert f2 == command.find_in_path(filename, searchable_paths=( d1, d2, )) assert f2 == command.find_in_path(filename, searchable_paths=( d2, d1, )) f1 = os.path.join(d1, filename) with open(f1, 'w') as o: o.write('some') assert f1 == command.find_in_path(filename, searchable_paths=( d1, d2, )) assert f2 == command.find_in_path(filename, searchable_paths=( d2, d1, ))
def get_msiinfo_bin_location(_cache=[]): """ Return the binary location for msiinfo from either: - a plugin-provided path, - the system PATH. Raise an Exception if no msiinfo command can be found. """ if _cache: return _cache[0] from plugincode.location_provider import get_location # try a plugin-provided path first cmd_loc = get_location(MSIINFO_BIN_LOCATION) # try the PATH if not cmd_loc: cmd = 'msiinfo' cmd_loc = find_in_path(cmd) if not cmd_loc: cmd_loc = which(cmd) if cmd_loc: warnings.warn( 'Using "msiinfo" command found in the PATH. ' 'Install instead a plugincode-msitools plugin for best support.' ) if not cmd_loc or not os.path.isfile(cmd_loc): raise Exception( 'CRITICAL: msiinfo not provided. ' 'Unable to continue: you need to install the plugin packagedcode-msitools' ) _cache.append(cmd_loc) return cmd_loc