-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add a tool to run x.py
from any subdirectory
#78658
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
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
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# This file is automatically @generated by Cargo. | ||
# It is not intended for manual editing. | ||
[[package]] | ||
name = "x" | ||
version = "0.1.0" |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "x" | ||
version = "0.1.0" | ||
description = "Run x.py slightly more conveniently" | ||
authors = ["Casey Rodarmor <casey@rodarmor.com>"] | ||
edition = "2018" | ||
publish = false |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# x | ||
|
||
`x` invokes `x.py` from any subdirectory. |
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
//! Run `x.py` from any subdirectory of a rust compiler checkout. | ||
//! | ||
//! We prefer `exec`, to avoid adding an extra process in the process tree. | ||
//! However, since `exec` isn't available on Windows, we indirect through | ||
//! `exec_or_status`, which will call `exec` on unix and `status` on Windows. | ||
//! | ||
//! We use `python`, `python3`, or `python2` as the python interpreter to run | ||
//! `x.py`, in that order of preference. | ||
|
||
use std::{ | ||
env, io, | ||
process::{self, Command, ExitStatus}, | ||
}; | ||
|
||
const PYTHON: &str = "python"; | ||
const PYTHON2: &str = "python2"; | ||
const PYTHON3: &str = "python3"; | ||
|
||
fn python() -> &'static str { | ||
let val = match env::var_os("PATH") { | ||
Some(val) => val, | ||
None => return PYTHON, | ||
}; | ||
|
||
let mut python2 = false; | ||
let mut python3 = false; | ||
|
||
for dir in env::split_paths(&val) { | ||
if dir.join(PYTHON).exists() { | ||
return PYTHON; | ||
} | ||
|
||
python2 |= dir.join(PYTHON2).exists(); | ||
python3 |= dir.join(PYTHON3).exists(); | ||
} | ||
|
||
if python3 { | ||
PYTHON3 | ||
} else if python2 { | ||
PYTHON2 | ||
} else { | ||
PYTHON | ||
} | ||
} | ||
|
||
#[cfg(unix)] | ||
fn exec_or_status(command: &mut Command) -> io::Result<ExitStatus> { | ||
use std::os::unix::process::CommandExt; | ||
Err(command.exec()) | ||
} | ||
|
||
#[cfg(not(unix))] | ||
fn exec_or_status(command: &mut Command) -> io::Result<ExitStatus> { | ||
command.status() | ||
} | ||
|
||
fn main() { | ||
let current = match env::current_dir() { | ||
Ok(dir) => dir, | ||
Err(err) => { | ||
eprintln!("Failed to get current directory: {}", err); | ||
process::exit(1); | ||
} | ||
}; | ||
|
||
for dir in current.ancestors() { | ||
let candidate = dir.join("x.py"); | ||
|
||
if candidate.exists() { | ||
let mut python = Command::new(python()); | ||
|
||
python.arg(&candidate).args(env::args().skip(1)).current_dir(dir); | ||
|
||
let result = exec_or_status(&mut python); | ||
|
||
match result { | ||
Err(error) => { | ||
eprintln!("Failed to invoke `{}`: {}", candidate.display(), error); | ||
} | ||
Ok(status) => { | ||
process::exit(status.code().unwrap_or(1)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
eprintln!( | ||
"x.py not found. Please run inside of a checkout of `https://github.com/rust-lang/rust`." | ||
); | ||
|
||
process::exit(1); | ||
} |
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.