|
| 1 | +// Python Tools for Visual Studio |
| 2 | +// Copyright(c) Microsoft Corporation |
| 3 | +// All rights reserved. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the License); you may not use |
| 6 | +// this file except in compliance with the License. You may obtain a copy of the |
| 7 | +// License at http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS |
| 10 | +// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY |
| 11 | +// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, |
| 12 | +// MERCHANTABILITY OR NON-INFRINGEMENT. |
| 13 | +// |
| 14 | +// See the Apache Version 2.0 License for specific language governing |
| 15 | +// permissions and limitations under the License. |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +namespace Microsoft.PythonTools.Profiling { |
| 20 | + using System; |
| 21 | + using System.Diagnostics; |
| 22 | + using System.IO; |
| 23 | + using System.Linq; |
| 24 | + using System.Windows; |
| 25 | + using Microsoft.PythonTools.Infrastructure; |
| 26 | + using Microsoft.PythonTools.Interpreter; |
| 27 | + |
| 28 | + internal class CommandArgumentBuilder { |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Constructs a <see cref="PythonProfilingCommandArgs"/> based on the provided profiling target. |
| 32 | + /// </summary> |
| 33 | + public PythonProfilingCommandArgs BuildCommandArgsFromTarget(ProfilingTarget target) { |
| 34 | + if (target == null) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + try { |
| 39 | + var pythonProfilingPackage = PythonProfilingPackage.Instance; |
| 40 | + var joinableTaskFactory = pythonProfilingPackage.JoinableTaskFactory; |
| 41 | + |
| 42 | + PythonProfilingCommandArgs command = null; |
| 43 | + |
| 44 | + joinableTaskFactory.Run(async () => { |
| 45 | + await joinableTaskFactory.SwitchToMainThreadAsync(); |
| 46 | + |
| 47 | + var name = target.GetProfilingName(pythonProfilingPackage, out var save); |
| 48 | + var explorer = await pythonProfilingPackage.ShowPerformanceExplorerAsync(); |
| 49 | + var session = explorer.Sessions.AddTarget(target, name, save); |
| 50 | + |
| 51 | + command = SelectBuilder(target, session); |
| 52 | + |
| 53 | + }); |
| 54 | + |
| 55 | + return command; |
| 56 | + } catch (Exception ex) { |
| 57 | + Debug.Fail($"Error building command: {ex.Message}"); |
| 58 | + throw; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Select the appropriate builder based on the provided profiling target. |
| 64 | + /// </summary> |
| 65 | + private PythonProfilingCommandArgs SelectBuilder(ProfilingTarget target, SessionNode session) { |
| 66 | + var projectTarget = target.ProjectTarget; |
| 67 | + var standaloneTarget = target.StandaloneTarget; |
| 68 | + |
| 69 | + if (projectTarget != null) { |
| 70 | + return BuildProjectCommandArgs(projectTarget, session); |
| 71 | + } else if (standaloneTarget != null) { |
| 72 | + return BuildStandaloneCommandArgs(standaloneTarget, session); |
| 73 | + } |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + private PythonProfilingCommandArgs BuildProjectCommandArgs(ProjectTarget projectTarget, SessionNode session) { |
| 78 | + var solution = PythonProfilingPackage.Instance.Solution; |
| 79 | + var project = solution.EnumerateLoadedPythonProjects() |
| 80 | + .SingleOrDefault(p => p.GetProjectIDGuidProperty() == projectTarget.TargetProject); |
| 81 | + |
| 82 | + if (project == null) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + LaunchConfiguration config = null; |
| 87 | + try { |
| 88 | + config = project?.GetLaunchConfigurationOrThrow(); |
| 89 | + } catch (NoInterpretersException ex) { |
| 90 | + PythonToolsPackage.OpenNoInterpretersHelpPage(session._serviceProvider, ex.HelpPage); |
| 91 | + return null; |
| 92 | + } catch (MissingInterpreterException ex) { |
| 93 | + MessageBox.Show(ex.Message, Strings.ProductTitle); |
| 94 | + return null; |
| 95 | + } catch (IOException ex) { |
| 96 | + MessageBox.Show(ex.Message, Strings.ProductTitle); |
| 97 | + return null; |
| 98 | + } |
| 99 | + if (config == null) { |
| 100 | + MessageBox.Show(Strings.ProjectInterpreterNotFound.FormatUI(project.GetNameProperty()), Strings.ProductTitle); |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + if (string.IsNullOrEmpty(config.ScriptName)) { |
| 105 | + MessageBox.Show(Strings.NoProjectStartupFile, Strings.ProductTitle); |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + if (string.IsNullOrEmpty(config.WorkingDirectory) || config.WorkingDirectory == ".") { |
| 110 | + config.WorkingDirectory = project.ProjectHome; |
| 111 | + if (string.IsNullOrEmpty(config.WorkingDirectory)) { |
| 112 | + config.WorkingDirectory = Path.GetDirectoryName(config.ScriptName); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + var pythonExePath = config.GetInterpreterPath(); |
| 117 | + var scriptPath = string.Join(" ", ProcessOutput.QuoteSingleArgument(config.ScriptName), config.ScriptArguments); |
| 118 | + var workingDir = config.WorkingDirectory; |
| 119 | + var envVars = session._serviceProvider.GetPythonToolsService().GetFullEnvironment(config); |
| 120 | + |
| 121 | + var command = new PythonProfilingCommandArgs { |
| 122 | + PythonExePath = pythonExePath, |
| 123 | + ScriptPath = scriptPath, |
| 124 | + WorkingDir = workingDir, |
| 125 | + Args = Array.Empty<string>(), |
| 126 | + EnvVars = envVars |
| 127 | + }; |
| 128 | + return command; |
| 129 | + } |
| 130 | + |
| 131 | + private PythonProfilingCommandArgs BuildStandaloneCommandArgs(StandaloneTarget standaloneTarget, SessionNode session) { |
| 132 | + if (standaloneTarget == null) { |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + LaunchConfiguration config = null; |
| 137 | + |
| 138 | + if (standaloneTarget.InterpreterPath != null) { |
| 139 | + config = new LaunchConfiguration(null); |
| 140 | + } |
| 141 | + |
| 142 | + if (standaloneTarget.PythonInterpreter != null) { |
| 143 | + var registry = session._serviceProvider.GetComponentModel().GetService<IInterpreterRegistryService>(); |
| 144 | + var interpreter = registry.FindConfiguration(standaloneTarget.PythonInterpreter.Id); |
| 145 | + if (interpreter == null) { |
| 146 | + return null; |
| 147 | + } |
| 148 | + |
| 149 | + config = new LaunchConfiguration(interpreter); |
| 150 | + } |
| 151 | + |
| 152 | + config.InterpreterPath = standaloneTarget.InterpreterPath; |
| 153 | + config.ScriptName = standaloneTarget.Script; |
| 154 | + config.ScriptArguments = standaloneTarget.Arguments; |
| 155 | + config.WorkingDirectory = standaloneTarget.WorkingDirectory; |
| 156 | + |
| 157 | + var argsInput = standaloneTarget.Arguments; |
| 158 | + var parsedArgs = string.IsNullOrWhiteSpace(argsInput) |
| 159 | + ? Array.Empty<string>() |
| 160 | + : argsInput.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); |
| 161 | + |
| 162 | + var envVars = session._serviceProvider.GetPythonToolsService().GetFullEnvironment(config); |
| 163 | + |
| 164 | + return new PythonProfilingCommandArgs { |
| 165 | + PythonExePath = config.GetInterpreterPath(), |
| 166 | + WorkingDir = standaloneTarget.WorkingDirectory, |
| 167 | + ScriptPath = standaloneTarget.Script, |
| 168 | + Args = parsedArgs, |
| 169 | + EnvVars = envVars |
| 170 | + }; |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | + |
0 commit comments