Exemple #1
0
        public void UnavailableEnvironments()
        {
            var collection = new Microsoft.Build.Evaluation.ProjectCollection();

            try {
                var service = new MockInterpreterOptionsService();
                var proj    = collection.LoadProject(TestData.GetPath(@"TestData\Environments\Unavailable.pyproj"));

                using (var provider = new MSBuildProjectInterpreterFactoryProvider(service, proj)) {
                    try {
                        provider.DiscoverInterpreters();
                        Assert.Fail("Expected InvalidDataException in DiscoverInterpreters");
                    } catch (InvalidDataException ex) {
                        AssertUtil.AreEqual(ex.Message
                                            .Replace(TestData.GetPath("TestData\\Environments\\"), "$")
                                            .Split('\r', '\n')
                                            .Where(s => !string.IsNullOrEmpty(s))
                                            .Select(s => s.Trim()),
                                            "Some project interpreters failed to load:",
                                            @"Interpreter $env\ has invalid value for 'Id': INVALID ID",
                                            @"Interpreter $env\ has invalid value for 'Version': INVALID VERSION",
                                            @"Base interpreter $env\ has invalid value for 'BaseInterpreter': INVALID BASE",
                                            @"Interpreter $env\ has invalid value for 'InterpreterPath': INVALID<>PATH",
                                            @"Interpreter $env\ has invalid value for 'WindowsInterpreterPath': INVALID<>PATH",
                                            @"Interpreter $env\ has invalid value for 'LibraryPath': INVALID<>PATH",
                                            @"Base interpreter $env\ has invalid value for 'BaseInterpreter': {98512745-4ac7-4abb-9f33-120af32edc77}"
                                            );
                    }

                    var factories = provider.GetInterpreterFactories().ToList();
                    foreach (var fact in factories)
                    {
                        Console.WriteLine("{0}: {1}", fact.GetType().FullName, fact.Description);
                    }

                    foreach (var fact in factories)
                    {
                        Assert.IsInstanceOfType(
                            fact,
                            typeof(MSBuildProjectInterpreterFactoryProvider.NotFoundInterpreterFactory),
                            string.Format("{0} was not correct type", fact.Description)
                            );
                        Assert.IsFalse(provider.IsAvailable(fact), string.Format("{0} was not unavailable", fact.Description));
                    }

                    AssertUtil.AreEqual(factories.Select(f => f.Description),
                                        "Invalid BaseInterpreter (unavailable)",
                                        "Invalid InterpreterPath (unavailable)",
                                        "Invalid WindowsInterpreterPath (unavailable)",
                                        "Invalid LibraryPath (unavailable)",
                                        "Absent BaseInterpreter (unavailable)",
                                        "Unknown Python 2.7"
                                        );
                }
            } finally {
                collection.UnloadAllProjects();
                collection.Dispose();
            }
        }
Exemple #2
0
        protected override ImageMoniker GetIconMoniker(bool open)
        {
            if (!_interpreters.IsAvailable(_factory))
            {
                // TODO: Find a better icon
                return(KnownMonikers.DocumentWarning);
            }
            else if (_interpreters.ActiveInterpreter == _factory)
            {
                return(KnownMonikers.ActiveEnvironment);
            }

            // TODO: Change to PYEnvironment
            return(KnownMonikers.DockPanel);
        }
Exemple #3
0
        public override object GetIconHandle(bool open)
        {
            if (ProjectMgr == null)
            {
                return(null);
            }

            int index;

            if (!_interpreters.IsAvailable(_factory))
            {
                index = ProjectMgr.GetIconIndex(PythonProjectImageName.MissingInterpreter);
            }
            else if (_interpreters.ActiveInterpreter == _factory)
            {
                index = ProjectMgr.GetIconIndex(PythonProjectImageName.ActiveInterpreter);
            }
            else
            {
                index = ProjectMgr.GetIconIndex(PythonProjectImageName.Interpreter);
            }
            return(this.ProjectMgr.ImageHandler.GetIconHandle(index));
        }
Exemple #4
0
        public bool Execute()
        {
            bool    returnActive;
            Guid    id;
            Version version;

            returnActive = !Guid.TryParse(InterpreterId, out id);

            if (!Version.TryParse(InterpreterVersion, out version))
            {
                if (!returnActive)
                {
                    _log.LogError(
                        "Invalid values for InterpreterId (\"{0}\") and InterpreterVersion (\"{1}\")",
                        InterpreterId,
                        InterpreterVersion
                        );
                    return(false);
                }
            }

            MSBuildProjectInterpreterFactoryProvider provider = null;
            ProjectCollection collection = null;
            Project           project    = null;

            var service = ServiceHolder.Create();

            if (service == null)
            {
                _log.LogError("Unable to obtain interpreter service.");
                return(false);
            }

            try {
                try {
                    project = ProjectCollection.GlobalProjectCollection.GetLoadedProjects(_projectPath).Single();
                } catch (InvalidOperationException) {
                    // Could not get exactly one project matching the path.
                }

                if (project == null)
                {
                    collection = new ProjectCollection();
                    project    = collection.LoadProject(_projectPath);
                }

                var projectHome = PathUtils.GetAbsoluteDirectoryPath(
                    project.DirectoryPath,
                    project.GetPropertyValue("ProjectHome")
                    );

                var searchPath = project.GetPropertyValue("SearchPath");
                if (!string.IsNullOrEmpty(searchPath))
                {
                    SearchPaths = searchPath.Split(';')
                                  .Select(p => PathUtils.GetAbsoluteFilePath(projectHome, p))
                                  .ToArray();
                }
                else
                {
                    SearchPaths = new string[0];
                }

                provider = new MSBuildProjectInterpreterFactoryProvider(service.Service, project);
                try {
                    provider.DiscoverInterpreters();
                } catch (InvalidDataException ex) {
                    _log.LogWarning("Errors while resolving environments: {0}", ex.Message);
                }

                IPythonInterpreterFactory factory = null;
                if (returnActive)
                {
                    factory = provider.ActiveInterpreter;
                }
                else
                {
                    factory = provider.FindInterpreter(id, version);
                }

                if (!provider.IsAvailable(factory))
                {
                    _log.LogError(
                        "The environment '{0}' is not available. Check your project configuration and try again.",
                        factory.Description
                        );
                    return(false);
                }
                else if (factory == service.Service.NoInterpretersValue)
                {
                    _log.LogError(
                        "No Python environments are configured. Please install or configure an environment and try " +
                        "again. See http://go.microsoft.com/fwlink/?LinkID=299429 for information on setting up a " +
                        "Python environment."
                        );
                    return(false);
                }
                else if (factory != null)
                {
                    PrefixPath = PathUtils.EnsureEndSeparator(factory.Configuration.PrefixPath);
                    if (PathUtils.IsSubpathOf(projectHome, PrefixPath))
                    {
                        ProjectRelativePrefixPath = PathUtils.GetRelativeDirectoryPath(projectHome, PrefixPath);
                    }
                    else
                    {
                        ProjectRelativePrefixPath = string.Empty;
                    }
                    InterpreterPath         = factory.Configuration.InterpreterPath;
                    WindowsInterpreterPath  = factory.Configuration.WindowsInterpreterPath;
                    LibraryPath             = PathUtils.EnsureEndSeparator(factory.Configuration.LibraryPath);
                    Architecture            = factory.Configuration.Architecture.ToString();
                    PathEnvironmentVariable = factory.Configuration.PathEnvironmentVariable;
                    Description             = factory.Description;
                    MajorVersion            = factory.Configuration.Version.Major.ToString();
                    MinorVersion            = factory.Configuration.Version.Minor.ToString();

                    return(true);
                }
                else if (returnActive)
                {
                    _log.LogError("Unable to resolve active environment.");
                }
                else
                {
                    _log.LogError("Unable to resolve environment {0} {1}", InterpreterId, InterpreterVersion);
                }
            } catch (Exception ex) {
                _log.LogErrorFromException(ex);
            } finally {
                if (provider != null)
                {
                    provider.Dispose();
                }
                if (collection != null)
                {
                    collection.UnloadAllProjects();
                    collection.Dispose();
                }
                service.Dispose();
            }

            _log.LogError("Unable to resolve environment");
            return(false);
        }