-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-127503: Emscripten make Python.sh function as proper Python CLI #127506
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 all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dc865d7
Fix realpath usage on systems with bsd coreutils
hoodmane 18f0424
Mount all native directories that aren't known to cause trouble
hoodmane 38b1f7a
Pass environment variables into Emscripten
hoodmane 4d45279
Change make target from commoninstall to all
hoodmane af99603
Pass flags in NODEFLAGS environment variable to node
hoodmane f941192
Apply mdboom's review comments
hoodmane 351aa4f
Fix NODEFLAGS reference
hoodmane 1e43493
Fix NODEFLAGS
hoodmane af17844
Fix python.sh template
hoodmane ca741bc
Be more careful with handling of the program name
hoodmane 60875c0
Apply code formatter
hoodmane 63d57c8
Redirect realpath --version to /dev/null again
hoodmane f9c9ace
Cleanup
hoodmane f70b15f
Merge branch 'main' into emscripten-cli-fixes
hoodmane ca2abc3
Put back in f string
hoodmane 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
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 |
---|---|---|
@@ -1,30 +1,47 @@ | ||
import EmscriptenModule from "./python.mjs"; | ||
import { dirname } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import fs from "node:fs"; | ||
|
||
if (process?.versions?.node) { | ||
const nodeVersion = Number(process.versions.node.split(".", 1)[0]); | ||
if (nodeVersion < 18) { | ||
process.stderr.write( | ||
`Node version must be >= 18, got version ${process.version}\n`, | ||
); | ||
process.exit(1); | ||
process.stderr.write( | ||
`Node version must be >= 18, got version ${process.version}\n`, | ||
); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
function rootDirsToMount(Module) { | ||
return fs | ||
.readdirSync("/") | ||
.filter((dir) => !["dev", "lib", "proc"].includes(dir)) | ||
.map((dir) => "/" + dir); | ||
} | ||
|
||
function mountDirectories(Module) { | ||
for (const dir of rootDirsToMount(Module)) { | ||
Module.FS.mkdirTree(dir); | ||
Module.FS.mount(Module.FS.filesystems.NODEFS, { root: dir }, dir); | ||
} | ||
} | ||
|
||
const thisProgram = "--this-program="; | ||
const thisProgramIndex = process.argv.findIndex((x) => | ||
x.startsWith(thisProgram), | ||
); | ||
|
||
const settings = { | ||
preRun(Module) { | ||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
Module.FS.mkdirTree("/lib/"); | ||
Module.FS.mount(Module.FS.filesystems.NODEFS, { root: __dirname + "/lib/" }, "/lib/"); | ||
mountDirectories(Module); | ||
Module.FS.chdir(process.cwd()); | ||
Object.assign(Module.ENV, process.env); | ||
}, | ||
// The first three arguments are: "node", path to this file, path to | ||
// python.sh. After that come the arguments the user passed to python.sh. | ||
arguments: process.argv.slice(3), | ||
// Ensure that sys.executable, sys._base_executable, etc point to python.sh | ||
// not to this file. To properly handle symlinks, python.sh needs to compute | ||
// its own path. | ||
thisProgram: process.argv[2], | ||
thisProgram: process.argv[thisProgramIndex], | ||
// After python.sh come the arguments thatthe user passed to python.sh. | ||
arguments: process.argv.slice(thisProgramIndex + 1), | ||
}; | ||
|
||
await EmscriptenModule(settings); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.