rustpython_vm/
version.rs

1/* Several function to retrieve version information.
2 */
3
4use chrono::{prelude::DateTime, Local};
5use std::time::{Duration, UNIX_EPOCH};
6
7// = 3.12.0alpha
8pub const MAJOR: usize = 3;
9pub const MINOR: usize = 12;
10pub const MICRO: usize = 0;
11pub const RELEASELEVEL: &str = "alpha";
12pub const RELEASELEVEL_N: usize = 0xA;
13pub const SERIAL: usize = 0;
14
15pub const VERSION_HEX: usize =
16    (MAJOR << 24) | (MINOR << 16) | (MICRO << 8) | (RELEASELEVEL_N << 4) | SERIAL;
17
18pub fn get_version() -> String {
19    format!(
20        "{:.80} ({:.80}) \n[{:.80}]", // \n is PyPy convention
21        get_version_number(),
22        get_build_info(),
23        get_compiler()
24    )
25}
26
27pub fn get_version_number() -> String {
28    format!("{MAJOR}.{MINOR}.{MICRO}{RELEASELEVEL}")
29}
30
31pub fn get_winver_number() -> String {
32    format!("{MAJOR}.{MINOR}")
33}
34
35pub fn get_compiler() -> String {
36    format!("rustc {}", env!("RUSTC_VERSION"))
37}
38
39pub fn get_build_info() -> String {
40    // See: https://reproducible-builds.org/docs/timestamps/
41    let git_revision = get_git_revision();
42    let separator = if git_revision.is_empty() { "" } else { ":" };
43
44    let git_identifier = get_git_identifier();
45
46    format!(
47        "{id}{sep}{revision}, {date:.20}, {time:.9}",
48        id = if git_identifier.is_empty() {
49            "default".to_owned()
50        } else {
51            git_identifier
52        },
53        sep = separator,
54        revision = git_revision,
55        date = get_git_date(),
56        time = get_git_time(),
57    )
58}
59
60pub fn get_git_revision() -> String {
61    option_env!("RUSTPYTHON_GIT_HASH").unwrap_or("").to_owned()
62}
63
64pub fn get_git_tag() -> String {
65    option_env!("RUSTPYTHON_GIT_TAG").unwrap_or("").to_owned()
66}
67
68pub fn get_git_branch() -> String {
69    option_env!("RUSTPYTHON_GIT_BRANCH")
70        .unwrap_or("")
71        .to_owned()
72}
73
74pub fn get_git_identifier() -> String {
75    let git_tag = get_git_tag();
76    let git_branch = get_git_branch();
77
78    if git_tag.is_empty() || git_tag == "undefined" {
79        git_branch
80    } else {
81        git_tag
82    }
83}
84
85fn get_git_timestamp_datetime() -> DateTime<Local> {
86    let timestamp = option_env!("RUSTPYTHON_GIT_TIMESTAMP")
87        .unwrap_or("")
88        .to_owned();
89    let timestamp = timestamp.parse::<u64>().unwrap_or(0);
90
91    let datetime = UNIX_EPOCH + Duration::from_secs(timestamp);
92
93    datetime.into()
94}
95
96pub fn get_git_date() -> String {
97    let datetime = get_git_timestamp_datetime();
98
99    datetime.format("%b %e %Y").to_string()
100}
101
102pub fn get_git_time() -> String {
103    let datetime = get_git_timestamp_datetime();
104
105    datetime.format("%H:%M:%S").to_string()
106}
107
108pub fn get_git_datetime() -> String {
109    let date = get_git_date();
110    let time = get_git_time();
111
112    format!("{date} {time}")
113}