Skip to content

Commit ef649be

Browse files
committed
Add git information in sys and platform modules.
1 parent 69980c2 commit ef649be

File tree

5 files changed

+119
-6
lines changed

5 files changed

+119
-6
lines changed

vm/build.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::process::Command;
2+
3+
fn main() {
4+
println!("cargo:rustc-env=RUSTPYTHON_GIT_HASH={}", git_hash());
5+
println!(
6+
"cargo:rustc-env=RUSTPYTHON_GIT_TIMESTAMP={}",
7+
git_timestamp()
8+
);
9+
println!("cargo:rustc-env=RUSTPYTHON_GIT_BRANCH={}", git_branch());
10+
}
11+
12+
fn git_hash() -> String {
13+
git(&["rev-parse", "HEAD"])
14+
}
15+
16+
fn git_timestamp() -> String {
17+
git(&["log", "-1", "--format=%cd"])
18+
}
19+
20+
fn git_branch() -> String {
21+
git(&["rev-parse", "--abbrev-ref", "HEAD"])
22+
}
23+
24+
fn git(args: &[&str]) -> String {
25+
command("git", args)
26+
}
27+
28+
fn command(cmd: &str, args: &[&str]) -> String {
29+
match Command::new(cmd).args(args).output() {
30+
Ok(output) => match String::from_utf8(output.stdout) {
31+
Ok(s) => s,
32+
Err(err) => format!("(output error: {})", err),
33+
},
34+
Err(err) => format!("(command error: {})", err),
35+
}
36+
}

vm/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub mod stdlib;
6060
mod sysmodule;
6161
mod traceback;
6262
pub mod util;
63+
mod version;
6364
mod vm;
6465

6566
// pub use self::pyobject::Executor;

vm/src/stdlib/platform.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
use crate::function::PyFuncArgs;
22
use crate::pyobject::{PyObjectRef, PyResult};
3+
use crate::version;
34
use crate::vm::VirtualMachine;
45

56
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
67
let ctx = &vm.ctx;
78
py_module!(vm, "platform", {
9+
"python_branch" => ctx.new_rustfunc(platform_python_branch),
10+
"python_build" => ctx.new_rustfunc(platform_python_build),
811
"python_compiler" => ctx.new_rustfunc(platform_python_compiler),
912
"python_implementation" => ctx.new_rustfunc(platform_python_implementation),
13+
"python_revision" => ctx.new_rustfunc(platform_python_revision),
1014
"python_version" => ctx.new_rustfunc(platform_python_version),
1115
})
1216
}
@@ -18,12 +22,28 @@ fn platform_python_implementation(vm: &VirtualMachine, args: PyFuncArgs) -> PyRe
1822

1923
fn platform_python_version(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
2024
arg_check!(vm, args);
21-
// TODO: fetch version from somewhere.
22-
Ok(vm.new_str("4.0.0".to_string()))
25+
Ok(vm.new_str(version::get_version_number()))
2326
}
2427

2528
fn platform_python_compiler(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
2629
arg_check!(vm, args);
27-
let version = rustc_version_runtime::version_meta();
28-
Ok(vm.new_str(format!("rustc {}", version.semver)))
30+
Ok(vm.new_str(version::get_compiler()))
31+
}
32+
33+
fn platform_python_build(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
34+
arg_check!(vm, args);
35+
let (git_hash, git_timestamp) = version::get_build_info();
36+
Ok(vm
37+
.ctx
38+
.new_tuple(vec![vm.new_str(git_hash), vm.new_str(git_timestamp)]))
39+
}
40+
41+
fn platform_python_branch(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
42+
arg_check!(vm, args);
43+
Ok(vm.new_str(version::get_git_branch()))
44+
}
45+
46+
fn platform_python_revision(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
47+
arg_check!(vm, args);
48+
Ok(vm.new_str(version::get_git_revision()))
2949
}

vm/src/sysmodule.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::frame::FrameRef;
55
use crate::function::{OptionalArg, PyFuncArgs};
66
use crate::obj::objstr::PyStringRef;
77
use crate::pyobject::{IntoPyObject, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyResult};
8+
use crate::version;
89
use crate::vm::VirtualMachine;
910

1011
/*
@@ -160,6 +161,8 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef, builtins: PyObjectR
160161
"unknown".to_string()
161162
};
162163

164+
let copyright = "Copyright (c) 2019 RustPython Team";
165+
163166
let sys_doc = "This module provides access to some objects used or maintained by the
164167
interpreter and to functions that interact strongly with the interpreter.
165168
@@ -229,16 +232,23 @@ setprofile() -- set the global profiling function
229232
setrecursionlimit() -- set the max recursion depth for the interpreter
230233
settrace() -- set the global debug tracing function
231234
";
232-
let mut module_names: Vec<_> = vm.stdlib_inits.borrow().keys().cloned().collect();
235+
let mut module_names: Vec<String> = vm.stdlib_inits.borrow().keys().cloned().collect();
233236
module_names.push("sys".to_string());
234237
module_names.push("builtins".to_string());
235238
module_names.sort();
239+
let builtin_module_names = ctx.new_tuple(
240+
module_names
241+
.iter()
242+
.map(|v| v.into_pyobject(vm).unwrap())
243+
.collect(),
244+
);
236245
let modules = ctx.new_dict();
237246
extend_module!(vm, module, {
238247
"__name__" => ctx.new_str(String::from("sys")),
239248
"argv" => argv(ctx),
240-
"builtin_module_names" => ctx.new_tuple(module_names.iter().map(|v| v.into_pyobject(vm).unwrap()).collect()),
249+
"builtin_module_names" => builtin_module_names,
241250
"byteorder" => ctx.new_str(bytorder),
251+
"copyright" => ctx.new_str(copyright.to_string()),
242252
"flags" => flags,
243253
"getrefcount" => ctx.new_rustfunc(sys_getrefcount),
244254
"getsizeof" => ctx.new_rustfunc(sys_getsizeof),
@@ -260,6 +270,7 @@ settrace() -- set the global debug tracing function
260270
"path_importer_cache" => ctx.new_dict(),
261271
"pycache_prefix" => vm.get_none(),
262272
"dont_write_bytecode" => vm.new_bool(true),
273+
"version" => vm.new_str(version::get_version()),
263274
});
264275

265276
modules.set_item("sys", module.clone(), vm).unwrap();

vm/src/version.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* Several function to retrieve version information.
2+
*/
3+
4+
pub fn get_version() -> String {
5+
format!(
6+
"{} {:?} {}",
7+
get_version_number(),
8+
get_build_info(),
9+
get_compiler()
10+
)
11+
}
12+
13+
pub fn get_version_number() -> String {
14+
format!(
15+
"{}.{}.{}{}",
16+
env!("CARGO_PKG_VERSION_MAJOR"),
17+
env!("CARGO_PKG_VERSION_MINOR"),
18+
env!("CARGO_PKG_VERSION_PATCH"),
19+
option_env!("CARGO_PKG_VERSION_PRE").unwrap_or("")
20+
)
21+
}
22+
23+
pub fn get_compiler() -> String {
24+
let rustc_version = rustc_version_runtime::version_meta();
25+
format!("rustc {}", rustc_version.semver)
26+
}
27+
28+
pub fn get_build_info() -> (String, String) {
29+
let git_hash = get_git_revision();
30+
// See: https://reproducible-builds.org/docs/timestamps/
31+
let git_timestamp = option_env!("RUSTPYTHON_GIT_TIMESTAMP")
32+
.unwrap_or("")
33+
.to_string();
34+
(git_hash, git_timestamp)
35+
}
36+
37+
pub fn get_git_revision() -> String {
38+
option_env!("RUSTPYTHON_GIT_HASH").unwrap_or("").to_string()
39+
}
40+
41+
pub fn get_git_branch() -> String {
42+
option_env!("RUSTPYTHON_GIT_BRANCH")
43+
.unwrap_or("")
44+
.to_string()
45+
}

0 commit comments

Comments
 (0)