Skip to content

Commit d3ff1c6

Browse files
Merge pull request #1485 from ChJR/feature/sys.version
Change format of sys.version
2 parents 1ad9406 + 0808f47 commit d3ff1c6

3 files changed

Lines changed: 59 additions & 11 deletions

File tree

vm/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ fn git_hash() -> String {
1515
}
1616

1717
fn git_timestamp() -> String {
18-
git(&["log", "-1", "--format=%cd"])
18+
git(&["log", "-1", "--format=%ct"])
1919
}
2020

2121
fn git_tag() -> String {
2222
git(&["describe", "--all", "--always", "--dirty"])
2323
}
2424

2525
fn git_branch() -> String {
26-
git(&["rev-parse", "--abbrev-ref", "HEAD"])
26+
git(&["name-rev", "--name-only", "HEAD"])
2727
}
2828

2929
fn git(args: &[&str]) -> String {

vm/src/stdlib/platform.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ fn platform_python_compiler(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
3232

3333
fn platform_python_build(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
3434
arg_check!(vm, args);
35-
let (git_hash, git_timestamp) = version::get_build_info();
35+
let git_hash = version::get_git_identifier();
36+
let git_timestamp = version::get_git_datetime();
3637
Ok(vm
3738
.ctx
3839
.new_tuple(vec![vm.new_str(git_hash), vm.new_str(git_timestamp)]))

vm/src/version.rs

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ pub struct VersionInfo {
1616
releaselevel: &'static str,
1717
serial: usize,
1818
}
19+
extern crate chrono;
20+
use chrono::prelude::DateTime;
21+
use chrono::Local;
22+
use std::time::{Duration, UNIX_EPOCH};
1923

2024
pub fn get_version() -> String {
2125
format!(
22-
"{} {:?} {}",
26+
"{:.80} ({:.80}) {:.80}",
2327
get_version_number(),
2428
get_build_info(),
2529
get_compiler()
@@ -42,16 +46,28 @@ pub fn get_version_number() -> String {
4246

4347
pub fn get_compiler() -> String {
4448
let rustc_version = rustc_version_runtime::version_meta();
45-
format!("rustc {}", rustc_version.semver)
49+
format!("\n[rustc {}]", rustc_version.semver)
4650
}
4751

48-
pub fn get_build_info() -> (String, String) {
49-
let git_hash = get_git_revision();
52+
pub fn get_build_info() -> String {
5053
// See: https://reproducible-builds.org/docs/timestamps/
51-
let git_timestamp = option_env!("RUSTPYTHON_GIT_TIMESTAMP")
52-
.unwrap_or("")
53-
.to_string();
54-
(git_hash, git_timestamp)
54+
let git_revision = get_git_revision();
55+
let separator = if git_revision.is_empty() { "" } else { ":" };
56+
57+
let git_identifier = get_git_identifier();
58+
59+
format!(
60+
"{id}{sep}{revision}, {date:.20}, {time:.9}",
61+
id = if git_identifier.is_empty() {
62+
"default".to_string()
63+
} else {
64+
git_identifier
65+
},
66+
sep = separator,
67+
revision = git_revision,
68+
date = get_git_date(),
69+
time = get_git_time(),
70+
)
5571
}
5672

5773
pub fn get_git_revision() -> String {
@@ -78,3 +94,34 @@ pub fn get_git_identifier() -> String {
7894
git_tag
7995
}
8096
}
97+
98+
fn get_git_timestamp_datetime() -> DateTime<Local> {
99+
let timestamp = option_env!("RUSTPYTHON_GIT_TIMESTAMP")
100+
.unwrap_or("")
101+
.to_string();
102+
let timestamp = timestamp.parse::<u64>().unwrap_or(0);
103+
104+
let datetime = UNIX_EPOCH + Duration::from_secs(timestamp);
105+
let datetime = DateTime::<Local>::from(datetime);
106+
107+
datetime
108+
}
109+
110+
pub fn get_git_date() -> String {
111+
let datetime = get_git_timestamp_datetime();
112+
113+
datetime.format("%b %e %Y").to_string()
114+
}
115+
116+
pub fn get_git_time() -> String {
117+
let datetime = get_git_timestamp_datetime();
118+
119+
datetime.format("%H:%M:%S").to_string()
120+
}
121+
122+
pub fn get_git_datetime() -> String {
123+
let date = get_git_date();
124+
let time = get_git_time();
125+
126+
format!("{} {}", date, time)
127+
}

0 commit comments

Comments
 (0)