in src/common/details/helpers/python_runner.cs [49:190]
public static string RunEmbeddedPythonScript(ICommandValues values, string scriptName, string? scriptArgs = null, Dictionary<string, string>? addToEnvironment = null, Action<string>? stdOutHandler = null, Action<string>? stdErrHandler = null, Action<string>? mergedOutputHandler = null)
{
var path = FileHelpers.FindFileInHelpPath($"help/include.python.script.{scriptName}.py");
if (path == null)
{
values.AddThrowError("ERROR:", $"Python script '{scriptName}' not found!");
return string.Empty;
}
var script = FileHelpers.ReadAllHelpText(path, Encoding.UTF8);
if (Program.Debug)
{
Console.WriteLine($"DEBUG: {scriptName}.py:\n{script}");
Console.WriteLine($"DEBUG: PythonRunner.RunEmbeddedPythonScript: '{scriptName}' {scriptArgs}");
var verbose = values.GetOrDefault("x.verbose", "false") != "false";
if (verbose)
{
var file = $"{scriptName}.py";
FileHelpers.WriteAllText(file, script, Encoding.UTF8);
ConsoleHelpers.WriteLineWithHighlight($"DEBUG: `{file} {scriptArgs}`");
}
}
var dbgOut = script.Replace("\n", "\\n").Replace("\r", "");
AI.DBG_TRACE_VERBOSE($"RunEmbeddedPythonScript: {scriptName}.py: {dbgOut}");
AI.DBG_TRACE_VERBOSE($"RunEmbeddedPythonScript: '{scriptName}' {scriptArgs}");
var process = PythonRunner.RunPythonScriptAsync(script, scriptArgs, addToEnvironment, stdOutHandler, stdErrHandler, mergedOutputHandler).Result;
var output = process.MergedOutput;
var exit = process.ExitCode;
if (exit != 0)
{
AI.DBG_TRACE_WARNING($"RunEmbeddedPythonScript: exit={exit}");
output = output?.Trim('\r', '\n', ' ');
output = "\n\n " + output?.Replace("\n", "\n ");
var info = new List<string>();
if (output.Contains("MESSAGE:") && output.Contains("EXCEPTION:") && output.Contains("TRACEBACK:"))
{
var messageLine = process.StdError.Split(new[] { '\r', '\n' }).FirstOrDefault(x => x.StartsWith("MESSAGE:"));
var message = messageLine?.Substring("MESSAGE:".Length)?.Trim();
FileHelpers.LogException(values, new PythonScriptException(output, exit));
if (output.Contains("az login"))
{
values.AddThrowError(
"WARNING:", "Azure CLI credential not found!",
"",
"TRY:", "az login",
"OR:", "az login --use-device-code",
"",
"SEE:", "https://docs.microsoft.com/cli/azure/authenticate-azure-cli");
}
else if (output.Contains("azure.ai.resources"))
{
info.Add("WARNING:");
info.Add("azure-ai-resources Python wheel not found!");
info.Add("");
info.Add("TRY:");
info.Add("pip install azure-ai-resources");
info.Add("SEE:");
info.Add("https://pypi.org/project/azure-ai-resources/");
info.Add("");
}
else if (output.Contains("azure.ai.generative"))
{
info.Add("WARNING:");
info.Add("azure-ai-resources Python wheel not found!");
info.Add("");
info.Add("TRY:");
info.Add("pip install azure-ai-generative");
info.Add("SEE:");
info.Add("https://pypi.org/project/azure-ai-generative/");
info.Add("");
}
else if (output.Contains("azure.identity"))
{
info.Add("WARNING:");
info.Add("azure-identity Python wheel not found!");
info.Add("");
info.Add("TRY:");
info.Add("pip install azure-identity");
info.Add("SEE:");
info.Add("https://pypi.org/project/azure-identity/");
info.Add("");
}
else if (output.Contains("azure.mgmt.resource"))
{
info.Add("WARNING:");
info.Add("azure-mgmt-resource Python wheel not found!");
info.Add("");
info.Add("TRY:");
info.Add("pip install azure-mgmt-resource");
info.Add("SEE:");
info.Add("https://pypi.org/project/azure-mgmt-resource/");
info.Add("");
}
else if (output.Contains("azure.ai.ml"))
{
info.Add("WARNING:");
info.Add("azure-ai-ml Python wheel not found!");
info.Add("");
info.Add("TRY:");
info.Add("pip install azure-ai-ml");
info.Add("SEE:");
info.Add("https://pypi.org/project/azure-ai-ml/");
info.Add("");
}
else if (output.Contains("ModuleNotFoundError"))
{
info.Add("WARNING:");
info.Add("Python wheel not found!");
info.Add("");
}
else
{
info.Add("WARNING:");
info.Add("Unhandled exception in Python script!");
info.Add("");
}
output = message;
}
info.Add("ERROR:");
info.Add($"Python script failed! (exit code={exit})");
info.Add("");
info.Add("OUTPUT:");
info.Add(output ?? string.Empty);
values.AddThrowError(info[0], info[1], info.Skip(2).ToArray());
}
return output != null
? ParseOutputAndSkipLinesUntilStartsWith(output, "---").Trim('\r', '\n', ' ')
: string.Empty;
}