func()

in src/startupscriptgenerator/src/python/scriptgenerator.go [46:148]


func (gen *PythonStartupScriptGenerator) GenerateEntrypointScript() string {
	logger := common.GetLogger("python.scriptgenerator.GenerateEntrypointScript")
	defer logger.Shutdown()

	logger.LogInformation("Generating script for source.")

	pythonInstallationRoot := fmt.Sprintf("/opt/python/%s", gen.Manifest.PythonVersion)

	common.PrintVersionInfo()

	scriptBuilder := strings.Builder{}
	scriptBuilder.WriteString("#!/bin/sh\n\n")

	if gen.Manifest.CompressDestinationDir == "true" {
		println("Output is compressed. Extracting it...")
		tarballFile := filepath.Join(gen.AppPath, "output.tar.gz")
		common.ExtractTarball(tarballFile, gen.Manifest.SourceDirectoryInBuildContainer)
		println(fmt.Sprintf("App path is set to '%s'", gen.Manifest.SourceDirectoryInBuildContainer))
	}

	scriptBuilder.WriteString(fmt.Sprintf("echo 'export APP_PATH=\"%s\"' >> ~/.bashrc\n", gen.getAppPath()))
	scriptBuilder.WriteString("echo 'cd $APP_PATH' >> ~/.bashrc\n")

	if gen.Configuration.EnableDynamicInstall && !common.PathExists(pythonInstallationRoot) {
		scriptBuilder.WriteString(fmt.Sprintf("oryx setupEnv -appPath %s\n", gen.getAppPath()))
	}

	common.SetupPreRunScript(&scriptBuilder, gen.getAppPath(), gen.Configuration.PreRunCommand)

	scriptBuilder.WriteString("\n# Enter the source directory to make sure the script runs where the user expects\n")
	scriptBuilder.WriteString("cd " + gen.getAppPath() + "\n\n")
	scriptBuilder.WriteString("export APP_PATH=\"" + gen.getAppPath() + "\"\n")

	common.SetEnvironmentVariableInScript(&scriptBuilder, "HOST", "", DefaultHost)
	common.SetEnvironmentVariableInScript(&scriptBuilder, "PORT", gen.BindPort, DefaultBindPort)

	scriptBuilder.WriteString(fmt.Sprintf("export PATH=\"%s/bin:${PATH}\"\n", pythonInstallationRoot))

	packageSetupBlock := gen.getPackageSetupCommand()
	scriptBuilder.WriteString(packageSetupBlock)

	appType := ""         // "Django", "Flask", etc.
	appDebugAdapter := "" // Used debugger adapter
	appDirectory := ""
	appModule := ""      // Suspected entry module in app
	appDebugModule := "" // Command to run under a debugger in case debugging mode was requested

	command := gen.UserStartupCommand // A custom command takes precedence over any framework defaults
	if command != "" {
		isPermissionAdded := common.ParseCommandAndAddExecutionPermission(gen.UserStartupCommand, gen.getAppPath())
		logger.LogInformation("Permission added: %t", isPermissionAdded)
		command = common.ExtendPathForCommand(command, gen.getAppPath())
	} else {
		var appFw PyAppFramework = DetectFramework(gen.getAppPath(), gen.VirtualEnvName)

		if appFw != nil {
			println("Detected an app based on " + appFw.Name())
			appType = appFw.Name()
			appDirectory = gen.getAppPath()
			appModule = appFw.GetGunicornModuleArg()
			appDebugModule = appFw.GetDebuggableModule()
		} else {
			println("No framework detected; using default app from " + gen.DefaultAppPath)
			logger.LogInformation("Using default app.")
			appType = "Default"
			appDirectory = gen.DefaultAppPath
			appModule = gen.DefaultAppModule
			appDebugModule = gen.DefaultAppDebugModule
		}

		if appModule != "" {
			// Patch all legacy ptvsd debug adaptor calls to debugpy
			if gen.DebugAdapter == "ptvsd" {
				gen.DebugAdapter = "debugpy"
			}

			if gen.shouldStartAppInDebugMode() {
				logger.LogInformation("Generating debug command for appDebugModule.")
				println(fmt.Sprintf(GeneratingCommandMessage, gen.DebugAdapter, appDebugModule))
				switch gen.DebugAdapter {
				case "debugpy":
					command = gen.buildDebugPyCommandForModule(appDebugModule, appDirectory)
				}

				appDebugAdapter = gen.DebugAdapter
			} else {
				logger.LogInformation("Generating command for appModule.")
				println(fmt.Sprintf(GeneratingCommandMessage, "gunicorn", appModule))
				command = gen.buildGunicornCommandForModule(appModule, appDirectory)
			}
		}
	}

	scriptBuilder.WriteString(command + "\n")

	logger.LogProperties(
		"Finalizing script",
		map[string]string{"appType": appType, "appDebugAdapter": appDebugAdapter,
			"appModule": appModule, "venv": gen.Manifest.VirtualEnvName})

	var runScript = scriptBuilder.String()
	return runScript
}