-
Notifications
You must be signed in to change notification settings - Fork 297
Locate dotnet tool ipy launcher #1936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,8 +6,10 @@ | |||||||||
using System.Collections.Generic; | ||||||||||
using System.Diagnostics; | ||||||||||
using System.IO; | ||||||||||
using System.IO.MemoryMappedFiles; | ||||||||||
using System.Reflection; | ||||||||||
using System.Runtime.InteropServices; | ||||||||||
using System.Runtime.Versioning; | ||||||||||
using System.Text; | ||||||||||
using System.Threading; | ||||||||||
|
||||||||||
|
@@ -261,17 +263,18 @@ private void InitializeModules() { | |||||||||
|
||||||||||
var name = Path.GetFileNameWithoutExtension(executable); | ||||||||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { | ||||||||||
var runner = Path.Combine(prefix, name + ".exe"); | ||||||||||
if (File.Exists(runner)) { | ||||||||||
var exename = name + ".exe"; | ||||||||||
var runner = Path.Combine(prefix, exename); | ||||||||||
if (File.Exists(runner) || FindRunner(prefix, exename, executable, out runner)) { | ||||||||||
executable = runner; | ||||||||||
} else { | ||||||||||
// TODO: was for .NET Core 2.1, can we drop this? | ||||||||||
// ipy.bat is created Install-IronPython.ps1, which installs from a zip file | ||||||||||
runner = Path.Combine(prefix, name + ".bat"); | ||||||||||
if (File.Exists(runner)) executable = runner; | ||||||||||
} | ||||||||||
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { | ||||||||||
var runner = Path.Combine(prefix, name); | ||||||||||
if (File.Exists(runner)) { | ||||||||||
if (File.Exists(runner) || FindRunner(prefix, name, executable, out runner)) { | ||||||||||
executable = runner; | ||||||||||
} else { | ||||||||||
runner = Path.Combine(prefix, name + ".sh"); | ||||||||||
|
@@ -289,7 +292,7 @@ private void InitializeModules() { | |||||||||
if (File.Exists(path)) { | ||||||||||
foreach (var line in File.ReadAllLines(path, Encoding.UTF8)) { // TODO: this actually needs to be decoded with surrogateescape | ||||||||||
if (line.StartsWith('#')) continue; | ||||||||||
var split = line.Split(new[] { '=' }, 2); | ||||||||||
var split = line.Split(['='], 2); | ||||||||||
if (split.Length != 2) continue; | ||||||||||
if (split[0].Trim() == "home") { | ||||||||||
pyvenv_prefix = split[1].Trim(); | ||||||||||
|
@@ -309,6 +312,59 @@ private void InitializeModules() { | |||||||||
} | ||||||||||
|
||||||||||
PythonContext.SetHostVariables(prefix ?? "", executable, null); | ||||||||||
|
||||||||||
|
||||||||||
// --- Local functions ------- | ||||||||||
|
||||||||||
static bool FindRunner(string prefix, string name, string assembly, out string runner) { | ||||||||||
runner = null; | ||||||||||
#if NET | ||||||||||
while (prefix != null) { | ||||||||||
runner = Path.Combine(prefix, name); | ||||||||||
if (File.Exists(runner)) { | ||||||||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || IsExecutable(runner)) { | ||||||||||
break; | ||||||||||
} | ||||||||||
} | ||||||||||
prefix = Path.GetDirectoryName(prefix); | ||||||||||
} | ||||||||||
if (runner != null && Path.GetExtension(assembly).Equals(".dll", StringComparison.OrdinalIgnoreCase)) { | ||||||||||
// make sure that the runner refers to this DLL | ||||||||||
var relativeAssemblyPath = assembly.Substring(prefix.Length + 1); // skip over the path separator | ||||||||||
byte[] fsAssemblyPath = Encoding.UTF8.GetBytes(relativeAssemblyPath); | ||||||||||
byte fsap0 = fsAssemblyPath[0]; | ||||||||||
|
||||||||||
try { | ||||||||||
using var mmf = MemoryMappedFile.CreateFromFile(runner, FileMode.Open, null, 0, MemoryMappedFileAccess.Read); | ||||||||||
using var accessor = mmf.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read); | ||||||||||
|
||||||||||
for (long i = accessor.Capacity - fsAssemblyPath.Length; i >= 0; i--) { // the path should be close to the end of the file | ||||||||||
if (accessor.ReadByte(i) != fsap0) continue; | ||||||||||
|
||||||||||
bool found = true; | ||||||||||
for (int j = 1; j < fsAssemblyPath.Length; j++) { | ||||||||||
if (accessor.ReadByte(i + j) != fsAssemblyPath[j]) { | ||||||||||
found = false; | ||||||||||
break; | ||||||||||
} | ||||||||||
} | ||||||||||
if (found) return true; | ||||||||||
} | ||||||||||
} catch { } | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Swallowing exceptions silently in the try-catch block may obscure underlying issues during runner validation. Consider logging or handling the exception explicitly to aid debugging.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||
} | ||||||||||
#endif | ||||||||||
return false; | ||||||||||
} | ||||||||||
|
||||||||||
#if NET | ||||||||||
[UnsupportedOSPlatform("windows")] | ||||||||||
static bool IsExecutable(string filePath) { | ||||||||||
var fileInfo = new Mono.Unix.UnixFileInfo(filePath); | ||||||||||
var fileMode = fileInfo.FileAccessPermissions; | ||||||||||
|
||||||||||
return (fileMode & (Mono.Unix.FileAccessPermissions.UserExecute | Mono.Unix.FileAccessPermissions.GroupExecute | Mono.Unix.FileAccessPermissions.OtherExecute)) != 0; | ||||||||||
} | ||||||||||
#endif | ||||||||||
} | ||||||||||
|
||||||||||
/// <summary> | ||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I guess
prefix
here could benull
if it doesn't find anything?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, the test is wrong. Thanks!