protected virtual List <string> GetScrapeArguments(IPythonInterpreterFactory factory)
        {
            var args = new List <string> {
                "-B", "-E"
            };

            ModulePath mp = AstPythonInterpreterFactory.FindModuleAsync(factory, _filePath)
                            .WaitAndUnwrapExceptions();

            if (string.IsNullOrEmpty(mp.FullName))
            {
                return(null);
            }

            if (!InstallPath.TryGetFile("scrape_module.py", out string sm))
            {
                return(null);
            }

            args.Add(sm);
            args.Add("-u8");
            args.Add(mp.ModuleName);
            args.Add(mp.LibraryPath);

            return(args);
        }
Exemple #2
0
        protected virtual string[] GetScrapeArguments(IPythonInterpreter interpreter)
        {
            var args = new List <string> {
                "-W", "ignore", "-B", "-E"
            };

            var mp = Interpreter.ModuleResolution.FindModule(FilePath);

            if (string.IsNullOrEmpty(mp.FullName))
            {
                return(null);
            }

            if (!InstallPath.TryGetFile("scrape_module.py", out var sm))
            {
                return(null);
            }

            args.Add(sm);
            args.Add("-u8");
            args.Add(mp.ModuleName);
            args.Add(mp.LibraryPath);

            return(args.ToArray());
        }
Exemple #3
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;
        }
        /// <summary>
        /// Gets the set of search paths by running the interpreter.
        /// </summary>
        /// <param name="interpreter">Path to the interpreter.</param>
        /// <returns>A list of search paths for the interpreter.</returns>
        /// <remarks>Added in 2.2, moved in 3.3</remarks>
        public static async Task <List <PythonLibraryPath> > GetUncachedDatabaseSearchPathsAsync(string interpreter)
        {
            // sys.path will include the working directory, so we make an empty
            // path that we can filter out later
            var tempWorkingDir = IOPath.Combine(IOPath.GetTempPath(), IOPath.GetRandomFileName());

            Directory.CreateDirectory(tempWorkingDir);
            if (!InstallPath.TryGetFile("get_search_paths.py", out var srcGetSearchPaths))
            {
                return(new List <PythonLibraryPath>());
            }
            var getSearchPaths = IOPath.Combine(tempWorkingDir, PathUtils.GetFileName(srcGetSearchPaths));

            File.Copy(srcGetSearchPaths, getSearchPaths);

            var lines      = new List <string>();
            var errorLines = new List <string> {
                "Cannot obtain list of paths"
            };

            try {
                using (var proc = new ProcessHelper(interpreter, new[] { "-S", "-E", getSearchPaths }, tempWorkingDir)) {
                    proc.OnOutputLine = lines.Add;
                    proc.OnErrorLine  = errorLines.Add;

                    proc.Start();
                    using (var cts = new CancellationTokenSource(30000)) {
                        int exitCode;
                        try {
                            exitCode = await proc.WaitAsync(cts.Token).ConfigureAwait(false);
                        } catch (OperationCanceledException) {
                            proc.Kill();
                            exitCode = -1;
                        }
                        if (exitCode != 0)
                        {
                            throw new InvalidOperationException(string.Join(Environment.NewLine, errorLines));
                        }
                    }
                }
            } finally {
                PathUtils.DeleteDirectory(tempWorkingDir);
            }

            return(lines.Select(s => {
                if (s.StartsWithOrdinal(tempWorkingDir, ignoreCase: true))
                {
                    return null;
                }
                try {
                    return Parse(s);
                } catch (ArgumentException) {
                    Debug.Fail("Invalid search path: " + (s ?? "<null>"));
                    return null;
                } catch (FormatException) {
                    Debug.Fail("Invalid format for search path: " + s);
                    return null;
                }
            }).Where(p => p != null).ToList());
        }
        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;
        }
 private static string FindTypingStub()
 {
     if (InstallPath.TryGetFile("typing-stub.pyi", out var fullPath))
     {
         return(fullPath);
     }
     throw new FileNotFoundException("typing-stub.pyi");
 }
        protected override List <string> GetScrapeArguments(IPythonInterpreterFactory factory)
        {
            if (!InstallPath.TryGetFile("scrape_module.py", out string sb))
            {
                return(null);
            }

            return(new List <string> {
                "-B", "-E", sb
            });
        }
Exemple #8
0
        protected override List <string> GetScrapeArguments(AstPythonInterpreter interpreter)
        {
            if (!InstallPath.TryGetFile("scrape_module.py", out string sm))
            {
                return(null);
            }

            return(new List <string> {
                "-B", "-E", sm, "-u8", Name
            });
        }
        /// <summary>
        /// Gets the set of search paths by running the interpreter.
        /// </summary>
        /// <param name="interpreter">Path to the interpreter.</param>
        /// <param name="fs">File system services.</param>
        /// <param name="ps">Process services.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>A list of search paths for the interpreter.</returns>
        public static async Task <List <PythonLibraryPath> > GetSearchPathsFromInterpreterAsync(string interpreter, IFileSystem fs, IProcessServices ps, CancellationToken cancellationToken = default)
        {
            // sys.path will include the working directory, so we make an empty
            // path that we can filter out later
            var tempWorkingDir = IOPath.Combine(IOPath.GetTempPath(), IOPath.GetRandomFileName());

            fs.CreateDirectory(tempWorkingDir);
            if (!InstallPath.TryGetFile("get_search_paths.py", out var srcGetSearchPaths))
            {
                return(new List <PythonLibraryPath>());
            }
            var getSearchPaths = IOPath.Combine(tempWorkingDir, PathUtils.GetFileName(srcGetSearchPaths));

            File.Copy(srcGetSearchPaths, getSearchPaths);

            var startInfo = new ProcessStartInfo(
                interpreter,
                new[] { "-S", "-E", getSearchPaths }.AsQuotedArguments()
                )
            {
                WorkingDirectory       = tempWorkingDir,
                UseShellExecute        = false,
                ErrorDialog            = false,
                CreateNoWindow         = true,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true
            };

            try {
                var output = await ps.ExecuteAndCaptureOutputAsync(startInfo, cancellationToken);

                return(output.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Select(s => {
                    try {
                        var p = Parse(s);

                        if (PathUtils.PathStartsWith(p.Path, tempWorkingDir))
                        {
                            return null;
                        }

                        return p;
                    } catch (ArgumentException) {
                        Debug.Fail("Invalid search path: " + (s ?? "<null>"));
                        return null;
                    } catch (FormatException) {
                        Debug.Fail("Invalid format for search path: " + s);
                        return null;
                    }
                }).Where(p => p != null).ToList());
            } finally {
                fs.DeleteDirectory(tempWorkingDir, true);
            }
        }
Exemple #10
0
        private JsonRpc CreateJsonRpc()
        {
            if (!InstallPath.TryGetFile("inspector.py", out var scriptPath))
            {
                return(null); // Throw?
            }

            var procServices = _services.GetService <IProcessServices>();
            var interpreter  = _services.GetService <IPythonInterpreter>();

            return(PythonRpc.Create(procServices, interpreter.Configuration.InterpreterPath, scriptPath));
        }
        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;
        }
Exemple #12
0
        /// <summary>
        /// Gets the set of search paths by running the interpreter.
        /// </summary>
        /// <param name="interpreter">Path to the interpreter.</param>
        /// <param name="fs">File system services.</param>
        /// <param name="ps">Process services.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>A list of search paths for the interpreter.</returns>
        public static async Task <ImmutableArray <PythonLibraryPath> > GetSearchPathsFromInterpreterAsync(string interpreter, IFileSystem fs, IProcessServices ps, CancellationToken cancellationToken = default)
        {
            // sys.path will include the working directory, so we make an empty
            // path that we can filter out later
            if (!InstallPath.TryGetFile("get_search_paths.py", out var getSearchPathScript))
            {
                return(new ImmutableArray <PythonLibraryPath>());
            }

            var startInfo = new ProcessStartInfo(
                interpreter,
                new[] { "-S", "-E", getSearchPathScript }.AsQuotedArguments()
                )
            {
                WorkingDirectory       = IOPath.GetDirectoryName(getSearchPathScript),
                UseShellExecute        = false,
                ErrorDialog            = false,
                CreateNoWindow         = true,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true
            };

            var output = await ps.ExecuteAndCaptureOutputAsync(startInfo, cancellationToken);

            return(output.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
                   .Skip(1).Select(s => {
                try {
                    return Parse(s);
                } catch (ArgumentException) {
                    Debug.Fail("Invalid search path: " + (s ?? "<null>"));
                    return null;
                } catch (FormatException) {
                    Debug.Fail("Invalid format for search path: " + s);
                    return null;
                }
            }).Where(p => p != null).ToImmutableArray());
        }
 protected override string[] GetScrapeArguments(IPythonInterpreter interpreter)
 => !InstallPath.TryGetFile("scrape_module.py", out var sb) ? null : new [] { "-W", "ignore", "-B", "-E", sb };
Exemple #14
0
 protected override IEnumerable <string> GetScrapeArguments(IPythonInterpreter interpreter)
 => !InstallPath.TryGetFile("scrape_module.py", out var sb) ? null : new List <string>
 {
     "-B", "-E", sb
 };