예제 #1
0
        /// <inheritdoc/>
        public PlatformDetectorResult Detect(DetectorContext context)
        {
            var sourceRepo = context.SourceRepo;

            string phpVersion      = null;
            var    hasComposerFile = sourceRepo.FileExists(PhpConstants.ComposerFileName);
            var    appDirectory    = string.Empty;

            if (hasComposerFile)
            {
                _logger.LogDebug($"File '{PhpConstants.ComposerFileName}' exists in source repo");
                phpVersion = GetVersion(context);
            }
            else
            {
                _logger.LogDebug($"File '{PhpConstants.ComposerFileName}' does not exist in source repo");

                var files = sourceRepo.EnumerateFiles(PhpConstants.PhpFileNamePattern, searchSubDirectories: true);
                if (files != null && files.Any())
                {
                    _logger.LogInformation(
                        $"Found files with extension '{PhpConstants.PhpFileNamePattern}' " +
                        $"in the repo.");
                    appDirectory = RelativeDirectoryHelper.GetRelativeDirectoryToRoot(files.FirstOrDefault(), sourceRepo.RootPath);
                }
                else
                {
                    _logger.LogInformation(
                        $"Could not find any file with extension '{PhpConstants.PhpFileNamePattern}' " +
                        $"in the repo.");
                    return(null);
                }
            }

            return(new PlatformDetectorResult
            {
                Platform = PhpConstants.PlatformName,
                PlatformVersion = phpVersion,
                AppDirectory = appDirectory,
            });
        }
예제 #2
0
        /// <inheritdoc/>
        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;
            }
            else
            {
                _logger.LogInformation(
                    $"Cound not find {PythonConstants.RequirementsFileName} at the root of the repo.");
            }
            if (sourceRepo.FileExists(PythonConstants.PyprojectTomlFileName))
            {
                _logger.LogInformation($"Found {PythonConstants.PyprojectTomlFileName} at the root of the repo.");
                hasPyprojectTomlFile = true;
            }
            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,
            });
        }
예제 #3
0
        /// <inheritdoc/>
        public PlatformDetectorResult Detect(DetectorContext context)
        {
            var sourceRepo                = context.SourceRepo;
            var appDirectory              = string.Empty;
            var hasRequirementsTxtFile    = false;
            var hasPyprojectTomlFile      = false;
            var customRequirementsTxtPath = this.options.CustomRequirementsTxtPath;
            var requirementsTxtPath       = customRequirementsTxtPath == null ? PythonConstants.RequirementsFileName : customRequirementsTxtPath;

            if (sourceRepo.FileExists(requirementsTxtPath))
            {
                this.logger.LogInformation($"Found {requirementsTxtPath.Hash()} at the root of the repo.");
                hasRequirementsTxtFile = true;

                // Warning if missing django module
                bool   hasDjangoModule = false;
                string filePath        = $"{sourceRepo.RootPath}/{requirementsTxtPath}";
                using (var reader = new StreamReader(filePath))
                {
                    while (!reader.EndOfStream && !hasDjangoModule)
                    {
                        string line = reader.ReadLine().ToLower();
                        if (line.StartsWith("django"))
                        {
                            hasDjangoModule = true;
                        }
                    }
                }

                if (!hasDjangoModule)
                {
                    this.logger.LogWarning($"Missing django module in {requirementsTxtPath.Hash()}");
                }
                else
                {
                    // detect django files exist
                    foreach (string djangoFileName in PythonConstants.DjangoFileNames)
                    {
                        if (!sourceRepo.FileExists(djangoFileName))
                        {
                            this.logger.LogWarning($"Missing {djangoFileName} at the root of the repo. More information: https://aka.ms/missing-django-files");
                        }
                    }
                }
            }
            else
            {
                string errorMsg = $"Cound not find {requirementsTxtPath.Hash()} at the root of the repo. More information: https://aka.ms/requirements-not-found";
                this.logger.LogError(errorMsg);
            }

            if (sourceRepo.FileExists(PythonConstants.PyprojectTomlFileName))
            {
                this.logger.LogInformation($"Found {PythonConstants.PyprojectTomlFileName} at the root of the repo.");
                hasPyprojectTomlFile = true;
            }
            else
            {
                this.logger.LogError($"Missing {PythonConstants.SetupDotPyFileName} at the root of the repo. More information: https://aka.ms/requirements-not-found");
            }

            if (sourceRepo.FileExists(PythonConstants.SetupDotPyFileName))
            {
                this.logger.LogInformation($"Found {PythonConstants.SetupDotPyFileName} at the root of the repo.");
                hasPyprojectTomlFile = true;
            }
            else
            {
                this.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) &&
                this.IsCondaEnvironmentFile(sourceRepo, PythonConstants.CondaEnvironmentYmlFileName))
            {
                this.logger.LogInformation(
                    $"Found {PythonConstants.CondaEnvironmentYmlFileName} at the root of the repo.");
                hasCondaEnvironmentYmlFile = true;
            }

            if (!hasCondaEnvironmentYmlFile &&
                sourceRepo.FileExists(PythonConstants.CondaEnvironmentYamlFileName) &&
                this.IsCondaEnvironmentFile(sourceRepo, PythonConstants.CondaEnvironmentYamlFileName))
            {
                this.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())
            {
                this.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 = this.DetectPythonVersionFromRuntimeFile(context.SourceRepo);

            if (!string.IsNullOrEmpty(versionFromRuntimeFile))
            {
                hasRuntimeTxtFile = true;
            }

            if (!hasRequirementsTxtFile &&
                !hasCondaEnvironmentYmlFile &&
                !hasJupyterNotebookFiles &&
                !hasRuntimeTxtFile &&
                !hasPyprojectTomlFile)
            {
                var searchSubDirectories = !this.options.DisableRecursiveLookUp;
                if (!searchSubDirectories)
                {
                    this.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())
                {
                    this.logger.LogInformation(
                        $"Found files with extension '{PythonConstants.PythonFileNamePattern}' " +
                        $"in the repo.");
                    appDirectory = RelativeDirectoryHelper.GetRelativeDirectoryToRoot(
                        files.FirstOrDefault(), sourceRepo.RootPath);
                }
                else
                {
                    this.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,
            });
        }