示例#1
0
        private SaveLoadResult SaveLoad(PythonLanguageVersion version, params AnalysisModule[] modules)
        {
            IPythonProjectEntry[] entries = new IPythonProjectEntry[modules.Length];

            var fact   = PythonInterpreterFactoryWithDatabase.CreateFromDatabase(version.ToVersion());
            var interp = fact.CreateInterpreter();

            var dbFolder = TestData.GetTempPath();

            var state = new PythonAnalysis(fact, interp);

            state.CreateProjectOnDisk = true;
            for (int i = 0; i < modules.Length; i++)
            {
                state.AddModule(modules[i].ModuleName, modules[i].Code, modules[i].Filename);
            }

            state.WaitForAnalysis();

            new SaveAnalysis().Save(state.Analyzer, dbFolder);
            File.WriteAllText(Path.Combine(dbFolder, "database.ver"), PythonTypeDatabase.CurrentVersion.ToString());

            File.Copy(
                Path.Combine(PythonTypeDatabase.BaselineDatabasePath, state.Analyzer._builtinName + ".idb"),
                Path.Combine(dbFolder, state.Analyzer._builtinName + ".idb"),
                true
                );

            var loadFactory = PythonInterpreterFactoryWithDatabase.CreateFromDatabase(version.ToVersion(), dbFolder);

            return(new SaveLoadResult(CreateAnalyzer(loadFactory), state.CodeFolder));
        }
示例#2
0
 public MockCompletionDB(string path, PythonLanguageVersion version)
 {
     DBPath          = path;
     LanguageVersion = version;
     _factory        = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion(), null, DBPath);
     Directory.CreateDirectory(DBPath);
 }
示例#3
0
 public MockCompletionDB(string path, PythonLanguageVersion version)
 {
     DBPath          = path;
     LanguageVersion = version;
     Directory.CreateDirectory(DBPath);
     _factory = PythonInterpreterFactoryWithDatabase.CreateFromDatabase(version.ToVersion(), DBPath);
 }
示例#4
0
        public void LayeredDatabase()
        {
            using (var db1 = MockCompletionDB.Create(PythonLanguageVersion.V27, "os"))
                using (var db2 = MockCompletionDB.Create(PythonLanguageVersion.V27, "posixpath")) {
                    Assert.IsNotNull(db1.Database.GetModule("os"));
                    Assert.IsNull(db1.Database.GetModule("posixpath"));

                    var ptd1 = db1.Database;
                    var ptd2 = ptd1.Clone();
                    ptd2.LoadDatabase(db2.DBPath);

                    Assert.IsNull(ptd1.GetModule("posixpath"));
                    Assert.IsNotNull(ptd2.GetModule("os"));
                    Assert.IsNotNull(ptd2.GetModule("posixpath"));
                    Assert.AreSame(ptd1.GetModule("os"), db1.Database.GetModule("os"));
                    Assert.AreSame(ptd2.GetModule("os"), db1.Database.GetModule("os"));
                }

            var factory = PythonInterpreterFactoryWithDatabase.CreateFromDatabase(new Version(2, 7));

            using (var db1 = MockCompletionDB.Create(PythonLanguageVersion.V27, "os", "posixpath"))
                using (var db2 = MockCompletionDB.Create(PythonLanguageVersion.V27, "posixpath")) {
                    var ptd1 = new PythonTypeDatabase(factory, new[] {
                        db1.DBPath,
                        db2.DBPath
                    });

                    Assert.IsNotNull(ptd1.GetModule("posixpath"));
                    Assert.AreNotSame(ptd1.GetModule("posixpath"), db1.Database.GetModule("posixpath"));
                    Assert.AreNotSame(ptd1.GetModule("posixpath"), db2.Database.GetModule("posixpath"));

                    var ptd2 = new PythonTypeDatabase(factory, new[] {
                        db2.DBPath,
                        db1.DBPath
                    });

                    Assert.IsNotNull(ptd2.GetModule("posixpath"));
                    Assert.AreNotSame(ptd2.GetModule("posixpath"), db1.Database.GetModule("posixpath"));
                    Assert.AreNotSame(ptd2.GetModule("posixpath"), db2.Database.GetModule("posixpath"));
                }

            using (var db1 = MockCompletionDB.Create(PythonLanguageVersion.V27, "os", "posixpath"))
                using (var db2 = MockCompletionDB.Create(PythonLanguageVersion.V27, "posixpath"))
                    using (var db3 = MockCompletionDB.Create(PythonLanguageVersion.V27, "ntpath")) {
                        var ptd = db1.Database;
                        Assert.AreSame(ptd.GetModule("posixpath"), db1.Database.GetModule("posixpath"));
                        Assert.AreNotSame(ptd.GetModule("posixpath"), db2.Database.GetModule("posixpath"));

                        var ptd2 = ptd.Clone();
                        ptd2.LoadDatabase(db2.DBPath);

                        Assert.AreNotSame(ptd2.GetModule("posixpath"), ptd.GetModule("posixpath"));

                        var ptd3 = ptd2.Clone();
                        ptd3.LoadDatabase(db3.DBPath);

                        Assert.IsNotNull(ptd3.GetModule("ntpath"));
                        Assert.IsNull(ptd2.GetModule("ntpath"));
                    }
        }
示例#5
0
        public IronPythonInterpreter(PythonInterpreterFactoryWithDatabase factory)
        {
#if DEBUG
            _id = Interlocked.Increment(ref _interpreterCount);
            Debug.WriteLine(String.Format("IronPython Interpreter Created {0}", _id));
            Debug.WriteLine(new StackTrace(true).ToString());
#endif

            AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolver.Instance.CurrentDomain_AssemblyResolve;

            InitializeRemoteDomain();

            try {
                LoadAssemblies();
            } catch {
                // IronPython not installed in the GAC...
            }

            var mod    = Remote.ImportBuiltinModule("__builtin__");
            var newMod = new IronPythonBuiltinModule(this, mod, "__builtin__");
            _modules[newMod.Name] = newMod;

            _factory = factory;
            _typeDb  = _factory.GetCurrentDatabase().CloneWithNewBuiltins(newMod);
            _factory.NewDatabaseAvailable += OnNewDatabaseAvailable;

            LoadModules();
        }
示例#6
0
 public CPythonInterpreter(PythonInterpreterFactoryWithDatabase factory)
 {
     _langVersion = factory.Configuration.Version;
     _factory     = factory;
     _typeDb      = _factory.GetCurrentDatabase();
     _factory.NewDatabaseAvailable += OnNewDatabaseAvailable;
 }
示例#7
0
        /// <summary>
        /// Creates the Canopy User interpreter. This handles layering of the
        /// User database on top of the App database, and ensures that refreshes
        /// work correctly.
        ///
        /// Because it is exposed as its own factory type, it can also be used
        /// as a base interpreter for DerivedInterpreterFactory (virtual
        /// environments).
        /// </summary>
        public static CanopyInterpreterFactory Create(
            PythonInterpreterFactoryWithDatabase baseFactory,
            string userPath,
            string canopyVersion
            )
        {
            var interpPath    = FindFile(userPath, CanopyInterpreterFactoryConstants.ConsoleExecutable);
            var winInterpPath = FindFile(userPath, CanopyInterpreterFactoryConstants.WindowsExecutable);
            var libPath       = Path.Combine(userPath, CanopyInterpreterFactoryConstants.LibrarySubPath);

            if (!File.Exists(interpPath))
            {
                throw new FileNotFoundException(interpPath);
            }
            if (!File.Exists(winInterpPath))
            {
                throw new FileNotFoundException(winInterpPath);
            }
            if (!Directory.Exists(libPath))
            {
                throw new DirectoryNotFoundException(libPath);
            }

            var id = (baseFactory.Configuration.Architecture == ProcessorArchitecture.Amd64) ?
                     CanopyInterpreterFactoryConstants.UserGuid64 :
                     CanopyInterpreterFactoryConstants.UserGuid32;

            // Make the description string look like "Canopy 1.1.0.46 (2.7 32-bit)"
            var description = "Canopy ";

            if (!string.IsNullOrEmpty(canopyVersion))
            {
                description += " " + canopyVersion;
            }
            description += string.Format(" ({0} ", baseFactory.Configuration.Version);
            if (baseFactory.Configuration.Architecture == ProcessorArchitecture.Amd64)
            {
                description += " 64-bit)";
            }
            else
            {
                description += " 32-bit)";
            }

            var config = new InterpreterConfiguration(
                userPath,
                interpPath,
                winInterpPath,
                libPath,
                CanopyInterpreterFactoryConstants.PathEnvironmentVariableName,
                baseFactory.Configuration.Architecture,
                baseFactory.Configuration.Version
                );

            return(new CanopyInterpreterFactory(id, description, baseFactory, config));
        }
示例#8
0
 public void Dispose()
 {
     if (_typeDb != null)
     {
         _typeDb = null;
     }
     if (_factory != null)
     {
         _factory.NewDatabaseAvailable -= OnNewDatabaseAvailable;
         _factory = null;
     }
 }
示例#9
0
        public void Dispose()
        {
            _typeDb = null;

            var factory = _factory;

            _factory = null;
            if (factory != null)
            {
                factory.NewDatabaseAvailable -= OnNewDatabaseAvailable;
            }
        }
示例#10
0
        public void Dispose()
        {
            _searchPathDb    = null;
            _zipPackageCache = null;
            _typeDb          = null;

            var factory = _factory;

            _factory = null;
            if (factory != null)
            {
                factory.NewDatabaseAvailable -= OnNewDatabaseAvailable;
            }
        }
示例#11
0
        public void TestPthFiles()
        {
            var outputPath = TestData.GetTempPath();

            Console.WriteLine("Writing to: " + outputPath);

            // run the analyzer
            using (var output = ProcessOutput.RunHiddenAndCapture("Microsoft.PythonTools.Analyzer.exe",
                                                                  "/lib", TestData.GetPath("TestData", "PathStdLib"),
                                                                  "/version", "2.7",
                                                                  "/outdir", outputPath,
                                                                  "/indir", CompletionDB,
                                                                  "/unittest",
                                                                  "/log", "AnalysisLog.txt")) {
                output.Wait();
                Console.WriteLine("* Stdout *");
                foreach (var line in output.StandardOutputLines)
                {
                    Console.WriteLine(line);
                }
                Console.WriteLine("* Stderr *");
                foreach (var line in output.StandardErrorLines)
                {
                    Console.WriteLine(line);
                }
                Assert.AreEqual(0, output.ExitCode);
            }

            File.Copy(Path.Combine(CompletionDB, "__builtin__.idb"), Path.Combine(outputPath, "__builtin__.idb"));

            var fact  = PythonInterpreterFactoryWithDatabase.CreateFromDatabase(new Version(2, 7));
            var paths = new List <string> {
                outputPath
            };

            paths.AddRange(Directory.EnumerateDirectories(outputPath));
            var typeDb = new PythonTypeDatabase(fact, paths);
            var module = typeDb.GetModule("SomeLib");

            Assert.IsNotNull(module, "Could not import SomeLib");
            var fobMod = typeDb.GetModule("SomeLib.fob");

            Assert.IsNotNull(fobMod, "Could not import SomeLib.fob");

            var cClass = ((IPythonModule)fobMod).GetMember(null, "C");

            Assert.IsNotNull(cClass, "Could not get SomeLib.fob.C");

            Assert.AreEqual(PythonMemberType.Class, cClass.MemberType);
        }
        private CanopyInterpreterFactory(
            PythonInterpreterFactoryWithDatabase baseFactory,
            InterpreterConfiguration config
            )
            : base(config, true)
        {
            if (baseFactory == null)
            {
                throw new ArgumentNullException("baseFactory");
            }

            _base = baseFactory;
            _base.IsCurrentChanged     += OnIsCurrentChanged;
            _base.NewDatabaseAvailable += OnNewDatabaseAvailable;
        }
示例#13
0
        private DjangoAnalyzer AnalyzerTest(string path)
        {
            string djangoDbPath = TestData.GetPath("TestData\\DjangoDB");

            Assert.IsTrue(
                PythonTypeDatabase.IsDatabaseVersionCurrent(djangoDbPath),
                "TestData\\DjangoDB needs updating."
                );

            var testFact = PythonInterpreterFactoryWithDatabase.CreateFromDatabase(
                new Version(2, 7),
                TestData.GetPath("CompletionDB"),
                djangoDbPath
                );

            var            serviceProvider = PythonToolsTestUtilities.CreateMockServiceProvider();
            PythonAnalyzer analyzer        = PythonAnalyzer.CreateAsync(testFact).WaitAndUnwrapExceptions();
            DjangoAnalyzer djangoAnalyzer  = new DjangoAnalyzer();

            djangoAnalyzer.Register(analyzer);

            analyzer.SetSearchPaths(new[] { path });

            List <IPythonProjectEntry> entries = new List <IPythonProjectEntry>();

            foreach (string file in Directory.EnumerateFiles(path, "*.py", SearchOption.AllDirectories))
            {
                var entry  = analyzer.AddModule(ModulePath.FromFullPath(file).ModuleName, file);
                var parser = Parser.CreateParser(
                    new FileStream(file, FileMode.Open, FileAccess.Read),
                    PythonLanguageVersion.V27
                    );
                using (var p = entry.BeginParse()) {
                    p.Tree = parser.ParseFile();
                    p.Complete();
                }
                entries.Add(entry);
            }

            foreach (var entry in entries)
            {
                entry.Analyze(CancellationToken.None, false);
            }

            return(djangoAnalyzer);
        }
示例#14
0
 internal DBExtensionProvider(PythonInterpreterFactoryWithDatabase factory)
 {
     _factory = factory;
     _factory.IsCurrentChanged     += Factory_IsCurrentChanged;
     _factory.NewDatabaseAvailable += Factory_NewDatabaseAvailable;
 }
示例#15
0
        public void Dispose() {
            _searchPathDb = null;
            _zipPackageCache = null;
            _typeDb = null;

            var factory = _factory;
            _factory = null;
            if (factory != null) {
                factory.NewDatabaseAvailable -= OnNewDatabaseAvailable;
            }
        }
 public override IPythonInterpreter MakeInterpreter(PythonInterpreterFactoryWithDatabase factory)
 {
     return(new IronPythonInterpreter(factory));
 }
示例#17
0
        public void PydInPackage()
        {
            PythonPaths.Python27.AssertInstalled();

            var outputPath = TestData.GetTempPath();

            Console.WriteLine("Writing to: " + outputPath);

            // run the analyzer
            using (var output = ProcessOutput.RunHiddenAndCapture("Microsoft.PythonTools.Analyzer.exe",
                                                                  "/python", PythonPaths.Python27.InterpreterPath,
                                                                  "/lib", TestData.GetPath(@"TestData\PydStdLib"),
                                                                  "/version", "2.7",
                                                                  "/outdir", outputPath,
                                                                  "/indir", CompletionDB,
                                                                  "/unittest",
                                                                  "/log", "AnalysisLog.txt")) {
                output.Wait();
                Console.WriteLine("* Stdout *");
                foreach (var line in output.StandardOutputLines)
                {
                    Console.WriteLine(line);
                }
                Console.WriteLine("* Stderr *");
                foreach (var line in output.StandardErrorLines)
                {
                    Console.WriteLine(line);
                }
                Assert.AreEqual(0, output.ExitCode);
            }

            var fact  = PythonInterpreterFactoryWithDatabase.CreateFromDatabase(new Version(2, 7));
            var paths = new List <string> {
                outputPath
            };

            paths.AddRange(Directory.EnumerateDirectories(outputPath));
            var typeDb = new PythonTypeDatabase(fact, paths);
            var module = typeDb.GetModule("Package.winsound");

            Assert.IsNotNull(module, "Package.winsound was not analyzed");
            var package = typeDb.GetModule("Package");

            Assert.IsNotNull(package, "Could not import Package");
            var member = package.GetMember(null, "winsound");

            Assert.IsNotNull(member, "Could not get member Package.winsound");
            Assert.AreSame(module, member);

            module = typeDb.GetModule("Package._testcapi");
            Assert.IsNotNull(module, "Package._testcapi was not analyzed");
            package = typeDb.GetModule("Package");
            Assert.IsNotNull(package, "Could not import Package");
            member = package.GetMember(null, "_testcapi");
            Assert.IsNotNull(member, "Could not get member Package._testcapi");
            Assert.IsNotInstanceOfType(member, typeof(CPythonMultipleMembers));
            Assert.AreSame(module, member);

            module = typeDb.GetModule("Package.select");
            Assert.IsNotNull(module, "Package.select was not analyzed");
            package = typeDb.GetModule("Package");
            Assert.IsNotNull(package, "Could not import Package");
            member = package.GetMember(null, "select");
            Assert.IsNotNull(member, "Could not get member Package.select");
            Assert.IsInstanceOfType(member, typeof(CPythonMultipleMembers));
            var mm = (CPythonMultipleMembers)member;

            AssertUtil.ContainsExactly(mm.Members.Select(m => m.MemberType),
                                       PythonMemberType.Module,
                                       PythonMemberType.Constant,
                                       PythonMemberType.Class
                                       );
            Assert.IsNotNull(mm.Members.Contains(module));

            try {
                // Only clean up if the test passed
                Directory.Delete(outputPath, true);
            } catch { }
        }
 public void Dispose() {
     if (_typeDb != null) {
         _typeDb = null;
     }
     if (_factory != null) {
         _factory.NewDatabaseAvailable -= OnNewDatabaseAvailable;
         _factory = null;
     }
 }
示例#19
0
 public DBExtensionProvider(PythonInterpreterFactoryWithDatabase factory)
 {
     _factory = factory;
     _factory.IsCurrentChanged += Factory_IsCurrentChanged;
 }
示例#20
0
        public void Dispose() {
            _typeDb = null;

            var factory = _factory;
            _factory = null;
            if (factory != null) {
                factory.NewDatabaseAvailable -= OnNewDatabaseAvailable;
            }
        }
示例#21
0
 public CPythonInterpreter(PythonInterpreterFactoryWithDatabase factory) {
     _langVersion = factory.Configuration.Version;
     _factory = factory;
     _typeDb = _factory.GetCurrentDatabase();
     _factory.NewDatabaseAvailable += OnNewDatabaseAvailable;
 }