コード例 #1
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);
        }
コード例 #2
0
ファイル: PythonProjectPackage.cs プロジェクト: yepeiwen/PTVS
        protected override void Initialize()
        {
            // The variable is inherited by MSBuild processes and is used to resolve test target
            // files.
            var    installPath = PathUtils.GetParent(PythonToolsInstallPath.GetFile("Microsoft.PythonTools.dll", GetType().Assembly));
            string rootDir;

            if (!((IServiceProvider)this).TryGetShellProperty((__VSSPROPID)__VSSPROPID2.VSSPROPID_InstallRootDir, out rootDir) ||
                !PathUtils.IsSubpathOf(rootDir, installPath))
            {
                MSBuild.ProjectCollection.GlobalProjectCollection.SetGlobalProperty("_PythonToolsPath", installPath);
                Environment.SetEnvironmentVariable("_PythonToolsPath", installPath);

                // Also find and set the UWP tools path
                var uwp = PathUtils.FindFile(PathUtils.GetParent(PathUtils.GetParent(installPath)), "Microsoft.PythonTools.Uwp.targets", depthLimit: 3);
                if (!string.IsNullOrEmpty(uwp))
                {
                    uwp = PathUtils.GetParent(uwp);
                    MSBuild.ProjectCollection.GlobalProjectCollection.SetGlobalProperty("_PythonUwpToolsPath", uwp);
                    Environment.SetEnvironmentVariable("_PythonUwpToolsPath", installPath);
                }
            }

            base.Initialize();
            RegisterProjectFactory(new PythonWebProjectFactory(this));
        }
コード例 #3
0
        internal static ConnErrorMessages AttachDkmWorker(int pid)
        {
            var hProcess = NativeMethods.OpenProcess(ProcessAccessFlags.All, false, pid);

            if (hProcess == IntPtr.Zero)
            {
                return(ConnErrorMessages.CannotOpenProcess);
            }

            string dllName = string.Format("Microsoft.PythonTools.Debugger.Helper.{0}.dll", IntPtr.Size == 4 ? "x86" : "x64");
            string dllPath = PythonToolsInstallPath.GetFile(dllName);

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

            // 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(InjectDll(dllPath, hProcess, hKernel32));
                }
            }
            return(error);
        }
コード例 #4
0
        private async Task CreateVenvWithoutPipThenInstallPip()
        {
            RemoveExistingVenv();

            _redirector.WriteLine(Strings.InstallingCookiecutterCreateEnvWithoutPip.FormatUI(_envFolderPath));
            var output = ProcessOutput.Run(
                _interpreter.InterpreterExecutablePath,
                new[] { "-m", "venv", _envFolderPath, "--without-pip" },
                null,
                null,
                false,
                _redirector
                );

            await WaitForOutput(_interpreter.InterpreterExecutablePath, output);

            _redirector.WriteLine(Strings.InstallingCookiecutterInstallPip.FormatUI(_envFolderPath));
            var pipScriptPath = PythonToolsInstallPath.GetFile("pip_downloader.py");

            output = ProcessOutput.Run(
                _envInterpreterPath,
                new[] { pipScriptPath },
                _interpreter.PrefixPath,
                null,
                false,
                _redirector
                );
            await WaitForOutput(_interpreter.InterpreterExecutablePath, output);
        }
コード例 #5
0
        private static async Task <ProcessOutputResult> RunCheckScript(string interpreterPath)
        {
            var scriptPath = PythonToolsInstallPath.GetFile("cookiecutter_check.py");
            var output     = ProcessOutput.RunHiddenAndCapture(interpreterPath, scriptPath);

            return(await WaitForOutput(interpreterPath, output));
        }
コード例 #6
0
ファイル: DebugAdapterProcess.cs プロジェクト: wzhiliang/PTVS
        private void StartProcess(string launchJson)
        {
            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  = true,
                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.ErrorDataReceived += OnErrorDataReceived;
            _process.Start();
            _process.BeginErrorReadLine();
            if (!_connectedEvent.WaitOne(_debuggerConnectionTimeout))
            {
                Debug.WriteLine("Timed out waiting for debuggee to connect.", nameof(DebugAdapterProcess));
            }
        }
コード例 #7
0
            private string GenerateDbFile(IPythonInterpreterFactory interpreter, string moduleName, string extensionModuleFilename, 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");
                }

                using (var output = interpreter.Run(
                           PythonToolsInstallPath.GetFile("ExtensionScraper.py"),
                           "scrape",
                           "-",                               // do not use __import__
                           extensionModuleFilename,           // extension module path
                           Path.ChangeExtension(dbFile, null) // output file path (minus .idb)
                           )) {
                    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}|{4}",
                                          extensionModuleFilename,
                                          interpreter.Id,
                                          interpreter.Configuration.Version,
                                          new FileInfo(extensionModuleFilename).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);
            }
コード例 #8
0
        private static Process CreateProcess(string args, string exeName) {
            string exePath = PythonToolsInstallPath.GetFile(exeName);
            if (string.IsNullOrEmpty(exePath)) {
                return null;
            }

            return ConfigureAndStartProcess(new ProcessStartInfo(exePath, args));
        }
コード例 #9
0
ファイル: ProfiledProcess.cs プロジェクト: zhangjinxing/PTVS
        public ProfiledProcess(PythonToolsService pyService, string exe, string args, string dir, Dictionary <string, string> envVars)
        {
            var arch = NativeMethods.GetBinaryType(exe);

            if (arch != ProcessorArchitecture.X86 && arch != ProcessorArchitecture.Amd64)
            {
                throw new InvalidOperationException(Strings.UnsupportedArchitecture.FormatUI(arch));
            }

            dir = PathUtils.TrimEndSeparator(dir);
            if (string.IsNullOrEmpty(dir))
            {
                dir = ".";
            }

            _pyService = pyService;
            _exe       = exe;
            _args      = args;
            _dir       = dir;
            _arch      = arch;

            ProcessStartInfo processInfo;
            string           pythonInstallDir = Path.GetDirectoryName(PythonToolsInstallPath.GetFile("VsPyProf.dll", typeof(ProfiledProcess).Assembly));

            string dll       = _arch == ProcessorArchitecture.Amd64 ? "VsPyProf.dll" : "VsPyProfX86.dll";
            string arguments = string.Join(" ",
                                           ProcessOutput.QuoteSingleArgument(Path.Combine(pythonInstallDir, "proflaun.py")),
                                           ProcessOutput.QuoteSingleArgument(Path.Combine(pythonInstallDir, dll)),
                                           ProcessOutput.QuoteSingleArgument(dir),
                                           _args
                                           );

            processInfo = new ProcessStartInfo(_exe, arguments);
            if (_pyService.DebuggerOptions.WaitOnNormalExit)
            {
                processInfo.EnvironmentVariables["VSPYPROF_WAIT_ON_NORMAL_EXIT"] = "1";
            }
            if (_pyService.DebuggerOptions.WaitOnAbnormalExit)
            {
                processInfo.EnvironmentVariables["VSPYPROF_WAIT_ON_ABNORMAL_EXIT"] = "1";
            }

            processInfo.CreateNoWindow         = false;
            processInfo.UseShellExecute        = false;
            processInfo.RedirectStandardOutput = false;
            processInfo.WorkingDirectory       = _dir;

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

            _process           = new Process();
            _process.StartInfo = processInfo;
        }
コード例 #10
0
        private static void RunVTune(SessionNode session, LaunchConfiguration config, bool openReport)
        {
            var interpreter = config.GetInterpreterPath();

            if (!File.Exists(interpreter))
            {
                MessageBox.Show(Strings.CannotFindPythonInterpreter, Strings.ProductTitle);
                return;
            }

            string outPathDir = Path.GetTempPath();
            var    subpath    = Path.Combine(outPathDir, Path.GetRandomFileName());

            while (Directory.Exists(subpath) || File.Exists(subpath))
            {
                subpath = Path.Combine(outPathDir, Path.GetRandomFileName());
            }
            outPathDir = subpath;

            string outPath = Path.Combine(outPathDir, "pythontrace.diagsession");

            var driver = PythonToolsInstallPath.GetFile(ExternalProfilerDriverExe, typeof(PythonProfilingPackage).Assembly);

            var procInfo = new ProcessStartInfo(driver)
            {
                CreateNoWindow = false,
                Arguments      = string.Join(" ", new[] {
                    "-d",
                    ProcessOutput.QuoteSingleArgument(outPathDir),
                    "--",
                    ProcessOutput.QuoteSingleArgument(interpreter),
                    config.InterpreterArguments,
                    string.IsNullOrEmpty(config.ScriptName) ? "" : ProcessOutput.QuoteSingleArgument(config.ScriptName),
                    config.ScriptArguments
                }),
                WorkingDirectory = config.WorkingDirectory,
            };

            var proc = new Process {
                StartInfo = procInfo
            };
            var dte = (EnvDTE.DTE)session._serviceProvider.GetService(typeof(EnvDTE.DTE));

            proc.EnableRaisingEvents = true;
            proc.Exited += (_, args) => {
                if (!File.Exists(Path.Combine(outPathDir, "Sample.dwjson")))
                {
                    MessageBox.Show(Strings.CannotFindGeneratedFile, Strings.ProductTitle);
                }
                else
                {
                    PackageTrace(outPathDir);
                    dte.ItemOperations.OpenFile(Path.Combine(outPathDir, "trace.diagsession"));
                }
            };
            proc.Start();
        }
コード例 #11
0
        internal static string GetDebuggerSearchPath(bool isLegacy)
        {
            if (isLegacy)
            {
                return(Path.GetDirectoryName(Path.GetDirectoryName(PythonToolsInstallPath.GetFile("ptvsd\\__init__.py"))));
            }

            return(Path.GetDirectoryName(Path.GetDirectoryName(PythonToolsInstallPath.GetFile("Packages\\ptvsd\\__init__.py"))));
        }
コード例 #12
0
ファイル: TestExecutor.cs プロジェクト: lenaantler/PTVS
        private static string GetDebuggerSearchPath(bool useLegacyDebugger)
        {
            if (useLegacyDebugger)
            {
                return(Path.GetDirectoryName(Path.GetDirectoryName(PythonToolsInstallPath.GetFile("ptvsd\\__init__.py"))));
            }

            return(Path.GetDirectoryName(Path.GetDirectoryName(PythonToolsInstallPath.GetFile("Packages\\ptvsd\\__init__.py"))));
        }
コード例 #13
0
        public void PreProcess(MSBuild.Project project)
        {
            project.SetProperty("ProjectHome", ".");
            project.SetProperty("WorkingDirectory", ".");

            var installPath = PathUtils.GetParent(PythonToolsInstallPath.GetFile("Microsoft.PythonTools.dll", typeof(PythonToolsPackage).Assembly));

            project.SetProperty("_PythonToolsPath", installPath);
            project.Xml.AddImport(Microsoft.PythonTools.Project.PythonProjectFactory.PtvsTargets);
        }
コード例 #14
0
ファイル: ProfiledProcess.cs プロジェクト: xNUTs/PTVS
        public ProfiledProcess(PythonToolsService pyService, string exe, string args, string dir, Dictionary <string, string> envVars, ProcessorArchitecture arch)
        {
            if (arch != ProcessorArchitecture.X86 && arch != ProcessorArchitecture.Amd64)
            {
                throw new InvalidOperationException(String.Format("Unsupported architecture: {0}", arch));
            }
            if (dir.EndsWith("\\"))
            {
                dir = dir.Substring(0, dir.Length - 1);
            }
            if (String.IsNullOrEmpty(dir))
            {
                dir = ".";
            }
            _pyService = pyService;
            _exe       = exe;
            _args      = args;
            _dir       = dir;
            _arch      = arch;

            ProcessStartInfo processInfo;
            string           pythonInstallDir = Path.GetDirectoryName(PythonToolsInstallPath.GetFile("VsPyProf.dll"));
            string           dll       = _arch == ProcessorArchitecture.Amd64 ? "VsPyProf.dll" : "VsPyProfX86.dll";
            string           arguments = "\"" + Path.Combine(pythonInstallDir, "proflaun.py") + "\" " +
                                         "\"" + Path.Combine(pythonInstallDir, dll) + "\" " +
                                         "\"" + dir + "\" " +
                                         _args;

            processInfo = new ProcessStartInfo(_exe, arguments);
            if (_pyService.DebuggerOptions.WaitOnNormalExit)
            {
                processInfo.EnvironmentVariables["VSPYPROF_WAIT_ON_NORMAL_EXIT"] = "1";
            }
            if (_pyService.DebuggerOptions.WaitOnAbnormalExit)
            {
                processInfo.EnvironmentVariables["VSPYPROF_WAIT_ON_ABNORMAL_EXIT"] = "1";
            }

            processInfo.CreateNoWindow         = false;
            processInfo.UseShellExecute        = false;
            processInfo.RedirectStandardOutput = false;
            processInfo.WorkingDirectory       = _dir;

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

            _process           = new Process();
            _process.StartInfo = processInfo;
        }
コード例 #15
0
        public ITargetHostProcess LaunchAdapter(IAdapterLaunchInfo launchInfo, ITargetHostInterop targetInterop)
        {
            if (launchInfo.LaunchType == LaunchType.Attach)
            {
                var debugAttachInfo = (DebugAttachInfo)_debugInfo;
                return(DebugAdapterRemoteProcess.Attach(debugAttachInfo));
            }

            var debugLaunchInfo         = (DebugLaunchInfo)_debugInfo;
            var debugPyAdapterDirectory = Path.GetDirectoryName(PythonToolsInstallPath.GetFile("debugpy\\adapter\\__init__.py"));
            var targetProcess           = new DebugAdapterProcess(_adapterHostContext, targetInterop, debugPyAdapterDirectory);

            return(targetProcess.StartProcess(debugLaunchInfo.InterpreterPathAndArguments.FirstOrDefault(), debugLaunchInfo.LaunchWebPageUrl));
        }
コード例 #16
0
ファイル: CookiecutterClient.cs プロジェクト: int19h/PTVS
        private async Task CreateVenvWithoutPipThenInstallPip()
        {
            // check for redirection
            await UpdateEnvPathForRedirection(_expectedEnvFolderPath);

            // The venv is not guaranteed to be created where expected, see GetRealPath() for more information.

            // Also, Python has a bug (https://bugs.python.org/issue45337) where it doesn't
            // keep track of the real location of the redirected venv when creating a venv with pip installed.
            // In this case, the call to python.exe will have an exit code of 106.

            // Therefore, the workaround is the following:
            // 1. Create the venv WITHOUT PIP every time
            // 2. Run python and check os.path.realpath against the expected venv path
            // 3. If the real path comes back different, that's the real venv path.
            // 5. Install pip using python.exe and the real venv path.

            RemoveExistingVenv(_envFolderPath);

            // create the venv without pip installed
            _redirector.WriteLine(Strings.InstallingCookiecutterCreateEnvWithoutPip.FormatUI(_expectedEnvFolderPath));
            var output = ProcessOutput.Run(
                _interpreter.InterpreterExecutablePath,
                new[] { "-m", "venv", _expectedEnvFolderPath, "--without-pip" },
                null,
                null,
                false,
                _redirector
                );

            await WaitForOutput(_interpreter.InterpreterExecutablePath, output);

            // If we get here, the environment was created successfully.
            // Check for redirection again, overwriting the existing value if there is
            await UpdateEnvPathForRedirection(_expectedEnvFolderPath, true);

            // install pip in the new environment, wherever it is
            _redirector.WriteLine(Strings.InstallingCookiecutterInstallPip.FormatUI(_envFolderPath));
            var pipScriptPath = PythonToolsInstallPath.GetFile("pip_downloader.py");

            output = ProcessOutput.Run(
                _envInterpreterPath,
                new[] { pipScriptPath },
                _interpreter.PrefixPath,
                null,
                false,
                _redirector
                );
            await WaitForOutput(_interpreter.InterpreterExecutablePath, output);
        }
コード例 #17
0
ファイル: Pip.cs プロジェクト: zuokaihuang/PTVS
        public static async Task InstallPip(IServiceProvider provider, IPythonInterpreterFactory factory, bool elevate, Redirector output = null)
        {
            factory.ThrowIfNotRunnable("factory");

            var pipDownloaderPath = PythonToolsInstallPath.GetFile("pip_downloader.py");

            if (output != null)
            {
                output.WriteLine(Strings.PipInstalling);
                if (provider.GetPythonToolsService().GeneralOptions.ShowOutputWindowForPackageInstallation)
                {
                    output.ShowAndActivate();
                }
                else
                {
                    output.Show();
                }
            }
            using (var proc = ProcessOutput.Run(
                       factory.Configuration.InterpreterPath,
                       new[] { pipDownloaderPath },
                       factory.Configuration.PrefixPath,
                       null,
                       false,
                       output,
                       elevate: elevate
                       )) {
                var exitCode = await proc;
                if (output != null)
                {
                    if (exitCode == 0)
                    {
                        output.WriteLine(Strings.PipInstallSucceeded);
                    }
                    else
                    {
                        output.WriteLine(Strings.PipInstallFailedExitCode.FormatUI(exitCode));
                    }
                    if (provider.GetPythonToolsService().GeneralOptions.ShowOutputWindowForPackageInstallation)
                    {
                        output.ShowAndActivate();
                    }
                    else
                    {
                        output.Show();
                    }
                }
            }
        }
コード例 #18
0
ファイル: PythonProjectPackage.cs プロジェクト: szh2bg/PTVS
        protected override void Initialize()
        {
            // The variable is inherited by MSBuild processes and is used to resolve test target
            // files.
            var    installPath = PathUtils.GetParent(PythonToolsInstallPath.GetFile("Microsoft.PythonTools.dll", GetType().Assembly));
            string rootDir;

            if (!((IServiceProvider)this).TryGetShellProperty((__VSSPROPID)__VSSPROPID2.VSSPROPID_InstallRootDir, out rootDir) ||
                !PathUtils.IsSubpathOf(rootDir, installPath))
            {
                MSBuild.ProjectCollection.GlobalProjectCollection.SetGlobalProperty("_PythonToolsPath", installPath);
                Environment.SetEnvironmentVariable("_PythonToolsPath", installPath);
            }

            base.Initialize();
            RegisterProjectFactory(new PythonWebProjectFactory(this));
        }
コード例 #19
0
        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 {
                        if (success)
                        {
                            IsPipInstalled = true;
                        }

                        OnOperationFinished(
                            success ? Resources.InstallingPipSuccess : Resources.InstallingPipFailed
                            );
                    }
                }
            }
        }
コード例 #20
0
ファイル: PipPackageManager.cs プロジェクト: whitestone7/PTVS
        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);
            }
        }
コード例 #21
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);
        }
コード例 #22
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)));
        }
コード例 #23
0
 public override IEnumerable <string> Prepare() => new[] { PythonToolsInstallPath.GetFile("pip_downloader.py", typeof(PipPackageManager).Assembly) };
コード例 #24
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);
            }
        }
コード例 #25
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));
        }
コード例 #26
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)));
        }
コード例 #27
0
        private string Check(string interpreterPath)
        {
            var checkScript = PythonToolsInstallPath.GetFile("cookiecutter_check.py");

            return(RunPythonScript(interpreterPath, checkScript, ""));
        }
コード例 #28
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)));
        }
コード例 #29
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)));
        }