def prepare_runtime(self, runtime_dir, data): """Prepare runtime directory. Preparation consists of: 1. Iterating over registered python process runtimes and copy them to python runtime subdirectory of the runtime_dir. 2. Copying the source code (aka program) to the file inside runtime directory. 3. Returning mapping between Python runtimes and Python program. """ source = data.process.run.get("program", "") parser = SafeParser(source) for base_class_name in parser.base_classes(): for module in python_runtimes_manager.necessary_modules( base_class_name): src_dir = os.path.dirname(inspect.getsourcefile(module)) dest_package_dir = os.path.join(runtime_dir, PYTHON_RUNTIME_DIRNAME, *module.__name__.split(".")) shutil.copytree(src_dir, dest_package_dir) os.chmod(dest_package_dir, 0o755) # Write python source file. program_path = os.path.join(runtime_dir, PYTHON_PROGRAM_FILENAME) with open(program_path, "w") as file: file.write(source) os.chmod(program_path, 0o755) # Generate volume maps required to expose needed files. volume_maps = { PYTHON_RUNTIME_DIRNAME: PYTHON_RUNTIME_VOLUME, PYTHON_PROGRAM_FILENAME: PYTHON_PROGRAM_VOLUME, } return volume_maps
def get_processes(process_dir, base_source_uri): """Find processes in path. :param str process_dir: Path to the directory where to search for processes :param str base_source_uri: Base URL of the source code repository with process definitions :return: Dictionary of processes where keys are URLs pointing to processes' source code and values are processes' definitions parsed from YAML files :rtype: dict :raises: ValueError: if multiple processes with the same slug are found """ global PROCESS_CACHE if PROCESS_CACHE is not None: return PROCESS_CACHE all_process_files = [] process_file_extensions = ["*.yaml", "*.yml", "*.py"] for root, _, filenames in os.walk(process_dir): for extension in process_file_extensions: for filename in fnmatch.filter(filenames, extension): all_process_files.append(os.path.join(root, filename)) def read_yaml_file(fname): """Read the yaml file.""" with open(fname) as f: return yaml.load(f, Loader=yaml.FullLoader) processes = [] for process_file in all_process_files: if process_file.endswith(".py"): parser = SafeParser(open(process_file).read()) processes_startlines = [(p.to_schema(), p.metadata.lineno) for p in parser.parse()] else: processes_startlines = [] for p in read_yaml_file(process_file): startline = get_process_definition_start( process_file, p["slug"]) processes_startlines.append((p, startline)) for process, startline in processes_startlines: # Put together URL to starting line of process definition. process["source_uri"] = (base_source_uri + process_file[len(process_dir) + 1:] + "#L" + str(startline)) if "category" not in process: process["category"] = "uncategorized" processes.append(process) PROCESS_CACHE = processes return processes
def discover_process(self, path): """Perform process discovery in given path. This method will be called during process registration and should return a list of dictionaries with discovered process schemas. """ if not path.lower().endswith(".py"): return [] parser = SafeParser(open(path).read()) processes = parser.parse() return [process.to_schema() for process in processes]
def discover_process(self, path): """Perform process discovery in given path. This method will be called during process registration and should return a list of dictionaries with discovered process schemas. """ if not path.lower().endswith('.py'): return [] parser = SafeParser(open(path).read()) processes = parser.parse() return [process.to_schema() for process in processes]