Пример #1
0
        public void LogFileRotation()
        {
            var logFile = Path.Combine(TestData.GetTempPath(), "Log.txt");
            var writer  = new AnalysisLogWriter(logFile, false, false);

            for (int i = 0; i < 100; ++i)
            {
                writer.Log("Event", i);
            }
            writer.Flush(synchronous: true);

            var lines = File.ReadAllLines(logFile);

            Assert.AreEqual(101, lines.Length);

            writer.Rotate(11);
            lines = File.ReadAllLines(logFile);
            AssertUtil.ContainsExactly(lines.Select(l => l.Substring(l.IndexOf(']') + 1).Trim()),
                                       "Event: 90",
                                       "Event: 91",
                                       "Event: 92",
                                       "Event: 93",
                                       "Event: 94",
                                       "Event: 95",
                                       "Event: 96",
                                       "Event: 97",
                                       "Event: 98",
                                       "Event: 99"
                                       );
        }
Пример #2
0
        private AstPythonInterpreterFactory(
            InterpreterConfiguration config,
            InterpreterFactoryCreationOptions options,
            bool useDefaultDatabase
            )
        {
            Configuration   = config ?? throw new ArgumentNullException(nameof(config));
            CreationOptions = options ?? new InterpreterFactoryCreationOptions();
            try {
                LanguageVersion = Configuration.Version.ToLanguageVersion();
            } catch (InvalidOperationException ex) {
                throw new ArgumentException(ex.Message, ex);
            }

            _useDefaultDatabase = useDefaultDatabase;
            if (!string.IsNullOrEmpty(CreationOptions.DatabasePath) && CreationOptions.TraceLevel != TraceLevel.Off)
            {
                _log = new AnalysisLogWriter(Path.Combine(CreationOptions.DatabasePath, "AnalysisLog.txt"), false, LogToConsole, LogCacheSize);
                _log.Rotate(LogRotationSize);
                _log.MinimumLevel = CreationOptions.TraceLevel;
            }

            ModuleCache      = new AstModuleCache(config, CreationOptions.DatabasePath, useDefaultDatabase, !CreationOptions.UseExistingCache, _log);
            ModuleResolution = new AstModuleResolution(ModuleCache, config, _log);
        }
Пример #3
0
 public AstAnalysisWalker(
     IPythonInterpreter interpreter,
     PythonAst ast,
     IPythonModule module,
     string filePath,
     Uri documentUri,
     Dictionary <string, IMember> members,
     bool includeLocationInfo,
     bool warnAboutUndefinedValues,
     AnalysisLogWriter log = null
     )
 {
     _log     = log ?? (interpreter as AstPythonInterpreter)?._log;
     _module  = module ?? throw new ArgumentNullException(nameof(module));
     _members = members ?? throw new ArgumentNullException(nameof(members));
     _scope   = new NameLookupContext(
         interpreter ?? throw new ArgumentNullException(nameof(interpreter)),
         interpreter.CreateModuleContext(),
         ast ?? throw new ArgumentNullException(nameof(ast)),
         _module,
         filePath,
         documentUri,
         includeLocationInfo,
         log: warnAboutUndefinedValues ? _log : null
         );
     _postWalkers             = new List <AstAnalysisFunctionWalker>();
     WarnAboutUndefinedValues = warnAboutUndefinedValues;
 }
Пример #4
0
 public AstAnalysisWalker(
     IPythonInterpreter interpreter,
     PathResolverSnapshot pathResolver,
     PythonAst ast,
     IPythonModule module,
     string filePath,
     Uri documentUri,
     Dictionary <string, IMember> members,
     bool includeLocationInfo,
     bool warnAboutUndefinedValues,
     bool suppressBuiltinLookup,
     AnalysisLogWriter log = null
     )
 {
     _log     = log ?? (interpreter as AstPythonInterpreter)?.Log;
     _module  = module ?? throw new ArgumentNullException(nameof(module));
     _members = members ?? throw new ArgumentNullException(nameof(members));
     _scope   = new NameLookupContext(
         interpreter ?? throw new ArgumentNullException(nameof(interpreter)),
         interpreter.CreateModuleContext(),
         ast ?? throw new ArgumentNullException(nameof(ast)),
         _module,
         filePath,
         documentUri,
         includeLocationInfo,
         _functionWalkers,
         log: warnAboutUndefinedValues ? _log : null
         );
     _ast          = ast;
     _interpreter  = interpreter;
     _pathResolver = pathResolver;
     _scope.SuppressBuiltinLookup = suppressBuiltinLookup;
     _scope.PushScope(_typingScope);
     WarnAboutUndefinedValues = warnAboutUndefinedValues;
 }
Пример #5
0
        private PythonTextBufferInfo(PythonEditorServices services, ITextBuffer buffer)
        {
            Services                = services;
            Buffer                  = buffer;
            _eventSinks             = new ConcurrentDictionary <object, IPythonTextBufferInfoEventSink>();
            _filename               = new Lazy <string>(GetOrCreateFilename);
            _documentUri            = new Lazy <Uri>(GetOrCreateDocumentUri);
            _tokenCache             = new TokenCache();
            _defaultLanguageVersion = PythonLanguageVersion.None;

            ITextDocument doc;

            if (Buffer.Properties.TryGetProperty(typeof(ITextDocument), out doc))
            {
                Document = doc;
                Document.EncodingChanged += Document_EncodingChanged;
            }
            Buffer.ContentTypeChanged += Buffer_ContentTypeChanged;
            Buffer.Changed            += Buffer_TextContentChanged;
            Buffer.ChangedLowPriority += Buffer_TextContentChangedLowPriority;

            if (Buffer is ITextBuffer2 buffer2)
            {
                _hasChangedOnBackground      = true;
                buffer2.ChangedOnBackground += Buffer_TextContentChangedOnBackground;
            }

            _locationTracker = new LocationTracker(Buffer.CurrentSnapshot);

            _traceLog = OpenTraceLog();
        }
 public AstModuleResolution(AstModuleCache moduleCache, InterpreterConfiguration configuration, AnalysisLogWriter log)
 {
     _moduleCache   = moduleCache;
     _configuration = configuration;
     _log           = log;
     _requireInitPy = ModulePath.PythonVersionRequiresInitPyFiles(_configuration.Version);
 }
Пример #7
0
        public AstPythonInterpreterFactory(
            InterpreterConfiguration config,
            InterpreterFactoryCreationOptions options
            )
        {
            Configuration   = config ?? throw new ArgumentNullException(nameof(config));
            CreationOptions = options ?? new InterpreterFactoryCreationOptions();
            try {
                LanguageVersion = Configuration.Version.ToLanguageVersion();
            } catch (InvalidOperationException ex) {
                throw new ArgumentException(ex.Message, ex);
            }

            _databasePath = CreationOptions.DatabasePath;
            if (!string.IsNullOrEmpty(_databasePath))
            {
                _searchPathCachePath = Path.Combine(_databasePath, "database.path");

                _log = new AnalysisLogWriter(Path.Combine(_databasePath, "AnalysisLog.txt"), false, LogToConsole, LogCacheSize);
                _log.Rotate(LogRotationSize);
                _log.MinimumLevel = CreationOptions.TraceLevel;
            }
            else
            {
                if (InstallPath.TryGetFile($"DefaultDB\\v{Configuration.Version.Major}\\python.pyi", out string biPath))
                {
                    CreationOptions.DatabasePath = _databasePath = Path.GetDirectoryName(biPath);
                    _skipWriteToCache            = true;
                }
            }
            _skipCache = !CreationOptions.UseExistingCache;
        }
        public AstPythonInterpreterFactory(
            InterpreterConfiguration config,
            InterpreterFactoryCreationOptions options
            )
        {
            Configuration   = config ?? throw new ArgumentNullException(nameof(config));
            LanguageVersion = Configuration.Version.ToLanguageVersion();

            options       = options ?? new InterpreterFactoryCreationOptions();
            _databasePath = options.DatabasePath;
            if (!string.IsNullOrEmpty(_databasePath))
            {
                _log = new AnalysisLogWriter(PathUtils.GetAbsoluteFilePath(_databasePath, "AnalysisLog.txt"), false, LogToConsole, LogCacheSize);
                _log.Rotate(LogRotationSize);
                _log.MinimumLevel = options.TraceLevel;
            }
            _skipCache = !options.UseExistingCache;

            if (!GlobalInterpreterOptions.SuppressPackageManagers)
            {
                try {
                    var pm = options.PackageManager;
                    if (pm != null)
                    {
                        pm.SetInterpreterFactory(this);
                        pm.InstalledFilesChanged += PackageManager_InstalledFilesChanged;
                        PackageManager            = pm;
                    }
                } catch (NotSupportedException) {
                }
            }
        }
        public NameLookupContext(
            IPythonInterpreter interpreter,
            IModuleContext context,
            PythonAst ast,
            IPythonModule self,
            string filePath,
            Uri documentUri,
            bool includeLocationInfo,
            IPythonModule builtinModule = null,
            AnalysisLogWriter log       = null
            )
        {
            Interpreter         = interpreter ?? throw new ArgumentNullException(nameof(interpreter));
            Context             = context;
            Ast                 = ast ?? throw new ArgumentNullException(nameof(ast));
            Module              = self;
            FilePath            = filePath;
            DocumentUri         = documentUri;
            IncludeLocationInfo = includeLocationInfo;

            DefaultLookupOptions = LookupOptions.Normal;

            _unknownType = Interpreter.GetBuiltinType(BuiltinTypeId.Unknown) ??
                           new FallbackBuiltinPythonType(new FallbackBuiltinModule(Ast.LanguageVersion), BuiltinTypeId.Unknown);

            _builtinModule = builtinModule == null ? new Lazy <IPythonModule>(ImportBuiltinModule) : new Lazy <IPythonModule>(() => builtinModule);
            _log           = log;
        }
Пример #10
0
        public AstModuleCache(InterpreterConfiguration configuration, string databasePath, bool useDefaultDatabase, bool useExistingCache, AnalysisLogWriter log)
        {
            _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));

            DatabasePath        = databasePath;
            _useDefaultDatabase = useDefaultDatabase;
            _log = log;

            if (_useDefaultDatabase)
            {
                var dbPath = Path.Combine("DefaultDB", $"v{_configuration.Version.Major}", "python.pyi");
                if (InstallPath.TryGetFile(dbPath, out string biPath))
                {
                    DatabasePath = Path.GetDirectoryName(biPath);
                }
                else
                {
                    _skipCache = true;
                }
            }
            else
            {
                SearchPathCachePath = Path.Combine(DatabasePath, "database.path");
            }
            _skipCache = !useExistingCache;
        }
Пример #11
0
        public AstPythonInterpreter(AstPythonInterpreterFactory factory, string workspaceRoot, AnalysisLogWriter log = null)
        {
            _factory = factory ?? throw new ArgumentNullException(nameof(factory));
            _factory.ImportableModulesChanged += Factory_ImportableModulesChanged;

            _workspaceRoot = workspaceRoot;
            Log            = log;
        }
Пример #12
0
 public AstModuleResolution(IPythonInterpreter interpreter, ConcurrentDictionary <string, IPythonModule> modules, AstModuleCache astModuleCache, InterpreterConfiguration configuration, AnalysisLogWriter log)
 {
     _interpreter    = interpreter;
     _modules        = modules;
     _astModuleCache = astModuleCache;
     _configuration  = configuration;
     _log            = log;
     _requireInitPy  = ModulePath.PythonVersionRequiresInitPyFiles(_configuration.Version);
 }
        public AstPythonInterpreter(AstPythonInterpreterFactory factory, bool useDefaultDatabase, AnalysisLogWriter log = null)
        {
            _factory = factory ?? throw new ArgumentNullException(nameof(factory));
            _factory.ImportableModulesChanged += Factory_ImportableModulesChanged;

            Log = log;

            ModuleCache      = new AstModuleCache(factory.Configuration, factory.CreationOptions.DatabasePath, useDefaultDatabase, factory.CreationOptions.UseExistingCache, log);
            ModuleResolution = new AstModuleResolution(this, _modules, ModuleCache, factory.Configuration, log);
        }
Пример #14
0
 public AstPythonInterpreter(AstPythonInterpreterFactory factory, AnalysisLogWriter log = null)
 {
     _factory = factory ?? throw new ArgumentNullException(nameof(factory));
     _log     = log;
     _factory.ImportableModulesChanged += Factory_ImportableModulesChanged;
     _modules      = new ConcurrentDictionary <string, IPythonModule>();
     _builtinTypes = new Dictionary <BuiltinTypeId, IPythonType>();
     _noneType     = new AstPythonBuiltinType("NoneType", BuiltinTypeId.NoneType);
     _builtinTypes[BuiltinTypeId.NoneType] = _noneType;
     _builtinTypes[BuiltinTypeId.Unknown]  = new AstPythonBuiltinType("Unknown", BuiltinTypeId.Unknown);
 }
Пример #15
0
 public AstPythonInterpreter(AstPythonInterpreterFactory factory, AnalysisLogWriter log = null)
 {
     _factory = factory ?? throw new ArgumentNullException(nameof(factory));
     _log     = log;
     _factory.ImportableModulesChanged += Factory_ImportableModulesChanged;
     _modules          = new ConcurrentDictionary <string, IPythonModule>();
     _builtinTypes     = new Dictionary <BuiltinTypeId, IPythonType>();
     BuiltinModuleName = _factory.LanguageVersion.Is3x() ? SharedDatabaseState.BuiltinName3x : SharedDatabaseState.BuiltinName2x;
     _noneType         = new AstPythonBuiltinType("NoneType", BuiltinTypeId.NoneType);
     _builtinTypes[BuiltinTypeId.NoneType] = _noneType;
 }
Пример #16
0
        private AstPythonInterpreterFactory(
            InterpreterConfiguration config,
            InterpreterFactoryCreationOptions options,
            bool useDefaultDatabase
            )
        {
            Configuration   = config ?? throw new ArgumentNullException(nameof(config));
            CreationOptions = options ?? new InterpreterFactoryCreationOptions();
            try {
                LanguageVersion = Configuration.Version.ToLanguageVersion();
            } catch (InvalidOperationException ex) {
                throw new ArgumentException(ex.Message, ex);
            }
            BuiltinModuleName = BuiltinTypeId.Unknown.GetModuleName(LanguageVersion);

            _databasePath       = CreationOptions.DatabasePath;
            _useDefaultDatabase = useDefaultDatabase;
            if (_useDefaultDatabase)
            {
                var dbPath = Path.Combine("DefaultDB", $"v{Configuration.Version.Major}", "python.pyi");
                if (InstallPath.TryGetFile(dbPath, out string biPath))
                {
                    CreationOptions.DatabasePath = _databasePath = Path.GetDirectoryName(biPath);
                }
                else
                {
                    _skipCache = true;
                }
            }
            else
            {
                _searchPathCachePath = Path.Combine(_databasePath, "database.path");

                _log = new AnalysisLogWriter(Path.Combine(_databasePath, "AnalysisLog.txt"), false, LogToConsole, LogCacheSize);
                _log.Rotate(LogRotationSize);
                _log.MinimumLevel = CreationOptions.TraceLevel;
            }
            _skipCache = !CreationOptions.UseExistingCache;
        }
Пример #17
0
        private PythonTextBufferInfo(PythonEditorServices services, ITextBuffer buffer)
        {
            Services                = services;
            Buffer                  = buffer;
            _eventSinks             = new ConcurrentDictionary <object, IPythonTextBufferInfoEventSink>();
            _filename               = new Lazy <string>(GetOrCreateFilename);
            _documentUri            = new Lazy <Uri>(GetOrCreateDocumentUri);
            _tokenCache             = new TokenCache();
            _defaultLanguageVersion = PythonLanguageVersion.None;

            ITextDocument doc;

            if (Buffer.Properties.TryGetProperty(typeof(ITextDocument), out doc))
            {
                Document = doc;
                Document.EncodingChanged += Document_EncodingChanged;
            }
            Buffer.ContentTypeChanged += Buffer_ContentTypeChanged;
            Buffer.Changed            += Buffer_TextContentChanged;
            Buffer.ChangedLowPriority += Buffer_TextContentChangedLowPriority;

            if (Buffer is ITextBuffer2 buffer2)
            {
                _hasChangedOnBackground      = true;
                buffer2.ChangedOnBackground += Buffer_TextContentChangedOnBackground;
            }

            _locationTracker = new LocationTracker(Buffer.CurrentSnapshot);

#if BUFFERINFO_TRACING
            _traceLog = new AnalysisLogWriter(
                PathUtils.GetAvailableFilename(System.IO.Path.GetTempPath(), "PythonTools_Buffer_{0}_{1:yyyyMMddHHmmss}".FormatInvariant(PathUtils.GetFileOrDirectoryName(_filename.Value), DateTime.Now), ".log"),
                false,
                false,
                cacheSize: 1
                );
#endif
        }
Пример #18
0
        public NameLookupContext(
            IPythonInterpreter interpreter,
            IModuleContext context,
            PythonAst ast,
            IPythonModule self,
            string filePath,
            bool includeLocationInfo,
            IPythonModule builtinModule = null,
            AnalysisLogWriter log       = null
            )
        {
            Interpreter         = interpreter ?? throw new ArgumentNullException(nameof(interpreter));
            Context             = context;
            Ast                 = ast ?? throw new ArgumentNullException(nameof(ast));
            Module              = self;
            FilePath            = filePath;
            IncludeLocationInfo = includeLocationInfo;

            DefaultLookupOptions = LookupOptions.Normal;

            _scopes        = new Stack <Dictionary <string, IMember> >();
            _builtinModule = builtinModule == null ? new Lazy <IPythonModule>(ImportBuiltinModule) : new Lazy <IPythonModule>(() => builtinModule);
            _log           = log;
        }