public Path packagePythonFunction()

in src/main/java/com/awslabs/general/helpers/implementations/BasicLambdaPackagingHelper.java [39:88]


    public Path packagePythonFunction(FunctionName functionName, PythonLambdaFunctionDirectory pythonLambdaFunctionDirectory) {
        File baseDirectory = pythonLambdaFunctionDirectory.getDirectory();

        // Determine the absolute path of the package directory
        File absolutePackageDirectory = new File(String.join("/", baseDirectory.getAbsolutePath(), PACKAGE_DIRECTORY));

        // Delete any existing package directory
        cleanUpPackageDirectory(absolutePackageDirectory);

        Path zipFilePath = getPythonZipFilePath(functionName, pythonLambdaFunctionDirectory);

        // Delete any existing ZIP file
        Try.of(() -> Files.deleteIfExists(zipFilePath.toAbsolutePath())).get();

        // Get a snapshot of all of the files we need to copy to the package directory
        List<Path> filesToCopyToPackageDirectory = getDirectorySnapshot(baseDirectory.toPath());

        if (hasDependencies(baseDirectory.toPath())) {
            log.debug(String.join("-", functionName.getName(), "Retrieving Python dependencies"));

            // Install the requirements in a package directory
            List<String> programAndArguments = List.of(
                    PIP_3,
                    "install",
                    "-r",
                    REQUIREMENTS_TXT,
                    "-t",
                    absolutePackageDirectory.getPath());

            ProcessBuilder processBuilder = processHelper.getProcessBuilder(programAndArguments);
            processBuilder.directory(baseDirectory);

            Option<ProcessOutput> processOutputOption = processHelper.getOutputFromProcess(processBuilder);

            checkPipStatus(processOutputOption);
        } else {
            log.debug(String.join("-", functionName.getName(), "No Python dependencies to install"));
        }

        // Now the dependencies are in the directory, copy the rest of the necessary files in
        filesToCopyToPackageDirectory.forEach(file -> copyToDirectory(file, absolutePackageDirectory));

        // Package up everything into a deployment package ZIP file
        ZipUtil.pack(absolutePackageDirectory, zipFilePath.toFile());

        // Delete the package directory
        cleanUpPackageDirectory(absolutePackageDirectory);

        return zipFilePath;
    }