Exemplo n.º 1
0
        private static PythonVersion GetCPythonVersion(PythonLanguageVersion version, bool x64 = false)
        {
            if (!x64)
            {
                foreach (var baseKey in new[] { Registry.LocalMachine, Registry.CurrentUser })
                {
                    using (var python = baseKey.OpenSubKey(PythonCorePath)) {
                        var res = TryGetCPythonPath(version, python, x64);
                        if (res != null)
                        {
                            return(res);
                        }
                    }
                }
            }

            if (Environment.Is64BitOperatingSystem && x64)
            {
                foreach (var baseHive in new[] { RegistryHive.LocalMachine, RegistryHive.CurrentUser })
                {
                    var python64 = RegistryKey.OpenBaseKey(baseHive, RegistryView.Registry64).OpenSubKey(PythonCorePath);
                    var res      = TryGetCPythonPath(version, python64, x64);
                    if (res != null)
                    {
                        return(res);
                    }
                }
            }

            var path = "C:\\Python" + version.ToString().Substring(1) + "\\python.exe";
            var arch = NativeMethods.GetBinaryType(path);

            if (arch == ProcessorArchitecture.X86 && !x64)
            {
                return(new PythonVersion(path, version, CPythonGuid));
            }
            else if (arch == ProcessorArchitecture.Amd64 && x64)
            {
                return(new PythonVersion(path, version, CPython64Guid));
            }

            if (x64)
            {
                path = "C:\\Python" + version.ToString().Substring(1) + "_x64\\python.exe";
                arch = NativeMethods.GetBinaryType(path);
                if (arch == ProcessorArchitecture.Amd64)
                {
                    return(new PythonVersion(path, version, CPython64Guid));
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a database containing the default modules and any overrides
        /// specified in modules. The overrides will be copied from
        /// TestData\Databases\Vxx, where Vxx is taken from the provided
        /// version number.
        ///
        /// Each tuple in modules specifies the source filename and destination
        /// filename, respectively.
        /// </summary>
        public static MockCompletionDB Create(PythonLanguageVersion version, params Tuple <string, string>[] modules)
        {
            var source1 = PythonTypeDatabase.BaselineDatabasePath;
            var source2 = TestData.GetPath(Path.Combine("TestData", "Databases", version.ToString()));

            Assert.IsTrue(Directory.Exists(source1), "Cannot find " + source1);
            Assert.IsTrue(Directory.Exists(source2), "Cannot find " + source2);

            var db = new MockCompletionDB(TestData.GetTempPath(), version);

            Assert.IsNotNull(db, "Unable to create DB path");
            Console.WriteLine("Creating temporary database at {0}", db.DBPath);

            foreach (var src in Directory.EnumerateFiles(source1, "*.idb"))
            {
                File.Copy(src, Path.Combine(db.DBPath, Path.GetFileName(src)));
            }

            foreach (var mod in modules)
            {
                var src = Path.Combine(source2, mod.Item1 + ".idb");
                Assert.IsTrue(File.Exists(src), "No IDB file for " + mod.Item1);

                Console.WriteLine("Copying {0} from {1} as {2}", mod.Item1, src, mod.Item2);
                File.Copy(src, Path.Combine(db.DBPath, mod.Item2 + ".idb"), true);
                Assert.IsTrue(db.Contains(mod.Item2), "Failed to copy module " + mod.Item1);
            }

            File.WriteAllText(Path.Combine(db.DBPath, "database.ver"),
                              PythonTypeDatabase.CurrentVersion.ToString());

            return(db);
        }
Exemplo n.º 3
0
        private static PythonVersion TryGetCPythonPath(PythonLanguageVersion version, RegistryKey python)
        {
            if (python != null)
            {
                string versionStr = version.ToString().Substring(1);
                versionStr = versionStr[0] + "." + versionStr[1];

                using (var versionKey = python.OpenSubKey(versionStr + "\\InstallPath")) {
                    if (versionKey != null)
                    {
                        var installPath = versionKey.GetValue("");
                        if (installPath != null)
                        {
                            var path = Path.Combine(installPath.ToString(), "python.exe");
                            var arch = Microsoft.PythonTools.Analysis.NativeMethods.GetBinaryType(path);
                            if (arch == ProcessorArchitecture.X86)
                            {
                                return(new PythonVersion(path, version, CPythonGuid));
                            }
                            else if (arch == ProcessorArchitecture.Amd64)
                            {
                                return(new PythonVersion(path, version, CPython64Guid));
                            }
                            else
                            {
                                return(null);
                            }
                        }
                    }
                }
            }
            return(null);
        }
Exemplo n.º 4
0
        private static PythonVersion TryGetCPythonPath(PythonLanguageVersion version, RegistryKey python, bool x64)
        {
            if (python != null)
            {
                string versionStr = version.ToString().Substring(1);
                string id         = versionStr = versionStr[0] + "." + versionStr[1];
                if (!x64 && version >= PythonLanguageVersion.V35)
                {
                    versionStr += "-32";
                }

                using (var versionKey = python.OpenSubKey(versionStr + "\\InstallPath")) {
                    if (versionKey != null)
                    {
                        var installPath = versionKey.GetValue("");
                        if (installPath != null)
                        {
                            var path = Path.Combine(installPath.ToString(), "python.exe");
                            var arch = NativeMethods.GetBinaryType(path);
                            if (arch == ProcessorArchitecture.X86)
                            {
                                return(x64 ? null : new PythonVersion(
                                           path,
                                           version,
                                           CPythonInterpreterFactoryConstants.GetIntepreterId(
                                               "PythonCore",
                                               arch,
                                               id
                                               ),
                                           false,
                                           false,
                                           true
                                           ));
                            }
                            else if (arch == ProcessorArchitecture.Amd64)
                            {
                                return(x64 ? new PythonVersion(
                                           path,
                                           version,
                                           CPythonInterpreterFactoryConstants.GetIntepreterId(
                                               "PythonCore",
                                               arch,
                                               id
                                               ),
                                           true,
                                           false,
                                           true
                                           ) : null);
                            }
                            else
                            {
                                return(null);
                            }
                        }
                    }
                }
            }
            return(null);
        }
Exemplo n.º 5
0
 public static InterpreterConfiguration GetRequiredCPythonConfiguration(PythonLanguageVersion version)
 => GetCPythonVersion(version, InterpreterArchitecture.x86) ?? GetCPythonVersion(version, InterpreterArchitecture.x64) ?? NotInstalled(version.ToString());
Exemplo n.º 6
0
        /// <summary>
        /// Creates a database containing the default modules and any overrides
        /// specified in modules. The overrides will be copied from
        /// TestData\Databases\Vxx, where Vxx is taken from the provided
        /// version number.
        /// 
        /// Each tuple in modules specifies the source filename and destination
        /// filename, respectively.
        /// </summary>
        public static MockCompletionDB Create(PythonLanguageVersion version, params Tuple<string, string>[] modules) {
            var source1 = PythonTypeDatabase.BaselineDatabasePath;
            var source2 = TestData.GetPath(Path.Combine("TestData", "Databases", version.ToString()));
            Assert.IsTrue(Directory.Exists(source1), "Cannot find " + source1);
            Assert.IsTrue(Directory.Exists(source2), "Cannot find " + source2);

            var db = new MockCompletionDB(TestData.GetTempPath(randomSubPath: true), version);
            Assert.IsNotNull(db, "Unable to create DB path");
            Console.WriteLine("Creating temporary database at {0}", db.DBPath);

            foreach (var src in Directory.EnumerateFiles(source1, "*.idb")) {
                File.Copy(src, Path.Combine(db.DBPath, Path.GetFileName(src)));
            }

            foreach (var mod in modules) {
                var src = Path.Combine(source2, mod.Item1 + ".idb");
                Assert.IsTrue(File.Exists(src), "No IDB file for " + mod.Item1);

                Console.WriteLine("Copying {0} from {1} as {2}", mod.Item1, src, mod.Item2);
                File.Copy(src, Path.Combine(db.DBPath, mod.Item2 + ".idb"), true);
                Assert.IsTrue(db.Contains(mod.Item2), "Failed to copy module " + mod.Item1);
            }

            File.WriteAllText(Path.Combine(db.DBPath, "database.ver"),
                PythonTypeDatabase.CurrentVersion.ToString());

            return db;
        }
Exemplo n.º 7
0
        private static PythonVersion GetCPythonVersion(PythonLanguageVersion version, bool x64 = false) {
            if (!x64) {
                foreach (var baseKey in new[] { Registry.LocalMachine, Registry.CurrentUser }) {
                    using (var python = baseKey.OpenSubKey(PythonCorePath)) {
                        var res = TryGetCPythonPath(version, python, x64);
                        if (res != null) {
                            return res;
                        }
                    }
                }
            }

            if (Environment.Is64BitOperatingSystem && x64) {
                foreach (var baseHive in new[] { RegistryHive.LocalMachine, RegistryHive.CurrentUser }) {
                    var python64 = RegistryKey.OpenBaseKey(baseHive, RegistryView.Registry64).OpenSubKey(PythonCorePath);
                    var res = TryGetCPythonPath(version, python64, x64);
                    if (res != null) {
                        return res;
                    }
                }
            }

            var path = "C:\\Python" + version.ToString().Substring(1) + "\\python.exe";
            var arch = NativeMethods.GetBinaryType(path);
            if (arch == ProcessorArchitecture.X86 && !x64) {
                return new PythonVersion(path, version, CPythonGuid);
            } else if (arch == ProcessorArchitecture.Amd64 && x64) {
                return new PythonVersion(path, version, CPython64Guid);
            }

            if (x64) {
                path = "C:\\Python" + version.ToString().Substring(1) + "_x64\\python.exe";
                arch = NativeMethods.GetBinaryType(path);
                if (arch == ProcessorArchitecture.Amd64) {
                    return new PythonVersion(path, version, CPython64Guid);
                }
            }

            return null;
        }
Exemplo n.º 8
0
        private static PythonVersion TryGetCPythonPath(PythonLanguageVersion version, RegistryKey python, bool x64) {
            if (python != null) {
                string versionStr = version.ToString().Substring(1);
                versionStr = versionStr[0] + "." + versionStr[1];
                if (!x64 && version >= PythonLanguageVersion.V35) {
                    versionStr += "-32";
                }

                using (var versionKey = python.OpenSubKey(versionStr + "\\InstallPath")) {
                    if (versionKey != null) {
                        var installPath = versionKey.GetValue("");
                        if (installPath != null) {
                            var path = Path.Combine(installPath.ToString(), "python.exe");
                            var arch = NativeMethods.GetBinaryType(path);
                            if (arch == ProcessorArchitecture.X86) {
                                return x64 ? null : new PythonVersion(path, version, CPythonGuid);
                            } else if (arch == ProcessorArchitecture.Amd64) {
                                return x64 ? new PythonVersion(path, version, CPython64Guid) : null;
                            } else {
                                return null;
                            }
                        }
                    }
                }
            }
            return null;
        }