class ResourceUtil {

	public static List<File> extractBuiltInDependencies(
			String tmpdir,
			String prefix,
			boolean skipShellScript) throws IOException, InterruptedException {
		List<File> extractedFiles = new ArrayList<>();
		for (String fileName : BUILT_IN_PYTHON_DEPENDENCIES) {
			if (skipShellScript && fileName.endsWith(".sh")) {
				continue;
			}

			File file = new File(tmpdir, prefix + fileName);
			if (fileName.endsWith(".sh")) {
				// TODO: This is a hacky solution to prevent subprocesses to hold the file descriptor of shell scripts,
				// which will cause the execution of shell scripts failed with the exception "test file is busy"
				// randomly. It's a bug of JDK, see https://bugs.openjdk.java.net/browse/JDK-8068370. After moving flink
				// python jar to lib directory, we can solve this problem elegantly by extracting these files only once.
				String javaExecutable = String.join(File.separator, System.getProperty("java.home"), "bin", "java");
				String classPath = new File(
					ResourceUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getAbsolutePath();
				new ProcessBuilder(
					javaExecutable,
					"-cp",
					classPath,
					ResourceUtil.class.getName(),
					tmpdir,
					prefix,
					fileName).inheritIO().start().waitFor();
			} else {
				Files.copy(
					ResourceUtil.class.getClassLoader().getResourceAsStream(fileName),
					Paths.get(file.getAbsolutePath()));
			}
			extractedFiles.add(file);
		}
		return extractedFiles;
	}

}