in src/Detector/Python/PythonDetector.cs [37:179]
public PlatformDetectorResult Detect(DetectorContext context)
{
var sourceRepo = context.SourceRepo;
var appDirectory = string.Empty;
var hasRequirementsTxtFile = false;
var hasPyprojectTomlFile = false;
if (sourceRepo.FileExists(PythonConstants.RequirementsFileName))
{
_logger.LogInformation($"Found {PythonConstants.RequirementsFileName} at the root of the repo.");
hasRequirementsTxtFile = true;
// Warning if missing django module
bool hasDjangoModule = false;
string filePath = $"{sourceRepo.RootPath}/{PythonConstants.RequirementsFileName}";
using (var reader = new StreamReader(filePath))
{
while (!reader.EndOfStream && !hasDjangoModule)
{
string line = reader.ReadLine().ToLower();
if (line.StartsWith("django"))
{
hasDjangoModule = true;
}
}
}
if (!hasDjangoModule)
{
_logger.LogWarning($"Missing django module in {PythonConstants.RequirementsFileName}");
}
else
{
// detect django files exist
foreach (string djangoFileName in PythonConstants.DjangoFileNames)
{
if (!sourceRepo.FileExists(djangoFileName))
{
_logger.LogWarning($"Missing {djangoFileName} at the root of the repo. More information: https://aka.ms/missing-django-files");
}
}
}
}
else
{
string errorMsg = $"Cound not find {PythonConstants.RequirementsFileName} at the root of the repo. More information: https://aka.ms/requirements-not-found";
_logger.LogError(errorMsg);
}
if (sourceRepo.FileExists(PythonConstants.PyprojectTomlFileName))
{
_logger.LogInformation($"Found {PythonConstants.PyprojectTomlFileName} at the root of the repo.");
hasPyprojectTomlFile = true;
}
else
{
_logger.LogError($"Missing {PythonConstants.SetupDotPyFileName} at the root of the repo. More information: https://aka.ms/requirements-not-found");
}
if (sourceRepo.FileExists(PythonConstants.SetupDotPyFileName))
{
_logger.LogInformation($"Found {PythonConstants.SetupDotPyFileName} at the root of the repo.");
hasPyprojectTomlFile = true;
}
else
{
_logger.LogError($"Missing {PythonConstants.SetupDotPyFileName} at the root of the repo. More information: https://aka.ms/requirements-not-found");
}
var hasCondaEnvironmentYmlFile = false;
if (sourceRepo.FileExists(PythonConstants.CondaEnvironmentYmlFileName) &&
IsCondaEnvironmentFile(sourceRepo, PythonConstants.CondaEnvironmentYmlFileName))
{
_logger.LogInformation(
$"Found {PythonConstants.CondaEnvironmentYmlFileName} at the root of the repo.");
hasCondaEnvironmentYmlFile = true;
}
if (!hasCondaEnvironmentYmlFile &&
sourceRepo.FileExists(PythonConstants.CondaEnvironmentYamlFileName) &&
IsCondaEnvironmentFile(sourceRepo, PythonConstants.CondaEnvironmentYamlFileName))
{
_logger.LogInformation(
$"Found {PythonConstants.CondaEnvironmentYamlFileName} at the root of the repo.");
hasCondaEnvironmentYmlFile = true;
}
var hasJupyterNotebookFiles = false;
var notebookFiles = sourceRepo.EnumerateFiles(
$"*.{PythonConstants.JupyterNotebookFileExtensionName}",
searchSubDirectories: false);
if (notebookFiles != null && notebookFiles.Any())
{
_logger.LogInformation(
$"Found files with extension {PythonConstants.JupyterNotebookFileExtensionName} " +
$"at the root of the repo.");
hasJupyterNotebookFiles = true;
}
// This detects if a runtime.txt file exists and if that is a python file
var hasRuntimeTxtFile = false;
var versionFromRuntimeFile = DetectPythonVersionFromRuntimeFile(context.SourceRepo);
if (!string.IsNullOrEmpty(versionFromRuntimeFile))
{
hasRuntimeTxtFile = true;
}
if (!hasRequirementsTxtFile &&
!hasCondaEnvironmentYmlFile &&
!hasJupyterNotebookFiles &&
!hasRuntimeTxtFile &&
!hasPyprojectTomlFile)
{
var searchSubDirectories = !_options.DisableRecursiveLookUp;
if (!searchSubDirectories)
{
_logger.LogDebug("Skipping search for files in sub-directories as it has been disabled.");
}
var files = sourceRepo.EnumerateFiles(PythonConstants.PythonFileNamePattern, searchSubDirectories);
if (files != null && files.Any())
{
_logger.LogInformation(
$"Found files with extension '{PythonConstants.PythonFileNamePattern}' " +
$"in the repo.");
appDirectory = RelativeDirectoryHelper.GetRelativeDirectoryToRoot(
files.FirstOrDefault(), sourceRepo.RootPath);
}
else
{
_logger.LogInformation(
$"Could not find any file with extension '{PythonConstants.PythonFileNamePattern}' " +
$"in the repo.");
return null;
}
}
return new PythonPlatformDetectorResult
{
Platform = PythonConstants.PlatformName,
PlatformVersion = versionFromRuntimeFile,
AppDirectory = appDirectory,
HasJupyterNotebookFiles = hasJupyterNotebookFiles,
HasCondaEnvironmentYmlFile = hasCondaEnvironmentYmlFile,
HasRequirementsTxtFile = hasRequirementsTxtFile,
HasPyprojectTomlFile = hasPyprojectTomlFile,
};
}