func buildFn()

in cmd/python/functions_framework/main.go [51:105]


func buildFn(ctx *gcp.Context) error {
	if err := validateSource(ctx); err != nil {
		return err
	}

	// Check for syntax errors to prevent failures that would only manifest at run time.
	if _, err := ctx.Exec([]string{"python3", "-m", "compileall", "-f", "-q", "."}, gcp.WithStdoutTail, gcp.WithUserAttribution); err != nil {
		return err
	}

	// Determine if the function has dependency on functions-framework.
	hasFrameworkDependency := false
	requirementsExists, err := ctx.FileExists("requirements.txt")
	if err != nil {
		return err
	}
	if requirementsExists {
		content, err := ctx.ReadFile("requirements.txt")
		if err != nil {
			return err
		}
		hasFrameworkDependency = containsFF(string(content))
	}

	// Install functions-framework if necessary.
	l, err := ctx.Layer(layerName, gcp.LaunchLayer, gcp.BuildLayer)
	if err != nil {
		return fmt.Errorf("creating %v layer: %w", layerName, err)
	}
	if hasFrameworkDependency {
		ctx.Logf("Handling functions with dependency on functions-framework.")
		if err := ctx.ClearLayer(l); err != nil {
			return fmt.Errorf("clearing layer %q: %w", l.Name, err)
		}
	} else {
		if _, isVendored := os.LookupEnv(python.VendorPipDepsEnv); isVendored {
			return gcp.UserErrorf("Vendored dependencies detected, please add functions-framework to requirements.txt and download it using pip")
		}
		ctx.Logf("Handling functions without dependency on functions-framework.")
		if err := cloudfunctions.AssertFrameworkInjectionAllowed(); err != nil {
			return err
		}

		// The pip install is performed by the pip buildpack; see python.InstallRequirements.
		ctx.Logf("Adding functions-framework requirements.txt to the list of requirements files to install.")
		r := filepath.Join(ctx.BuildpackRoot(), "converter", "requirements.txt")
		l.BuildEnvironment.Append(python.RequirementsFilesEnv, string(os.PathListSeparator), r)
	}

	if err := ctx.SetFunctionsEnvVars(l); err != nil {
		return err
	}
	ctx.AddWebProcess([]string{"functions-framework"})
	return nil
}