Exemple #1
0
 private bool IsRealInterpreter(IPythonInterpreterFactory factory) {
     if (factory == null) {
         return false;
     }
     var interpreterService = _serviceProvider.GetComponentModel().GetService<IInterpreterOptionsService>();
     return interpreterService != null && interpreterService.NoInterpretersValue != factory;
 }
        internal static IVsInteractiveWindow/*!*/ EnsureReplWindow(IServiceProvider serviceProvider, IPythonInterpreterFactory factory, PythonProjectNode project) {
            var compModel = serviceProvider.GetComponentModel();
            var provider = compModel.GetService<InteractiveWindowProvider>();

            string replId = PythonReplEvaluatorProvider.GetReplId(factory, project);
            var window = provider.FindReplWindow(replId);
            if (window == null) {
                window = provider.CreateInteractiveWindow(
                    serviceProvider.GetPythonContentType(),
                    factory.Description + " Interactive",
                    typeof(PythonLanguageInfo).GUID,
                    replId
                );

                var toolWindow = window as ToolWindowPane;
                if (toolWindow != null) {
                    toolWindow.BitmapImageMoniker = KnownMonikers.PYInteractiveWindow;
                }

                var pyService = serviceProvider.GetPythonToolsService();
                window.InteractiveWindow.SetSmartUpDown(pyService.GetInteractiveOptions(factory).ReplSmartHistory);
            }

            if (project != null && project.Interpreters.IsProjectSpecific(factory)) {
                project.AddActionOnClose(window, BasePythonReplEvaluator.CloseReplWindow);
            }

            return window;
        }
 public void AddFactory(IPythonInterpreterFactory factory) {
     _factories.Add(factory);
     var evt = InterpreterFactoriesChanged;
     if (evt != null) {
         evt(this, EventArgs.Empty);
     }
 }
Exemple #4
0
        private static ProcessOutput Run(
            IPythonInterpreterFactory factory,
            Redirector output,
            bool elevate,
            params string[] cmd
        ) {
            factory.ThrowIfNotRunnable("factory");

            IEnumerable<string> args;
            if (factory.Configuration.Version >= SupportsDashMPip) {
                args = new[] { "-m", "pip" }.Concat(cmd);
            } else {
                // Manually quote the code, since we are passing false to
                // quoteArgs below.
                args = new[] { "-c", "\"import pip; pip.main()\"" }.Concat(cmd);
            }

            return ProcessOutput.Run(
                factory.Configuration.InterpreterPath,
                args,
                factory.Configuration.PrefixPath,
                UnbufferedEnv,
                false,
                output,
                quoteArgs: false,
                elevate: elevate
            );
        }
        public AddVirtualEnvironmentView(
            PythonProjectNode project,
            IInterpreterRegistryService interpreterService,
            IPythonInterpreterFactory selectInterpreter
        ) {
            _interpreterService = interpreterService;
            _project = project;
            VirtualEnvBasePath = _projectHome = project.ProjectHome;
            Interpreters = new ObservableCollection<InterpreterView>(InterpreterView.GetInterpreters(project.Site, project));
            var selection = Interpreters.FirstOrDefault(v => v.Interpreter == selectInterpreter);
            if (selection == null) {
                selection = Interpreters.FirstOrDefault(v => v.Interpreter == project.GetInterpreterFactory())
                    ?? Interpreters.LastOrDefault();
            }
            BaseInterpreter = selection;

            _project.InterpreterFactoriesChanged += OnInterpretersChanged;

            var venvName = "env";
            for (int i = 1; Directory.Exists(Path.Combine(_projectHome, venvName)); ++i) {
                venvName = "env" + i.ToString();
            }
            VirtualEnvName = venvName;

            CanInstallRequirementsTxt = File.Exists(PathUtils.GetAbsoluteFilePath(_projectHome, "requirements.txt"));
            WillInstallRequirementsTxt = CanInstallRequirementsTxt;
        }
        public void Save(IPythonInterpreterFactory interpreter) {
            base.Save();
            _pyService.SaveString(_id + InterpreterOptionsSetting, _category, InterpreterOptions ?? "");
            _pyService.SaveBool(_id + EnableAttachSetting, _category, EnableAttach);
            _pyService.SaveString(_id + ExecutionModeSetting, _category, ExecutionMode ?? "");
            _pyService.SaveString(_id + StartupScriptSetting, _category, StartupScript ?? "");
            var replProvider = _serviceProvider.GetComponentModel().GetService<IReplWindowProvider>();
            if (replProvider != null) {
                // propagate changed settings to existing REPL windows
                foreach (var replWindow in replProvider.GetReplWindows()) {
                    PythonReplEvaluator pyEval = replWindow.Evaluator as PythonReplEvaluator;
                    if (EvaluatorUsesThisInterpreter(pyEval, interpreter)) {
                        if (UseInterpreterPrompts) {
                            replWindow.UseInterpreterPrompts();
                        } else {
                            replWindow.SetPrompts(PrimaryPrompt, SecondaryPrompt);
                        }
#if !DEV14_OR_LATER
                        replWindow.SetOptionValue(ReplOptions.DisplayPromptInMargin, !InlinePrompts);
#endif
                        replWindow.SetSmartUpDown(ReplSmartHistory);
                    }
                }
            }
        }
Exemple #7
0
        public static async Task<HashSet<string>> List(IPythonInterpreterFactory factory) {
            using (var proc = Run(factory, null, false, "list")) {
                if (await proc == 0) {
                    return new HashSet<string>(proc.StandardOutputLines
                        .Select(line => Regex.Match(line, "(?<name>.+?) \\((?<version>.+?)\\)"))
                        .Where(match => match.Success)
                        .Select(match => string.Format("{0}=={1}",
                            match.Groups["name"].Value,
                            match.Groups["version"].Value
                        ))
                    );
                }
            }

            // Pip failed, so return a directory listing
            var packagesPath = Path.Combine(factory.Configuration.LibraryPath, "site-packages");
            HashSet<string> result = null;
            if (Directory.Exists(packagesPath)) {
                result = await Task.Run(() => new HashSet<string>(Directory.EnumerateDirectories(packagesPath)
                    .Select(path => Path.GetFileName(path))
                    .Select(name => PackageNameRegex.Match(name))
                    .Where(match => match.Success)
                    .Select(match => match.Groups["name"].Value)
                ))
                    .SilenceException<IOException, HashSet<string>>()
                    .SilenceException<UnauthorizedAccessException, HashSet<string>>()
                    .HandleAllExceptions(SR.ProductName, typeof(Pip));
            }

            return result ?? new HashSet<string>();
        }
Exemple #8
0
        private static async Task<int> ContinueRun(
            IPythonInterpreterFactory factory,
            Redirector output,
            bool elevate,
            params string[] cmd
        ) {
            bool isScript;
            var easyInstallPath = GetEasyInstallPath(factory, out isScript);
            if (easyInstallPath == null) {
                throw new FileNotFoundException("Cannot find setuptools ('easy_install.exe')");
            }

            var args = cmd.ToList();
            args.Insert(0, "--always-copy");
            args.Insert(0, "--always-unzip");
            if (isScript) {
                args.Insert(0, ProcessOutput.QuoteSingleArgument(easyInstallPath));
                easyInstallPath = factory.Configuration.InterpreterPath;
            }
            using (var proc = ProcessOutput.Run(
                easyInstallPath,
                args,
                factory.Configuration.PrefixPath,
                UnbufferedEnv,
                false,
                output,
                false,
                elevate
            )) {
                return await proc;
            }
        }
Exemple #9
0
 public IronPythonAnalysis(
     IPythonInterpreterFactory factory,
     IPythonInterpreter interpreter = null,
     string builtinName = null
 ) : base(factory, interpreter, builtinName) {
     ((IronPythonInterpreter)Analyzer.Interpreter).Remote.AddAssembly(new ObjectHandle(typeof(IronPythonAnalysisTest).Assembly));
 }
Exemple #10
0
 public PythonReplEvaluator(IPythonInterpreterFactory interpreter, IServiceProvider serviceProvider, PythonReplEvaluatorOptions options, IInterpreterOptionsService interpreterService = null)
     : base(serviceProvider, serviceProvider.GetPythonToolsService(), options) {
     _interpreter = interpreter;
     _interpreterService = interpreterService;
     if (_interpreterService != null) {
         _interpreterService.InterpretersChanged += InterpretersChanged;
     }
 }
        public void SetDefault(IPythonInterpreterFactory factory) {
            Assert.IsNotNull(factory, "Cannot set default to null");
            var interpreterService = _model.GetService<IInterpreterOptionsService>();
            Assert.IsNotNull(interpreterService);

            CurrentDefault = factory;
            interpreterService.DefaultInterpreter = factory;
        }
Exemple #12
0
        private static async Task<IPythonInterpreterFactory> TryGetCondaFactoryAsync(
            IPythonInterpreterFactory target,
            IInterpreterOptionsService service
        ) {
            var condaMetaPath = CommonUtils.GetAbsoluteDirectoryPath(
                target.Configuration.PrefixPath,
                "conda-meta"
            );

            if (!Directory.Exists(condaMetaPath)) {
                return null;
            }

            string metaFile;
            try {
                metaFile = Directory.EnumerateFiles(condaMetaPath, "*.json").FirstOrDefault();
            } catch (Exception ex) {
                if (ex.IsCriticalException()) {
                    throw;
                }
                return null;
            }

            if (!string.IsNullOrEmpty(metaFile)) {
                string text = string.Empty;
                try {
                    text = File.ReadAllText(metaFile);
                } catch (Exception ex) {
                    if (ex.IsCriticalException()) {
                        throw;
                    }
                }

                var m = Regex.Match(text, @"\{[^{]+link.+?\{.+?""source""\s*:\s*""(.+?)""", RegexOptions.Singleline);
                if (m.Success) {
                    var pkg = m.Groups[1].Value;
                    if (!Directory.Exists(pkg)) {
                        return null;
                    }

                    var prefix = Path.GetDirectoryName(Path.GetDirectoryName(pkg));
                    var factory = service.Interpreters.FirstOrDefault(
                        f => CommonUtils.IsSameDirectory(f.Configuration.PrefixPath, prefix)
                    );

                    if (factory != null && !(await factory.FindModulesAsync("conda")).Any()) {
                        factory = null;
                    }

                    return factory;
                }
            }

            if ((await target.FindModulesAsync("conda")).Any()) {
                return target;
            }
            return null;
        }
        /// <summary>
        /// Determines whether two interpreter factories are equivalent.
        /// </summary>
        public static bool IsEqual(this IPythonInterpreterFactory x, IPythonInterpreterFactory y) {
            if (x == null || y == null) {
                return x == null && y == null;
            }
            if (x.GetType() != y.GetType()) {
                return false;
            }

            return x.Configuration.Equals(y.Configuration);
        }
        public DefaultInterpreterSetter(IPythonInterpreterFactory factory) {
            var props = VsIdeTestHostContext.Dte.get_Properties("Python Tools", "Interpreters");
            Assert.IsNotNull(props);

            OriginalInterpreter = props.Item("DefaultInterpreter").Value;
            OriginalVersion = props.Item("DefaultInterpreterVersion").Value;

            props.Item("DefaultInterpreter").Value = factory.Id;
            props.Item("DefaultInterpreterVersion").Value = string.Format("{0}.{1}", factory.Configuration.Version.Major, factory.Configuration.Version.Minor);
        }
 public bool RemoveFactory(IPythonInterpreterFactory factory) {
     if (_factories.Remove(factory)) {
         var evt = InterpreterFactoriesChanged;
         if (evt != null) {
             evt(this, EventArgs.Empty);
         }
         return true;
     }
     return false;
 }
        public DefaultInterpreterSetter(IPythonInterpreterFactory factory, IServiceProvider site = null) {
            Assert.IsNotNull(factory, "Cannot set default to null");
            _model = (IComponentModel)(site ?? VSTestContext.ServiceProvider).GetService(typeof(SComponentModel));
            var interpreterService = _model.GetService<IInterpreterOptionsService>();
            Assert.IsNotNull(interpreterService);

            OriginalInterpreter = interpreterService.DefaultInterpreter;
            CurrentDefault = factory;
            interpreterService.DefaultInterpreter = factory;
        }
Exemple #17
0
        internal EnvironmentView(
            IInterpreterOptionsService service,
            IInterpreterRegistryService registry,
            IPythonInterpreterFactory factory,
            Redirector redirector
        ) {
            if (service == null) {
                throw new ArgumentNullException(nameof(service));
            }
            if (registry == null) {
                throw new ArgumentNullException(nameof(registry));
            }
            if (factory == null) {
                throw new ArgumentNullException(nameof(factory));
            }
            if (factory.Configuration == null) {
                throw new ArgumentException("factory must include a configuration");
            }

            _service = service;
            _registry = registry;
            Factory = factory;
            Configuration = Factory.Configuration;

            _withDb = factory as IPythonInterpreterFactoryWithDatabase;
            if (_withDb != null) {
                _withDb.IsCurrentChanged += Factory_IsCurrentChanged;
                IsCheckingDatabase = _withDb.IsCheckingDatabase;
                IsCurrent = _withDb.IsCurrent;
            }
            

            if (_service.IsConfigurable(Factory.Configuration.Id)) {
                IsConfigurable = true;
            }

            Description = Factory.Configuration.Description;
            IsDefault = (_service != null && _service.DefaultInterpreterId == Configuration.Id);

            PrefixPath = Factory.Configuration.PrefixPath;
            InterpreterPath = Factory.Configuration.InterpreterPath;
            WindowsInterpreterPath = Factory.Configuration.WindowsInterpreterPath;

            Extensions = new ObservableCollection<object>();
            Extensions.Add(new EnvironmentPathsExtensionProvider());
            if (IsConfigurable) {
                Extensions.Add(new ConfigurationExtensionProvider(_service, alwaysCreateNew: false));
            }

            CanBeDefault = Factory.CanBeDefault();

            Company = _registry.GetProperty(Factory.Configuration.Id, CompanyKey) as string ?? "";
            SupportUrl = _registry.GetProperty(Factory.Configuration.Id, SupportUrlKey) as string ?? "";
        }
Exemple #18
0
 /// <summary>
 /// Creates a provider for managing packages through pip.
 /// </summary>
 /// <param name="factory">The associated interpreter.</param>
 /// <param name="index">
 /// The index URL. Defaults to https://pypi.python.org/pypi/
 /// </param>
 /// <param name="indexName">
 /// Display name of the index. Defaults to PyPI.
 /// </param>
 public PipExtensionProvider(
     IPythonInterpreterFactory factory,
     string index = null,
     string indexName = null
 ) {
     _factory = factory;
     _output = new PipRedirector(this);
     if (!string.IsNullOrEmpty(index) && Uri.TryCreate(index, UriKind.Absolute, out _index)) {
         _indexName = string.IsNullOrEmpty(indexName) ? _index.Host : indexName;
     }
     _cache = PipPackageCache.GetCache(_index, _indexName);
 }
 public static InstallPythonPackageView ShowDialog(
     IServiceProvider serviceProvider,
     IPythonInterpreterFactory factory,
     IInterpreterOptionsService service
 ) {
     var wnd = new InstallPythonPackage(serviceProvider, factory, service);
     if (wnd.ShowModal() ?? false) {
         return wnd._view;
     } else {
         return null;
     }
 }
Exemple #20
0
 /// <summary>
 /// Installs virtualenv. If pip is not installed, the returned task will
 /// succeed but error text will be passed to the redirector.
 /// </summary>
 public static Task<bool> Install(IServiceProvider provider, IPythonInterpreterFactory factory, Redirector output = null) {
     bool elevate = provider.GetPythonToolsService().GeneralOptions.ElevatePip;
     if (factory.Configuration.Version < new Version(2, 5)) {
         if (output != null) {
             output.WriteErrorLine("Python versions earlier than 2.5 are not supported by PTVS.");
         }
         throw new OperationCanceledException();
     } else if (factory.Configuration.Version == new Version(2, 5)) {
         return Pip.Install(provider, factory, "https://go.microsoft.com/fwlink/?LinkID=317970", elevate, output);
     } else {
         return Pip.Install(provider, factory, "https://go.microsoft.com/fwlink/?LinkID=317969", elevate, output);
     }
 }
Exemple #21
0
        public PythonAnalysis CreateAnalyzer(IPythonInterpreterFactory factory = null, bool allowParseErrors = false) {
            var analysis = CreateAnalyzerInternal(factory ?? DefaultFactoryV2);
            analysis.AssertOnParseErrors = !allowParseErrors;
            analysis.ModuleContext = DefaultContext;
            analysis.SetLimits(GetLimits());

            if (_toDispose == null) {
                _toDispose = new List<IDisposable>();
            }
            _toDispose.Add(analysis);

            return analysis;
        }
Exemple #22
0
        private static string GetEasyInstallPath(IPythonInterpreterFactory factory, out bool isScript) {
            factory.ThrowIfNotRunnable("factory");

            foreach (var path in EasyInstallLocations) {
                string easyInstallPath = Path.Combine(factory.Configuration.PrefixPath, path.Key);
                isScript = path.Value;
                if (File.Exists(easyInstallPath)) {
                    return easyInstallPath;
                }
            }
            isScript = false;
            return null;
        }
Exemple #23
0
 /// <summary>
 /// Installs virtualenv. If pip is not installed, the returned task will
 /// succeed but error text will be passed to the redirector.
 /// </summary>
 public static Task<bool> Install(IServiceProvider provider, IPythonInterpreterFactory factory) {
     var ui = new VsPackageManagerUI(provider);
     if (factory.Configuration.Version < new Version(2, 5)) {
         ui.OnErrorTextReceived(null, "Python versions earlier than 2.5 are not supported by PTVS.\n");
         throw new OperationCanceledException();
     } else if (factory.PackageManager == null) {
         ui.OnErrorTextReceived(null, Strings.PackageManagementNotSupported_Package.FormatUI("virtualenv"));
         throw new OperationCanceledException();
     } else if (factory.Configuration.Version == new Version(2, 5)) {
         return factory.PackageManager.InstallAsync(PackageSpec.FromArguments("https://go.microsoft.com/fwlink/?LinkID=317970"), ui, CancellationToken.None);
     } else {
         return factory.PackageManager.InstallAsync(PackageSpec.FromArguments("https://go.microsoft.com/fwlink/?LinkID=317969"), ui, CancellationToken.None);
     }
 }
        private InstallPythonPackage(
            IServiceProvider serviceProvider,
            IPythonInterpreterFactory factory,
            IInterpreterOptionsService service
        ) {
            _view = new InstallPythonPackageView(
                serviceProvider,
                !Pip.IsSecureInstall(factory),
                Conda.CanInstall(factory, service)
            );
            DataContext = _view;

            InitializeComponent();
            _textBox.Focus();
        }
Exemple #25
0
        internal EnvironmentView(
            IInterpreterOptionsService service,
            IPythonInterpreterFactory factory,
            Redirector redirector
        ) {
            if (service == null) {
                throw new ArgumentNullException("service");
            }
            if (factory == null) {
                throw new ArgumentNullException("factory");
            }

            _service = service;
            Factory = factory;

            _withDb = factory as IPythonInterpreterFactoryWithDatabase;
            if (_withDb != null) {
                _withDb.IsCurrentChanged += Factory_IsCurrentChanged;
                IsCheckingDatabase = _withDb.IsCheckingDatabase;
                IsCurrent = _withDb.IsCurrent;
            }

            var configurableProvider = _service != null ?
                _service.KnownProviders
                    .OfType<ConfigurablePythonInterpreterFactoryProvider>()
                    .FirstOrDefault() :
                null;

            if (configurableProvider != null && configurableProvider.IsConfigurable(factory)) {
                IsConfigurable = true;
            }

            Description = Factory.Description;
            IsDefault = (_service != null && _service.DefaultInterpreter == Factory);

            PrefixPath = Factory.Configuration.PrefixPath;
            InterpreterPath = Factory.Configuration.InterpreterPath;
            WindowsInterpreterPath = Factory.Configuration.WindowsInterpreterPath;
            LibraryPath = Factory.Configuration.LibraryPath;

            Extensions = new ObservableCollection<object>();
            Extensions.Add(new EnvironmentPathsExtensionProvider());
            if (IsConfigurable) {
                Extensions.Add(new ConfigurationExtensionProvider(configurableProvider));
            }

            CanBeDefault = Factory.CanBeDefault();
        }
Exemple #26
0
        public TestAnalyzer(
            IPythonInterpreterFactory factory,
            string containerFilePath,
            string codeFileBasePath,
            Uri executorUri
        ) {
            _analyzer = PythonAnalyzer.CreateAsync(factory).WaitAndUnwrapExceptions();
            _analyzer.Limits = AnalysisLimits.GetStandardLibraryLimits();
            _analyzer.Limits.ProcessCustomDecorators = false;

            _containerFilePath = containerFilePath;
            _codeFileBasePath = codeFileBasePath;
            _executorUri = executorUri;

            _entries = new List<IPythonProjectEntry>();
        }
        /// <summary>
        /// Creates a provider for managing packages through pip.
        /// </summary>
        /// <param name="factory">The associated interpreter.</param>
        /// <param name="index">
        /// The index URL. Defaults to https://pypi.python.org/pypi/
        /// </param>
        /// <param name="indexName">
        /// Display name of the index. Defaults to PyPI.
        /// </param>
        public PipExtensionProvider(
            IPythonInterpreterFactory factory,
            string index = null,
            string indexName = null
        ) {
            _factory = factory;
            _packageManager = _factory.PackageManager;
            if (_packageManager == null) {
                throw new NotSupportedException();
            }

            if (!string.IsNullOrEmpty(index) && Uri.TryCreate(index, UriKind.Absolute, out _index)) {
                _indexName = string.IsNullOrEmpty(indexName) ? _index.Host : indexName;
            }
            _cache = PipPackageCache.GetCache(_index, _indexName);
        }
 public IPythonInterpreterFactory EnsureFactory() {
     if (Factory == null) {
         lock (this) {
             if (Factory == null) {
                 Factory = InterpreterFactoryCreator.CreateInterpreterFactory(
                     Configuration,
                     new InterpreterFactoryCreationOptions {
                         PackageManager = new PipPackageManager(),
                         WatchFileSystem = true
                     }
                 );
             }
         }
     }
     return Factory;
 }
Exemple #29
0
        public BaseAnalysisTest(IPythonInterpreterFactory factory, IPythonInterpreter interpreter) {
            InterpreterFactory = factory;
            Interpreter = interpreter;
            var objectType = Interpreter.GetBuiltinType(BuiltinTypeId.Object);
            Assert.IsNotNull(objectType);
            var intType = Interpreter.GetBuiltinType(BuiltinTypeId.Int);
            var bytesType = Interpreter.GetBuiltinType(BuiltinTypeId.Bytes);
            var listType = Interpreter.GetBuiltinType(BuiltinTypeId.List);
            var functionType = Interpreter.GetBuiltinType(BuiltinTypeId.Function);

            _objectMembers = objectType.GetMemberNames(DefaultContext).ToArray();
            _strMembers = bytesType.GetMemberNames(DefaultContext).ToArray();
            _listMembers = listType.GetMemberNames(DefaultContext).ToArray();
            _intMembers = intType.GetMemberNames(DefaultContext).ToArray();
            _functionMembers = functionType.GetMemberNames(DefaultContext).ToArray();
        }
Exemple #30
0
        public void Load() {
            var configurable = _pyService._interpreterOptionsService.KnownProviders.OfType<ConfigurablePythonInterpreterFactoryProvider>().FirstOrDefault();
            Debug.Assert(configurable != null);

            Display = _interpreter.Description;
            Id = _interpreter.Id;
            InterpreterPath = _interpreter.Configuration.InterpreterPath;
            WindowsInterpreterPath = _interpreter.Configuration.WindowsInterpreterPath;
            LibraryPath = _interpreter.Configuration.LibraryPath;
            Version = _interpreter.Configuration.Version.ToString();
            Architecture = FormatArchitecture(_interpreter.Configuration.Architecture);
            PathEnvironmentVariable = _interpreter.Configuration.PathEnvironmentVariable;
            IsConfigurable = configurable != null && configurable.IsConfigurable(_interpreter);
            SupportsCompletionDb = _interpreter is IPythonInterpreterFactoryWithDatabase;
            Factory = _interpreter;
        }
Exemple #31
0
 /// <summary>
 /// Returns true if installing a package will be secure.
 ///
 /// This returns false for Python 2.5 and earlier because it does not
 /// include the required SSL support by default. No detection is done to
 /// determine whether the support has been added separately.
 /// </summary>
 public static bool IsSecureInstall(IPythonInterpreterFactory factory)
 {
     return(factory.Configuration.Version > new Version(2, 5));
 }
Exemple #32
0
        public AnalysisView(
            string dbDir,
            Version version     = null,
            bool withContention = false,
            bool withRecursion  = false
            )
        {
            var paths = new List <string>();

            paths.Add(dbDir);
            while (!File.Exists(IOPath.Combine(paths[0], "__builtin__.idb")) &&
                   !File.Exists(IOPath.Combine(paths[0], "builtins.idb")))
            {
                var upOne = IOPath.GetDirectoryName(paths[0]);
                if (string.IsNullOrEmpty(upOne) || upOne == paths[0])
                {
                    break;
                }
                paths.Insert(0, upOne);
            }

            if (withRecursion)
            {
                paths.AddRange(Directory.EnumerateDirectories(dbDir, "*", SearchOption.AllDirectories));
            }

            if (version == null)
            {
                if (File.Exists(IOPath.Combine(paths[0], "builtins.idb")))
                {
                    version = new Version(3, 3);
                }
                else
                {
                    version = new Version(2, 7);
                }
            }

            _factory = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(
                version,
                dbDir,
                paths.ToArray()
                );
            Path         = dbDir;
            _interpreter = _factory.CreateInterpreter();
            _context     = _interpreter.CreateModuleContext();

            var modNames = _interpreter.GetModuleNames();
            IEnumerable <Tuple <string, string> > modItems;

            if (!withRecursion)
            {
                modItems = modNames
                           .Select(n => Tuple.Create(n, IOPath.Combine(dbDir, n + ".idb")))
                           .Where(t => File.Exists(t.Item2));
            }
            else
            {
                modItems = modNames
                           .Select(n => Tuple.Create(
                                       n,
                                       Directory.EnumerateFiles(dbDir, n + ".idb", SearchOption.AllDirectories).FirstOrDefault()
                                       ))
                           .Where(t => File.Exists(t.Item2));
            }

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            if (withContention)
            {
                modItems = modItems
                           .AsParallel()
                           .WithExecutionMode(ParallelExecutionMode.ForceParallelism);
            }
            _modules = modItems
                       .Select(t => new ModuleView(_interpreter, _context, t.Item1, t.Item2))
                       .OrderBy(m => m.SortKey)
                       .ThenBy(m => m.Name)
                       .ToList <IAnalysisItemView>();
            stopwatch.Stop();

            _modules.Insert(0, new KnownTypesView(_interpreter, version));

            LoadMilliseconds    = stopwatch.ElapsedMilliseconds;
            TopLevelModuleCount = _modules.Count - 1;
        }
Exemple #33
0
        /// <summary>
        /// Removes an interpreter factory from the project. This function will
        /// modify the project, but does not handle source control.
        /// </summary>
        /// <param name="factory">
        /// The id of the factory to remove. The function returns silently if
        /// the factory is not known by this provider.
        /// </param>
        public void RemoveInterpreterFactory(IPythonInterpreterFactory factory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }

            if (!_factories.ContainsKey(factory))
            {
                return;
            }

            string rootPath;

            if (_rootPaths.TryGetValue(factory.Id, out rootPath))
            {
                _rootPaths.Remove(factory.Id);
                foreach (var item in _project.GetItems(InterpreterItem))
                {
                    Guid itemId;
                    if (Guid.TryParse(item.GetMetadataValue(IdKey), out itemId) && factory.Id == itemId)
                    {
                        _project.RemoveItem(item);
                        _project.MarkDirty();
                        break;
                    }
                }
            }
            else
            {
                foreach (var item in _project.GetItems(InterpreterReferenceItem))
                {
                    Guid    itemId;
                    Version itemVer;
                    var     match = InterpreterReferencePath.Match(item.EvaluatedInclude);
                    if (match != null && match.Success && match.Groups.Cast <Group>().All(g => g.Success) &&
                        Guid.TryParse(match.Groups["id"].Value, out itemId) &&
                        Version.TryParse(match.Groups["version"].Value, out itemVer) &&
                        factory.Id == itemId &&
                        factory.Configuration.Version == itemVer)
                    {
                        _project.RemoveItem(item);
                        _project.MarkDirty();
                        break;
                    }
                }
            }

            bool        raiseEvent;
            FactoryInfo factInfo;

            lock (_factoriesLock) {
                raiseEvent = _factories.TryGetValue(factory, out factInfo) && _factories.Remove(factory);
            }
            if (factInfo != null &&
                factInfo.Owned &&
                factory is IDisposable)
            {
                ((IDisposable)factory).Dispose();
            }
            UpdateActiveInterpreter();
            if (raiseEvent)
            {
                OnInterpreterFactoriesChanged();
            }
        }
 public bool IsInterpreterLocked(IPythonInterpreterFactory factory, object moniker)
 {
     throw new NotImplementedException();
 }
Exemple #35
0
 /// <summary>
 /// Returns true if the provided interpreter is available to this
 /// project.
 /// </summary>
 /// <remarks>
 /// If the project did not specify any interpreters, the global default
 /// interpreter is available, but this function will still return false.
 /// </remarks>
 public bool Contains(IPythonInterpreterFactory factory)
 {
     lock (_factoriesLock) {
         return(_factories.ContainsKey(factory));
     }
 }
        private async Task ApplyCustomAsync()
        {
            IPythonInterpreterFactory factory = null;

            if (RegisterCustomEnv)
            {
                Version version;
                if (!Version.TryParse(VersionName, out version))
                {
                    version = null;
                }

                factory = await CustomEnv.CreateCustomEnv(
                    RegistryService,
                    OptionsService,
                    PrefixPath,
                    InterpreterPath,
                    WindowsInterpreterPath,
                    PathEnvironmentVariable,
                    InterpreterArchitecture.TryParse(ArchitectureName ?? ""),
                    version,
                    Description
                    );
            }
            else
            {
                Debug.Assert(SelectedProject != null, "Project is null, UI should not have allowed this");
                if (SelectedProject != null)
                {
                    Version version;
                    if (!Version.TryParse(VersionName, out version))
                    {
                        version = null;
                    }

                    factory = SelectedProject.Node.AddMSBuildEnvironment(
                        RegistryService,
                        PrefixPath,
                        InterpreterPath,
                        WindowsInterpreterPath,
                        PathEnvironmentVariable,
                        version,
                        InterpreterArchitecture.TryParse(ArchitectureName ?? ""),
                        Description
                        );
                }
            }

            if (factory != null)
            {
                if (SelectedProject != null)
                {
                    SelectedProject.Node.AddInterpreter(factory.Configuration.Id);
                    if (SetAsCurrent)
                    {
                        SelectedProject.Node.SetInterpreterFactory(factory);
                    }
                }

                if (SetAsDefault)
                {
                    OptionsService.DefaultInterpreter = factory;
                }
            }
        }
Exemple #37
0
        private static async Task ContinueCreate(IServiceProvider provider, IPythonInterpreterFactory factory, string path, bool useVEnv, Redirector output)
        {
            path = PathUtils.TrimEndSeparator(path);
            var name = Path.GetFileName(path);
            var dir  = Path.GetDirectoryName(path);

            if (output != null)
            {
                output.WriteLine(Strings.VirtualEnvCreating.FormatUI(path));
                if (provider.GetPythonToolsService().GeneralOptions.ShowOutputWindowForVirtualEnvCreate)
                {
                    output.ShowAndActivate();
                }
                else
                {
                    output.Show();
                }
            }

            var workspaceFactoryProvider = provider.GetComponentModel().GetService <WorkspaceInterpreterFactoryProvider>();

            using (workspaceFactoryProvider?.SuppressDiscoverFactories(forceDiscoveryOnDispose: true)) {
                // Ensure the target directory exists.
                Directory.CreateDirectory(dir);

                using (var proc = ProcessOutput.Run(
                           factory.Configuration.InterpreterPath,
                           new[] { "-m", useVEnv ? "venv" : "virtualenv", name },
                           dir,
                           UnbufferedEnv,
                           false,
                           output
                           )) {
                    var exitCode = await proc;

                    if (output != null)
                    {
                        if (exitCode == 0)
                        {
                            output.WriteLine(Strings.VirtualEnvCreationSucceeded.FormatUI(path));
                        }
                        else
                        {
                            output.WriteLine(Strings.VirtualEnvCreationFailedExitCode.FormatUI(path, exitCode));
                        }
                        if (provider.GetPythonToolsService().GeneralOptions.ShowOutputWindowForVirtualEnvCreate)
                        {
                            output.ShowAndActivate();
                        }
                        else
                        {
                            output.Show();
                        }
                    }

                    if (exitCode != 0 || !Directory.Exists(path))
                    {
                        throw new InvalidOperationException(Strings.VirtualEnvCreationFailed.FormatUI(path));
                    }
                }
            }
        }
Exemple #38
0
        /// <summary>
        /// Gets the set of search paths for the specified factory as
        /// efficiently as possible. This may involve executing the
        /// interpreter, and may cache the paths for retrieval later.
        /// </summary>
        public static async Task <IList <PythonLibraryPath> > GetDatabaseSearchPathsAsync(IPythonInterpreterFactory factory)
        {
            var dbPath = (factory as PythonInterpreterFactoryWithDatabase)?.DatabasePath;

            if (!string.IsNullOrEmpty(dbPath))
            {
                var paths = GetCachedDatabaseSearchPaths(dbPath);
                if (paths != null)
                {
                    return(paths);
                }
            }

            try {
                var paths = await GetUncachedDatabaseSearchPathsAsync(factory.Configuration.InterpreterPath);

                if (!string.IsNullOrEmpty(dbPath))
                {
                    WriteDatabaseSearchPaths(dbPath, paths);
                }
                return(paths);
            } catch (InvalidOperationException) {
                // Failed to get paths
            }

            var ospy = PathUtils.FindFile(factory.Configuration.PrefixPath, "os.py", firstCheck: new[] { "Lib" });

            if (!string.IsNullOrEmpty(ospy))
            {
                return(GetDefaultDatabaseSearchPaths(PathUtils.GetParent(ospy)));
            }

            return(Array.Empty <PythonLibraryPath>());
        }
Exemple #39
0
        internal EnvironmentView(
            IInterpreterOptionsService service,
            IInterpreterRegistryService registry,
            IPythonInterpreterFactory factory,
            Redirector redirector
            )
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            if (registry == null)
            {
                throw new ArgumentNullException(nameof(registry));
            }
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }
            if (factory.Configuration == null)
            {
                throw new ArgumentException("factory must include a configuration");
            }

            _service                 = service;
            _registry                = registry;
            Factory                  = factory;
            Configuration            = Factory.Configuration;
            LocalizedDisplayName     = Configuration.Description;
            IsBroken                 = !Configuration.IsRunnable();
            BrokenEnvironmentHelpUrl = "https://go.microsoft.com/fwlink/?linkid=863373";

            var withDb = factory as Interpreter.LegacyDB.IPythonInterpreterFactoryWithDatabase;

            if (withDb != null)
            {
                withDb.IsCurrentChanged += Factory_IsCurrentChanged;
                IsCheckingDatabase       = withDb.IsCheckingDatabase;
                IsCurrent = withDb.IsCurrent;
            }


            if (_service.IsConfigurable(Factory.Configuration.Id))
            {
                IsConfigurable = true;
            }

            Description = Factory.Configuration.Description;
            IsDefault   = (_service != null && _service.DefaultInterpreterId == Configuration.Id);

            PrefixPath             = Factory.Configuration.PrefixPath;
            InterpreterPath        = Factory.Configuration.InterpreterPath;
            WindowsInterpreterPath = Factory.Configuration.WindowsInterpreterPath;

            Extensions = new ObservableCollection <object>();
            Extensions.Add(new EnvironmentPathsExtensionProvider());
            if (IsConfigurable)
            {
                Extensions.Add(new ConfigurationExtensionProvider(_service, alwaysCreateNew: false));
            }

            CanBeDefault = Factory.CanBeDefault();

            Company    = _registry.GetProperty(Factory.Configuration.Id, CompanyKey) as string ?? "";
            SupportUrl = _registry.GetProperty(Factory.Configuration.Id, SupportUrlKey) as string ?? "";

            LocalizedHelpText = Company;
        }
Exemple #40
0
 public Task SetInterpreterFactoryAsync(IPythonInterpreterFactory factory)
 {
     return(_workspace.SetInterpreterFactoryAsync(factory));
 }
Exemple #41
0
        internal static IVsInteractiveWindow /*!*/ EnsureReplWindow(IServiceProvider serviceProvider, IPythonInterpreterFactory factory, PythonProjectNode project)
        {
            var compModel = serviceProvider.GetComponentModel();
            var provider  = compModel.GetService <InteractiveWindowProvider>();

            string replId = PythonReplEvaluatorProvider.GetReplId(factory, project);
            var    window = provider.FindReplWindow(replId);

            if (window == null)
            {
                window = provider.CreateInteractiveWindow(
                    serviceProvider.GetPythonContentType(),
                    factory.Description + " Interactive",
                    typeof(PythonLanguageInfo).GUID,
                    replId
                    );

                var toolWindow = window as ToolWindowPane;
                if (toolWindow != null)
                {
                    toolWindow.BitmapImageMoniker = KnownMonikers.PYInteractiveWindow;
                }

                var pyService = serviceProvider.GetPythonToolsService();
                window.InteractiveWindow.SetSmartUpDown(pyService.GetInteractiveOptions(factory).ReplSmartHistory);
            }

            if (project != null && project.Interpreters.IsProjectSpecific(factory))
            {
                project.AddActionOnClose(window, BasePythonReplEvaluator.CloseReplWindow);
            }

            return(window);
        }
Exemple #42
0
 public PythonAnalyzer(IPythonInterpreterFactory factory, IPythonInterpreter interpreter = null)
     : this(factory, interpreter, null)
 {
     ReloadModulesAsync().WaitAndUnwrapExceptions();
 }
 public static bool CanBeDeleted(this IPythonInterpreterFactory factory)
 {
     return(factory.Configuration.Id.StartsWithOrdinal(CondaEnvironmentFactoryProvider.EnvironmentCompanyName));
 }
Exemple #44
0
        public PythonEditor(
            string content = null,
            PythonLanguageVersion version = PythonLanguageVersion.V27,
            MockVs vs = null,
            IPythonInterpreterFactory factory = null,
            VsProjectAnalyzer analyzer        = null,
            string filename = null
            )
        {
            if (vs == null)
            {
                _disposeVS = true;
                vs         = new MockVs();
            }
            MockVsTextView view = null;

            try {
                AdvancedEditorOptions advancedOptions = null;
                vs.InvokeSync(() => {
                    advancedOptions = vs.GetPyService().AdvancedOptions;
                    advancedOptions.AutoListMembers     = true;
                    advancedOptions.AutoListIdentifiers = false;
                });
                AdvancedOptions = advancedOptions;

                if (factory == null)
                {
                    _disposeFactory = true;
                    factory         = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
                }
                if (analyzer == null)
                {
                    _disposeAnalyzer = true;
                    vs.InvokeSync(() => {
                        analyzer = new VsProjectAnalyzer(vs.ServiceProvider, factory);
                    });
                    var task = analyzer.ReloadTask;
                    if (task != null)
                    {
                        task.WaitAndUnwrapExceptions();
                    }
                }

                var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
                using (var mre = new ManualResetEventSlim()) {
                    EventHandler evt = (s, e) => mre.SetIfNotDisposed();
                    analyzer.AnalysisStarted += evt;
                    view = vs.CreateTextView(PythonCoreConstants.ContentType, content ?? "", v => {
                        v.TextView.TextBuffer.Properties.AddProperty(typeof(VsProjectAnalyzer), analyzer);
                    }, filename);

                    try {
                        while (!mre.Wait(500, cts.Token) && !vs.HasPendingException)
                        {
                        }
                        analyzer.WaitForCompleteAnalysis(x => !cts.IsCancellationRequested && !vs.HasPendingException);
                    } catch (OperationCanceledException) {
                    } finally {
                        analyzer.AnalysisStarted -= evt;
                    }
                    if (cts.IsCancellationRequested)
                    {
                        Assert.Fail("Timed out waiting for code analysis");
                    }
                    vs.ThrowPendingException();
                }

                View     = view;
                view     = null;
                Analyzer = analyzer;
                analyzer = null;
                Factory  = factory;
                factory  = null;
                VS       = vs;
                vs       = null;
            } finally {
                if (view != null)
                {
                    view.Dispose();
                }
                if (analyzer != null && _disposeAnalyzer)
                {
                    analyzer.Dispose();
                }
                if (factory != null && _disposeFactory)
                {
                    var disp = factory as IDisposable;
                    if (disp != null)
                    {
                        disp.Dispose();
                    }
                }
                if (vs != null && _disposeVS)
                {
                    vs.Dispose();
                }
            }
        }
Exemple #45
0
 public void SetInterpreterFactory(IPythonInterpreterFactory factory)
 {
 }
Exemple #46
0
        public static async Task <IPythonInterpreterFactory> CreateAndAddFactory(
            IServiceProvider site,
            IInterpreterRegistryService registry,
            IInterpreterOptionsService options,
            PythonProjectNode project,
            IPythonWorkspaceContext workspace,
            string path,
            IPythonInterpreterFactory baseInterp,
            bool registerAsCustomEnv,
            string customEnvName,
            bool preferVEnv = false
            )
        {
            if (site == null)
            {
                throw new ArgumentNullException(nameof(site));
            }

            if (registry == null)
            {
                throw new ArgumentNullException(nameof(registry));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (baseInterp == null)
            {
                throw new ArgumentNullException(nameof(baseInterp));
            }

            if (preferVEnv)
            {
                await CreateWithVEnv(site, baseInterp, path);
            }
            else
            {
                await CreateWithVirtualEnv(site, baseInterp, path);
            }

            if (registerAsCustomEnv)
            {
                GetVirtualEnvConfig(path, baseInterp, out string interpExe, out string winterpExe, out string pathVar);

                var factory = await CustomEnv.CreateCustomEnv(
                    registry,
                    options,
                    path,
                    interpExe,
                    winterpExe,
                    pathVar,
                    baseInterp.Configuration.Architecture,
                    baseInterp.Configuration.Version,
                    customEnvName
                    );

                if (factory != null)
                {
                    if (project != null)
                    {
                        project.AddInterpreter(factory.Configuration.Id);
                    }
                    else if (workspace != null)
                    {
                        await workspace.SetInterpreterFactoryAsync(factory);
                    }
                }

                return(factory);
            }
            else
            {
                if (project != null)
                {
                    return(project.AddVirtualEnvironment(registry, path, baseInterp));
                }
                else if (workspace != null)
                {
                    // In workspaces, always store the path to the virtual env's python.exe
                    GetVirtualEnvConfig(path, baseInterp, out string interpExe, out string winterpExe, out string pathVar);

                    var workspaceFactoryProvider = site.GetComponentModel().GetService <WorkspaceInterpreterFactoryProvider>();
                    using (workspaceFactoryProvider?.SuppressDiscoverFactories(forceDiscoveryOnDispose: true)) {
                        var relativeInterpExe = PathUtils.GetRelativeFilePath(workspace.Location, interpExe);
                        await workspace.SetInterpreterAsync(relativeInterpExe);
                    }

                    var factory = workspaceFactoryProvider?
                                  .GetInterpreterFactories()
                                  .FirstOrDefault(f => PathUtils.IsSamePath(f.Configuration.InterpreterPath, interpExe));
                    return(factory);
                }
                else
                {
                    return(null);
                }
            }
        }
        internal static async System.Threading.Tasks.Task OpenAtAsync(IServiceProvider site, IPythonInterpreterFactory interpreter, Type extension = null)
        {
            var service = (IPythonToolsToolWindowService)site?.GetService(typeof(IPythonToolsToolWindowService));

            if (service == null)
            {
                Debug.Fail("Failed to get environment list window");
                return;
            }

            var wnd = await service.GetWindowPaneAsync(typeof(InterpreterListToolWindow), true) as InterpreterListToolWindow;

            if (!(wnd?.Content is ToolWindow envs))
            {
                Debug.Fail("Failed to get environment list window");
                return;
            }

            ErrorHandler.ThrowOnFailure((wnd.Frame as IVsWindowFrame)?.Show() ?? 0);

            if (extension == null)
            {
                SelectEnv(envs, interpreter, 3);
            }
            else
            {
                SelectEnvAndExt(envs, interpreter, extension, 3);
            }
        }
Exemple #48
0
 /// <summary>
 /// Creates a new analyzer that is not ready for use. You must call and
 /// wait for <see cref="ReloadModulesAsync"/> to complete before using.
 /// </summary>
 public static PythonAnalyzer Create(IPythonInterpreterFactory factory, IPythonInterpreter interpreter = null)
 {
     return(new PythonAnalyzer(factory, interpreter, null));
 }
 internal static bool IsCondaEnv(IPythonInterpreterFactory factory)
 {
     return(factory.Configuration.Id.StartsWithOrdinal(CondaEnvironmentFactoryProvider.FactoryProviderName + "|"));
 }
 public MockInterpreterOptionsService()
 {
     _providers           = new List <IPythonInterpreterFactoryProvider>();
     _noInterpretersValue = new MockPythonInterpreterFactory(new InterpreterConfiguration("2.7", "No Interpreters", version: new Version(2, 7)));
     _packageManagers     = new Dictionary <IPythonInterpreterFactory, IReadOnlyList <IPackageManager> >();
 }
Exemple #51
0
 /// <summary>
 /// Returns true if the provided interpreter was resolved when it was
 /// loaded.
 /// </summary>
 /// <remarks>
 /// This may return true even if <see cref="Contains"/> would return
 /// false for the same interpreter.
 /// </remarks>
 public bool IsAvailable(IPythonInterpreterFactory factory)
 {
     return(factory != null &&
            !(factory is NotFoundInterpreterFactory) &&
            factory.Configuration != null);
 }
 /// <summary>
 /// Executes the interpreter with the specified arguments. Any output is
 /// captured and returned via the <see cref="ProcessOutput"/> object.
 /// </summary>
 internal static ProcessOutput Run(
     this IPythonInterpreterFactory factory,
     params string[] arguments)
 {
     return(ProcessOutput.RunHiddenAndCapture(factory.Configuration.InterpreterPath, arguments));
 }
Exemple #53
0
        /// <summary>
        /// Adds the specified factory to the project. If the factory was
        /// created by this provider, it will be added as an Interpreter element
        /// with full details. If the factory was not created by this provider,
        /// it will be added as an InterpreterReference element with only the
        /// ID and version.
        /// </summary>
        /// <param name="factory">The factory to add.</param>
        public void AddInterpreter(IPythonInterpreterFactory factory, bool disposeInterpreter = false)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }

            if (_factories.ContainsKey(factory))
            {
                return;
            }

            MSBuild.ProjectItem item;

            var derived = factory as DerivedInterpreterFactory;

            if (derived != null)
            {
                var projectHome = PathUtils.GetAbsoluteDirectoryPath(_project.DirectoryPath, _project.GetPropertyValue("ProjectHome"));
                var rootPath    = PathUtils.EnsureEndSeparator(factory.Configuration.PrefixPath);
                _rootPaths[factory.Id] = rootPath;

                item = _project.AddItem(InterpreterItem,
                                        PathUtils.GetRelativeDirectoryPath(projectHome, rootPath),
                                        new Dictionary <string, string> {
                    { IdKey, derived.Id.ToString("B") },
                    { BaseInterpreterKey, derived.BaseInterpreter.Id.ToString("B") },
                    { VersionKey, derived.BaseInterpreter.Configuration.Version.ToString() },
                    { DescriptionKey, derived.Description },
                    { InterpreterPathKey, PathUtils.GetRelativeFilePath(rootPath, derived.Configuration.InterpreterPath) },
                    { WindowsPathKey, PathUtils.GetRelativeFilePath(rootPath, derived.Configuration.WindowsInterpreterPath) },
                    { LibraryPathKey, PathUtils.GetRelativeDirectoryPath(rootPath, derived.Configuration.LibraryPath) },
                    { PathEnvVarKey, derived.Configuration.PathEnvironmentVariable },
                    { ArchitectureKey, derived.Configuration.Architecture.ToString() }
                }).FirstOrDefault();
            }
            else if (_service.FindInterpreter(factory.Id, factory.Configuration.Version) != null)
            {
                // The interpreter exists globally, so add a reference.
                item = _project.AddItem(InterpreterReferenceItem,
                                        string.Format("{0:B}\\{1}", factory.Id, factory.Configuration.Version)
                                        ).FirstOrDefault();
            }
            else
            {
                // Can't find the interpreter anywhere else, so add its
                // configuration to the project file.
                var projectHome = PathUtils.GetAbsoluteDirectoryPath(_project.DirectoryPath, _project.GetPropertyValue("ProjectHome"));
                var rootPath    = PathUtils.EnsureEndSeparator(factory.Configuration.PrefixPath);

                item = _project.AddItem(InterpreterItem,
                                        PathUtils.GetRelativeDirectoryPath(projectHome, rootPath),
                                        new Dictionary <string, string> {
                    { IdKey, factory.Id.ToString("B") },
                    { VersionKey, factory.Configuration.Version.ToString() },
                    { DescriptionKey, factory.Description },
                    { InterpreterPathKey, PathUtils.GetRelativeFilePath(rootPath, factory.Configuration.InterpreterPath) },
                    { WindowsPathKey, PathUtils.GetRelativeFilePath(rootPath, factory.Configuration.WindowsInterpreterPath) },
                    { LibraryPathKey, PathUtils.GetRelativeDirectoryPath(rootPath, factory.Configuration.LibraryPath) },
                    { PathEnvVarKey, factory.Configuration.PathEnvironmentVariable },
                    { ArchitectureKey, factory.Configuration.Architecture.ToString() }
                }).FirstOrDefault();
            }

            lock (_factoriesLock) {
                _factories[factory] = new FactoryInfo(item, disposeInterpreter);
            }
            OnInterpreterFactoriesChanged();
            UpdateActiveInterpreter();
        }
Exemple #54
0
        public static async Task <bool> Install(
            IServiceProvider provider,
            IPythonInterpreterFactory factory,
            string package,
            IServiceProvider site,
            bool elevate,
            Redirector output = null
            )
        {
            factory.ThrowIfNotRunnable("factory");

            if (!(await factory.FindModulesAsync("pip")).Any())
            {
                if (site != null)
                {
                    try {
                        await QueryInstallPip(factory, site, Strings.InstallPip, elevate, output);
                    } catch (OperationCanceledException) {
                        return(false);
                    }
                }
                else
                {
                    await InstallPip(provider, factory, elevate, output);
                }
            }

            if (output != null)
            {
                output.WriteLine(Strings.PackageInstalling.FormatUI(package));
                if (provider.GetPythonToolsService().GeneralOptions.ShowOutputWindowForPackageInstallation)
                {
                    output.ShowAndActivate();
                }
                else
                {
                    output.Show();
                }
            }

            using (var proc = Run(factory, output, elevate, "install", GetInsecureArg(factory, output), package)) {
                var exitCode = await proc;

                if (output != null)
                {
                    if (exitCode == 0)
                    {
                        output.WriteLine(Strings.PackageInstallSucceeded.FormatUI(package));
                    }
                    else
                    {
                        output.WriteLine(Strings.PackageInstallFailedExitCode.FormatUI(package, exitCode));
                    }
                    if (provider.GetPythonToolsService().GeneralOptions.ShowOutputWindowForPackageInstallation)
                    {
                        output.ShowAndActivate();
                    }
                    else
                    {
                        output.Show();
                    }
                }
                return(exitCode == 0);
            }
        }
 public void AddPackageManagers(IPythonInterpreterFactory factory, IReadOnlyList <IPackageManager> packageManagers)
 {
     _packageManagers[factory] = packageManagers;
 }
 public Task <object> LockInterpreterAsync(IPythonInterpreterFactory factory, object moniker, TimeSpan timeout)
 {
     throw new NotImplementedException();
 }
 private static bool AreEqual(IPythonInterpreterFactory factory, Guid id, Version version)
 {
     return(factory != null && factory.Id.Equals(id) && (factory.Configuration.Version == null || factory.Configuration.Version.Equals(version)));
 }
 public MockInterpreterOptionsService()
 {
     _providers           = new List <IPythonInterpreterFactoryProvider>();
     _noInterpretersValue = new MockPythonInterpreterFactory(new InterpreterConfiguration("2.7", "No Interpreters", version: new Version(2, 7)));
 }
 protected override List <string> GetScrapeArguments(IPythonInterpreterFactory factory)
 {
     // Cannot scrape this module
     return(null);
 }
 public bool IsInterpreterGeneratingDatabase(IPythonInterpreterFactory interpreter)
 {
     throw new NotImplementedException();
 }