Exemplo n.º 1
0
        private void GetFullUrl(LaunchConfiguration config, out Uri uri, out int port)
        {
            int p;

            if (!int.TryParse(config.GetLaunchOption(PythonConstants.WebBrowserPortSetting) ?? "", out p))
            {
                p = TestServerPort;
            }
            port = p;

            var host = config.GetLaunchOption(PythonConstants.WebBrowserUrlSetting);

            if (string.IsNullOrEmpty(host))
            {
                uri = null;
                return;
            }
            try {
                uri = GetFullUrl(host, p);
            } catch (UriFormatException) {
                var output = OutputWindowRedirector.GetGeneral(_serviceProvider);
                output.WriteErrorLine(Strings.ErrorInvalidLaunchUrl.FormatUI(host));
                output.ShowAndActivate();
                uri = null;
            }
        }
Exemplo n.º 2
0
        internal static void GetFullUrl(IServiceProvider provider, LaunchConfiguration config, out Uri uri, out int port)
        {
            int p;

            if (int.TryParse(config.GetLaunchOption(PythonConstants.WebBrowserPortSetting) ?? "", out p))
            {
                port = p;
            }
            else
            {
                SocketUtils.GetRandomPortListener(IPAddress.Loopback, out port).Stop();
                p = -1;
            }

            var host = config.GetLaunchOption(PythonConstants.WebBrowserUrlSetting);

            if (string.IsNullOrEmpty(host))
            {
                uri = null;
                return;
            }

            try {
                UriBuilder builder;
                if (Uri.TryCreate(host, UriKind.Absolute, out uri))
                {
                    builder = new UriBuilder(uri);
                }
                else
                {
                    builder        = new UriBuilder();
                    builder.Scheme = Uri.UriSchemeHttp;
                    builder.Host   = "localhost";
                    builder.Path   = host;
                }

                if (p >= 0)
                {
                    builder.Port = p;
                }
                else if (builder.Port < 0 || (builder.Uri.IsDefaultPort && !host.Contains(":{0}".FormatInvariant(builder.Port))))
                {
                    SocketUtils.GetRandomPortListener(IPAddress.Loopback, out port).Stop();
                    builder.Port = port;
                }

                uri  = builder.Uri;
                port = uri.Port;
            } catch (UriFormatException) {
                if (provider == null)
                {
                    throw;
                }
                var output = OutputWindowRedirector.GetGeneral(provider);
                output.WriteErrorLine(Strings.ErrorInvalidLaunchUrl.FormatUI(host));
                output.ShowAndActivate();
                uri = null;
            }
        }
Exemplo n.º 3
0
        private static IEnumerable <string> GetLaunchConfigurationOptions(LaunchConfiguration config)
        {
            yield return(string.Format("{0}={1}", AD7Engine.VersionSetting, config.Interpreter.Version));

            yield return(string.Format("{0}={1}",
                                       AD7Engine.InterpreterOptions,
                                       config.InterpreterArguments ?? string.Empty
                                       ));

            var url = config.GetLaunchOption(PythonConstants.WebBrowserUrlSetting);

            if (!string.IsNullOrWhiteSpace(url))
            {
                yield return(string.Format("{0}={1}", AD7Engine.WebBrowserUrl, HttpUtility.UrlEncode(url)));
            }

            if (config.GetLaunchOption("DjangoDebug").IsTrue())
            {
                yield return(AD7Engine.EnableDjangoDebugging + "=True");
            }
        }
Exemplo n.º 4
0
        public static ProcessStartInfo CreateProcessStartInfo(IServiceProvider provider, LaunchConfiguration config)
        {
            var psi = new ProcessStartInfo {
                FileName  = config.GetInterpreterPath(),
                Arguments = string.Join(" ", new[] {
                    config.InterpreterArguments,
                    config.ScriptName == null ? "" : ProcessOutput.QuoteSingleArgument(config.ScriptName),
                    config.ScriptArguments
                }.Where(s => !string.IsNullOrEmpty(s))),
                WorkingDirectory = config.WorkingDirectory,
                UseShellExecute  = false
            };

            if (string.IsNullOrEmpty(psi.FileName))
            {
                throw new FileNotFoundException(Strings.DebugLaunchInterpreterMissing);
            }
            if (!File.Exists(psi.FileName))
            {
                throw new FileNotFoundException(Strings.DebugLaunchInterpreterMissing_Path.FormatUI(psi.FileName));
            }
            if (string.IsNullOrEmpty(psi.WorkingDirectory))
            {
                psi.WorkingDirectory = PathUtils.GetParent(config.ScriptName);
            }
            if (string.IsNullOrEmpty(psi.WorkingDirectory))
            {
                throw new DirectoryNotFoundException(Strings.DebugLaunchWorkingDirectoryMissing);
            }
            if (!Directory.Exists(psi.WorkingDirectory))
            {
                throw new DirectoryNotFoundException(Strings.DebugLaunchWorkingDirectoryMissing_Path.FormatUI(psi.WorkingDirectory));
            }

            foreach (var kv in provider.GetPythonToolsService().GetFullEnvironment(config))
            {
                psi.Environment[kv.Key] = kv.Value;
            }

            var pyService = provider.GetPythonToolsService();
            // Pause if the user has requested it.
            string pauseCommand = null;

            if (config.GetLaunchOption(PythonConstants.NeverPauseOnExit).IsTrue())
            {
                // Do nothing
            }
            else if (pyService.DebuggerOptions.WaitOnAbnormalExit && pyService.DebuggerOptions.WaitOnNormalExit)
            {
                pauseCommand = "pause";
            }
            else if (pyService.DebuggerOptions.WaitOnAbnormalExit && !pyService.DebuggerOptions.WaitOnNormalExit)
            {
                pauseCommand = "if errorlevel 1 pause";
            }
            else if (pyService.DebuggerOptions.WaitOnNormalExit && !pyService.DebuggerOptions.WaitOnAbnormalExit)
            {
                pauseCommand = "if not errorlevel 1 pause";
            }

            if (!string.IsNullOrEmpty(pauseCommand))
            {
                psi.Arguments = string.Format("/c \"{0} {1}\" & {2}",
                                              ProcessOutput.QuoteSingleArgument(psi.FileName),
                                              psi.Arguments,
                                              pauseCommand
                                              );
                psi.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
            }

            return(psi);
        }
Exemplo n.º 5
0
        public static unsafe DebugTargetInfo CreateDebugTargetInfo(IServiceProvider provider, LaunchConfiguration config)
        {
            if (config.Interpreter.Version < new Version(2, 6) && config.Interpreter.Version > new Version(0, 0))
            {
                // We don't support Python 2.5 now that our debugger needs the json module
                throw new NotSupportedException(Strings.DebuggerPythonVersionNotSupported);
            }

            var dti = new DebugTargetInfo(provider);

            try {
                dti.Info.dlo        = DEBUG_LAUNCH_OPERATION.DLO_CreateProcess;
                dti.Info.bstrExe    = config.GetInterpreterPath();
                dti.Info.bstrCurDir = string.IsNullOrEmpty(config.WorkingDirectory) ? PathUtils.GetParent(config.ScriptName) : config.WorkingDirectory;

                dti.Info.bstrRemoteMachine         = null;
                dti.Info.fSendStdoutToOutputWindow = 0;

                bool nativeDebug = config.GetLaunchOption(PythonConstants.EnableNativeCodeDebugging).IsTrue();
                if (!nativeDebug)
                {
                    dti.Info.bstrOptions = GetOptions(provider, config);
                }

                // Environment variables should be passed as a
                // null-terminated block of null-terminated strings.
                // Each string is in the following form:name=value\0
                var buf             = new StringBuilder();
                var fullEnvironment = provider.GetPythonToolsService().GetFullEnvironment(config);
                foreach (var kv in fullEnvironment)
                {
                    buf.AppendFormat("{0}={1}\0", kv.Key, kv.Value);
                }
                if (buf.Length > 0)
                {
                    buf.Append("\0");
                    dti.Info.bstrEnv = buf.ToString();
                }

                dti.Info.bstrArg = GetArgs(config);

                if (nativeDebug)
                {
                    dti.Info.dwClsidCount = 2;
                    dti.Info.pClsidList   = Marshal.AllocCoTaskMem(sizeof(Guid) * 2);
                    var engineGuids = (Guid *)dti.Info.pClsidList;
                    engineGuids[0] = dti.Info.clsidCustom = DkmEngineId.NativeEng;
                    engineGuids[1] = AD7Engine.DebugEngineGuid;
                }
                else
                {
                    var pyService = provider.GetPythonToolsService();
                    // Set the Python debugger
                    dti.Info.clsidCustom = pyService.DebuggerOptions.UseLegacyDebugger ? AD7Engine.DebugEngineGuid : DebugAdapterLauncher.VSCodeDebugEngine;
                    dti.Info.grfLaunch   = (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_StopDebuggingOnEnd;

                    if (!pyService.DebuggerOptions.UseLegacyDebugger)
                    {
                        dti.Info.bstrOptions = GetLaunchJsonForVsCodeDebugAdapter(provider, config, fullEnvironment);
                    }
                }

                // Null out dti so that it is not disposed before we return.
                var result = dti;
                dti = null;
                return(result);
            } finally {
                if (dti != null)
                {
                    dti.Dispose();
                }
            }
        }
Exemplo n.º 6
0
        public static unsafe DebugTargetInfo CreateDebugTargetInfo(IServiceProvider provider, LaunchConfiguration config)
        {
            var pyService = provider.GetPythonToolsService();
            var dti       = new DebugTargetInfo(provider);

            try {
                dti.Info.dlo        = DEBUG_LAUNCH_OPERATION.DLO_CreateProcess;
                dti.Info.bstrExe    = config.GetInterpreterPath();
                dti.Info.bstrCurDir = config.WorkingDirectory;
                if (string.IsNullOrEmpty(dti.Info.bstrCurDir))
                {
                    dti.Info.bstrCurDir = PathUtils.GetParent(config.ScriptName);
                }

                dti.Info.bstrRemoteMachine         = null;
                dti.Info.fSendStdoutToOutputWindow = 0;

                bool nativeDebug = config.GetLaunchOption(PythonConstants.EnableNativeCodeDebugging).IsTrue();
                if (!nativeDebug)
                {
                    dti.Info.bstrOptions = string.Join(";",
                                                       GetGlobalDebuggerOptions(pyService)
                                                       .Concat(GetLaunchConfigurationOptions(config))
                                                       .Where(s => !string.IsNullOrEmpty(s))
                                                       .Select(s => s.Replace(";", ";;"))
                                                       );
                }

                // Environment variables should be passed as a
                // null-terminated block of null-terminated strings.
                // Each string is in the following form:name=value\0
                var buf = new StringBuilder();
                foreach (var kv in provider.GetPythonToolsService().GetFullEnvironment(config))
                {
                    buf.AppendFormat("{0}={1}\0", kv.Key, kv.Value);
                }
                if (buf.Length > 0)
                {
                    buf.Append("\0");
                    dti.Info.bstrEnv = buf.ToString();
                }

                var args = string.Join(" ", new[] {
                    config.InterpreterArguments,
                    config.ScriptName == null ? "" : ProcessOutput.QuoteSingleArgument(config.ScriptName),
                    config.ScriptArguments
                }.Where(s => !string.IsNullOrEmpty(s)));

                if (config.Environment != null)
                {
                    args = DoSubstitutions(config.Environment, args);
                }
                dti.Info.bstrArg = args;

                if (nativeDebug)
                {
                    dti.Info.dwClsidCount = 2;
                    dti.Info.pClsidList   = Marshal.AllocCoTaskMem(sizeof(Guid) * 2);
                    var engineGuids = (Guid *)dti.Info.pClsidList;
                    engineGuids[0] = dti.Info.clsidCustom = DkmEngineId.NativeEng;
                    engineGuids[1] = AD7Engine.DebugEngineGuid;
                }
                else
                {
                    // Set the Python debugger
                    dti.Info.clsidCustom = new Guid(AD7Engine.DebugEngineId);
                    dti.Info.grfLaunch   = (uint)__VSDBGLAUNCHFLAGS.DBGLAUNCH_StopDebuggingOnEnd;
                }

                // Null out dti so that it is not disposed before we return.
                var result = dti;
                dti = null;
                return(result);
            } finally {
                if (dti != null)
                {
                    dti.Dispose();
                }
            }
        }