コード例 #1
0
        public async Task PrepareAsync(IPackageManagerUI ui, CancellationToken cancellationToken)
        {
            if (IsReady)
            {
                return;
            }

            AbortOnInvalidConfiguration();

            await UpdateIsReadyAsync(false, cancellationToken);

            if (IsReady)
            {
                return;
            }

            var operation = "pip_downloader.py";
            var args      = _extraInterpreterArgs
                            .Concat(new[] { PythonToolsInstallPath.GetFile("pip_downloader.py", GetType().Assembly) });

            using (await _working.LockAsync(cancellationToken)) {
                ui?.OnOperationStarted(this, operation);
                ui?.OnOutputTextReceived(this, Strings.InstallingPipStarted);

                using (var proc = ProcessOutput.Run(
                           _factory.Configuration.InterpreterPath,
                           args,
                           _factory.Configuration.PrefixPath,
                           UnbufferedEnv,
                           false,
                           PackageManagerUIRedirector.Get(this, ui),
                           elevate: await ShouldElevate(ui, operation)
                           )) {
                    try {
                        IsReady = (await proc == 0);
                    } catch (OperationCanceledException) {
                        IsReady = false;
                    }
                }

                ui?.OnOutputTextReceived(this, IsReady ? Strings.InstallingPipSuccess : Strings.InstallingPackageFailed);
                ui?.OnOperationFinished(this, operation, IsReady);
            }
        }
コード例 #2
0
        /// <summary>
        /// Attaches to the specified PID and returns a DebugAttach object indicating the result.
        /// </summary>
        internal static DebugAttach AttachAD7Worker(int pid, int portNum, Guid debugId, string debugOptions, EventWaitHandle attachDoneEvent = null)
        {
            var hProcess = NativeMethods.OpenProcess(ProcessAccessFlags.All, false, pid);

            if (hProcess != IntPtr.Zero)
            {
                string dllPath;
                if (IntPtr.Size == 4)
                {
                    dllPath = PythonToolsInstallPath.GetFile("PyDebugAttachX86.dll");
                }
                else
                {
                    dllPath = PythonToolsInstallPath.GetFile("PyDebugAttach.dll");
                }

                if (!File.Exists(dllPath))
                {
                    return(new DebugAttach(ConnErrorMessages.PyDebugAttachNotFound));
                }

                // load our code into the process...

                // http://msdn.microsoft.com/en-us/library/windows/desktop/ms682631(v=vs.85).aspx
                // If the module list in the target process is corrupted or not yet initialized,
                // or if the module list changes during the function call as a result of DLLs
                // being loaded or unloaded, EnumProcessModules may fail or return incorrect
                // information.
                // So we'll retry a handful of times to get the attach...
                ConnErrorMessages error = ConnErrorMessages.None;
                for (int i = 0; i < 5; i++)
                {
                    IntPtr hKernel32;
                    if ((error = FindKernel32(hProcess, out hKernel32)) == ConnErrorMessages.None)
                    {
                        return(InjectDebugger(dllPath, hProcess, hKernel32, portNum, pid, debugId, debugOptions, attachDoneEvent));
                    }
                }

                return(new DebugAttach(error));
            }

            return(new DebugAttach(ConnErrorMessages.CannotOpenProcess));
        }
コード例 #3
0
ファイル: PipRequirementsUtils.cs プロジェクト: zyxws012/PTVS
        /// <summary>
        /// Returns true if a missing package is detected. A package could be missing and not be detected (Git+...)
        /// Returns false when a missing package is not detected such as file not found exception or invalid file, etc
        /// </summary>
        /// <param name="interpreterPath"></param>
        /// <param name="reqTxtPath"></param>
        /// <returns></returns>
        internal static async Task <bool> DetectMissingPackagesAsync(string interpreterPath, string reqTxtPath)
        {
            try {
                var processOutput = ProcessOutput.RunHiddenAndCapture(
                    interpreterPath,
                    PythonToolsInstallPath.GetFile("missing_req_packages.py"),
                    reqTxtPath
                    );

                await processOutput;
                if (processOutput.ExitCode == 1)
                {
                    return(true);
                }
            } catch (Exception) {
                // Do nothing. End of function will return false because no missing packages detected due to exception
            }

            return(false);
        }
コード例 #4
0
ファイル: PipExtensionProvider.cs プロジェクト: krus/PTVS
        public async Task InstallPip()
        {
            AbortOnInvalidConfiguration();

            using (await WaitAndLockPip()) {
                OnOperationStarted(Resources.InstallingPipStarted);
                using (var output = ProcessOutput.Run(
                           _factory.Configuration.InterpreterPath,
                           new[] { PythonToolsInstallPath.GetFile("pip_downloader.py") },
                           _factory.Configuration.PrefixPath,
                           UnbufferedEnv,
                           false,
                           _output,
                           elevate: ShouldElevate
                           )) {
                    bool success = true;
                    try {
                        var exitCode = await output;
                        if (exitCode != 0)
                        {
                            success = false;
                            throw new PipException(Resources.InstallationFailed);
                        }
                    } catch (OperationCanceledException) {
                        success = false;
                    } catch (Exception ex) {
                        success = false;
                        if (ex.IsCriticalException())
                        {
                            throw;
                        }
                        ToolWindow.UnhandledException.Execute(ExceptionDispatchInfo.Capture(ex), WpfObject);
                    } finally {
                        OnOperationFinished(
                            success ? Resources.InstallingPipSuccess : Resources.InstallingPipFailed
                            );
                    }
                }
            }
        }
コード例 #5
0
        public void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
            if (!projectItem.Name.Equals("web.debug.config", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            var projectDir = PathUtils.GetParent(projectItem.FileNames[0]);

            // Also copy Microsoft.PythonTools.WebRole.dll and ptvsd into the project
            var ptvsdSource = PythonToolsInstallPath.TryGetFile("ptvsd\\__init__.py", GetType().Assembly);
            var ptvsdDest   = PathUtils.GetAbsoluteDirectoryPath(projectDir, "ptvsd");

            if (File.Exists(ptvsdSource) && !Directory.Exists(ptvsdDest))
            {
                Directory.CreateDirectory(ptvsdDest);
                var sourceDir = PathUtils.GetParent(ptvsdSource);
                foreach (var file in PathUtils.EnumerateFiles(sourceDir, pattern: "*.py", fullPaths: false))
                {
                    var destFile = PathUtils.GetAbsoluteFilePath(ptvsdDest, file);
                    if (!Directory.Exists(PathUtils.GetParent(destFile)))
                    {
                        Directory.CreateDirectory(PathUtils.GetParent(destFile));
                    }

                    File.Copy(PathUtils.GetAbsoluteFilePath(sourceDir, file), destFile, true);
                }

                projectItem.ContainingProject.ProjectItems.AddFromDirectory(PathUtils.TrimEndSeparator(ptvsdDest));
            }


            var webRoleSource = PythonToolsInstallPath.TryGetFile("Microsoft.PythonTools.WebRole.dll", GetType().Assembly);

            if (File.Exists(webRoleSource))
            {
                projectItem.ContainingProject.ProjectItems.AddFromFileCopy(webRoleSource);
            }
        }
コード例 #6
0
        protected override void Connect()
        {
            _serviceProvider.GetUIThread().MustBeCalledFromUIThread();

            var configurableOptions = CurrentOptions as ConfigurablePythonReplOptions;

            if (configurableOptions != null)
            {
                _interpreter = configurableOptions.InterpreterFactory ?? _interpreter;
            }

            if (Interpreter == null || Interpreter is UnavailableFactory)
            {
                Window.WriteError(SR.GetString(SR.ReplEvaluatorInterpreterNotFound));
                return;
            }
            else if (String.IsNullOrWhiteSpace(Interpreter.Configuration.InterpreterPath))
            {
                Window.WriteError(SR.GetString(SR.ReplEvaluatorInterpreterNotConfigured, Interpreter.Description));
                return;
            }
            var processInfo = new ProcessStartInfo(Interpreter.Configuration.InterpreterPath);

#if DEBUG
            bool debugMode = Environment.GetEnvironmentVariable("DEBUG_REPL") != null;
            processInfo.CreateNoWindow         = !debugMode;
            processInfo.UseShellExecute        = debugMode;
            processInfo.RedirectStandardOutput = !debugMode;
            processInfo.RedirectStandardError  = !debugMode;
            processInfo.RedirectStandardInput  = !debugMode;
#else
            processInfo.CreateNoWindow         = true;
            processInfo.UseShellExecute        = false;
            processInfo.RedirectStandardOutput = true;
            processInfo.RedirectStandardError  = true;
            processInfo.RedirectStandardInput  = true;
#endif

            Socket conn;
            int    portNum;
            CreateConnection(out conn, out portNum);


            List <string> args = new List <string>();

            if (!String.IsNullOrWhiteSpace(CurrentOptions.InterpreterOptions))
            {
                args.Add(CurrentOptions.InterpreterOptions);
            }

            var workingDir = CurrentOptions.WorkingDirectory;
            if (!string.IsNullOrEmpty(workingDir))
            {
                processInfo.WorkingDirectory = workingDir;
            }
            else if (configurableOptions != null && configurableOptions.Project != null)
            {
                processInfo.WorkingDirectory = configurableOptions.Project.GetWorkingDirectory();
            }
            else
            {
                processInfo.WorkingDirectory = Interpreter.Configuration.PrefixPath;
            }

#if DEBUG
            if (!debugMode)
            {
#endif
            var envVars = CurrentOptions.EnvironmentVariables;
            if (envVars != null)
            {
                foreach (var keyValue in envVars)
                {
                    processInfo.EnvironmentVariables[keyValue.Key] = keyValue.Value;
                }
            }

            string pathEnvVar = Interpreter.Configuration.PathEnvironmentVariable ?? "";

            if (!string.IsNullOrWhiteSpace(pathEnvVar))
            {
                var searchPaths = CurrentOptions.SearchPaths;

                if (string.IsNullOrEmpty(searchPaths))
                {
                    if (_serviceProvider.GetPythonToolsService().GeneralOptions.ClearGlobalPythonPath)
                    {
                        processInfo.EnvironmentVariables[pathEnvVar] = "";
                    }
                }
                else if (_serviceProvider.GetPythonToolsService().GeneralOptions.ClearGlobalPythonPath)
                {
                    processInfo.EnvironmentVariables[pathEnvVar] = searchPaths;
                }
                else
                {
                    processInfo.EnvironmentVariables[pathEnvVar] = searchPaths + ";" + Environment.GetEnvironmentVariable(pathEnvVar);
                }
            }
#if DEBUG
        }
#endif
            var interpreterArgs = CurrentOptions.InterpreterArguments;
            if (!String.IsNullOrWhiteSpace(interpreterArgs))
            {
                args.Add(interpreterArgs);
            }

            var analyzer = CurrentOptions.ProjectAnalyzer;
            if (analyzer != null && analyzer.InterpreterFactory == _interpreter)
            {
                if (_replAnalyzer != null && _replAnalyzer != analyzer)
                {
                    analyzer.SwitchAnalyzers(_replAnalyzer);
                }
                _replAnalyzer = analyzer;
                _ownsAnalyzer = false;
            }

            args.Add(ProcessOutput.QuoteSingleArgument(PythonToolsInstallPath.GetFile("visualstudio_py_repl.py")));
            args.Add("--port");
            args.Add(portNum.ToString());

            if (!String.IsNullOrWhiteSpace(CurrentOptions.StartupScript))
            {
                args.Add("--launch_file");
                args.Add(ProcessOutput.QuoteSingleArgument(CurrentOptions.StartupScript));
            }

            _enableAttach = CurrentOptions.EnableAttach;
            if (CurrentOptions.EnableAttach)
            {
                args.Add("--enable-attach");
            }

            bool multipleScopes = true;
            if (!String.IsNullOrWhiteSpace(CurrentOptions.ExecutionMode))
            {
                // change ID to module name if we have a registered mode
                var    modes     = Microsoft.PythonTools.Options.ExecutionMode.GetRegisteredModes(_serviceProvider);
                string modeValue = CurrentOptions.ExecutionMode;
                foreach (var mode in modes)
                {
                    if (mode.Id == CurrentOptions.ExecutionMode)
                    {
                        modeValue      = mode.Type;
                        multipleScopes = mode.SupportsMultipleScopes;
                        _supportsMultipleCompleteStatementInputs = mode.SupportsMultipleCompleteStatementInputs;
                        break;
                    }
                }
                args.Add("--execution_mode");
                args.Add(modeValue);
            }

            SetMultipleScopes(multipleScopes);

            processInfo.Arguments = String.Join(" ", args);

            var process = new Process();
            process.StartInfo = processInfo;
            try {
                if (!File.Exists(processInfo.FileName))
                {
                    throw new Win32Exception(Microsoft.VisualStudioTools.Project.NativeMethods.ERROR_FILE_NOT_FOUND);
                }
                process.Start();
            } catch (Exception e) {
                if (e.IsCriticalException())
                {
                    throw;
                }

                Win32Exception wex = e as Win32Exception;
                if (wex != null && wex.NativeErrorCode == Microsoft.VisualStudioTools.Project.NativeMethods.ERROR_FILE_NOT_FOUND)
                {
                    Window.WriteError(SR.GetString(SR.ReplEvaluatorInterpreterNotFound));
                }
                else
                {
                    Window.WriteError(SR.GetString(SR.ErrorStartingInteractiveProcess, e.ToString()));
                }
                return;
            }

            CreateCommandProcessor(conn, processInfo.RedirectStandardOutput, process);
        }
        protected virtual Task <CommandProcessorThread> ConnectAsync(CancellationToken ct)
        {
            _serviceProvider.GetUIThread().MustBeCalledFromUIThreadOrThrow();

            var interpreterPath = Configuration?.GetInterpreterPath();

            if (string.IsNullOrWhiteSpace(interpreterPath))
            {
                WriteError(Strings.ReplEvaluatorInterpreterNotConfigured.FormatUI(DisplayName));
                return(null);
            }
            else if (!File.Exists(interpreterPath))
            {
                WriteError(Strings.ReplEvaluatorInterpreterNotFound);
                return(null);
            }

            var processInfo = new ProcessStartInfo(interpreterPath);

#if DEBUG
            bool debugMode = Environment.GetEnvironmentVariable("_PTVS_DEBUG_REPL") != null;
            processInfo.CreateNoWindow         = !debugMode;
            processInfo.UseShellExecute        = debugMode;
            processInfo.RedirectStandardOutput = !debugMode;
            processInfo.RedirectStandardError  = !debugMode;
            processInfo.RedirectStandardInput  = !debugMode;
#else
            processInfo.CreateNoWindow         = true;
            processInfo.UseShellExecute        = false;
            processInfo.RedirectStandardOutput = true;
            processInfo.RedirectStandardError  = true;
            processInfo.RedirectStandardInput  = true;
#endif

            var conn = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            conn.Bind(new IPEndPoint(IPAddress.Loopback, 0));
            conn.Listen(0);
            var portNum = ((IPEndPoint)conn.LocalEndPoint).Port;

            var workingDirectory = Configuration.WorkingDirectory;
            if (!string.IsNullOrEmpty(workingDirectory))
            {
                processInfo.WorkingDirectory = workingDirectory;
            }
            else
            {
                processInfo.WorkingDirectory = CommonUtils.GetParent(processInfo.FileName);
            }

#if DEBUG
            if (!debugMode)
            {
#endif
            var env = processInfo.Environment;
            foreach (var kv in _serviceProvider.GetPythonToolsService().GetFullEnvironment(Configuration))
            {
                env[kv.Key] = kv.Value;
            }
#if DEBUG
        }
#endif

            var args = new List <string>();
            var interpreterArguments = Configuration.InterpreterArguments;
            if (!string.IsNullOrWhiteSpace(interpreterArguments))
            {
                args.Add(interpreterArguments);
            }

            args.Add(ProcessOutput.QuoteSingleArgument(PythonToolsInstallPath.GetFile("visualstudio_py_repl.py")));
            args.Add("--port");
            args.Add(portNum.ToString());

            args.Add("--execution-mode");
            args.Add(string.IsNullOrEmpty(BackendName) ? "standard" : BackendName);

            processInfo.Arguments = string.Join(" ", args);

            Process process;
            try {
                if (!File.Exists(processInfo.FileName))
                {
                    throw new Win32Exception(Microsoft.VisualStudioTools.Project.NativeMethods.ERROR_FILE_NOT_FOUND);
                }
                process = Process.Start(processInfo);
                if (process.WaitForExit(100))
                {
                    throw new Win32Exception(process.ExitCode);
                }
            } catch (Win32Exception e) {
                if (e.NativeErrorCode == Microsoft.VisualStudioTools.Project.NativeMethods.ERROR_FILE_NOT_FOUND)
                {
                    WriteError(Strings.ReplEvaluatorInterpreterNotFound);
                }
                else
                {
                    WriteError(Strings.ErrorStartingInteractiveProcess.FormatUI(e.ToString()));
                }
                return(null);
            } catch (Exception e) when(!e.IsCriticalException())
            {
                return(null);
            }

            return(Task.FromResult(CommandProcessorThread.Create(this, conn, process)));
        }
コード例 #8
0
        private async Task <ProcessOutputResult> RunCheckScript(string interpreterPath)
        {
            var scriptPath = PythonToolsInstallPath.GetFile("cookiecutter_check.py");

            return(await RunPythonScript(interpreterPath, scriptPath, ""));
        }
コード例 #9
0
        /// <summary>
        /// Runs the file with the provided settings as a user with
        /// administrative permissions. The window is always hidden and output
        /// is provided to the redirector when the process terminates.
        /// </summary>
        /// <param name="filename">Executable file to run.</param>
        /// <param name="arguments">Arguments to pass.</param>
        /// <param name="workingDirectory">Starting directory.</param>
        /// <param name="redirector">
        /// An object to receive redirected output.
        /// </param>
        /// <param name="quoteArgs"></param>
        /// <returns>A <see cref="ProcessOutput"/> object.</returns>
        public static ProcessOutput RunElevated(
            string filename,
            IEnumerable <string> arguments,
            string workingDirectory,
            IEnumerable <KeyValuePair <string, string> > env,
            Redirector redirector,
            bool quoteArgs          = true,
            bool elevate            = true,
            Encoding outputEncoding = null,
            Encoding errorEncoding  = null
            )
        {
            var psi = new ProcessStartInfo(PythonToolsInstallPath.GetFile("Microsoft.PythonTools.RunElevated.exe", typeof(ProcessOutput).Assembly));

            psi.CreateNoWindow = true;
            psi.WindowStyle    = ProcessWindowStyle.Hidden;

            var utf8 = new UTF8Encoding(false);
            // Send args and env as base64 to avoid newline issues
            string args;

            if (quoteArgs)
            {
                args = string.Join("|", arguments
                                   .Where(a => a != null)
                                   .Select(a => Convert.ToBase64String(utf8.GetBytes(QuoteSingleArgument(a))))
                                   );
            }
            else
            {
                args = string.Join("|", arguments
                                   .Where(a => a != null)
                                   .Select(a => Convert.ToBase64String(utf8.GetBytes(a)))
                                   );
            }

            var fullEnv = env != null?
                          string.Join("|", env.Select(kv => kv.Key + "=" + Convert.ToBase64String(utf8.GetBytes(kv.Value)))) :
                              "";

            TcpListener      listener   = null;
            Task <TcpClient> clientTask = null;

            try {
                listener      = SocketUtils.GetRandomPortListener(IPAddress.Loopback, out int port);
                psi.Arguments = port.ToString();
                clientTask    = listener.AcceptTcpClientAsync();
            } catch (Exception ex) {
                listener?.Stop();
                throw new InvalidOperationException(Strings.UnableToElevate, ex);
            }

            var process = new Process();

            clientTask.ContinueWith(t => {
                listener.Stop();
                TcpClient client;
                try {
                    client = t.Result;
                } catch (AggregateException ae) {
                    try {
                        process.Kill();
                    } catch (InvalidOperationException) {
                    } catch (Win32Exception) {
                    }

                    if (redirector != null)
                    {
                        foreach (var ex in ae.InnerExceptions.DefaultIfEmpty(ae))
                        {
                            using (var reader = new StringReader(ex.ToString())) {
                                for (var line = reader.ReadLine(); line != null; line = reader.ReadLine())
                                {
                                    redirector.WriteErrorLine(line);
                                }
                            }
                        }
                    }
                    return;
                }
                using (var writer = new StreamWriter(client.GetStream(), utf8, 4096, true)) {
                    writer.WriteLine(filename);
                    writer.WriteLine(args);
                    writer.WriteLine(workingDirectory);
                    writer.WriteLine(fullEnv);
                    writer.WriteLine(outputEncoding?.WebName ?? "");
                    writer.WriteLine(errorEncoding?.WebName ?? "");
                }

                if (redirector != null)
                {
                    var reader = new StreamReader(client.GetStream(), utf8, false, 4096, true);
                    Task.Run(() => {
                        try {
                            for (var line = reader.ReadLine(); line != null; line = reader.ReadLine())
                            {
                                if (line.StartsWithOrdinal("OUT:"))
                                {
                                    redirector.WriteLine(line.Substring(4));
                                }
                                else if (line.StartsWithOrdinal("ERR:"))
                                {
                                    redirector.WriteErrorLine(line.Substring(4));
                                }
                                else
                                {
                                    redirector.WriteLine(line);
                                }
                            }
                        } catch (IOException) {
                        } catch (ObjectDisposedException) {
                        }
                    });
                }
            });

            process.StartInfo = psi;

            return(new ProcessOutput(process, redirector));
        }
コード例 #10
0
        private static async Task <ProcessOutputResult> RunRenderContextScript(Redirector redirector, string interpreterPath, string templateFolderPath, string userConfigFilePath, string outputFolderPath, string contextPath)
        {
            var scriptPath = PythonToolsInstallPath.GetFile("cookiecutter_render.py");

            return(await RunPythonScript(redirector, interpreterPath, scriptPath, "\"{0}\" \"{1}\" \"{2}\" \"{3}\"".FormatInvariant(templateFolderPath, userConfigFilePath, PathUtils.TrimEndSeparator(outputFolderPath), contextPath)));
        }
コード例 #11
0
            private string GenerateDbFile(IPythonInterpreterFactory interpreter, ModulePath moduleName, List <string> existingModules, string dbFile, FileStream fs)
            {
                // we need to generate the DB file
                dbFile = Path.Combine(ReferencesDatabasePath, moduleName + ".$project.idb");
                int retryCount = 0;

                while (File.Exists(dbFile))
                {
                    dbFile = Path.Combine(ReferencesDatabasePath, moduleName + "." + ++retryCount + ".$project.idb");
                }

                var args = new List <string> {
                    PythonToolsInstallPath.GetFile("ExtensionScraper.py"),
                    "scrape",
                };

                if (moduleName.IsNativeExtension)
                {
                    args.Add("-");
                    args.Add(moduleName.SourceFile);
                }
                else
                {
                    args.Add(moduleName.ModuleName);
                    args.Add(moduleName.LibraryPath);
                }
                args.Add(Path.ChangeExtension(dbFile, null));

                using (var output = ProcessOutput.RunHiddenAndCapture(interpreter.Configuration.InterpreterPath, args.ToArray())) {
                    if (_cancel.CanBeCanceled)
                    {
                        if (WaitHandle.WaitAny(new[] { _cancel.WaitHandle, output.WaitHandle }) != 1)
                        {
                            // we were cancelled
                            return(null);
                        }
                    }
                    else
                    {
                        output.Wait();
                    }

                    if (output.ExitCode == 0)
                    {
                        // [FileName]|interpGuid|interpVersion|DateTimeStamp|[db_file.idb]
                        // save the new entry in the DB file
                        existingModules.Add(
                            String.Format("{0}|{1}|{2}|{3}",
                                          moduleName.SourceFile,
                                          interpreter.Configuration.Id,
                                          new FileInfo(moduleName.SourceFile).LastWriteTime.ToString("O"),
                                          dbFile
                                          )
                            );

                        fs.Seek(0, SeekOrigin.Begin);
                        fs.SetLength(0);
                        using (var sw = new StreamWriter(fs)) {
                            sw.Write(String.Join(Environment.NewLine, existingModules));
                            sw.Flush();
                        }
                    }
                    else
                    {
                        throw new CannotAnalyzeExtensionException(string.Join(Environment.NewLine, output.StandardErrorLines));
                    }
                }

                return(dbFile);
            }
コード例 #12
0
        /// <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</remarks>
        public static async Task <List <PythonLibraryPath> > GetUncachedDatabaseSearchPathsAsync(string interpreter)
        {
            List <string> lines;

            // sys.path will include the working directory, so we make an empty
            // path that we can filter out later
            var tempWorkingDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            Directory.CreateDirectory(tempWorkingDir);
            var srcGetSearchPaths = PythonToolsInstallPath.GetFile("get_search_paths.py", typeof(PythonTypeDatabase).Assembly);
            var getSearchPaths    = PathUtils.GetAbsoluteFilePath(tempWorkingDir, PathUtils.GetFileOrDirectoryName(srcGetSearchPaths));

            File.Copy(srcGetSearchPaths, getSearchPaths);

            try {
                using (var proc = ProcessOutput.Run(
                           interpreter,
                           new[] {
                    "-S",       // don't import site - we do that in code
                    "-E",       // ignore environment
                    getSearchPaths
                },
                           tempWorkingDir,
                           null,
                           false,
                           null
                           )) {
                    int exitCode = -1;
                    try {
                        exitCode = await proc;
                    } catch (OperationCanceledException) {
                    }
                    if (exitCode != 0)
                    {
                        throw new InvalidOperationException(string.Format(
                                                                "Cannot obtain list of paths{0}{1}",
                                                                Environment.NewLine,
                                                                string.Join(Environment.NewLine, proc.StandardErrorLines))
                                                            );
                    }

                    lines = proc.StandardOutputLines.ToList();
                }
            } finally {
                try {
                    Directory.Delete(tempWorkingDir, true);
                } catch (Exception ex) {
                    if (ex.IsCriticalException())
                    {
                        throw;
                    }
                }
            }

            return(lines.Select(s => {
                if (s.StartsWith(tempWorkingDir, StringComparison.OrdinalIgnoreCase))
                {
                    return null;
                }
                try {
                    return PythonLibraryPath.Parse(s);
                } catch (FormatException) {
                    Debug.Fail("Invalid format for search path: " + s);
                    return null;
                }
            }).Where(p => p != null).ToList());
        }
コード例 #13
0
        public void Imported(IModuleContext context)
        {
            if (_scraped)
            {
                return;
            }
            _scraped = true;

            var interp = context as AstPythonInterpreter;
            var fact   = interp?.Factory;

            if (fact == null || !File.Exists(fact.Configuration.InterpreterPath))
            {
                return;
            }

            ModulePath mp = AstPythonInterpreterFactory.FindModule(fact, _filePath);

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

            var sm = PythonToolsInstallPath.TryGetFile("scrape_module.py", GetType().Assembly);

            if (!File.Exists(sm))
            {
                return;
            }

            Stream code = null;

            using (var p = ProcessOutput.RunHiddenAndCapture(
                       fact.Configuration.InterpreterPath, "-E", sm, mp.LibraryPath, mp.ModuleName
                       )) {
                p.Wait();
                if (p.ExitCode == 0)
                {
                    var ms = new MemoryStream();
                    code = ms;
                    using (var sw = new StreamWriter(ms, Encoding.UTF8, 4096, true)) {
                        foreach (var line in p.StandardOutputLines)
                        {
                            sw.WriteLine(line);
                        }
                    }
                }
            }

            if (code == null)
            {
                return;
            }

            PythonAst ast;

            code.Seek(0, SeekOrigin.Begin);
            using (var sr = new StreamReader(code, Encoding.UTF8))
                using (var parser = Parser.CreateParser(sr, fact.GetLanguageVersion())) {
                    ast = parser.ParseFile();
                }

            lock (_members) {
                var walker = new AstAnalysisWalker(interp, ast, this, _filePath, _members, false);
                ast.Walk(walker);
            }
        }
コード例 #14
0
ファイル: HpcLauncher.cs プロジェクト: xNUTs/PTVS
        /// <summary>
        /// Publishes the project if the user has configured the publish on run.
        /// </summary>
        private bool TryPublishProject(ClusterEnvironment environment, string publishOverrideUrl)
        {
            if (_project.PublishBeforeRun() || environment.HeadNode == "localhost")
            {
                string msg = null;
                try {
                    var vsInstallDir = (string)HpcSupportPackage.Instance.ApplicationRegistryRoot.GetValue("InstallDir");

                    List <IPublishFile> allFiles = new List <IPublishFile>();

                    // add python debugger files
                    string pyInstallDir = Path.GetDirectoryName(PythonToolsInstallPath.GetFile(_pyDebuggerFiles[0]));
                    foreach (var file in _pyDebuggerFiles)
                    {
                        allFiles.Add(new CopyFile(Path.Combine(pyInstallDir, file), file));
                    }

                    string pyHpcInstallDir = Path.GetDirectoryName(PythonToolsInstallPath.GetFile(MpiShimExe));
                    foreach (var file in _hpcDebuggerFiles)
                    {
                        allFiles.Add(new CopyFile(Path.Combine(pyHpcInstallDir, file), file));
                    }

                    // add VS components that we need to run
                    foreach (var file in _vsAssemblies)
                    {
                        allFiles.Add(new CopyFile(file, Path.GetFileName(file)));
                    }

                    // Add vs remote debugger components.
                    string basePath = Path.Combine(Path.Combine(vsInstallDir, "Remote Debugger"), _project.TargetPlatform().ToString());
                    if (!Directory.Exists(basePath))
                    {
                        basePath = Path.Combine(
                            (string)HpcSupportPackage.Instance.ApplicationRegistryRoot.GetValue("ShellFolder", ""),
                            "Common7",
                            "Packages",
                            "Debugger",
                            _project.TargetPlatform().ToString()
                            );
                    }
                    basePath += "\\";
                    foreach (var file in Directory.GetFiles(basePath, "*", SearchOption.AllDirectories))
                    {
                        allFiles.Add(new CopyFile(file, file.Substring(basePath.Length)));
                    }

                    if (!_project.Publish(new PublishProjectOptions(allFiles.ToArray(), publishOverrideUrl)))
                    {
                        msg = "Publishing not configured or unknown publishing schema";
                    }
                } catch (PublishFailedException e) {
                    msg = e.InnerException.Message;
                }

                if (msg != null && CancelLaunch(msg))
                {
                    return(false);
                }
            }
            return(true);
        }
コード例 #15
0
        private static string GetServerLocation()
        {
            var filePath = PythonToolsInstallPath.GetFile(@"pylance\dist\pylance-langserver.bundle.js");

            return(File.Exists(filePath) ? filePath : null);
        }
コード例 #16
0
        private static async Task <ProcessOutputResult> RunGenerateContextScript(Redirector redirector, string interpreterPath, string templateFolderPath, string userConfigFilePath)
        {
            var scriptPath = PythonToolsInstallPath.GetFile("cookiecutter_load.py");

            return(await RunPythonScript(redirector, interpreterPath, scriptPath, "\"{0}\" \"{1}\"".FormatInvariant(templateFolderPath, userConfigFilePath)));
        }
コード例 #17
0
ファイル: DebugAdapterProcess.cs プロジェクト: serg32m/PTVS
        private void StartProcess(string launchJson)
        {
            var connection = InitializeListenerSocket();

            var json = JObject.Parse(launchJson);
            var exe  = json["exe"].Value <string>();
            var args = json["args"].Value <string>();
            var cwd  = json["cwd"].Value <string>();

            ParseOptions(json["options"].Value <string>());

            var argsList = new List <string> {
                string.IsNullOrWhiteSpace(_interpreterOptions) ? "" : _interpreterOptions,
                PythonToolsInstallPath.GetFile("ptvsd_launcher.py"),
                cwd.Trim('\\'),
                $"{_listenerPort}",
                $"{_processGuid}",
                $"{_debugOptions}",
                "-g",
                args
            };
            var arguments = string.Join(" ", argsList.Where(a => !string.IsNullOrWhiteSpace(a)).Select(ProcessOutput.QuoteSingleArgument));

            ProcessStartInfo psi = new ProcessStartInfo {
                FileName               = exe,
                Arguments              = arguments,
                WorkingDirectory       = cwd,
                RedirectStandardError  = false,
                RedirectStandardInput  = false,
                RedirectStandardOutput = false,
                UseShellExecute        = false,
                CreateNoWindow         = false,
            };

            var env = json["env"].Value <JArray>();

            foreach (JObject curValue in env)
            {
                var name  = curValue["name"].Value <string>();
                var value = curValue["value"].Value <string>();
                if (!string.IsNullOrWhiteSpace(name))
                {
                    psi.EnvironmentVariables[name] = value;
                }
            }

            _process = new Process {
                EnableRaisingEvents = true,
                StartInfo           = psi
            };

            _process.Exited += OnExited;
            _process.Start();

            try {
                if (connection.Wait(_debuggerConnectionTimeout))
                {
                    var socket = connection.Result;
                    if (socket != null)
                    {
                        _stream = new DebugAdapterProcessStream(new NetworkStream(connection.Result, ownsSocket: true));
                    }
                }
                else
                {
                    Debug.WriteLine("Timed out waiting for debuggee to connect.", nameof(DebugAdapterProcess));
                }
            } catch (AggregateException ex) {
                Debug.WriteLine("Error waiting for debuggee to connect {0}".FormatInvariant(ex.InnerException ?? ex), nameof(DebugAdapterProcess));
            }

            if (_stream == null)
            {
                _process.Kill();
            }
        }
コード例 #18
0
        private static async Task <ProcessOutputResult> RunRunScript(Redirector redirector, string interpreterPath, string templateFolderPath, string userConfigFilePath, string outputFolderPath, string contextPath)
        {
            var scriptPath = PythonToolsInstallPath.GetFile("cookiecutter_run.py");

            return(await RunPythonScript(redirector, interpreterPath, scriptPath, GetRunArguments(templateFolderPath, userConfigFilePath, outputFolderPath, contextPath)));
        }
コード例 #19
0
ファイル: DebugAdapterProcess.cs プロジェクト: yepeiwen/PTVS
        private void StartProcess(string launchJson)
        {
            var connection = InitializeListenerSocket();

            var json = JObject.Parse(launchJson);
            var exe  = json["exe"].Value <string>();
            var scriptAndScriptArgs = json["args"].Value <string>();
            var cwd = json["cwd"].Value <string>();

            ParseOptions(json["options"].Value <string>());

            var argsList = new List <string> {
                string.IsNullOrWhiteSpace(_interpreterOptions) ? "" : _interpreterOptions,
                PythonToolsInstallPath.GetFile("ptvsd_launcher.py"),
                cwd.Trim('\\'),
                $"{_listenerPort}",
                $"{_processGuid}",
                $"{_debugOptions}",
                "-g"
            };
            var launcherArgs = string.Join(" ", argsList.Where(a => !string.IsNullOrWhiteSpace(a)).Select(ProcessOutput.QuoteSingleArgument));
            var arguments    = $"{launcherArgs} {scriptAndScriptArgs}";

            ProcessStartInfo psi = new ProcessStartInfo {
                FileName               = exe,
                Arguments              = arguments,
                WorkingDirectory       = cwd,
                RedirectStandardError  = false,
                RedirectStandardInput  = false,
                RedirectStandardOutput = false,
                UseShellExecute        = false,
                CreateNoWindow         = false,
            };

            var env = json["env"].Value <JArray>();

            foreach (JObject curValue in env)
            {
                var name  = curValue["name"].Value <string>();
                var value = curValue["value"].Value <string>();
                if (!string.IsNullOrWhiteSpace(name))
                {
                    psi.EnvironmentVariables[name] = value;
                }
            }

            _process = new Process {
                EnableRaisingEvents = true,
                StartInfo           = psi
            };

            _process.Exited += OnExited;
            _process.Start();

            var logger = (IPythonToolsLogger)VisualStudio.Shell.ServiceProvider.GlobalProvider.GetService(typeof(IPythonToolsLogger));

            try {
                if (connection.Wait(_debuggerConnectionTimeout))
                {
                    var socket = connection.Result;
                    if (socket != null)
                    {
                        _debuggerConnected      = true;
                        _stream                 = new DebugAdapterProcessStream(new NetworkStream(socket, ownsSocket: true));
                        _stream.Initialized    += OnInitialized;
                        _stream.LegacyDebugger += OnLegacyDebugger;
                        if (!string.IsNullOrEmpty(_webBrowserUrl) && Uri.TryCreate(_webBrowserUrl, UriKind.RelativeOrAbsolute, out Uri uri))
                        {
                            OnPortOpenedHandler.CreateHandler(uri.Port, null, null, ProcessExited, LaunchBrowserDebugger);
                        }
                    }
                }
                else
                {
                    Debug.WriteLine("Timed out waiting for debuggee to connect.", nameof(DebugAdapterProcess));
                    logger?.LogEvent(PythonLogEvent.DebugAdapterConnectionTimeout, "Launch");
                }
            } catch (AggregateException ex) {
                Debug.WriteLine("Error waiting for debuggee to connect {0}".FormatInvariant(ex.InnerException ?? ex), nameof(DebugAdapterProcess));
            }

            if (_stream == null && !_process.HasExited)
            {
                _process.Kill();
            }
        }
コード例 #20
0
        public static async Task <int> GenerateAsync(PythonTypeDatabaseCreationRequest request)
        {
            var fact = request.Factory;
            var evt  = request.OnExit;

            if (fact == null || !Directory.Exists(fact.Configuration.LibraryPath))
            {
                if (evt != null)
                {
                    evt(NotSupportedExitCode);
                }
                return(NotSupportedExitCode);
            }
            var outPath = request.OutputPath;

            var analyzerPath = PythonToolsInstallPath.GetFile("Microsoft.PythonTools.Analyzer.exe");

            Directory.CreateDirectory(CompletionDatabasePath);

            var baseDb = BaselineDatabasePath;

            if (request.ExtraInputDatabases.Any())
            {
                baseDb = baseDb + ";" + string.Join(";", request.ExtraInputDatabases);
            }

            var logPath  = Path.Combine(outPath, "AnalysisLog.txt");
            var glogPath = Path.Combine(CompletionDatabasePath, "AnalysisLog.txt");

            using (var output = ProcessOutput.RunHiddenAndCapture(
                       analyzerPath,
                       "/id", fact.Configuration.Id,
                       "/version", fact.Configuration.Version.ToString(),
                       "/python", fact.Configuration.InterpreterPath,
                       request.DetectLibraryPath ? null : "/library",
                       request.DetectLibraryPath ? null : fact.Configuration.LibraryPath,
                       "/outdir", outPath,
                       "/basedb", baseDb,
                       (request.SkipUnchanged ? null : "/all"), // null will be filtered out; empty strings are quoted
                       "/log", logPath,
                       "/glog", glogPath,
                       "/wait", (request.WaitFor != null ? AnalyzerStatusUpdater.GetIdentifier(request.WaitFor) : "")
                       )) {
                output.PriorityClass = ProcessPriorityClass.BelowNormal;
                int exitCode = await output;

                if (exitCode > -10 && exitCode < 0)
                {
                    try {
                        File.AppendAllLines(
                            glogPath,
                            new[] { string.Format("FAIL_STDLIB: ({0}) {1}", exitCode, output.Arguments) }
                            .Concat(output.StandardErrorLines)
                            );
                    } catch (IOException) {
                    } catch (ArgumentException) {
                    } catch (SecurityException) {
                    } catch (UnauthorizedAccessException) {
                    }
                }

                if (evt != null)
                {
                    evt(exitCode);
                }
                return(exitCode);
            }
        }
コード例 #21
0
        private string Check(string interpreterPath)
        {
            var checkScript = PythonToolsInstallPath.GetFile("cookiecutter_check.py");

            return(RunPythonScript(interpreterPath, checkScript, ""));
        }
コード例 #22
0
 public override IEnumerable <string> Prepare() => new[] { PythonToolsInstallPath.GetFile("pip_downloader.py", typeof(PipPackageManager).Assembly) };
コード例 #23
0
        private async Task <ProcessOutputResult> RunGenerateContextScript(string interpreterPath, string templateFolderPath, string userConfigFilePath)
        {
            var scriptPath = PythonToolsInstallPath.GetFile("cookiecutter_load.py");

            return(await RunPythonScript(interpreterPath, scriptPath, string.Format("\"{0}\" \"{1}\"", templateFolderPath, userConfigFilePath)));
        }