Skip to content

Commit

Permalink
Added support for python3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
gavin-aguiar committed Aug 21, 2024
1 parent ae9eff2 commit 7dad9e2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/Azure.Functions.Cli/Azure.Functions.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@
<EmbeddedResource Include="StaticResources\Dockerfile.python3.11">
<LogicalName>$(AssemblyName).Dockerfile.python3.11</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="StaticResources\Dockerfile.python3.12">
<LogicalName>$(AssemblyName).Dockerfile.python3.12</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="StaticResources\Dockerfile.javascript">
<LogicalName>$(AssemblyName).Dockerfile.javascript</LogicalName>
</EmbeddedResource>
Expand Down
2 changes: 2 additions & 0 deletions src/Azure.Functions.Cli/Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ public static class DockerImages
public const string LinuxPython39ImageAmd64 = "mcr.microsoft.com/azure-functions/python:3.0.15066-python3.9-buildenv";
public const string LinuxPython310ImageAmd64 = "mcr.microsoft.com/azure-functions/python:4-python3.10-buildenv";
public const string LinuxPython311ImageAmd64 = "mcr.microsoft.com/azure-functions/python:4-python3.11-buildenv";
public const string LinuxPython311ImageAmd64 = "mcr.microsoft.com/azure-functions/python:4-python3.12-buildenv";

}

public static class StaticResourcesNames
Expand Down
28 changes: 16 additions & 12 deletions src/Azure.Functions.Cli/Helpers/PythonHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,31 +167,31 @@ public static void AssertPythonVersion(WorkerLanguageVersionInfo pythonVersion,
{
if (pythonVersion?.Version == null)
{
var message = "Could not find a Python version. Python 3.6.x, 3.7.x, 3.8.x, 3.9.x, 3.10.x or 3.11.x is recommended, and used in Azure Functions.";
var message = "Could not find a Python version. 3.7.x, 3.8.x, 3.9.x, 3.10.x, 3.11.x or 3.12.x is recommended, and used in Azure Functions.";
if (errorIfNoVersion) throw new CliException(message);
ColoredConsole.WriteLine(WarningColor(message));
return;
}

ColoredConsole.WriteLine(AdditionalInfoColor($"Found Python version {pythonVersion.Version} ({pythonVersion.ExecutablePath})."));

// Python 3.[6|7|8|9|10|11] (supported)
// Python 3.[7|8|9|10|11|12] (supported)
if (IsVersionSupported(pythonVersion))
{
return;
}

// Python 3.x (but not 3.[6|7|8|9|10|11]), not recommended, may fail. E.g.: 3.4, 3.5.
// Python 3.x (but not 3.[7|8|9|10|11|12]), not recommended, may fail. E.g.: 3.4, 3.5.
if (pythonVersion.Major == 3)
{
if (errorIfNotSupported)
throw new CliException($"Python 3.6.x to 3.11.x is required for this operation. " +
$"Please install Python 3.6, 3.7, 3.8, 3.9, 3.10 or 3.11 and use a virtual environment to switch to Python 3.6, 3.7, 3.8, 3.9, 3.10 or 3.11.");
ColoredConsole.WriteLine(WarningColor("Python 3.6.x, 3.7.x, 3.8.x, 3.9.x, 3.10.x or 3.11.x is recommended, and used in Azure Functions."));
throw new CliException($"Python 3.7.x to 3.12.x is required for this operation. " +
$"Please install Python 3.7, 3.8, 3.9, 3.10, 3.11 or 3.12 and use a virtual environment to switch to Python 3.7, 3.8, 3.9, 3.10, 3.11 or 3.12.");
ColoredConsole.WriteLine(WarningColor("Python 3.7.x, 3.8.x, 3.9.x, 3.10.x, 3.11.x or 3.12.x is recommended, and used in Azure Functions."));
}

// No Python 3
var error = "Python 3.x (recommended version 3.[6|7|8|9|10|11]) is required.";
var error = "Python 3.x (recommended version 3.[7|8|9|10|11|12]) is required.";
if (errorIfNoVersion) throw new CliException(error);
ColoredConsole.WriteLine(WarningColor(error));
}
Expand Down Expand Up @@ -225,6 +225,7 @@ public static async Task<WorkerLanguageVersionInfo> GetEnvironmentPythonVersion(
var python39GetVersionTask = GetVersion("python3.9");
var python310GetVersionTask = GetVersion("python3.10");
var python311GetVersionTask = GetVersion("python3.11");
var python312GetVersionTask = GetVersion("python3.12");

var versions = new List<WorkerLanguageVersionInfo>
{
Expand All @@ -237,6 +238,7 @@ public static async Task<WorkerLanguageVersionInfo> GetEnvironmentPythonVersion(
await python39GetVersionTask,
await python310GetVersionTask,
await python311GetVersionTask,
await python312GetVersionTask
};

// Highest preference -- Go through the list, if we find the first python 3.6 or python 3.7 worker, we prioritize that.
Expand Down Expand Up @@ -582,6 +584,8 @@ private static string GetBuildNativeDepsEnvironmentImage(WorkerLanguageVersionIn
return Constants.DockerImages.LinuxPython310ImageAmd64;
case 11:
return Constants.DockerImages.LinuxPython311ImageAmd64;
case 12:
return Constants.DockerImages.LinuxPython312ImageAmd64;
}
}
return Constants.DockerImages.LinuxPython36ImageAmd64;
Expand All @@ -593,24 +597,24 @@ private static bool IsVersionSupported(WorkerLanguageVersionInfo info)
{
switch (info?.Minor)
{
case 12:
case 11:
case 10:
case 9:
case 8:
case 7:
case 6: return true;
case 7: return true;
default: return false;
}
} else return false;
}

public static bool IsLinuxFxVersionRuntimeVersionMatched(string linuxFxVersion, int? major, int? minor)
{
// No linux fx version will default to python 3.6
// No linux fx version will default to python 3.11
if (string.IsNullOrEmpty(linuxFxVersion))
{
// Match if version is 3.6
return major == 3 && minor == 6;
// Match if version is 3.11
return major == 3 && minor == 11;
}
// Only validate on LinuxFxVersion that follows the pattern PYTHON|<version>
else if (!linuxFxVersion.StartsWith("PYTHON|", StringComparison.OrdinalIgnoreCase))
Expand Down
11 changes: 11 additions & 0 deletions src/Azure.Functions.Cli/StaticResources/Dockerfile.python3.12
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To enable ssh & remote debugging on app service change the base image to the one below
# FROM mcr.microsoft.com/azure-functions/python:4-python3.12-appservice
FROM mcr.microsoft.com/azure-functions/python:4-python3.12

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY requirements.txt /
RUN pip install -r /requirements.txt

COPY . /home/site/wwwroot
2 changes: 2 additions & 0 deletions src/Azure.Functions.Cli/StaticResources/StaticResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public static async Task<string> GetValue(string name)

public static Task<string> DockerfilePython311 => GetValue("Dockerfile.python3.11");

public static Task<string> DockerfilePython312 => GetValue("Dockerfile.python3.12");

public static Task<string> DockerfilePowershell7 => GetValue("Dockerfile.powershell7");

public static Task<string> DockerfilePowershell72 => GetValue("Dockerfile.powershell7.2");
Expand Down
4 changes: 2 additions & 2 deletions test/Azure.Functions.Cli.Tests/PythonHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ public SkipIfPythonNonExistFact()
string[] pythons;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
pythons = new string[] { "python.exe", "python3.exe", "python36.exe", "python37.exe", "python38.exe", "python39.exe", "python310.exe", "python311.exe", "py.exe" };
pythons = new string[] { "python.exe", "python3.exe", "python37.exe", "python38.exe", "python39.exe", "python310.exe", "python311.exe", "python312.exe", "py.exe" };
}
else
{
pythons = new string[] { "python", "python3", "python36", "python37", "python38", "python39", "python310", "python311" };
pythons = new string[] { "python", "python3", "python37", "python38", "python39", "python310", "python311", "python312" };
}

string pythonExe = pythons.FirstOrDefault(p => CheckIfPythonExist(p));
Expand Down

0 comments on commit 7dad9e2

Please sign in to comment.