def _get_libspec_mutex_name(libspec_filename): from robocorp_ls_core.system_mutex import generate_mutex_name libspec_filename = _norm_filename(libspec_filename) basename = os.path.basename(libspec_filename) name = os.path.splitext(basename)[0] return generate_mutex_name(libspec_filename, prefix="%s_" % (name,))
def _gen_builtin_libraries(self): """ Generates .libspec files for the libraries builtin (if needed). """ try: import time from concurrent import futures from robotframework_ls.impl import robot_constants from robocorp_ls_core.system_mutex import timed_acquire_mutex from robocorp_ls_core.system_mutex import generate_mutex_name from robotframework_ls.impl.robot_constants import RESERVED_LIB initial_time = time.time() wait_for = [] max_workers = min(10, (os.cpu_count() or 1) + 4) thread_pool = futures.ThreadPoolExecutor(max_workers=max_workers) try: log.debug("Waiting for mutex to generate builtins.") with timed_acquire_mutex( generate_mutex_name( _norm_filename(self._builtins_libspec_dir), prefix="gen_builtins_", ), timeout=100, ): log.debug("Obtained mutex to generate builtins.") for libname in robot_constants.STDLIBS: if libname == RESERVED_LIB: continue builtins_libspec_dir = self._builtins_libspec_dir if not os.path.exists( os.path.join(builtins_libspec_dir, f"{libname}.libspec") ): wait_for.append( thread_pool.submit( self._create_libspec, libname, is_builtin=True ) ) for future in wait_for: future.result() if wait_for: log.debug( "Total time to generate builtins: %.2fs" % (time.time() - initial_time) ) self.synchronize_internal_libspec_folders() finally: thread_pool.shutdown(wait=False) except: log.exception("Error creating builtin libraries.") finally: log.info("Finished creating builtin libraries.")
def _gen_builtin_libraries(self): """ Generates .libspec files for the libraries builtin (if needed). """ try: import time from robotframework_ls.impl import robot_constants from robocorp_ls_core.system_mutex import timed_acquire_mutex from robocorp_ls_core.system_mutex import generate_mutex_name initial_time = time.time() wait_for = [] log.debug("Waiting for mutex to generate builtins.") with timed_acquire_mutex( generate_mutex_name( _norm_filename(self._builtins_libspec_dir), prefix="gen_builtins_", ), timeout=100, ): log.debug("Obtained mutex to generate builtins.") def get_builtins(): try: import robot.libraries return robot.libraries.STDLIBS except: pass return robot_constants.STDLIBS for libname in get_builtins(): builtins_libspec_dir = self._builtins_libspec_dir if not os.path.exists( os.path.join(builtins_libspec_dir, f"{libname}.libspec")): wait_for.append( self.thread_pool.submit(self._create_libspec, libname, is_builtin=True)) for future in wait_for: future.result() if wait_for: log.debug("Total time to generate builtins: %.2fs" % (time.time() - initial_time)) self.synchronize_internal_libspec_folders() except BaseException: log.exception("Error creating builtin libraries.") finally: log.info("Finished creating builtin libraries.")
def make_numbered_dir(root: Path, prefix: str) -> Path: """create a directory with an increased number as suffix for the given prefix""" from robocorp_ls_core.system_mutex import generate_mutex_name from robocorp_ls_core.system_mutex import timed_acquire_mutex with timed_acquire_mutex(generate_mutex_name(f"generate_numbered{root}")): max_existing = max(map(parse_num, find_suffixes(root, prefix)), default=-1) new_number = max_existing + 1 new_path = root.joinpath("{}{}".format(prefix, new_number)) new_path.mkdir() return new_path
def test_gen_mutex_name_from_path(): from robocorp_ls_core.system_mutex import generate_mutex_name mutex_name = "my/snth\\nsth" mutex_name = generate_mutex_name(mutex_name, prefix="my_") assert mutex_name == "my_f9c932bf450ef164"